Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Transformations/LinearTransformation.cs

Last change on this file was 15939, checked in by pfleck, 6 years ago

#2906 Added backwards compatability

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.DataAnalysis {
32  [Item("Linear", "Linear transformation x' = Scale * x + Offset")]
33  [StorableClass]
34  public class LinearTransformation : Transformation<double> {
35
36    #region Parameters
37    private FixedValueParameter<DoubleValue> ScaleParameter {
38      get { return (FixedValueParameter<DoubleValue>)Parameters["Scale"]; }
39    }
40
41    private FixedValueParameter<DoubleValue> OffsetParameter {
42      get { return (FixedValueParameter<DoubleValue>)Parameters["Offset"]; }
43    }
44    #endregion
45
46    #region Properties
47    public double Scale {
48      get { return ScaleParameter.Value.Value; }
49      set { ScaleParameter.Value.Value = value; }
50    }
51
52    public double Offset {
53      get { return OffsetParameter.Value.Value; }
54      set { OffsetParameter.Value.Value = value; }
55    }
56    #endregion
57
58    #region Constructor, Cloning & Persistence
59    public LinearTransformation()
60      : base() {
61      Parameters.Add(new FixedValueParameter<DoubleValue>("Scale", "Multiplicative factor (Slope)", new DoubleValue(1.0)));
62      Parameters.Add(new FixedValueParameter<DoubleValue>("Offset", "Additive factor (Intercept)", new DoubleValue(0.0)));
63    }
64
65    protected LinearTransformation(LinearTransformation original, Cloner cloner)
66      : base(original, cloner) {
67    }
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new LinearTransformation(this, cloner);
70    }
71
72    [StorableConstructor]
73    protected LinearTransformation(bool deserializing)
74      : base(deserializing) {
75    }
76
77    [StorableHook(HookType.AfterDeserialization)]
78    private void AfterDeserialization() {
79      if (Parameters.TryGetValue("Slope", out IParameter slopeParameter)) {
80        Parameters.Add(new FixedValueParameter<DoubleValue>("Scale", "Multiplicative factor (Slope)", ((IValueParameter<DoubleValue>)slopeParameter).Value));
81        Parameters.Remove("Slope");
82      }
83      if (Parameters.TryGetValue("Intercept", out IParameter interceptParameter)) {
84        Parameters.Add(new FixedValueParameter<DoubleValue>("Offset", "Additive factor (Intercept)", ((IValueParameter<DoubleValue>)interceptParameter).Value));
85        Parameters.Remove("Intercept");
86      }
87    }
88    #endregion
89
90
91    public override IEnumerable<double> Apply(IEnumerable<double> data) {
92      return Apply(data, Scale, Offset);
93    }
94
95    public override IEnumerable<double> InverseApply(IEnumerable<double> data) {
96      return InverseApply(data, Scale, Offset);
97    }
98
99
100    public static IEnumerable<double> Apply(IEnumerable<double> data, double scale = 1.0, double offset = 0.0) {
101      if (scale.IsAlmost(0.0))
102        throw new InvalidOperationException($"Cannot transform with a {nameof(scale)} of zero because inverse transformation would be invalid.");
103
104      return data.Select(x => scale * x + offset);
105    }
106
107    public static IEnumerable<double> InverseApply(IEnumerable<double> data, double scale = 1.0, double offset = 0.0) {
108      if (scale.IsAlmost(0.0))
109        throw new InvalidOperationException($"Cannot inverse transform with a {nameof(scale)} of zero.");
110
111      return data.Select(x => (x - offset) / scale);
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.