Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/23/18 14:10:15 (5 years ago)
Author:
pfleck
Message:

#2906 Implemented transformation re-apply of an already reverse-transformed model.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblemData.cs

    r15848 r15856  
    242242    }
    243243
    244     public virtual IDataAnalysisProblemData InverseTransform() {
    245       var newDataset = InverseTransform(Dataset, Transformations);
     244    public virtual IDataAnalysisProblemData Transform() {
     245      var newDataset = Transform(Dataset, Transformations);
     246
     247      var extendedInputs = ExtendInputVariables(AllowedInputVariables, Transformations);
     248      var checkedInputs = new CheckedItemList<StringValue>(newDataset.VariableNames.Select(x => new StringValue(x)));
     249      foreach (var input in checkedInputs) checkedInputs.SetItemCheckedState(input, extendedInputs.Contains(input.Value));
    246250
    247251      // TODO: Cannot create concrete instance here (maybe derived Create-method?)
    248252      var cloner = new Cloner();
    249253      cloner.RegisterClonedObject(Dataset, newDataset);
    250       cloner.RegisterClonedObject(TransformationsParameter.Value, new ItemList<IDataAnalysisTransformation>().AsReadOnly());
     254      cloner.RegisterClonedObject(InputVariables, checkedInputs.AsReadOnly());
     255      //cloner.RegisterClonedObject(TransformationsParameter.Value, new ItemList<IDataAnalysisTransformation>(transformations).AsReadOnly());
     256
     257      return cloner.Clone(this);
     258    }
     259
     260    public virtual IDataAnalysisProblemData InverseTransform() {
     261      var newDataset = InverseTransform(Dataset, Transformations);
     262
     263      var checkedInputs = new CheckedItemList<StringValue>(newDataset.VariableNames.Select(x => new StringValue(x)));
     264      foreach (var input in checkedInputs) checkedInputs.SetItemCheckedState(input, AllowedInputVariables.Contains(input.Value));
     265
     266      // TODO: Cannot create concrete instance here (maybe derived Create-method?)
     267      var cloner = new Cloner();
     268      cloner.RegisterClonedObject(Dataset, newDataset);
     269      cloner.RegisterClonedObject(InputVariables, checkedInputs.AsReadOnly());
     270      //cloner.RegisterClonedObject(TransformationsParameter.Value, new ItemList<IDataAnalysisTransformation>().AsReadOnly());
    251271
    252272      return cloner.Clone(this);
     
    281301        var trans = (ITransformation<double>)transformation.Transformation;
    282302
    283         var transformedData = modifiableDataset.GetDoubleValues(transformation.TransformedVariable);
    284 
    285         var originalData = trans.InverseApply(transformedData).ToList();
    286         modifiableDataset.ReplaceVariable(transformation.OriginalVariable, originalData);
     303        var prevTransformations = transformations.Except(transformationsStack);
     304        bool originalWasChanged = prevTransformations.Any(x => x.TransformedVariable == transformation.OriginalVariable);
     305        if (originalWasChanged) {
     306          var transformedData = modifiableDataset.GetDoubleValues(transformation.TransformedVariable);
     307
     308          var originalData = trans.InverseApply(transformedData).ToList();
     309          modifiableDataset.ReplaceVariable(transformation.OriginalVariable, originalData);
     310        }
    287311
    288312        bool transformedVariablePending = transformationsStack.Any(x => x.OriginalVariable == transformation.TransformedVariable);
     
    293317      return modifiableDataset;
    294318    }
     319
     320    public static IEnumerable<string> ExtendInputVariables(IEnumerable<string> oldInputVariables, IEnumerable<IDataAnalysisTransformation> transformations) {
     321      var inputs = new HashSet<string>(oldInputVariables);
     322
     323      foreach (var transformation in transformations) {
     324        if (inputs.Contains(transformation.OriginalVariable))
     325          inputs.Add(transformation.TransformedVariable);
     326      }
     327
     328      return inputs;
     329    }
     330
     331    public static IEnumerable<string> RemoveVirtualVariables(IEnumerable<string> variables, IEnumerable<IDataAnalysisTransformation> transformations) {
     332      var remainingVariables = new HashSet<string>(variables);
     333
     334      var transformationsStack = new Stack<IDataAnalysisTransformation>(transformations);
     335
     336      while (transformationsStack.Any()) {
     337        var transformation = transformationsStack.Pop();
     338
     339
     340        bool transformedVariablePending = transformationsStack.Any(x => x.OriginalVariable == transformation.TransformedVariable);
     341        if (!transformedVariablePending)
     342          remainingVariables.Remove(transformation.TransformedVariable);
     343      }
     344
     345      return remainingVariables;
     346    }
    295347  }
    296348}
Note: See TracChangeset for help on using the changeset viewer.