Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15847


Ignore:
Timestamp:
03/19/18 17:46:57 (7 years ago)
Author:
pfleck
Message:

#2906 Implemented chained transformations on target and input variables.

Location:
branches/2906_Transformations
Files:
3 added
9 edited

Legend:

Unmodified
Added
Removed
  • branches/2906_Transformations/HeuristicLab.DataPreprocessing.Views/3.4/HeuristicLab.DataPreprocessing.Views-3.4.csproj

    r15846 r15847  
    347347    <EmbeddedResource Include="ScatterPlotSingleView.resx">
    348348      <DependentUpon>ScatterPlotSingleView.cs</DependentUpon>
    349     </EmbeddedResource>
    350     <EmbeddedResource Include="TransformationView.resx">
    351       <DependentUpon>TransformationView.cs</DependentUpon>
    352349    </EmbeddedResource>
    353350  </ItemGroup>
  • branches/2906_Transformations/HeuristicLab.DataPreprocessing/3.4/ProblemDataCreator.cs

    r15846 r15847  
    8383
    8484    private IDataAnalysisProblemData CreateRegressionData(RegressionProblemData oldProblemData) {
     85      // TODO: transformations (additional inputs, target changed)
    8586      var targetVariable = oldProblemData.TargetVariable;
    8687      if (!context.Data.VariableNames.Contains(targetVariable))
  • branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis.Views/3.4/HeuristicLab.Problems.DataAnalysis.Views-3.4.csproj

    r15846 r15847  
    184184    <Reference Include="HeuristicLab.Problems.DataAnalysis.Symbolic-3.4, Version=3.4.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
    185185      <SpecificVersion>False</SpecificVersion>
    186     <HintPath>..\..\..\..\trunk\bin\HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.dll</HintPath>
     186      <HintPath>..\..\..\..\trunk\bin\HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.dll</HintPath>
    187187      <Private>False</Private>
    188188    </Reference>
     
    586586      <DependentUpon>AbstractFeatureCorrelationView.cs</DependentUpon>
    587587    </EmbeddedResource>
    588     <EmbeddedResource Include="Solution Views\RegressionSolutionView.resx">
    589       <DependentUpon>RegressionSolutionView.cs</DependentUpon>
    590     </EmbeddedResource>
    591588  </ItemGroup>
    592589  <ItemGroup>
  • branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis.Views/3.4/Solution Views/RegressionSolutionView.cs

    r15846 r15847  
    6060
    6161    private void transformButton_Click(object sender, System.EventArgs e) {
    62       var targetVariable = TransformedRegressionModel.GetOriginalTragetVariable(Content.Model.TargetVariable, Content.ProblemData.Transformations);
    6362      var transformedModel = new TransformedRegressionModel(Content.Model, Content.ProblemData.Transformations);
    64       var transformedSolution = new TransformedRegressionSolution(transformedModel, new RegressionProblemData(Content.ProblemData) { TargetVariable = targetVariable });
     63      var originalProblemData = (IRegressionProblemData)Content.ProblemData.InverseTransform();
     64      var transformedSolution = new TransformedRegressionSolution(transformedModel, originalProblemData);
    6565      MainFormManager.MainForm.ShowContent(transformedSolution);
    6666    }
  • branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblemData.cs

    r15846 r15847  
    241241      }
    242242    }
     243
     244    public virtual IDataAnalysisProblemData InverseTransform() {
     245      var newDataset = InverseTransform(Dataset, Transformations);
     246
     247      // TODO: Cannot create concrete instance here (maybe derived Create-method?)
     248      var cloner = new Cloner();
     249      cloner.RegisterClonedObject(Dataset, newDataset);
     250      cloner.RegisterClonedObject(TransformationsParameter.Value, new ItemList<IDataAnalysisTransformation>().AsReadOnly());
     251
     252      return cloner.Clone(this);
     253    }
     254
     255    public static IDataset Transform(IDataset dataset, IEnumerable<IDataAnalysisTransformation> transformations) {
     256      var modifiableDataset = ((Dataset)dataset).ToModifiable();
     257
     258      foreach (var transformation in transformations) {
     259        var trans = (ITransformation<double>)transformation.Transformation;
     260
     261        var originalData = modifiableDataset.GetDoubleValues(transformation.OriginalVariable);
     262        if (!trans.Check(originalData, out string errorMessage))
     263          throw new InvalidOperationException($"Cannot estimate Values, Transformation is invalid: {errorMessage}");
     264
     265        var transformedData = trans.Apply(originalData).ToList();
     266        if (modifiableDataset.VariableNames.Contains(transformation.TransformedVariable))
     267          modifiableDataset.ReplaceVariable(transformation.TransformedVariable, transformedData);
     268        else
     269          modifiableDataset.AddVariable(transformation.TransformedVariable, transformedData);
     270      }
     271
     272      return modifiableDataset;
     273    }
     274
     275    public static IDataset InverseTransform(IDataset dataset, IEnumerable<IDataAnalysisTransformation> transformations) {
     276      var modifiableDataset = ((Dataset)dataset).ToModifiable();
     277
     278      foreach (var transformation in transformations.Reverse()) {
     279        var trans = (ITransformation<double>)transformation.Transformation;
     280
     281        var transformedData = modifiableDataset.GetDoubleValues(transformation.TransformedVariable);
     282
     283        var originalData = trans.InverseApply(transformedData).ToList();
     284        modifiableDataset.ReplaceVariable(transformation.OriginalVariable, originalData);
     285      }
     286
     287      return modifiableDataset;
     288    }
    243289  }
    244290}
  • branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionProblemData.cs

    r15846 r15847  
    180180      base.AdjustProblemDataProperties(problemData);
    181181    }
     182
     183    public override IDataAnalysisProblemData InverseTransform() {
     184      var transformedProblemData = (IRegressionProblemData)base.InverseTransform();
     185      var targetVariable = GetOriginalTragetVariable(TargetVariable, Transformations);
     186      transformedProblemData.TargetVariable = targetVariable;
     187      return transformedProblemData;
     188    }
     189
     190    public static string GetOriginalTragetVariable(string transformedTarget, IEnumerable<IDataAnalysisTransformation> transformations) {
     191      var originalTarget = transformedTarget;
     192      foreach (var transformation in transformations.Reverse()) {
     193        if (transformation.TransformedVariable == originalTarget)
     194          originalTarget = transformation.OriginalVariable;
     195      }
     196      return originalTarget;
     197    }
    182198  }
    183199}
  • branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/TransformedRegressionModel.cs

    r15846 r15847  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using System.Linq;
     
    4443    #region Constructor, Cloning & Persistence
    4544    public TransformedRegressionModel(IRegressionModel originalModel, IEnumerable<IDataAnalysisTransformation> transformations)
    46       : base(GetOriginalTragetVariable(originalModel.TargetVariable, transformations)) {
     45      : base(RegressionProblemData.GetOriginalTragetVariable(originalModel.TargetVariable, transformations)) {
     46      Name = "Transformed " + originalModel.Name;
    4747      OriginalModel = originalModel;
    4848      Transformations = new ItemList<IDataAnalysisTransformation>(transformations);
     
    6464    #endregion
    6565
     66    // dataset in original data range
    6667    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
    6768      var transformedDataset = TransformInputs(dataset, Transformations);
     
    6970      var estimates = OriginalModel.GetEstimatedValues(transformedDataset, rows);
    7071
    71       return TransformEstimates(estimates, Transformations, OriginalModel.TargetVariable);
     72      return InverseTransformEstimates(estimates, Transformations, OriginalModel.TargetVariable);
    7273    }
    7374
     75    // problemData in original data range
    7476    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
    75       return new TransformedRegressionSolution(this, new RegressionProblemData(problemData) { TargetVariable = GetOriginalTragetVariable(TargetVariable, Transformations) });
     77      return new TransformedRegressionSolution(this, new RegressionProblemData(problemData));
    7678    }
    7779
    7880    private static IDataset TransformInputs(IDataset dataset, IEnumerable<IDataAnalysisTransformation> transformations) {
    79       return dataset;
     81      return DataAnalysisProblemData.Transform(dataset, transformations);
     82    }
    8083
    81       var modifiableDataset = ((Dataset)dataset).ToModifiable();
     84    private static IEnumerable<double> InverseTransformEstimates(IEnumerable<double> data, IEnumerable<IDataAnalysisTransformation> transformations, string targetVariable) {
     85      var estimates = data.ToList();
    8286
    8387      foreach (var transformation in transformations.Reverse()) {
    84         var trans = (ITransformation<double>)transformation.Transformation;
     88        if (transformation.TransformedVariable == targetVariable) {
     89          var trans = (ITransformation<double>)transformation.Transformation;
    8590
    86         // no transformations on target (but what if target is an input?)
     91          estimates = trans.InverseApply(estimates).ToList();
    8792
    88         var originalData = modifiableDataset.GetDoubleValues(transformation.OriginalVariable);
    89         if (!trans.Check(originalData, out string errorMessage))
    90           throw new InvalidOperationException($"Cannot estimate Values, Transformation is invalid: {errorMessage}");
    91         var transformedData = trans.Apply(originalData).ToList();
    92 
    93         if (modifiableDataset.VariableNames.Contains(transformation.TransformedVariable))
    94           modifiableDataset.ReplaceVariable(transformation.TransformedVariable, transformedData);
    95         else
    96           modifiableDataset.AddVariable(transformation.TransformedVariable, transformedData);
     93          // setup next iteration
     94          targetVariable = transformation.OriginalVariable;
     95        }
    9796      }
    9897
    99       return modifiableDataset;
    100     }
    101 
    102     private static IEnumerable<double> TransformEstimates(IEnumerable<double> data, IEnumerable<IDataAnalysisTransformation> transformations, string targetVariable) {
    103       var targetTransformations = transformations.Where(x => x.TransformedVariable == targetVariable);
    104 
    105       var transformedData = data.ToList();
    106 
    107       foreach (var transformation in targetTransformations.Reverse()) {
    108         var trans = (ITransformation<double>)transformation.Transformation;
    109 
    110         if (!trans.Check(transformedData, out string errorMessage))
    111           throw new InvalidOperationException($"Cannot estimate Values, Transformation is invalid: {errorMessage}");
    112 
    113         transformedData = trans.InverseApply(transformedData).ToList();
    114       }
    115 
    116       return transformedData;
    117     }
    118 
    119     public static string GetOriginalTragetVariable(string transformedTarget, IEnumerable<IDataAnalysisTransformation> transformations) {
    120       var target = transformedTarget;
    121       foreach (var transformation in transformations.Reverse()) {
    122         if (transformation.TransformedVariable == target)
    123           target = transformation.OriginalVariable;
    124       }
    125       return target;
     98      return estimates;
    12699    }
    127100  }
  • branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/IDataAnalysisProblemData.cs

    r15846 r15847  
    4444
    4545    IEnumerable<IDataAnalysisTransformation> Transformations { get; }
     46    IDataAnalysisProblemData InverseTransform();
    4647
    4748    bool IsTrainingSample(int index);
  • branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/IDataAnalysisTransformation.cs

    r15846 r15847  
    2929
    3030    ITransformation Transformation { get; set; }
    31 
    3231  }
    3332}
Note: See TracChangeset for help on using the changeset viewer.