Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/19/18 10:25:45 (7 years ago)
Author:
pfleck
Message:

#2906 First concept of simple transformation (single target transformation)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2906_Transformations/HeuristicLab.DataPreprocessing/3.4/Content/TransformationContent.cs

    r15583 r15846  
    2020#endregion
    2121
     22using System.Collections.Generic;
    2223using System.Drawing;
     24using System.Linq;
    2325using HeuristicLab.Common;
    2426using HeuristicLab.Core;
     
    3537
    3638    [Storable]
    37     public ICheckedItemList<ITransformation> CheckedTransformationList { get; private set; }
     39    public ICheckedItemList<IDataAnalysisTransformation> CheckedTransformationList { get; private set; }
    3840
    3941    #region Constructor, Cloning & Persistence
    4042    public TransformationContent(IFilteredPreprocessingData preprocessingData)
    4143      : base(preprocessingData) {
    42       CheckedTransformationList = new CheckedItemList<ITransformation>();
     44      CheckedTransformationList = new CheckedItemList<IDataAnalysisTransformation>();
    4345    }
    4446
     
    5557      : base(deserializing) { }
    5658    #endregion
     59
     60    public bool ApplyTransformations(out IEnumerable<string> errorMessages) {
     61      var transformations = CheckedTransformationList.CheckedItems.Select(x => x.Value);
     62
     63      bool success = true;
     64      var errors = new List<string>();
     65      errorMessages = errors;
     66
     67      foreach (var transformation in transformations) {
     68        var sourceVariable = transformation.OriginalVariable;
     69        var targetVariable = transformation.TransformedVariable ?? sourceVariable + " Transformed";
     70        var sourceIdx = PreprocessingData.GetColumnIndex(sourceVariable);
     71
     72        if (transformation.Transformation is ITransformation<double> trans && PreprocessingData.VariableHasType<double>(sourceIdx)) {
     73          if (!PreprocessingData.VariableNames.Contains(targetVariable))
     74            PreprocessingData.InsertColumn<double>(targetVariable, PreprocessingData.Columns);
     75          var targetIdx = PreprocessingData.GetColumnIndex(targetVariable);
     76
     77          if (!PreprocessingData.VariableHasType<double>(targetIdx)) { success = false; errors.Add("Target column is not double."); continue; }
     78
     79          var sourceData = PreprocessingData.GetValues<double>(sourceIdx);
     80          if (!trans.Check(sourceData, out string msg)) { success = false; errors.Add(msg); continue; }
     81
     82          trans.Configure(sourceData);
     83          var transformedData = trans.Apply(sourceData).ToList();
     84
     85          PreprocessingData.SetValues(targetIdx, transformedData);
     86
     87          PreprocessingData.Transformations.Add(transformation);
     88          CheckedTransformationList.SetItemCheckedState(transformation, false);
     89        } else {
     90          success = false;
     91          errors.Add("Transformation datatype is not supported.");
     92        }
     93      }
     94
     95      return success;
     96    }
    5797  }
    5898}
Note: See TracChangeset for help on using the changeset viewer.