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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Problems.DataAnalysis.Transformations;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.DataPreprocessing {
|
---|
28 | public class PreprocessingTransformator {
|
---|
29 | private readonly ITransactionalPreprocessingData preprocessingData;
|
---|
30 |
|
---|
31 | public PreprocessingTransformator(IPreprocessingData preprocessingData) {
|
---|
32 | this.preprocessingData = (ITransactionalPreprocessingData)preprocessingData;
|
---|
33 | }
|
---|
34 |
|
---|
35 | public bool ApplyTransformations(IEnumerable<ITransformation> transformations, out string errorMsg) {
|
---|
36 | bool success;
|
---|
37 |
|
---|
38 | preprocessingData.BeginTransaction(DataPreprocessingChangedEventType.Transformation);
|
---|
39 | try {
|
---|
40 | var doubleTransformations = transformations.OfType<Transformation<double>>().ToList();
|
---|
41 | ApplyDoubleTranformations(doubleTransformations, out success, out errorMsg);
|
---|
42 | } finally {
|
---|
43 | preprocessingData.EndTransaction();
|
---|
44 | //if (!success)
|
---|
45 | //preprocessingData.Undo();
|
---|
46 | }
|
---|
47 |
|
---|
48 | return success;
|
---|
49 | }
|
---|
50 |
|
---|
51 | private void ApplyDoubleTranformations(IEnumerable<Transformation<double>> transformations, out bool success, out string errorMsg) {
|
---|
52 | errorMsg = string.Empty;
|
---|
53 | success = true;
|
---|
54 | foreach (var transformation in transformations) {
|
---|
55 | int colIndex = preprocessingData.GetColumnIndex(transformation.Column);
|
---|
56 |
|
---|
57 | var originalData = preprocessingData.GetValues<double>(colIndex);
|
---|
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;
|
---|
64 | preprocessingData.SetValues(colIndex, transformedData.ToList());
|
---|
65 | preprocessingData.Transformations.Add(transformation);
|
---|
66 | }
|
---|
67 |
|
---|
68 | }
|
---|
69 |
|
---|
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);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|