Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2906 First concept of simple transformation (single target transformation)

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