- Timestamp:
- 06/04/14 14:57:53 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/PreprocessingTransformator.cs
r10819 r10948 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Text; 26 using HeuristicLab.Data; 25 27 using HeuristicLab.Problems.DataAnalysis.Transformations; 26 28 … … 29 31 private readonly ITransactionalPreprocessingData preprocessingData; 30 32 33 private readonly IDictionary<string, IList<double>> originalColumns; 34 31 35 public PreprocessingTransformator(IPreprocessingData preprocessingData) { 32 36 this.preprocessingData = (ITransactionalPreprocessingData)preprocessingData; 37 originalColumns = new Dictionary<string, IList<double>>(); 33 38 } 34 39 35 public bool ApplyTransformations(IEnumerable<ITransformation> transformations, out string errorMsg) {40 public bool ApplyTransformations(IEnumerable<ITransformation> transformations, bool preserveColumns, out string errorMsg) { 36 41 bool success; 37 42 … … 39 44 try { 40 45 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 } 42 52 } finally { 43 53 preprocessingData.EndTransaction(); 44 //if (!success)45 //preprocessingData.Undo();46 54 } 47 55 … … 49 57 } 50 58 51 private void ApplyDoubleTranformations(I Enumerable<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) { 52 60 errorMsg = string.Empty; 53 61 success = true; … … 56 64 57 65 var originalData = preprocessingData.GetValues<double>(colIndex); 66 67 if (preserveColumns && !originalColumns.ContainsKey(transformation.Column)) 68 originalColumns.Add(transformation.Column, originalData); 69 58 70 string errorMsgPart; 59 71 bool successPart; 60 72 var transformedData = ApplyDoubleTransformation(transformation, originalData, out successPart, out errorMsgPart); 61 73 errorMsg += errorMsgPart + Environment.NewLine; 62 //if (!success) return; 74 63 75 if (!successPart) success = false; 64 76 preprocessingData.SetValues(colIndex, transformedData.ToList()); 65 77 preprocessingData.Transformations.Add(transformation); 66 78 } 67 68 79 } 69 80 70 private IEnumerable<double> ApplyDoubleTransformation(Transformation<double> transformation, I Enumerable<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) { 71 82 success = transformation.Check(data, out errorMsg); 72 83 return transformation.Apply(data); 73 84 } 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 } 74 113 } 75 114 }
Note: See TracChangeset
for help on using the changeset viewer.