Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10820 was 10819, checked in by pfleck, 11 years ago
  • Implemented handling of transformation error messages.
File size: 3.0 KB
RevLine 
[10786]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.Transformations;
26
27namespace HeuristicLab.DataPreprocessing {
28  public class PreprocessingTransformator {
29    private readonly ITransactionalPreprocessingData preprocessingData;
30
31    public PreprocessingTransformator(IPreprocessingData preprocessingData) {
32      this.preprocessingData = (ITransactionalPreprocessingData)preprocessingData;
33    }
34
[10819]35    public bool ApplyTransformations(IEnumerable<ITransformation> transformations, out string errorMsg) {
36      bool success;
[10786]37
[10819]38      preprocessingData.BeginTransaction(DataPreprocessingChangedEventType.Transformation);
39      try {
[10786]40        var doubleTransformations = transformations.OfType<Transformation<double>>().ToList();
[10819]41        ApplyDoubleTranformations(doubleTransformations, out success, out errorMsg);
42      } finally {
43        preprocessingData.EndTransaction();
44        //if (!success)
45        //preprocessingData.Undo();
[10786]46      }
47
48      return success;
49    }
50
[10819]51    private void ApplyDoubleTranformations(IEnumerable<Transformation<double>> transformations, out bool success, out string errorMsg) {
52      errorMsg = string.Empty;
53      success = true;
[10786]54      foreach (var transformation in transformations) {
55        int colIndex = preprocessingData.GetColumnIndex(transformation.Column);
56
[10811]57        var originalData = preprocessingData.GetValues<double>(colIndex);
[10819]58        string errorMsgPart;
59        bool successPart;
60        var transformedData = ApplyDoubleTransformation(transformation, originalData, out successPart, out errorMsgPart);
61        errorMsg += errorMsgPart + Environment.NewLine;
62        //if (!success) return;
63        if (!successPart) success = false;
[10786]64        preprocessingData.SetValues(colIndex, transformedData.ToList());
65        preprocessingData.Transformations.Add(transformation);
66      }
[10819]67
[10786]68    }
69
[10819]70    private IEnumerable<double> ApplyDoubleTransformation(Transformation<double> transformation, IEnumerable<double> data, out bool success, out string errorMsg) {
71      success = transformation.Check(data, out errorMsg);
72      return transformation.Apply(data);
[10786]73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.