1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Problems.DataAnalysis;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.DataPreprocessing {
|
---|
30 | public class PreprocessingTransformator {
|
---|
31 | private readonly IPreprocessingData preprocessingData;
|
---|
32 |
|
---|
33 | private readonly IDictionary<string, IList<double>> originalColumns;
|
---|
34 |
|
---|
35 | private readonly IDictionary<string, string> renamedColumns;
|
---|
36 |
|
---|
37 | public PreprocessingTransformator(IPreprocessingData preprocessingData) {
|
---|
38 | this.preprocessingData = preprocessingData;
|
---|
39 | originalColumns = new Dictionary<string, IList<double>>();
|
---|
40 | renamedColumns = new Dictionary<string, string>();
|
---|
41 | }
|
---|
42 |
|
---|
43 | public bool ApplyTransformations(IEnumerable<ITransformation> transformations, bool preserveColumns, out string errorMsg) {
|
---|
44 | bool success = false;
|
---|
45 | errorMsg = string.Empty;
|
---|
46 | preprocessingData.BeginTransaction(DataPreprocessingChangedEventType.Transformation);
|
---|
47 |
|
---|
48 | try {
|
---|
49 | var doubleTransformations = transformations.OfType<Transformation<double>>().ToList();
|
---|
50 |
|
---|
51 | if (preserveColumns) {
|
---|
52 | PreserveColumns(doubleTransformations);
|
---|
53 | }
|
---|
54 |
|
---|
55 | // all transformations are performed inplace. no creation of new columns for transformations
|
---|
56 | ApplyDoubleTranformationsInplace(doubleTransformations, preserveColumns, out success, out errorMsg);
|
---|
57 |
|
---|
58 | if (preserveColumns) {
|
---|
59 | RenameTransformedColumsAndRestorePreservedColumns(doubleTransformations);
|
---|
60 | RenameTransformationColumnParameter(doubleTransformations);
|
---|
61 | InsertCopyColumTransformations(doubleTransformations);
|
---|
62 |
|
---|
63 | originalColumns.Clear();
|
---|
64 | renamedColumns.Clear();
|
---|
65 | }
|
---|
66 | // only accept changes if everything was successful
|
---|
67 | if (!success) {
|
---|
68 | preprocessingData.Undo();
|
---|
69 | }
|
---|
70 | } catch (Exception e) {
|
---|
71 | preprocessingData.Undo();
|
---|
72 | if (string.IsNullOrEmpty(errorMsg)) errorMsg = e.Message;
|
---|
73 | } finally {
|
---|
74 | preprocessingData.EndTransaction();
|
---|
75 | }
|
---|
76 |
|
---|
77 | return success;
|
---|
78 | }
|
---|
79 |
|
---|
80 | private void PreserveColumns(IEnumerable<Transformation<double>> transformations) {
|
---|
81 | foreach (var transformation in transformations) {
|
---|
82 | if (!originalColumns.ContainsKey(transformation.Column)) {
|
---|
83 | int colIndex = preprocessingData.GetColumnIndex(transformation.Column);
|
---|
84 | var originalData = preprocessingData.GetValues<double>(colIndex);
|
---|
85 | originalColumns.Add(transformation.Column, originalData);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | private void ApplyDoubleTranformationsInplace(IEnumerable<Transformation<double>> transformations, bool preserveColumns, out bool success, out string errorMsg) {
|
---|
91 | errorMsg = string.Empty;
|
---|
92 | success = true;
|
---|
93 | foreach (var transformation in transformations) {
|
---|
94 | int colIndex = preprocessingData.GetColumnIndex(transformation.Column);
|
---|
95 |
|
---|
96 | var originalData = preprocessingData.GetValues<double>(colIndex);
|
---|
97 |
|
---|
98 | string errorMsgPart;
|
---|
99 | bool successPart;
|
---|
100 | var transformedData = ApplyDoubleTransformation(transformation, originalData, out successPart, out errorMsgPart);
|
---|
101 | errorMsg += errorMsgPart + Environment.NewLine;
|
---|
102 |
|
---|
103 | if (!successPart) success = false;
|
---|
104 | preprocessingData.SetValues(colIndex, transformedData.ToList());
|
---|
105 | preprocessingData.Transformations.Add(transformation);
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | private IEnumerable<double> ApplyDoubleTransformation(Transformation<double> transformation, IList<double> data, out bool success, out string errorMsg) {
|
---|
110 | success = transformation.Check(data, out errorMsg);
|
---|
111 | // don't apply when the check fails
|
---|
112 | if (success)
|
---|
113 | return transformation.ConfigureAndApply(data);
|
---|
114 | else
|
---|
115 | return data;
|
---|
116 | }
|
---|
117 |
|
---|
118 | private void RenameTransformationColumnParameter(List<Transformation<double>> transformations) {
|
---|
119 | foreach (var transformation in transformations) {
|
---|
120 | var newColumnName = new StringValue(renamedColumns[transformation.Column]);
|
---|
121 | transformation.ColumnParameter.ValidValues.Add(newColumnName);
|
---|
122 | transformation.ColumnParameter.Value = newColumnName;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | private void InsertCopyColumTransformations(IList<Transformation<double>> transformations) {
|
---|
127 | foreach (var renaming in renamedColumns) {
|
---|
128 | string oldName = renaming.Key;
|
---|
129 | string newName = renaming.Value;
|
---|
130 |
|
---|
131 | var copyTransformation = CreateCopyTransformation(oldName, newName);
|
---|
132 | preprocessingData.Transformations.Insert(0, copyTransformation);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | private CopyColumnTransformation CreateCopyTransformation(string oldColumn, string newColumn) {
|
---|
137 | var newColumName = new StringValue(newColumn);
|
---|
138 |
|
---|
139 | var copyTransformation = new CopyColumnTransformation();
|
---|
140 | copyTransformation.ColumnParameter.ValidValues.Add(newColumName);
|
---|
141 | copyTransformation.ColumnParameter.Value = newColumName;
|
---|
142 |
|
---|
143 | copyTransformation.CopiedColumnNameParameter.Value.Value = oldColumn;
|
---|
144 | return copyTransformation;
|
---|
145 | }
|
---|
146 |
|
---|
147 | private void RenameTransformedColumsAndRestorePreservedColumns(IList<Transformation<double>> transformations) {
|
---|
148 | foreach (var column in originalColumns) {
|
---|
149 | int originalColumnIndex = preprocessingData.GetColumnIndex(column.Key);
|
---|
150 | int newColumnIndex = originalColumnIndex + 1;
|
---|
151 | string newColumnName = GetTransformatedColumnName(transformations, column.Key);
|
---|
152 | // save renaming mapping
|
---|
153 | renamedColumns[column.Key] = newColumnName;
|
---|
154 | // create new transformed column
|
---|
155 | preprocessingData.InsertColumn<double>(newColumnName, newColumnIndex);
|
---|
156 | preprocessingData.SetValues(newColumnIndex, preprocessingData.GetValues<double>(originalColumnIndex));
|
---|
157 | // restore old values
|
---|
158 | preprocessingData.SetValues(originalColumnIndex, column.Value);
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | private string GetTransformatedColumnName(IList<Transformation<double>> transformations, string column) {
|
---|
163 | string suffix = GetTransformationSuffix(transformations, column);
|
---|
164 | return column + "_" + suffix;
|
---|
165 | }
|
---|
166 |
|
---|
167 | private string GetTransformationSuffix(IList<Transformation<double>> transformations, string column) {
|
---|
168 | var suffixes = transformations.Where(t => t.Column == column).Select(t => t.ShortName);
|
---|
169 | var builder = new StringBuilder();
|
---|
170 | foreach (var suffix in suffixes) {
|
---|
171 | builder.Append(suffix);
|
---|
172 | }
|
---|
173 | return builder.ToString();
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|