[10786] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10786] | 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;
|
---|
[10948] | 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
[11068] | 27 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[10786] | 28 |
|
---|
| 29 | namespace HeuristicLab.DataPreprocessing {
|
---|
| 30 | public class PreprocessingTransformator {
|
---|
| 31 | private readonly ITransactionalPreprocessingData preprocessingData;
|
---|
| 32 |
|
---|
[10948] | 33 | private readonly IDictionary<string, IList<double>> originalColumns;
|
---|
| 34 |
|
---|
[10976] | 35 | private readonly IDictionary<string, string> renamedColumns;
|
---|
| 36 |
|
---|
[10786] | 37 | public PreprocessingTransformator(IPreprocessingData preprocessingData) {
|
---|
| 38 | this.preprocessingData = (ITransactionalPreprocessingData)preprocessingData;
|
---|
[10948] | 39 | originalColumns = new Dictionary<string, IList<double>>();
|
---|
[10976] | 40 | renamedColumns = new Dictionary<string, string>();
|
---|
[10786] | 41 | }
|
---|
| 42 |
|
---|
[10948] | 43 | public bool ApplyTransformations(IEnumerable<ITransformation> transformations, bool preserveColumns, out string errorMsg) {
|
---|
[11159] | 44 | bool success = false;
|
---|
| 45 | errorMsg = string.Empty;
|
---|
[10819] | 46 | preprocessingData.BeginTransaction(DataPreprocessingChangedEventType.Transformation);
|
---|
[10976] | 47 |
|
---|
[10819] | 48 | try {
|
---|
[10786] | 49 | var doubleTransformations = transformations.OfType<Transformation<double>>().ToList();
|
---|
[10948] | 50 |
|
---|
| 51 | if (preserveColumns) {
|
---|
[10976] | 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 |
|
---|
[10948] | 63 | originalColumns.Clear();
|
---|
[10976] | 64 | renamedColumns.Clear();
|
---|
[10948] | 65 | }
|
---|
[11159] | 66 | // only accept changes if everything was successful
|
---|
| 67 | if (!success) {
|
---|
| 68 | preprocessingData.Undo();
|
---|
| 69 | }
|
---|
[11068] | 70 | }
|
---|
[11159] | 71 | catch (Exception e) {
|
---|
| 72 | preprocessingData.Undo();
|
---|
| 73 | if (string.IsNullOrEmpty(errorMsg)) errorMsg = e.Message;
|
---|
| 74 | }
|
---|
[11068] | 75 | finally {
|
---|
[10819] | 76 | preprocessingData.EndTransaction();
|
---|
[10786] | 77 | }
|
---|
| 78 |
|
---|
| 79 | return success;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[11068] | 82 | private void PreserveColumns(IEnumerable<Transformation<double>> transformations) {
|
---|
[10976] | 83 | foreach (var transformation in transformations) {
|
---|
| 84 | if (!originalColumns.ContainsKey(transformation.Column)) {
|
---|
| 85 | int colIndex = preprocessingData.GetColumnIndex(transformation.Column);
|
---|
| 86 | var originalData = preprocessingData.GetValues<double>(colIndex);
|
---|
| 87 | originalColumns.Add(transformation.Column, originalData);
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[11068] | 92 | private void ApplyDoubleTranformationsInplace(IEnumerable<Transformation<double>> transformations, bool preserveColumns, out bool success, out string errorMsg) {
|
---|
[10819] | 93 | errorMsg = string.Empty;
|
---|
| 94 | success = true;
|
---|
[10786] | 95 | foreach (var transformation in transformations) {
|
---|
| 96 | int colIndex = preprocessingData.GetColumnIndex(transformation.Column);
|
---|
| 97 |
|
---|
[10811] | 98 | var originalData = preprocessingData.GetValues<double>(colIndex);
|
---|
[10948] | 99 |
|
---|
[10819] | 100 | string errorMsgPart;
|
---|
| 101 | bool successPart;
|
---|
| 102 | var transformedData = ApplyDoubleTransformation(transformation, originalData, out successPart, out errorMsgPart);
|
---|
| 103 | errorMsg += errorMsgPart + Environment.NewLine;
|
---|
[10948] | 104 |
|
---|
[10819] | 105 | if (!successPart) success = false;
|
---|
[10786] | 106 | preprocessingData.SetValues(colIndex, transformedData.ToList());
|
---|
| 107 | preprocessingData.Transformations.Add(transformation);
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[10948] | 111 | private IEnumerable<double> ApplyDoubleTransformation(Transformation<double> transformation, IList<double> data, out bool success, out string errorMsg) {
|
---|
[10819] | 112 | success = transformation.Check(data, out errorMsg);
|
---|
[11159] | 113 | // don't apply when the check fails
|
---|
| 114 | if (success)
|
---|
[15142] | 115 | return transformation.ConfigureAndApply(data);
|
---|
[11159] | 116 | else
|
---|
| 117 | return data;
|
---|
[10786] | 118 | }
|
---|
[10948] | 119 |
|
---|
[10976] | 120 | private void RenameTransformationColumnParameter(List<Transformation<double>> transformations) {
|
---|
| 121 | foreach (var transformation in transformations) {
|
---|
| 122 | var newColumnName = new StringValue(renamedColumns[transformation.Column]);
|
---|
| 123 | transformation.ColumnParameter.ValidValues.Add(newColumnName);
|
---|
| 124 | transformation.ColumnParameter.Value = newColumnName;
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | private void InsertCopyColumTransformations(IList<Transformation<double>> transformations) {
|
---|
| 129 | foreach (var renaming in renamedColumns) {
|
---|
| 130 | string oldName = renaming.Key;
|
---|
| 131 | string newName = renaming.Value;
|
---|
| 132 |
|
---|
| 133 | var copyTransformation = CreateCopyTransformation(oldName, newName);
|
---|
[10980] | 134 | preprocessingData.Transformations.Insert(0, copyTransformation);
|
---|
[10976] | 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | private CopyColumnTransformation CreateCopyTransformation(string oldColumn, string newColumn) {
|
---|
| 139 | var newColumName = new StringValue(newColumn);
|
---|
| 140 |
|
---|
| 141 | var copyTransformation = new CopyColumnTransformation();
|
---|
| 142 | copyTransformation.ColumnParameter.ValidValues.Add(newColumName);
|
---|
| 143 | copyTransformation.ColumnParameter.Value = newColumName;
|
---|
| 144 |
|
---|
| 145 | copyTransformation.CopiedColumnNameParameter.Value.Value = oldColumn;
|
---|
| 146 | return copyTransformation;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | private void RenameTransformedColumsAndRestorePreservedColumns(IList<Transformation<double>> transformations) {
|
---|
[10948] | 150 | foreach (var column in originalColumns) {
|
---|
| 151 | int originalColumnIndex = preprocessingData.GetColumnIndex(column.Key);
|
---|
| 152 | int newColumnIndex = originalColumnIndex + 1;
|
---|
| 153 | string newColumnName = GetTransformatedColumnName(transformations, column.Key);
|
---|
[10976] | 154 | // save renaming mapping
|
---|
| 155 | renamedColumns[column.Key] = newColumnName;
|
---|
[10948] | 156 | // create new transformed column
|
---|
| 157 | preprocessingData.InsertColumn<double>(newColumnName, newColumnIndex);
|
---|
| 158 | preprocessingData.SetValues(newColumnIndex, preprocessingData.GetValues<double>(originalColumnIndex));
|
---|
| 159 | // restore old values
|
---|
| 160 | preprocessingData.SetValues(originalColumnIndex, column.Value);
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | private string GetTransformatedColumnName(IList<Transformation<double>> transformations, string column) {
|
---|
| 165 | string suffix = GetTransformationSuffix(transformations, column);
|
---|
| 166 | return column + "_" + suffix;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | private string GetTransformationSuffix(IList<Transformation<double>> transformations, string column) {
|
---|
| 170 | var suffixes = transformations.Where(t => t.Column == column).Select(t => t.ShortName);
|
---|
| 171 | var builder = new StringBuilder();
|
---|
| 172 | foreach (var suffix in suffixes) {
|
---|
| 173 | builder.Append(suffix);
|
---|
| 174 | }
|
---|
| 175 | return builder.ToString();
|
---|
| 176 | }
|
---|
[10786] | 177 | }
|
---|
| 178 | }
|
---|