source: branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisTransformation.cs @ 15858

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

#2906 Fixed inverse transform early removal of columns.

File size: 5.8 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 System.Runtime.CompilerServices;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis {
33  [Item("Transformation", "A transformation applied to a DataAnalysisProblemData")]
34  [StorableClass]
35  public class DataAnalysisTransformation : ParameterizedNamedItem, IDataAnalysisTransformation {
36    #region Parameters
37    public IConstrainedValueParameter<StringValue> OriginalVariableParameter {
38      get { return (IConstrainedValueParameter<StringValue>)Parameters["Original Variable"]; }
39    }
40
41    public IConstrainedValueParameter<StringValue> TransformedVariableParameter {
42      get { return (IConstrainedValueParameter<StringValue>)Parameters["Transformed Variable"]; }
43    }
44
45    public IValueParameter<ITransformation> TransformationParameter {
46      get { return (IValueParameter<ITransformation>)Parameters["Transformation"]; }
47    }
48    #endregion
49
50    #region Properties
51    public string OriginalVariable {
52      get { return OriginalVariableParameter.Value.Value; }
53      set { SetConstrainedValue(OriginalVariableParameter, value); }
54    }
55
56    public string TransformedVariable {
57      get { return TransformedVariableParameter.Value.Value; }
58      set { SetConstrainedValue(TransformedVariableParameter, value); }
59    }
60
61    public ITransformation Transformation {
62      get { return TransformationParameter.Value; }
63      set { TransformationParameter.Value = value; }
64    }
65
66    private static void SetConstrainedValue(IConstrainedValueParameter<StringValue> parameter, string value, [CallerMemberName] string caller = null) {
67      if (value == null) throw new ArgumentNullException(caller);
68      if (value == parameter.Value.Value) return;
69
70      var matchingValue = parameter.ValidValues.SingleOrDefault(v => v.Value == value);
71      if (matchingValue == null) throw new ArgumentException("The provided value is not valid.", caller);
72      parameter.Value = matchingValue;
73    }
74    #endregion
75
76    #region Constructor, Cloning & Persistence
77    public DataAnalysisTransformation(IEnumerable<StringValue> variableNames)
78      : base() {
79      var originalVariables = new ItemSet<StringValue>(variableNames.Select(x => x.AsReadOnly()));
80      var transformedVariables = new ItemSet<StringValue>(variableNames.Select(x => x.AsReadOnly()));
81      Parameters.Add(new ConstrainedValueParameter<StringValue>("Original Variable", new ItemSet<StringValue>(originalVariables), originalVariables.FirstOrDefault()));
82      Parameters.Add(new ConstrainedValueParameter<StringValue>("Transformed Variable", new ItemSet<StringValue>(transformedVariables), transformedVariables.FirstOrDefault()));
83
84      //var transformations = new ItemSet<ITransformation>(ApplicationManager.Manager.GetInstances<ITransformation>());
85      Parameters.Add(new ValueParameter<ITransformation>("Transformation", new IdentityTransformation()));
86
87      RegisterEventHandlers();
88    }
89
90    protected DataAnalysisTransformation(DataAnalysisTransformation original, Cloner cloner)
91      : base(original, cloner) {
92      RegisterEventHandlers();
93    }
94
95    public override IDeepCloneable Clone(Cloner cloner) {
96      return new DataAnalysisTransformation(this, cloner);
97    }
98
99    [StorableConstructor]
100    protected DataAnalysisTransformation(bool deserializing)
101      : base(deserializing) { }
102
103    [StorableHook(HookType.AfterDeserialization)]
104    private void AfterDeserialization() {
105      RegisterEventHandlers();
106    }
107    #endregion
108
109    #region Event-Handling
110    private void RegisterEventHandlers() {
111      OriginalVariableParameter.ValueChanged += OriginalVariableParameter_ValueChanged;
112      TransformedVariableParameter.ValueChanged += TransformedVariableParameter_ValueChanged;
113      TransformationParameter.ValueChanged += TransformationParameter_ValueChanged;
114    }
115
116    private void OriginalVariableParameter_ValueChanged(object sender, EventArgs e) {
117      OriginalVariableParameter.Value.ValueChanged += OriginalVariableParameterValue_ValueChanged;
118      OnToStringChanged();
119    }
120    private void OriginalVariableParameterValue_ValueChanged(object sender, EventArgs e) {
121      OnToStringChanged();
122    }
123
124    private void TransformedVariableParameter_ValueChanged(object sender, EventArgs e) {
125      TransformedVariableParameter.Value.ValueChanged += TransformedVariableParameterValue_ValueChanged;
126      OnToStringChanged();
127    }
128    private void TransformedVariableParameterValue_ValueChanged(object sender, EventArgs e) {
129      OnToStringChanged();
130    }
131
132    private void TransformationParameter_ValueChanged(object sender, EventArgs e) {
133      OnToStringChanged();
134    }
135    #endregion
136
137
138    public override string ToString() {
139      return $"{Transformation} ({OriginalVariable} -> {TransformedVariable})";
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.