Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/04/14 14:57:53 (10 years ago)
Author:
pfleck
Message:
  • Implemented preserve/copy columns on transformations.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/PreprocessingTransformator.cs

    r10819 r10948  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using System.Text;
     26using HeuristicLab.Data;
    2527using HeuristicLab.Problems.DataAnalysis.Transformations;
    2628
     
    2931    private readonly ITransactionalPreprocessingData preprocessingData;
    3032
     33    private readonly IDictionary<string, IList<double>> originalColumns;
     34
    3135    public PreprocessingTransformator(IPreprocessingData preprocessingData) {
    3236      this.preprocessingData = (ITransactionalPreprocessingData)preprocessingData;
     37      originalColumns = new Dictionary<string, IList<double>>();
    3338    }
    3439
    35     public bool ApplyTransformations(IEnumerable<ITransformation> transformations, out string errorMsg) {
     40    public bool ApplyTransformations(IEnumerable<ITransformation> transformations, bool preserveColumns, out string errorMsg) {
    3641      bool success;
    3742
     
    3944      try {
    4045        var doubleTransformations = transformations.OfType<Transformation<double>>().ToList();
    41         ApplyDoubleTranformations(doubleTransformations, out success, out errorMsg);
     46        ApplyDoubleTranformations(doubleTransformations, preserveColumns, out success, out errorMsg);
     47
     48        if (preserveColumns) {
     49          RestorePreservedColumns(doubleTransformations);
     50          originalColumns.Clear();
     51        }
    4252      } finally {
    4353        preprocessingData.EndTransaction();
    44         //if (!success)
    45         //preprocessingData.Undo();
    4654      }
    4755
     
    4957    }
    5058
    51     private void ApplyDoubleTranformations(IEnumerable<Transformation<double>> transformations, out bool success, out string errorMsg) {
     59    private void ApplyDoubleTranformations(IList<Transformation<double>> transformations, bool preserveColumns, out bool success, out string errorMsg) {
    5260      errorMsg = string.Empty;
    5361      success = true;
     
    5664
    5765        var originalData = preprocessingData.GetValues<double>(colIndex);
     66
     67        if (preserveColumns && !originalColumns.ContainsKey(transformation.Column))
     68          originalColumns.Add(transformation.Column, originalData);
     69
    5870        string errorMsgPart;
    5971        bool successPart;
    6072        var transformedData = ApplyDoubleTransformation(transformation, originalData, out successPart, out errorMsgPart);
    6173        errorMsg += errorMsgPart + Environment.NewLine;
    62         //if (!success) return;
     74
    6375        if (!successPart) success = false;
    6476        preprocessingData.SetValues(colIndex, transformedData.ToList());
    6577        preprocessingData.Transformations.Add(transformation);
    6678      }
    67 
    6879    }
    6980
    70     private IEnumerable<double> ApplyDoubleTransformation(Transformation<double> transformation, IEnumerable<double> data, out bool success, out string errorMsg) {
     81    private IEnumerable<double> ApplyDoubleTransformation(Transformation<double> transformation, IList<double> data, out bool success, out string errorMsg) {
    7182      success = transformation.Check(data, out errorMsg);
    7283      return transformation.Apply(data);
    7384    }
     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    }
    74113  }
    75114}
Note: See TracChangeset for help on using the changeset viewer.