Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/PreprocessingTransformator.cs @ 10980

Last change on this file since 10980 was 10980, checked in by pfleck, 10 years ago
  • Inverse iterate when backtransforming model
  • Insert CopyColumnTransformation in front of Transformations-List
File size: 6.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Text;
26using HeuristicLab.Data;
27using HeuristicLab.Problems.DataAnalysis.Transformations;
28
29namespace HeuristicLab.DataPreprocessing {
30  public class PreprocessingTransformator {
31    private readonly ITransactionalPreprocessingData 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 = (ITransactionalPreprocessingData)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;
45
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      } finally {
67        preprocessingData.EndTransaction();
68      }
69
70      return success;
71    }
72
73    private void PreserveColumns(IList<Transformation<double>> transformations) {
74      foreach (var transformation in transformations) {
75        if (!originalColumns.ContainsKey(transformation.Column)) {
76          int colIndex = preprocessingData.GetColumnIndex(transformation.Column);
77          var originalData = preprocessingData.GetValues<double>(colIndex);
78          originalColumns.Add(transformation.Column, originalData);
79        }
80      }
81    }
82
83    private void ApplyDoubleTranformationsInplace(IList<Transformation<double>> transformations, bool preserveColumns, out bool success, out string errorMsg) {
84      errorMsg = string.Empty;
85      success = true;
86      foreach (var transformation in transformations) {
87        int colIndex = preprocessingData.GetColumnIndex(transformation.Column);
88
89        var originalData = preprocessingData.GetValues<double>(colIndex);
90
91        string errorMsgPart;
92        bool successPart;
93        var transformedData = ApplyDoubleTransformation(transformation, originalData, out successPart, out errorMsgPart);
94        errorMsg += errorMsgPart + Environment.NewLine;
95
96        if (!successPart) success = false;
97        preprocessingData.SetValues(colIndex, transformedData.ToList());
98        preprocessingData.Transformations.Add(transformation);
99      }
100    }
101
102    private IEnumerable<double> ApplyDoubleTransformation(Transformation<double> transformation, IList<double> data, out bool success, out string errorMsg) {
103      success = transformation.Check(data, out errorMsg);
104      return transformation.Apply(data);
105    }
106
107    private void RenameTransformationColumnParameter(List<Transformation<double>> transformations) {
108      foreach (var transformation in transformations) {
109        var newColumnName = new StringValue(renamedColumns[transformation.Column]);
110        transformation.ColumnParameter.ValidValues.Add(newColumnName);
111        transformation.ColumnParameter.Value = newColumnName;
112      }
113    }
114
115    private void InsertCopyColumTransformations(IList<Transformation<double>> transformations) {
116      foreach (var renaming in renamedColumns) {
117        string oldName = renaming.Key;
118        string newName = renaming.Value;
119
120        var copyTransformation = CreateCopyTransformation(oldName, newName);
121        preprocessingData.Transformations.Insert(0, copyTransformation);
122      }
123    }
124
125    private CopyColumnTransformation CreateCopyTransformation(string oldColumn, string newColumn) {
126      var newColumName = new StringValue(newColumn);
127
128      var copyTransformation = new CopyColumnTransformation();
129      copyTransformation.ColumnParameter.ValidValues.Add(newColumName);
130      copyTransformation.ColumnParameter.Value = newColumName;
131
132      copyTransformation.CopiedColumnNameParameter.Value.Value = oldColumn;
133      return copyTransformation;
134    }
135
136    private void RenameTransformedColumsAndRestorePreservedColumns(IList<Transformation<double>> transformations) {
137      foreach (var column in originalColumns) {
138        int originalColumnIndex = preprocessingData.GetColumnIndex(column.Key);
139        int newColumnIndex = originalColumnIndex + 1;
140        string newColumnName = GetTransformatedColumnName(transformations, column.Key);
141        // save renaming mapping
142        renamedColumns[column.Key] = newColumnName;
143        // create new transformed column
144        preprocessingData.InsertColumn<double>(newColumnName, newColumnIndex);
145        preprocessingData.SetValues(newColumnIndex, preprocessingData.GetValues<double>(originalColumnIndex));
146        // restore old values
147        preprocessingData.SetValues(originalColumnIndex, column.Value);
148      }
149    }
150
151    private string GetTransformatedColumnName(IList<Transformation<double>> transformations, string column) {
152      string suffix = GetTransformationSuffix(transformations, column);
153      return column + "_" + suffix;
154    }
155
156    private string GetTransformationSuffix(IList<Transformation<double>> transformations, string column) {
157      var suffixes = transformations.Where(t => t.Column == column).Select(t => t.ShortName);
158      var builder = new StringBuilder();
159      foreach (var suffix in suffixes) {
160        builder.Append(suffix);
161      }
162      return builder.ToString();
163    }
164  }
165}
Note: See TracBrowser for help on using the repository browser.