Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2906

  • Implemented for classification, clustering, etc.
  • Simplified Transformation interfaces (read-only, ...).
  • Started moving transformation logic out of ProblemData.
File size: 6.1 KB
RevLine 
[15846]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
[15870]22using System;
[15846]23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis {
[15870]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.")]
[15846]31  [StorableClass]
[15870]32  public abstract class DataAnalysisTransformationModel : DataAnalysisModel, IDataAnalysisTransformationModel {
[15846]33
34    [Storable]
[15870]35    public IDataAnalysisModel OriginalModel { get; protected set; }
[15846]36
37    [Storable]
[15870]38    public ReadOnlyItemList<IDataAnalysisTransformation> InputTransformations { get; protected set; }
[15846]39
[15870]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
[15846]56    public override IEnumerable<string> VariablesUsedForPrediction {
[15870]57      get { return OriginalModel.VariablesUsedForPrediction; /* TODO: reduce extend-inputs */}
[15846]58    }
59
60    #region Constructor, Cloning & Persistence
[15870]61    protected DataAnalysisTransformationModel(IDataAnalysisModel originalModel, IEnumerable<IDataAnalysisTransformation> transformations)
62      : base(originalModel.Name) {
[15846]63      OriginalModel = originalModel;
[15870]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>();
[15846]67    }
68
[15870]69    protected DataAnalysisTransformationModel(DataAnalysisTransformationModel original, Cloner cloner)
[15846]70      : base(original, cloner) {
71      OriginalModel = cloner.Clone(original.OriginalModel);
[15870]72      InputTransformations = cloner.Clone(original.InputTransformations);
73      TargetTransformations = cloner.Clone(original.TargetTransformations);
74      targetVariable = original.targetVariable;
[15846]75    }
76
77    [StorableConstructor]
[15870]78    protected DataAnalysisTransformationModel(bool deserializing)
[15846]79      : base(deserializing) { }
80    #endregion
81
[15870]82    public static ISet<string> CalculateTransitiveVariables(IEnumerable<string> inputVariables, IEnumerable<IDataAnalysisTransformation> transformations) {
83      var transitiveInputs = new HashSet<string>(inputVariables);
[15846]84
[15870]85      foreach (var transformation in transformations.Reverse()) {
86        if (transitiveInputs.Contains(transformation.TransformedVariable)) {
87          transitiveInputs.Add(transformation.OriginalVariable);
88        }
89      }
[15846]90
[15870]91      return transitiveInputs;
[15846]92    }
93
[15870]94    public static IDataset Transform(IDataset dataset, IEnumerable<IDataAnalysisTransformation> transformations) {
95      var modifiableDataset = ((Dataset)dataset).ToModifiable();
96
97      foreach (var transformation in transformations) {
98        var trans = (ITransformation<double>)transformation.Transformation;
99
100        var originalData = modifiableDataset.GetDoubleValues(transformation.OriginalVariable);
101        if (!trans.Check(originalData, out string errorMessage))
102          throw new InvalidOperationException($"Cannot estimate Values, Transformation is invalid: {errorMessage}");
103
104        var transformedData = trans.Apply(originalData).ToList();
105        if (modifiableDataset.VariableNames.Contains(transformation.TransformedVariable))
106          modifiableDataset.ReplaceVariable(transformation.TransformedVariable, transformedData);
107        else
108          modifiableDataset.AddVariable(transformation.TransformedVariable, transformedData);
109      }
110
111      return modifiableDataset;
[15846]112    }
113
[15870]114
115
116    #region Events
117    public event EventHandler TargetVariableChanged;
118    private void OnTargetVariableChanged(object sender, EventArgs args) {
119      var changed = TargetVariableChanged;
120      if (changed != null)
121        changed(sender, args);
[15847]122    }
[15870]123    #endregion
[15846]124
125
126
127
128
[15870]129
130    /*
131    // dataset in original data range
132    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
133      var transformedDataset = Transform(dataset, Transformations);
134
135      var estimates = OriginalModel.GetEstimatedValues(transformedDataset, rows);
136
137      return InverseTransform(estimates, Transformations, OriginalModel.TargetVariable);
[15846]138    }
[15870]139
140    // problemData in original data range
141    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
142      // TODO: specialized views for the original solution type are lost (RandomForestSolutionView, ...)
143      return new RegressionSolution(this, new RegressionProblemData(problemData));
144    } */
[15846]145  }
146}
Note: See TracBrowser for help on using the repository browser.