Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2434_crossvalidation/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Transformations/ShiftToRangeTransformation.cs @ 16749

Last change on this file since 16749 was 12612, checked in by gkronber, 9 years ago

#2415: added StorableClass attribute to transformation functions (+code improvements)

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 {
10  [StorableClass]
11  [Item("Shift to Range Transformation", "f(x) = k * x + d, start <= f(x) <= end | Represents a linear Transformation using Parameters defining a target range")]
12  public class ShiftToRangeTransformation : LinearTransformation {
13    protected const string RangeParameterName = "Range";
14
15    #region Parameters
16    public IValueParameter<DoubleRange> RangeParameter {
17      get { return (IValueParameter<DoubleRange>)Parameters[RangeParameterName]; }
18    }
19    #endregion
20
21    #region properties
22    public override string ShortName {
23      get { return "Sft"; }
24    }
25    public DoubleRange Range {
26      get { return RangeParameter.Value; }
27    }
28    #endregion
29
30    [StorableConstructor]
31    protected ShiftToRangeTransformation(bool deserializing) : base(deserializing) { }
32    protected ShiftToRangeTransformation(ShiftToRangeTransformation original, Cloner cloner)
33      : base(original, cloner) {
34    }
35    public ShiftToRangeTransformation(IEnumerable<string> allowedColumns)
36      : base(allowedColumns) {
37      MultiplierParameter.Hidden = true;
38      AddendParameter.Hidden = true;
39      Parameters.Add(new ValueParameter<DoubleRange>(RangeParameterName, "start, end | Range for the target window of the linear transformation", new DoubleRange(0.0, 1.0)));
40    }
41
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new ShiftToRangeTransformation(this, cloner);
44    }
45
46    public override IEnumerable<double> Apply(IEnumerable<double> data) {
47      ConfigureParameters(data);
48      return base.Apply(data);
49    }
50
51    public override bool Check(IEnumerable<double> data, out string errorMsg) {
52      ConfigureParameters(data);
53      return base.Check(data, out errorMsg);
54    }
55
56    protected void ConfigureParameters(IEnumerable<double> data) {
57      double originalRangeStart = data.Min();
58      double originalRangeEnd = data.Max();
59
60      double originalRangeWidth = originalRangeEnd - originalRangeStart;
61      double targetRangeWidth = Range.End - Range.Start;
62
63      Multiplier = targetRangeWidth / originalRangeWidth;
64      Addend = Range.Start - originalRangeStart * Multiplier;
65    }
66  }
67}
Note: See TracBrowser for help on using the repository browser.