Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.Problems.DataAnalysis.Transformations/3.3/ShiftToRangeTransformation.cs @ 10920

Last change on this file since 10920 was 10909, checked in by tsteinre, 11 years ago
  • modified Transformations Names / Parameter Names
File size: 2.4 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Parameters;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8
9namespace HeuristicLab.Problems.DataAnalysis.Transformations {
10  [Item("Shift to Range Transformation", "f(x) = k * x + d, start <= f(x) <= end | Represents a linear Transformation using Parameters defining a target range")]
11  public class ShiftToRangeTransformation : LinearTransformation {
12    protected const string RangeParameterName = "Range";
13
14    #region Parameters
15    public IValueParameter<DoubleRange> RangeParameter {
16      get { return (IValueParameter<DoubleRange>)Parameters[RangeParameterName]; }
17    }
18    #endregion
19
20    #region properties
21    public DoubleRange Range {
22      get { return RangeParameter.Value; }
23    }
24    #endregion
25
26    [StorableConstructor]
27    protected ShiftToRangeTransformation(bool deserializing) : base(deserializing) { }
28    protected ShiftToRangeTransformation(Transformation<double> original, Cloner cloner)
29      : base(original, cloner) {
30    }
31    public ShiftToRangeTransformation(IEnumerable<string> allowedColumns)
32      : base(allowedColumns) {
33      MultiplierParameter.Hidden = true;
34      AddendParameter.Hidden = true;
35      Parameters.Add(new ValueParameter<DoubleRange>(RangeParameterName, "start, end | Range for the target window of the linear transformation", new DoubleRange(0.0, 1.0)));
36    }
37
38    public override IDeepCloneable Clone(Cloner cloner) {
39      return new ShiftToRangeTransformation(this, cloner);
40    }
41
42    public override IEnumerable<double> Apply(IEnumerable<double> data) {
43      ConfigureParameters(data);
44      return base.Apply(data);
45    }
46
47    public override bool Check(IEnumerable<double> data, out string errorMsg) {
48      ConfigureParameters(data);
49      return base.Check(data, out errorMsg);
50    }
51
52    protected void ConfigureParameters(IEnumerable<double> data) {
53      double originalRangeStart = data.Min();
54      double originalRangeEnd = data.Max();
55
56      double originalRangeWidth = originalRangeEnd - originalRangeStart;
57      double targetRangeWidth = Range.End - Range.Start;
58
59      Multiplier = targetRangeWidth / originalRangeWidth;
60      Addend = Range.Start - originalRangeStart * Multiplier;
61    }
62  }
63}
Note: See TracBrowser for help on using the repository browser.