Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/PreprocessingTransformator.cs @ 10809

Last change on this file since 10809 was 10809, checked in by sbreuer, 11 years ago
  • selected average and co. implemented
  • SelectionChanged NullPointer fixed
File size: 2.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 HeuristicLab.Problems.DataAnalysis.Symbolic;
26using HeuristicLab.Problems.DataAnalysis.Transformations;
27
28namespace HeuristicLab.DataPreprocessing {
29  public class PreprocessingTransformator {
30    private readonly ITransactionalPreprocessingData preprocessingData;
31
32    public PreprocessingTransformator(IPreprocessingData preprocessingData) {
33      this.preprocessingData = (ITransactionalPreprocessingData)preprocessingData;
34    }
35
36    public bool ApplyTransformations(IEnumerable<ITransformation> transformations) {
37      bool success = true;
38
39      preprocessingData.InTransaction(() => {
40        var doubleTransformations = transformations.OfType<Transformation<double>>().ToList();
41        ApplyDoubleTranformations(doubleTransformations, out success);
42      }, DataPreprocessingChangedEventType.Transformation);
43
44      if (!success) {
45        // TODO: check undo remove transformations
46        preprocessingData.Undo();
47      }
48
49      return success;
50    }
51
52    private void ApplyDoubleTranformations(IEnumerable<Transformation<double>> transformations, out bool success) {
53      foreach (var transformation in transformations) {
54        int colIndex = preprocessingData.GetColumnIndex(transformation.Column);
55
56        var originalData = preprocessingData.GetValues<double>(colIndex, false);
57        var transformedData = ApplyDoubleTransformation(transformation, originalData, out success);
58        if (!success) return;
59
60        preprocessingData.SetValues(colIndex, transformedData.ToList());
61        preprocessingData.Transformations.Add(transformation);
62      }
63      success = true;
64    }
65
66    private IEnumerable<double> ApplyDoubleTransformation(Transformation<double> transformation, IEnumerable<double> data, out bool success) {
67      string errorMsg;
68      if (transformation.Check(data, out errorMsg)) {
69        success = true;
70        return transformation.Apply(data);
71      }
72      throw new NotImplementedException("Display Check Result");
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.