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

Last change on this file since 15884 was 15884, checked in by pfleck, 15 months ago

#2906 Refactoring

  • Moved transformation-specific parts out of existing interfaces.
  • Moved all Transformation logic to DataAnalysisTransformation.
  • Simplified (Inverse)Transformation of Dataset/ProblemData/Model/Solution.
File size: 4.1 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    IEnumerable<IDataAnalysisTransformation> IDataAnalysisTransformationModel.InputTransformations {
38      get { return InputTransformations; }
39    }
40
41    [Storable]
42    public ReadOnlyItemList<IDataAnalysisTransformation> InputTransformations { get; protected set; }
43
44    [Storable]
45    public ReadOnlyItemList<IDataAnalysisTransformation> TargetTransformations { get; protected set; }
46
47    // Usually, the TargetVariable is usually only implemented for Regression and Classification.
48    // However, we implement it in the base class for reducing code duplication and to avoid quasi-identical views.
49    [Storable]
50    private string targetVariable;
51    public string TargetVariable {
52      get { return targetVariable; }
53      set {
54        if (string.IsNullOrEmpty(value) || targetVariable == value) return;
55        targetVariable = value;
56        OnTargetVariableChanged(this, EventArgs.Empty);
57      }
58    }
59
60    public override IEnumerable<string> VariablesUsedForPrediction {
61      get { return DataAnalysisTransformation.ReduceVariables(OriginalModel.VariablesUsedForPrediction, InputTransformations); }
62    }
63
64    #region Constructor, Cloning & Persistence
65    protected DataAnalysisTransformationModel(IDataAnalysisModel originalModel, IEnumerable<IDataAnalysisTransformation> transformations)
66      : base("Transformation Model " + originalModel.Name) {
67      OriginalModel = originalModel;
68      var transitiveInputs = DataAnalysisTransformation.GetTransitiveVariables(originalModel.VariablesUsedForPrediction, transformations, inverse: true);
69      InputTransformations = new ItemList<IDataAnalysisTransformation>(transformations.Where(t => transitiveInputs.Contains(t.OriginalVariable))).AsReadOnly();
70      TargetTransformations = new ReadOnlyItemList<IDataAnalysisTransformation>();
71    }
72
73    protected DataAnalysisTransformationModel(DataAnalysisTransformationModel original, Cloner cloner)
74      : base(original, cloner) {
75      OriginalModel = cloner.Clone(original.OriginalModel);
76      InputTransformations = cloner.Clone(original.InputTransformations);
77      TargetTransformations = cloner.Clone(original.TargetTransformations);
78      targetVariable = original.targetVariable;
79    }
80
81    [StorableConstructor]
82    protected DataAnalysisTransformationModel(bool deserializing)
83      : base(deserializing) { }
84    #endregion
85
86    #region Events
87    public event EventHandler TargetVariableChanged;
88    private void OnTargetVariableChanged(object sender, EventArgs args) {
89      var changed = TargetVariableChanged;
90      if (changed != null)
91        changed(sender, args);
92    }
93    #endregion
94  }
95}
Note: See TracBrowser for help on using the repository browser.