Changeset 10586 for branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/PreprocessingData.cs
- Timestamp:
- 03/12/14 17:03:45 (10 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/PreprocessingData.cs
r10581 r10586 31 31 namespace HeuristicLab.DataPreprocessing { 32 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 47 33 [Item("PreprocessingData", "Represents data used for preprocessing.")] 48 34 public class PreprocessingData : NamedItem, IPreprocessingData { 49 35 50 private const int MAX_UNDO_DEPTH = 5; 51 52 private IDictionary<int, IList> variableValues; 53 54 private IList<string> variableNames; 55 56 private double trainingToTestRatio; 57 58 private IList<PDSnapshot> undoHistory; 59 60 //TODO: refactor extract Transaction logic in a own class 61 private int transactionDepth = 0; 62 63 private PreprocessingData(PreprocessingData original, Cloner cloner) 36 protected IDictionary<int, IList> variableValues; 37 38 protected IList<string> variableNames; 39 40 protected double trainingToTestRatio; 41 42 protected PreprocessingData(PreprocessingData original, Cloner cloner) 64 43 : base(original, cloner) { 65 44 variableValues = CopyVariableValues(original.variableValues); 66 45 variableNames = new List<string>(original.variableNames); 67 46 trainingToTestRatio = original.trainingToTestRatio; 68 undoHistory = new List<PDSnapshot>();69 47 } 70 48 … … 92 70 93 71 trainingToTestRatio = (double)problemData.TrainingPartition.Size / Math.Max(problemData.Dataset.Rows, double.Epsilon); 94 undoHistory = new List<PDSnapshot>();95 72 } 96 73 … … 103 80 } 104 81 105 pr ivateIDictionary<int, IList> CopyVariableValues(IDictionary<int, IList> original) {82 protected IDictionary<int, IList> CopyVariableValues(IDictionary<int, IList> original) { 106 83 var copy = new Dictionary<int, IList>(variableValues); 107 84 for (int i = 0; i < original.Count; i++) { … … 111 88 } 112 89 113 private void SaveSnapshot(DataPreprocessingChangedEventType changedType, int column, int row) {114 if (transactionDepth > 0) return;115 116 PDSnapshot currentSnapshot = new PDSnapshot();117 currentSnapshot.VariableValues = CopyVariableValues(variableValues);118 currentSnapshot.VariableNames = new List<string>(variableNames);119 currentSnapshot.TrainingToTestRatio = trainingToTestRatio;120 currentSnapshot.ChangedType = changedType;121 currentSnapshot.ChangedColumn = column;122 currentSnapshot.ChangedRow = row;123 124 if (undoHistory.Count >= MAX_UNDO_DEPTH)125 undoHistory.RemoveAt(0);126 127 undoHistory.Add(currentSnapshot);128 }129 130 90 #region NamedItem abstract Member Implementations 131 91 … … 143 103 144 104 145 public void SetCell<T>(int columnIndex, int rowIndex, T value) { 146 SaveSnapshot(DataPreprocessingChangedEventType.ChangeItem, columnIndex, rowIndex); 105 public virtual void SetCell<T>(int columnIndex, int rowIndex, T value) { 147 106 variableValues[columnIndex][rowIndex] = value; 148 if (transactionDepth <= 0)149 OnChanged(DataPreprocessingChangedEventType.ChangeItem, columnIndex, rowIndex);150 107 } 151 108 … … 165 122 } 166 123 167 public v oid SetValues<T>(int columnIndex, IList<T> values) {124 public virtual void SetValues<T>(int columnIndex, IList<T> values) { 168 125 if (IsType<T>(columnIndex)) { 169 SaveSnapshot(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1);170 126 variableValues[columnIndex] = (IList)values; 171 127 } else { 172 128 throw new ArgumentException("The datatype of column " + columnIndex + " must be of type " + variableValues[columnIndex].GetType().Name + " but was " + typeof(T).Name); 173 129 } 174 if (transactionDepth <= 0) 175 OnChanged(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1); 176 } 177 178 public void InsertRow(int rowIndex) { 179 SaveSnapshot(DataPreprocessingChangedEventType.DeleteRow, -1, rowIndex); 130 } 131 132 public virtual void InsertRow(int rowIndex) { 180 133 foreach (IList column in variableValues.Values) { 181 134 Type type = column.GetType().GetGenericArguments()[0]; 182 135 column.Insert(rowIndex, type.IsValueType ? Activator.CreateInstance(type) : null); 183 136 } 184 if (transactionDepth <= 0) 185 OnChanged(DataPreprocessingChangedEventType.AddRow, -1, rowIndex); 186 } 187 188 public void DeleteRow(int rowIndex) { 189 SaveSnapshot(DataPreprocessingChangedEventType.AddRow, -1, rowIndex); 137 } 138 139 public virtual void DeleteRow(int rowIndex) { 190 140 foreach (IList column in variableValues.Values) { 191 141 column.RemoveAt(rowIndex); 192 142 } 193 if (transactionDepth <= 0) 194 OnChanged(DataPreprocessingChangedEventType.DeleteRow, -1, rowIndex); 195 } 196 197 public void InsertColumn<T>(string variableName, int columnIndex) { 198 SaveSnapshot(DataPreprocessingChangedEventType.DeleteColumn, columnIndex, -1); 143 } 144 145 public virtual void InsertColumn<T>(string variableName, int columnIndex) { 199 146 variableValues.Add(columnIndex, new List<T>(Rows)); 200 147 variableNames.Insert(columnIndex, variableName); 201 if (transactionDepth <= 0) 202 OnChanged(DataPreprocessingChangedEventType.AddColumn, columnIndex, -1); 203 } 204 205 public void DeleteColumn(int columnIndex) { 206 SaveSnapshot(DataPreprocessingChangedEventType.AddColumn, columnIndex, -1); 148 } 149 150 public virtual void DeleteColumn(int columnIndex) { 207 151 variableValues.Remove(columnIndex); 208 152 variableNames.RemoveAt(columnIndex); 209 if (transactionDepth <= 0)210 OnChanged(DataPreprocessingChangedEventType.DeleteColumn, columnIndex, -1);211 153 } 212 154 … … 252 194 var dataset = new Dataset(variableNames, values); 253 195 return dataset; 254 }255 256 public event DataPreprocessingChangedEventHandler Changed;257 protected virtual void OnChanged(DataPreprocessingChangedEventType type, int column, int row) {258 var listeners = Changed;259 if (listeners != null) listeners(this, new DataPreprocessingChangedEventArgs(type, column, row));260 }261 262 public bool IsUndoAvailable {263 get { return undoHistory.Count > 0; }264 }265 266 public void Undo() {267 if (IsUndoAvailable) {268 PDSnapshot previousSnapshot = undoHistory[undoHistory.Count - 1];269 variableValues = previousSnapshot.VariableValues;270 variableNames = previousSnapshot.VariableNames;271 trainingToTestRatio = previousSnapshot.TrainingToTestRatio;272 undoHistory.Remove(previousSnapshot);273 OnChanged(previousSnapshot.ChangedType,274 previousSnapshot.ChangedColumn,275 previousSnapshot.ChangedRow);276 }277 }278 279 public void BeginTransaction() {280 SaveSnapshot(DataPreprocessingChangedEventType.Any, -1, -1);281 transactionDepth++;282 }283 284 public void EndTransaction() {285 transactionDepth--;286 if (transactionDepth < 0)287 throw new InvalidOperationException("There is no open transaction that can be ended.");288 if (transactionDepth == 0)289 OnChanged(DataPreprocessingChangedEventType.Any, -1, -1);290 196 } 291 197
Note: See TracChangeset
for help on using the changeset viewer.