Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10948 was 10948, checked in by pfleck, 10 years ago
  • Implemented preserve/copy columns on transformations.
File size: 4.7 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    public PreprocessingTransformator(IPreprocessingData preprocessingData) {
36      this.preprocessingData = (ITransactionalPreprocessingData)preprocessingData;
37      originalColumns = new Dictionary<string, IList<double>>();
38    }
39
40    public bool ApplyTransformations(IEnumerable<ITransformation> transformations, bool preserveColumns, out string errorMsg) {
41      bool success;
42
43      preprocessingData.BeginTransaction(DataPreprocessingChangedEventType.Transformation);
44      try {
45        var doubleTransformations = transformations.OfType<Transformation<double>>().ToList();
46        ApplyDoubleTranformations(doubleTransformations, preserveColumns, out success, out errorMsg);
47
48        if (preserveColumns) {
49          RestorePreservedColumns(doubleTransformations);
50          originalColumns.Clear();
51        }
52      } finally {
53        preprocessingData.EndTransaction();
54      }
55
56      return success;
57    }
58
59    private void ApplyDoubleTranformations(IList<Transformation<double>> transformations, bool preserveColumns, out bool success, out string errorMsg) {
60      errorMsg = string.Empty;
61      success = true;
62      foreach (var transformation in transformations) {
63        int colIndex = preprocessingData.GetColumnIndex(transformation.Column);
64
65        var originalData = preprocessingData.GetValues<double>(colIndex);
66
67        if (preserveColumns && !originalColumns.ContainsKey(transformation.Column))
68          originalColumns.Add(transformation.Column, originalData);
69
70        string errorMsgPart;
71        bool successPart;
72        var transformedData = ApplyDoubleTransformation(transformation, originalData, out successPart, out errorMsgPart);
73        errorMsg += errorMsgPart + Environment.NewLine;
74
75        if (!successPart) success = false;
76        preprocessingData.SetValues(colIndex, transformedData.ToList());
77        preprocessingData.Transformations.Add(transformation);
78      }
79    }
80
81    private IEnumerable<double> ApplyDoubleTransformation(Transformation<double> transformation, IList<double> data, out bool success, out string errorMsg) {
82      success = transformation.Check(data, out errorMsg);
83      return transformation.Apply(data);
84    }
85
86    private void RestorePreservedColumns(IList<Transformation<double>> transformations) {
87      foreach (var column in originalColumns) {
88        int originalColumnIndex = preprocessingData.GetColumnIndex(column.Key);
89        int newColumnIndex = originalColumnIndex + 1;
90        string newColumnName = GetTransformatedColumnName(transformations, column.Key);
91
92        // create new transformed column
93        preprocessingData.InsertColumn<double>(newColumnName, newColumnIndex);
94        preprocessingData.SetValues(newColumnIndex, preprocessingData.GetValues<double>(originalColumnIndex));
95        // restore old values
96        preprocessingData.SetValues(originalColumnIndex, column.Value);
97      }
98    }
99
100    private string GetTransformatedColumnName(IList<Transformation<double>> transformations, string column) {
101      string suffix = GetTransformationSuffix(transformations, column);
102      return column + "_" + suffix;
103    }
104
105    private string GetTransformationSuffix(IList<Transformation<double>> transformations, string column) {
106      var suffixes = transformations.Where(t => t.Column == column).Select(t => t.ShortName);
107      var builder = new StringBuilder();
108      foreach (var suffix in suffixes) {
109        builder.Append(suffix);
110      }
111      return builder.ToString();
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.