Changeset 10948


Ignore:
Timestamp:
06/04/14 14:57:53 (8 years ago)
Author:
pfleck
Message:
  • Implemented preserve/copy columns on transformations.
Location:
branches/DataPreprocessing
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.4/TransformationView.Designer.cs

    r10902 r10948  
    4949      this.transformationListView = new HeuristicLab.DataPreprocessing.Views.CheckedTransformationListView();
    5050      this.applyButton = new System.Windows.Forms.Button();
     51      this.preserveColumnsCheckbox = new System.Windows.Forms.CheckBox();
    5152      this.SuspendLayout();
    5253      //
    5354      // transformationListView
    5455      //
    55       this.transformationListView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    56             | System.Windows.Forms.AnchorStyles.Left)
     56      this.transformationListView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
     57            | System.Windows.Forms.AnchorStyles.Left) 
    5758            | System.Windows.Forms.AnchorStyles.Right)));
    5859      this.transformationListView.Caption = "Transformations";
     
    7576      this.applyButton.Click += new System.EventHandler(this.applyButton_Click);
    7677      //
     78      // preserveColumnsCheckbox
     79      //
     80      this.preserveColumnsCheckbox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
     81      this.preserveColumnsCheckbox.AutoSize = true;
     82      this.preserveColumnsCheckbox.Location = new System.Drawing.Point(337, 492);
     83      this.preserveColumnsCheckbox.Name = "preserveColumnsCheckbox";
     84      this.preserveColumnsCheckbox.Size = new System.Drawing.Size(147, 17);
     85      this.preserveColumnsCheckbox.TabIndex = 2;
     86      this.preserveColumnsCheckbox.Text = "Preserve original Columns";
     87      this.preserveColumnsCheckbox.UseVisualStyleBackColor = true;
     88      //
    7789      // TransformationView
    7890      //
    7991      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    8092      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
     93      this.Controls.Add(this.preserveColumnsCheckbox);
    8194      this.Controls.Add(this.applyButton);
    8295      this.Controls.Add(this.transformationListView);
     
    8497      this.Size = new System.Drawing.Size(627, 514);
    8598      this.ResumeLayout(false);
     99      this.PerformLayout();
    86100
    87101    }
     
    90104    private CheckedTransformationListView transformationListView;
    91105    private System.Windows.Forms.Button applyButton;
     106    private System.Windows.Forms.CheckBox preserveColumnsCheckbox;
    92107  }
    93108}
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.4/TransformationView.cs

    r10945 r10948  
    6060
    6161      var transformator = new PreprocessingTransformator(Content.Data);
     62      bool preserve = preserveColumnsCheckbox.CheckState == CheckState.Checked;
    6263      string errorMsg;
    63       bool success = transformator.ApplyTransformations(transformations, out errorMsg);
     64      bool success = transformator.ApplyTransformations(transformations, preserve, out errorMsg);
    6465      if (success) {
    6566        Content.CheckedTransformationList.Clear();
  • 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.