Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisTransformationModel.cs @ 15880

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

#2906 forgot to commit file

File size: 6.5 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.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis {
30  [Item("Data Analysis Transformation Model", "A model that was transformed back to match the original variables after the training was performed on transformed variables.")]
31  [StorableClass]
32  public abstract class DataAnalysisTransformationModel : DataAnalysisModel, IDataAnalysisTransformationModel {
33
34    [Storable]
35    public IDataAnalysisModel OriginalModel { get; protected set; }
36
37    [Storable]
38    public ReadOnlyItemList<IDataAnalysisTransformation> InputTransformations { get; protected set; }
39
40    [Storable]
41    public ReadOnlyItemList<IDataAnalysisTransformation> TargetTransformations { get; protected set; }
42
43    // Usually, the TargetVariable is usually only implemented for Regression and Classification.
44    // However, we implement it in the base class for reducing code duplication and to avoid quasi-identical views.
45    [Storable]
46    private string targetVariable;
47    public string TargetVariable {
48      get { return targetVariable; }
49      set {
50        if (string.IsNullOrEmpty(value) || targetVariable == value) return;
51        targetVariable = value;
52        OnTargetVariableChanged(this, EventArgs.Empty);
53      }
54    }
55
56    public override IEnumerable<string> VariablesUsedForPrediction {
57      get { return OriginalModel.VariablesUsedForPrediction; /* TODO: reduce extend-inputs */}
58    }
59
60    #region Constructor, Cloning & Persistence
61    protected DataAnalysisTransformationModel(IDataAnalysisModel originalModel, IEnumerable<IDataAnalysisTransformation> transformations)
62      : base(originalModel.Name) {
63      OriginalModel = originalModel;
64      var transitiveInputs = CalculateTransitiveVariables(originalModel.VariablesUsedForPrediction, transformations);
65      InputTransformations = new ItemList<IDataAnalysisTransformation>(transformations.Where(t => transitiveInputs.Contains(t.OriginalVariable))).AsReadOnly();
66      TargetTransformations = new ReadOnlyItemList<IDataAnalysisTransformation>();
67    }
68
69    protected DataAnalysisTransformationModel(DataAnalysisTransformationModel original, Cloner cloner)
70      : base(original, cloner) {
71      OriginalModel = cloner.Clone(original.OriginalModel);
72      InputTransformations = cloner.Clone(original.InputTransformations);
73      TargetTransformations = cloner.Clone(original.TargetTransformations);
74      targetVariable = original.targetVariable;
75    }
76
77    [StorableConstructor]
78    protected DataAnalysisTransformationModel(bool deserializing)
79      : base(deserializing) { }
80    #endregion
81
82    // extended -> include originals
83    public static ISet<string> CalculateTransitiveVariables(IEnumerable<string> inputVariables, IEnumerable<IDataAnalysisTransformation> transformations) {
84      var transitiveInputs = new HashSet<string>(inputVariables);
85
86      foreach (var transformation in transformations.Reverse()) {
87        if (transitiveInputs.Contains(transformation.TransformedVariable)) {
88          transitiveInputs.Add(transformation.OriginalVariable);
89        }
90      }
91
92      return transitiveInputs;
93    }
94    // originals => include extended
95    public static IEnumerable<string> ExtendInputVariables(IEnumerable<string> oldInputVariables, IEnumerable<IDataAnalysisTransformation> transformations) {
96      var inputs = new HashSet<string>(oldInputVariables);
97
98      foreach (var transformation in transformations) {
99        if (inputs.Contains(transformation.OriginalVariable))
100          inputs.Add(transformation.TransformedVariable);
101      }
102
103      return inputs;
104    }
105    [Obsolete]
106    public static IEnumerable<string> RemoveVirtualVariables(IEnumerable<string> variables, IEnumerable<IDataAnalysisTransformation> transformations) {
107      var remainingVariables = new HashSet<string>(variables);
108
109      var transformationsStack = new Stack<IDataAnalysisTransformation>(transformations);
110
111      while (transformationsStack.Any()) {
112        var transformation = transformationsStack.Pop();
113
114
115        bool transformedVariablePending = transformationsStack.Any(x => x.OriginalVariable == transformation.TransformedVariable);
116        if (!transformedVariablePending)
117          remainingVariables.Remove(transformation.TransformedVariable);
118      }
119
120      return remainingVariables;
121    }
122
123    public static IDataset Transform(IDataset dataset, IEnumerable<IDataAnalysisTransformation> transformations) {
124      var modifiableDataset = ((Dataset)dataset).ToModifiable();
125
126      foreach (var transformation in transformations) {
127        var trans = (ITransformation<double>)transformation.Transformation;
128
129        var originalData = modifiableDataset.GetDoubleValues(transformation.OriginalVariable);
130        if (!trans.Check(originalData, out string errorMessage))
131          throw new InvalidOperationException($"Cannot estimate Values, Transformation is invalid: {errorMessage}");
132
133        var transformedData = trans.Apply(originalData).ToList();
134        if (modifiableDataset.VariableNames.Contains(transformation.TransformedVariable))
135          modifiableDataset.ReplaceVariable(transformation.TransformedVariable, transformedData);
136        else
137          modifiableDataset.AddVariable(transformation.TransformedVariable, transformedData);
138      }
139
140      return modifiableDataset;
141    }
142
143    #region Events
144    public event EventHandler TargetVariableChanged;
145    private void OnTargetVariableChanged(object sender, EventArgs args) {
146      var changed = TargetVariableChanged;
147      if (changed != null)
148        changed(sender, args);
149    }
150    #endregion
151  }
152}
Note: See TracBrowser for help on using the repository browser.