Changeset 15847 for branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation
- Timestamp:
- 03/19/18 17:46:57 (7 years ago)
- Location:
- branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation
- Files:
-
- 3 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblemData.cs
r15846 r15847 241 241 } 242 242 } 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 } 243 289 } 244 290 } -
branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionProblemData.cs
r15846 r15847 180 180 base.AdjustProblemDataProperties(problemData); 181 181 } 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 } 182 198 } 183 199 } -
branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/TransformedRegressionModel.cs
r15846 r15847 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using System.Linq; … … 44 43 #region Constructor, Cloning & Persistence 45 44 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; 47 47 OriginalModel = originalModel; 48 48 Transformations = new ItemList<IDataAnalysisTransformation>(transformations); … … 64 64 #endregion 65 65 66 // dataset in original data range 66 67 public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) { 67 68 var transformedDataset = TransformInputs(dataset, Transformations); … … 69 70 var estimates = OriginalModel.GetEstimatedValues(transformedDataset, rows); 70 71 71 return TransformEstimates(estimates, Transformations, OriginalModel.TargetVariable);72 return InverseTransformEstimates(estimates, Transformations, OriginalModel.TargetVariable); 72 73 } 73 74 75 // problemData in original data range 74 76 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)); 76 78 } 77 79 78 80 private static IDataset TransformInputs(IDataset dataset, IEnumerable<IDataAnalysisTransformation> transformations) { 79 return dataset; 81 return DataAnalysisProblemData.Transform(dataset, transformations); 82 } 80 83 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(); 82 86 83 87 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; 85 90 86 // no transformations on target (but what if target is an input?)91 estimates = trans.InverseApply(estimates).ToList(); 87 92 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 } 97 96 } 98 97 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; 126 99 } 127 100 }
Note: See TracChangeset
for help on using the changeset viewer.