Changeset 10612
- Timestamp:
- 03/19/14 10:48:23 (11 years ago)
- Location:
- branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/ManipulationLogic.cs
r10590 r10612 44 44 45 45 public void ReplaceIndicesByAverageValue(Dictionary<int, List<int>> cells) { 46 preprocessingData. BeginTransaction();47 foreach (var column in cells) {48 double average = statisticInfo.GetAverage(column.Key);49 ReplaceIndicesByValue<double>(column.Key, column.Value, average);50 }51 preprocessingData.EndTransaction();46 preprocessingData.InTransaction(() => { 47 foreach (var column in cells) { 48 double average = statisticInfo.GetAverage(column.Key); 49 ReplaceIndicesByValue<double>(column.Key, column.Value, average); 50 } 51 }); 52 52 } 53 53 54 54 public void ReplaceIndicesByMedianValue(Dictionary<int, List<int>> cells) { 55 preprocessingData. BeginTransaction();56 foreach (var column in cells) {57 double median = statisticInfo.GetMedian(column.Key);58 ReplaceIndicesByValue<double>(column.Key, column.Value, median);59 }60 preprocessingData.EndTransaction();55 preprocessingData.InTransaction(() => { 56 foreach (var column in cells) { 57 double median = statisticInfo.GetMedian(column.Key); 58 ReplaceIndicesByValue<double>(column.Key, column.Value, median); 59 } 60 }); 61 61 } 62 62 63 63 public void ReplaceIndicesByRandomValue(Dictionary<int, List<int>> cells) { 64 preprocessingData. BeginTransaction();65 Random r = new Random();66 67 foreach (var column in cells) {68 double max = statisticInfo.GetMax<double>(column.Key);69 double min = statisticInfo.GetMin<double>(column.Key);70 double randMultiplier = (max - min);71 foreach (int index in column.Value) {72 double rand = r.NextDouble() * randMultiplier + min;73 preprocessingData.SetCell<double>(column.Key, index, rand);74 }75 }76 preprocessingData.EndTransaction();64 preprocessingData.InTransaction(() => { 65 Random r = new Random(); 66 67 foreach (var column in cells) { 68 double max = statisticInfo.GetMax<double>(column.Key); 69 double min = statisticInfo.GetMin<double>(column.Key); 70 double randMultiplier = (max - min); 71 foreach (int index in column.Value) { 72 double rand = r.NextDouble() * randMultiplier + min; 73 preprocessingData.SetCell<double>(column.Key, index, rand); 74 } 75 } 76 }); 77 77 } 78 78 79 79 public void ReplaceIndicesByLinearInterpolationOfNeighbours(Dictionary<int, List<int>> cells) { 80 preprocessingData.BeginTransaction(); 81 foreach (var column in cells) { 82 int countValues = preprocessingData.GetValues<double>(column.Key).Count(); 83 foreach (int index in column.Value) { 84 // dont replace first or last values 85 if (index > 0 && index < countValues) { 86 int prevIndex = indexOfPrevPresentValue(column.Key, index); 87 int nextIndex = indexOfNextPresentValue(column.Key, index); 88 89 // no neighbours found 90 if (prevIndex < 0 && nextIndex >= countValues) { 91 continue; 80 preprocessingData.InTransaction(() => { 81 foreach (var column in cells) { 82 int countValues = preprocessingData.GetValues<double>(column.Key).Count(); 83 foreach (int index in column.Value) { 84 // dont replace first or last values 85 if (index > 0 && index < countValues) { 86 int prevIndex = indexOfPrevPresentValue(column.Key, index); 87 int nextIndex = indexOfNextPresentValue(column.Key, index); 88 89 // no neighbours found 90 if (prevIndex < 0 && nextIndex >= countValues) { 91 continue; 92 } 93 double prev = preprocessingData.GetCell<double>(column.Key, prevIndex); 94 double next = preprocessingData.GetCell<double>(column.Key, nextIndex); 95 96 int valuesToInterpolate = nextIndex - prevIndex; 97 98 double interpolationStep = (prev + next) / valuesToInterpolate; 99 100 for (int i = prevIndex; i < nextIndex; ++i) { 101 double interpolated = prev + (interpolationStep * (i - prevIndex)); 102 preprocessingData.SetCell<double>(column.Key, i, interpolated); 103 } 92 104 } 93 double prev = preprocessingData.GetCell<double>(column.Key, prevIndex); 94 double next = preprocessingData.GetCell<double>(column.Key, nextIndex); 95 96 int valuesToInterpolate = nextIndex - prevIndex; 97 98 double interpolationStep = (prev + next) / valuesToInterpolate; 99 100 for (int i = prevIndex; i < nextIndex; ++i) { 101 double interpolated = prev + (interpolationStep * (i - prevIndex)); 102 preprocessingData.SetCell<double>(column.Key, i, interpolated); 103 } 104 } 105 } 106 } 107 preprocessingData.EndTransaction(); 105 } 106 } 107 }); 108 108 } 109 109 … … 127 127 128 128 public void ReplaceIndicesByMostCommonValue(Dictionary<int, List<int>> cells) { 129 preprocessingData. BeginTransaction();130 foreach (var column in cells) {131 if (preprocessingData.IsType<double>(column.Key)) {132 ReplaceIndicesByValue<double>(column.Key, column.Value, statisticInfo.GetMostCommonValue<double>(column.Key));133 } else if (preprocessingData.IsType<string>(column.Key)) {134 ReplaceIndicesByValue<string>(column.Key, column.Value, statisticInfo.GetMostCommonValue<string>(column.Key));135 } else if (preprocessingData.IsType<DateTime>(column.Key)) {136 ReplaceIndicesByValue<DateTime>(column.Key, column.Value, statisticInfo.GetMostCommonValue<DateTime>(column.Key));137 } else {138 throw new ArgumentException("column with index: " + column.Key + " contains a non supported type.");139 }140 }141 preprocessingData.EndTransaction();129 preprocessingData.InTransaction(() => { 130 foreach (var column in cells) { 131 if (preprocessingData.IsType<double>(column.Key)) { 132 ReplaceIndicesByValue<double>(column.Key, column.Value, statisticInfo.GetMostCommonValue<double>(column.Key)); 133 } else if (preprocessingData.IsType<string>(column.Key)) { 134 ReplaceIndicesByValue<string>(column.Key, column.Value, statisticInfo.GetMostCommonValue<string>(column.Key)); 135 } else if (preprocessingData.IsType<DateTime>(column.Key)) { 136 ReplaceIndicesByValue<DateTime>(column.Key, column.Value, statisticInfo.GetMostCommonValue<DateTime>(column.Key)); 137 } else { 138 throw new ArgumentException("column with index: " + column.Key + " contains a non supported type."); 139 } 140 } 141 }); 142 142 } 143 143 … … 146 146 Random random = new Random(); 147 147 148 preprocessingData. BeginTransaction();149 // process all given ranges - e.g. TrainingPartition, Trainingpartition150 foreach (IntRange range in ranges) {151 List<Tuple<int, int>> shuffledIndices = new List<Tuple<int, int>>();152 153 // generate random indices used for shuffeling each column154 for (int i = range.End; i > range.Start; --i) {155 int rand = random.Next(range.Start, i);156 shuffledIndices.Add(new Tuple<int, int>(i, rand));157 }158 159 ReOrderToIndices(shuffledIndices);160 }161 preprocessingData.EndTransaction();148 preprocessingData.InTransaction(() => { 149 // process all given ranges - e.g. TrainingPartition, Trainingpartition 150 foreach (IntRange range in ranges) { 151 List<Tuple<int, int>> shuffledIndices = new List<Tuple<int, int>>(); 152 153 // generate random indices used for shuffeling each column 154 for (int i = range.End; i > range.Start; --i) { 155 int rand = random.Next(range.Start, i); 156 shuffledIndices.Add(new Tuple<int, int>(i, rand)); 157 } 158 159 ReOrderToIndices(shuffledIndices); 160 } 161 }); 162 162 } 163 163 … … 173 173 174 174 public void ReOrderToIndices(IList<System.Tuple<int, int>> indices) { 175 preprocessingData. BeginTransaction();176 for (int i = 0; i < preprocessingData.Columns; ++i) {177 if (preprocessingData.IsType<double>(i)) {178 reOrderToIndices<double>(i, indices);179 } else if (preprocessingData.IsType<string>(i)) {180 reOrderToIndices<string>(i, indices);181 } else if (preprocessingData.IsType<DateTime>(i)) {182 reOrderToIndices<DateTime>(i, indices);183 }184 }185 preprocessingData.EndTransaction();175 preprocessingData.InTransaction(() => { 176 for (int i = 0; i < preprocessingData.Columns; ++i) { 177 if (preprocessingData.IsType<double>(i)) { 178 reOrderToIndices<double>(i, indices); 179 } else if (preprocessingData.IsType<string>(i)) { 180 reOrderToIndices<string>(i, indices); 181 } else if (preprocessingData.IsType<DateTime>(i)) { 182 reOrderToIndices<DateTime>(i, indices); 183 } 184 } 185 }); 186 186 } 187 187 -
branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/TransactionalPreprocessingData.cs
r10609 r10612 158 158 } 159 159 160 public void InTransaction(Action action) { 161 BeginTransaction(); 162 action(); 163 EndTransaction(); 164 } 165 160 166 public void BeginTransaction() { 161 167 SaveSnapshot(DataPreprocessingChangedEventType.Any, -1, -1); -
branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Interfaces/ITransactionalPreprocessingData.cs
r10586 r10612 21 21 22 22 23 using System; 23 24 namespace HeuristicLab.DataPreprocessing { 24 25 … … 29 30 bool IsUndoAvailable { get; } 30 31 void Undo(); 32 void InTransaction(Action action); 31 33 void BeginTransaction(); 32 34 void EndTransaction();
Note: See TracChangeset
for help on using the changeset viewer.