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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Drawing;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.DataPreprocessing {
|
---|
31 | [Item("Transformation", "Represents the transformation grid.")]
|
---|
32 | [StorableClass]
|
---|
33 | public class TransformationContent : PreprocessingContent, IViewShortcut {
|
---|
34 | public static new Image StaticItemImage {
|
---|
35 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Method; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | [Storable]
|
---|
39 | public ICheckedItemList<IDataAnalysisTransformation> CheckedTransformationList { get; private set; }
|
---|
40 |
|
---|
41 | #region Constructor, Cloning & Persistence
|
---|
42 | public TransformationContent(IFilteredPreprocessingData preprocessingData)
|
---|
43 | : base(preprocessingData) {
|
---|
44 | CheckedTransformationList = new CheckedItemList<IDataAnalysisTransformation>();
|
---|
45 | }
|
---|
46 |
|
---|
47 | public TransformationContent(TransformationContent original, Cloner cloner)
|
---|
48 | : base(original, cloner) {
|
---|
49 | CheckedTransformationList = cloner.Clone(original.CheckedTransformationList);
|
---|
50 | }
|
---|
51 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
52 | return new TransformationContent(this, cloner);
|
---|
53 | }
|
---|
54 |
|
---|
55 | [StorableConstructor]
|
---|
56 | protected TransformationContent(bool deserializing)
|
---|
57 | : base(deserializing) { }
|
---|
58 | #endregion
|
---|
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 | }
|
---|
97 | }
|
---|
98 | }
|
---|