Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.Problems.DataAnalysis.Transformations/3.3/LinearTransformation.cs @ 10778

Last change on this file since 10778 was 10778, checked in by pfleck, 10 years ago
  • Added CheckedTransformationCollectionView for providing an own type selector dialog.
File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22
23using System;
24using System.Collections.Generic;
25using System.Linq;
26using System.Text;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.DataAnalysis.Transformations {
34  [Item("LinearTransformation", "Represents a linear transformation with multiplication and addition.")]
35  public class LinearTransformation : Transformation<double> {
36    protected const string MultiplierParameterName = "Multiplier";
37    protected const string AddendParameterName = "Addend";
38
39    #region Parameters
40    public IValueParameter<DoubleValue> MultiplierParameter {
41      get { return (IValueParameter<DoubleValue>)Parameters[MultiplierParameterName]; }
42    }
43    public IValueParameter<DoubleValue> AddendParameter {
44      get { return (IValueParameter<DoubleValue>)Parameters[AddendParameterName]; }
45    }
46    #endregion
47
48    #region properties
49    public double Multiplier {
50      get { return MultiplierParameter.Value.Value; }
51      protected set {
52        MultiplierParameter.Value.Value = value;
53      }
54    }
55
56    public double Addend {
57      get { return AddendParameter.Value.Value; }
58      protected set {
59        AddendParameter.Value.Value = value;
60      }
61    }
62    #endregion
63
64    [StorableConstructor]
65    protected LinearTransformation(bool deserializing) : base(deserializing) { }
66    protected LinearTransformation(Transformation<double> original, Cloner cloner)
67      : base(original, cloner) {
68    }
69    public LinearTransformation(IEnumerable<string> allowedColumns)
70      : base(allowedColumns) {
71      Parameters.Add(new ValueParameter<DoubleValue>(MultiplierParameterName, "Multiplier for linear transformation", new DoubleValue(1.0)));
72      Parameters.Add(new ValueParameter<DoubleValue>(AddendParameterName, "Addend for linear transformation", new DoubleValue(0.0)));
73    }
74
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new LinearTransformation(this, cloner);
77    }
78
79    public override IEnumerable<double> Apply(IEnumerable<double> data) {
80      return data.Select(e => e * Multiplier + Addend);
81    }
82
83    public override bool Check(IEnumerable<double> data, out string errorMsg) {
84      StringBuilder sb = new StringBuilder();
85      bool success = true;
86      errorMsg = null;
87
88      if (Multiplier == 0.0) {
89        sb.Append(String.Format("Multiplicand is 0, all {0} entries will be set to {1}. Reversal will not be possble", data.Count(), Addend));
90        errorMsg = sb.ToString();
91        success = false;
92      }
93      return success;
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.