Changeset 10976
- Timestamp:
- 06/11/14 12:24:37 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/PreprocessingTransformator.cs
r10948 r10976 33 33 private readonly IDictionary<string, IList<double>> originalColumns; 34 34 35 private readonly IDictionary<string, string> renamedColumns; 36 35 37 public PreprocessingTransformator(IPreprocessingData preprocessingData) { 36 38 this.preprocessingData = (ITransactionalPreprocessingData)preprocessingData; 37 39 originalColumns = new Dictionary<string, IList<double>>(); 40 renamedColumns = new Dictionary<string, string>(); 38 41 } 39 42 … … 42 45 43 46 preprocessingData.BeginTransaction(DataPreprocessingChangedEventType.Transformation); 47 44 48 try { 45 49 var doubleTransformations = transformations.OfType<Transformation<double>>().ToList(); 46 ApplyDoubleTranformations(doubleTransformations, preserveColumns, out success, out errorMsg);47 50 48 51 if (preserveColumns) { 49 RestorePreservedColumns(doubleTransformations); 52 PreserveColumns(doubleTransformations); 53 } 54 55 // all transformations are performed inplace. no creation of new columns for transformations 56 ApplyDoubleTranformationsInplace(doubleTransformations, preserveColumns, out success, out errorMsg); 57 58 if (preserveColumns) { 59 RenameTransformedColumsAndRestorePreservedColumns(doubleTransformations); 60 RenameTransformationColumnParameter(doubleTransformations); 61 InsertCopyColumTransformations(doubleTransformations); 62 50 63 originalColumns.Clear(); 64 renamedColumns.Clear(); 51 65 } 52 66 } finally { … … 57 71 } 58 72 59 private void ApplyDoubleTranformations(IList<Transformation<double>> transformations, bool preserveColumns, out bool success, out string errorMsg) { 73 private void PreserveColumns(IList<Transformation<double>> transformations) { 74 foreach (var transformation in transformations) { 75 if (!originalColumns.ContainsKey(transformation.Column)) { 76 int colIndex = preprocessingData.GetColumnIndex(transformation.Column); 77 var originalData = preprocessingData.GetValues<double>(colIndex); 78 originalColumns.Add(transformation.Column, originalData); 79 } 80 } 81 } 82 83 private void ApplyDoubleTranformationsInplace(IList<Transformation<double>> transformations, bool preserveColumns, out bool success, out string errorMsg) { 60 84 errorMsg = string.Empty; 61 85 success = true; … … 64 88 65 89 var originalData = preprocessingData.GetValues<double>(colIndex); 66 67 if (preserveColumns && !originalColumns.ContainsKey(transformation.Column))68 originalColumns.Add(transformation.Column, originalData);69 90 70 91 string errorMsgPart; … … 84 105 } 85 106 86 private void RestorePreservedColumns(IList<Transformation<double>> transformations) { 107 private void RenameTransformationColumnParameter(List<Transformation<double>> transformations) { 108 foreach (var transformation in transformations) { 109 var newColumnName = new StringValue(renamedColumns[transformation.Column]); 110 transformation.ColumnParameter.ValidValues.Add(newColumnName); 111 transformation.ColumnParameter.Value = newColumnName; 112 } 113 } 114 115 private void InsertCopyColumTransformations(IList<Transformation<double>> transformations) { 116 foreach (var renaming in renamedColumns) { 117 string oldName = renaming.Key; 118 string newName = renaming.Value; 119 120 var copyTransformation = CreateCopyTransformation(oldName, newName); 121 preprocessingData.Transformations.Add(copyTransformation); 122 123 } 124 125 //var transformedColumns = transformations.Select(x => x.Column).Distinct(); 126 //foreach (var column in transformedColumns) { 127 // var copyTransformation = CreateCopyTransformation(column); 128 // preprocessingData.Transformations.Add(copyTransformation); 129 //} 130 } 131 132 private CopyColumnTransformation CreateCopyTransformation(string oldColumn, string newColumn) { 133 var newColumName = new StringValue(newColumn); 134 135 var copyTransformation = new CopyColumnTransformation(); 136 copyTransformation.ColumnParameter.ValidValues.Add(newColumName); 137 copyTransformation.ColumnParameter.Value = newColumName; 138 139 copyTransformation.CopiedColumnNameParameter.Value.Value = oldColumn; 140 return copyTransformation; 141 } 142 143 private void RenameTransformedColumsAndRestorePreservedColumns(IList<Transformation<double>> transformations) { 87 144 foreach (var column in originalColumns) { 88 145 int originalColumnIndex = preprocessingData.GetColumnIndex(column.Key); 89 146 int newColumnIndex = originalColumnIndex + 1; 90 147 string newColumnName = GetTransformatedColumnName(transformations, column.Key); 91 148 // save renaming mapping 149 renamedColumns[column.Key] = newColumnName; 92 150 // create new transformed column 93 151 preprocessingData.InsertColumn<double>(newColumnName, newColumnIndex);
Note: See TracChangeset
for help on using the changeset viewer.