Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2906_Transformations/HeuristicLab.DataPreprocessing/3.4/Content/TransformationContent.cs @ 18066

Last change on this file since 18066 was 15865, checked in by pfleck, 7 years ago

#2906 Added PreprocessingTransformation as a custom view-model for transformations in preprocessing.

File size: 3.9 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.Collections.Generic;
23using System.Drawing;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.DataPreprocessing {
31
32  [Item("Transformation", "Represents the transformation grid.")]
33  [StorableClass]
34  public class TransformationContent : PreprocessingContent, IViewShortcut {
35    public static new Image StaticItemImage {
36      get { return HeuristicLab.Common.Resources.VSImageLibrary.Method; }
37    }
38
39    [Storable]
40    public IItemList<PreprocessingTransformation> TransformationList { get; private set; }
41
42    #region Constructor, Cloning & Persistence
43    public TransformationContent(IFilteredPreprocessingData preprocessingData)
44      : base(preprocessingData) {
45      TransformationList = new ItemList<PreprocessingTransformation>();
46    }
47
48    public TransformationContent(TransformationContent original, Cloner cloner)
49      : base(original, cloner) {
50      TransformationList = cloner.Clone(original.TransformationList);
51    }
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new TransformationContent(this, cloner);
54    }
55
56    [StorableConstructor]
57    protected TransformationContent(bool deserializing)
58      : base(deserializing) { }
59    #endregion
60
61    public bool ApplyTransformations(out IEnumerable<string> errorMessages) {
62      bool success = true;
63      var errors = new List<string>();
64      errorMessages = errors;
65
66      foreach (var transformation in TransformationList.Where(x => !x.IsApplied)) {
67        var sourceVariable = transformation.OriginalVariable;
68        var targetVariable = transformation.TransformedVariable ?? sourceVariable + " Transformed";
69        var sourceIdx = PreprocessingData.GetColumnIndex(sourceVariable);
70
71        if (transformation.Transformation is ITransformation<double> trans && PreprocessingData.VariableHasType<double>(sourceIdx)) {
72          if (!PreprocessingData.VariableNames.Contains(targetVariable))
73            PreprocessingData.InsertColumn<double>(targetVariable, PreprocessingData.Columns);
74          var targetIdx = PreprocessingData.GetColumnIndex(targetVariable);
75
76          if (!PreprocessingData.VariableHasType<double>(targetIdx)) { success = false; errors.Add("Target column is not double."); continue; }
77
78          var sourceData = PreprocessingData.GetValues<double>(sourceIdx);
79          if (!trans.Check(sourceData, out string msg)) { success = false; errors.Add(msg); continue; }
80
81          trans.Configure(sourceData);
82          var transformedData = trans.Apply(sourceData).ToList();
83
84          PreprocessingData.SetValues(targetIdx, transformedData);
85
86          PreprocessingData.Transformations.Add(transformation);
87          transformation.IsApplied = true;
88          // TODO: remove unused valid values in constrainedParameters
89        } else {
90          success = false;
91          errors.Add("Transformation datatype is not supported.");
92        }
93      }
94
95      return success;
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.