- Timestamp:
- 03/05/14 17:14:26 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/PreprocessingData.cs
r10548 r10550 30 30 31 31 namespace HeuristicLab.DataPreprocessing { 32 33 internal class PDSnapshot { 34 public IDictionary<int, IList> VariableValues { get; set; } 35 36 public IList<string> VariableNames { get; set; } 37 38 public double TrainingToTestRatio { get; set; } 39 40 public DataPreprocessingChangedEventType ChangedType { get; set; } 41 42 public int ChangedColumn { get; set; } 43 44 public int ChangedRow { get; set; } 45 } 46 32 47 [Item("PreprocessingData", "Represents data used for preprocessing.")] 33 48 public class PreprocessingData : NamedItem, IPreprocessingData { 34 49 50 private const int MAX_UNDO_DEPTH = 5; 51 35 52 private IDictionary<int, IList> variableValues; 36 53 … … 38 55 39 56 private double trainingToTestRatio; 57 58 private IList<PDSnapshot> undoHistory; 40 59 41 60 private PreprocessingData(PreprocessingData original, Cloner cloner) 42 61 : base(original, cloner) { 43 variableValues = new Dictionary<int, IList>(original.variableValues); 44 for (int i = 0; i < variableValues.Count; i++) 45 variableValues[i] = new ArrayList(original.variableValues[i]); 62 variableValues = CopyVariableValues(original.variableValues); 46 63 variableNames = new List<string>(original.variableNames); 47 64 trainingToTestRatio = original.trainingToTestRatio; 65 undoHistory = new List<PDSnapshot>(); 48 66 } 49 67 … … 71 89 72 90 trainingToTestRatio = (double)problemData.TrainingPartition.Size / Math.Max(problemData.Dataset.Rows, double.Epsilon); 91 undoHistory = new List<PDSnapshot>(); 73 92 } 74 93 … … 81 100 } 82 101 102 private IDictionary<int, IList> CopyVariableValues(IDictionary<int, IList> original) { 103 var copy = new Dictionary<int, IList>(variableValues); 104 for (int i = 0; i < original.Count; i++) { 105 if (variableValues[i] is IList<double>) { 106 copy[i] = new List<double>((IList<double>)variableValues[i]); 107 } else if (variableValues[i] is IList<DateTime>) { 108 copy[i] = new List<DateTime>((IList<DateTime>)variableValues[i]); 109 } else if (variableValues[i] is IList<string>) { 110 copy[i] = new List<string>((IList<string>)variableValues[i]); 111 } else { 112 throw new NotImplementedException("The Type is not Supported"); 113 } 114 } 115 return copy; 116 } 117 118 private void SaveSnapshot(DataPreprocessingChangedEventType changedType, int column, int row) { 119 PDSnapshot currentSnapshot = new PDSnapshot(); 120 currentSnapshot.VariableValues = CopyVariableValues(variableValues); 121 currentSnapshot.VariableNames = new List<string>(variableNames); 122 currentSnapshot.TrainingToTestRatio = trainingToTestRatio; 123 currentSnapshot.ChangedType = changedType; 124 currentSnapshot.ChangedColumn = column; 125 currentSnapshot.ChangedRow = row; 126 127 if (undoHistory.Count >= MAX_UNDO_DEPTH) 128 undoHistory.RemoveAt(0); 129 130 undoHistory.Add(currentSnapshot); 131 } 132 83 133 #region NamedItem abstract Member Implementations 84 134 … … 96 146 97 147 98 public void SetCell<T>(int columnIndex, int rowIndex, T value) { // Undo-sensitive 148 public void SetCell<T>(int columnIndex, int rowIndex, T value) { 149 SaveSnapshot(DataPreprocessingChangedEventType.ChangeItem, columnIndex, rowIndex); 99 150 variableValues[columnIndex][rowIndex] = value; 100 151 OnChanged(DataPreprocessingChangedEventType.ChangeItem, columnIndex, rowIndex); … … 116 167 } 117 168 118 public void SetValues<T>(int columnIndex, IList<T> values) { // Undo-sensitive169 public void SetValues<T>(int columnIndex, IList<T> values) { 119 170 if (IsType<T>(columnIndex)) { 171 SaveSnapshot(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1); 120 172 variableValues[columnIndex] = (IList)values; 121 173 } else { … … 125 177 } 126 178 127 public void InsertRow(int rowIndex) { // Undo-sensitive 179 public void InsertRow(int rowIndex) { 180 SaveSnapshot(DataPreprocessingChangedEventType.DeleteRow, -1, rowIndex); 128 181 foreach (IList column in variableValues.Values) { 129 182 Type type = column.GetType().GetGenericArguments()[0]; … … 133 186 } 134 187 135 public void DeleteRow(int rowIndex) { // Undo-sensitive 188 public void DeleteRow(int rowIndex) { 189 SaveSnapshot(DataPreprocessingChangedEventType.AddRow, -1, rowIndex); 136 190 foreach (IList column in variableValues.Values) { 137 191 column.RemoveAt(rowIndex); … … 140 194 } 141 195 142 public void InsertColumn<T>(string variableName, int columnIndex) { // Undo-sensitive 196 public void InsertColumn<T>(string variableName, int columnIndex) { 197 SaveSnapshot(DataPreprocessingChangedEventType.DeleteColumn, columnIndex, -1); 143 198 variableValues.Add(columnIndex, new List<T>(Rows)); 144 199 variableNames.Insert(columnIndex, variableName); … … 146 201 } 147 202 148 public void DeleteColumn(int columnIndex) { // Undo-sensitive 203 public void DeleteColumn(int columnIndex) { 204 SaveSnapshot(DataPreprocessingChangedEventType.AddColumn, columnIndex, -1); 149 205 variableValues.Remove(columnIndex); 150 206 variableNames.RemoveAt(columnIndex); … … 203 259 204 260 public bool IsUndoAvailable { 205 get { throw new NotImplementedException(); }261 get { return undoHistory.Count > 0; } 206 262 } 207 263 208 264 public void Undo() { 209 throw new NotImplementedException(); 265 if (IsUndoAvailable) { 266 PDSnapshot previousSnapshot = undoHistory[undoHistory.Count - 1]; 267 variableValues = previousSnapshot.VariableValues; 268 variableNames = previousSnapshot.VariableNames; 269 trainingToTestRatio = previousSnapshot.TrainingToTestRatio; 270 undoHistory.Remove(previousSnapshot); 271 OnChanged(previousSnapshot.ChangedType, 272 previousSnapshot.ChangedColumn, 273 previousSnapshot.ChangedRow); 274 } 210 275 } 211 276
Note: See TracChangeset
for help on using the changeset viewer.