1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Globalization;
|
---|
26 | using System.Linq;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.DataPreprocessing {
|
---|
33 | [Item("PreprocessingData", "Represents data used for preprocessing.")]
|
---|
34 | public class TransactionalPreprocessingData : PreprocessingData, ITransactionalPreprocessingData {
|
---|
35 |
|
---|
36 | private class Snapshot {
|
---|
37 | public IList<IList> VariableValues { get; set; }
|
---|
38 | public IList<string> VariableNames { get; set; }
|
---|
39 |
|
---|
40 | public IntRange TrainingPartition { get; set; }
|
---|
41 | public IntRange TestPartition { get; set; }
|
---|
42 | public IList<ITransformation> Transformations { get; set; }
|
---|
43 | public DataPreprocessingChangedEventType ChangedType { get; set; }
|
---|
44 |
|
---|
45 | public int ChangedColumn { get; set; }
|
---|
46 | public int ChangedRow { get; set; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | private const int MAX_UNDO_DEPTH = 5;
|
---|
50 |
|
---|
51 | private readonly IList<Snapshot> undoHistory = new List<Snapshot>();
|
---|
52 | private readonly Stack<DataPreprocessingChangedEventType> eventStack = new Stack<DataPreprocessingChangedEventType>();
|
---|
53 |
|
---|
54 | public bool IsInTransaction { get { return eventStack.Count > 0; } }
|
---|
55 |
|
---|
56 | public TransactionalPreprocessingData(IDataAnalysisProblemData problemData)
|
---|
57 | : base(problemData) {
|
---|
58 | }
|
---|
59 |
|
---|
60 | protected TransactionalPreprocessingData(TransactionalPreprocessingData original, Cloner cloner)
|
---|
61 | : base(original, cloner) {
|
---|
62 | }
|
---|
63 |
|
---|
64 | private void SaveSnapshot(DataPreprocessingChangedEventType changedType, int column, int row) {
|
---|
65 | if (IsInTransaction) return;
|
---|
66 |
|
---|
67 | var currentSnapshot = new Snapshot {
|
---|
68 | VariableValues = CopyVariableValues(variableValues),
|
---|
69 | VariableNames = new List<string>(variableNames),
|
---|
70 | TrainingPartition = new IntRange(TrainingPartition.Start, TrainingPartition.End),
|
---|
71 | TestPartition = new IntRange(TestPartition.Start, TestPartition.End),
|
---|
72 | Transformations = new List<ITransformation>(transformations),
|
---|
73 | ChangedType = changedType,
|
---|
74 | ChangedColumn = column,
|
---|
75 | ChangedRow = row
|
---|
76 | };
|
---|
77 |
|
---|
78 | if (undoHistory.Count >= MAX_UNDO_DEPTH)
|
---|
79 | undoHistory.RemoveAt(0);
|
---|
80 |
|
---|
81 | undoHistory.Add(currentSnapshot);
|
---|
82 | }
|
---|
83 |
|
---|
84 | #region NamedItem abstract Member Implementations
|
---|
85 |
|
---|
86 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
87 | return new TransactionalPreprocessingData(this, cloner);
|
---|
88 | }
|
---|
89 |
|
---|
90 | #endregion
|
---|
91 |
|
---|
92 | #region Overridden IPreprocessingData Members
|
---|
93 |
|
---|
94 | public override T GetCell<T>(int columnIndex, int rowIndex) {
|
---|
95 | return (T)variableValues[columnIndex][rowIndex];
|
---|
96 | }
|
---|
97 |
|
---|
98 | public override void SetCell<T>(int columnIndex, int rowIndex, T value) {
|
---|
99 | SaveSnapshot(DataPreprocessingChangedEventType.ChangeItem, columnIndex, rowIndex);
|
---|
100 |
|
---|
101 | for (int i = Rows; i <= rowIndex; i++)
|
---|
102 | InsertRow(i);
|
---|
103 | for (int i = Columns; i <= columnIndex; i++)
|
---|
104 | InsertColumn<T>(i.ToString(), i);
|
---|
105 |
|
---|
106 | variableValues[columnIndex][rowIndex] = value;
|
---|
107 | if (!IsInTransaction)
|
---|
108 | OnChanged(DataPreprocessingChangedEventType.ChangeItem, columnIndex, rowIndex);
|
---|
109 | }
|
---|
110 |
|
---|
111 | public override string GetCellAsString(int columnIndex, int rowIndex) {
|
---|
112 | return variableValues[columnIndex][rowIndex].ToString();
|
---|
113 | }
|
---|
114 |
|
---|
115 | public override string GetVariableName(int columnIndex) {
|
---|
116 | return variableNames[columnIndex];
|
---|
117 | }
|
---|
118 |
|
---|
119 | public override int GetColumnIndex(string variableName) {
|
---|
120 | return variableNames.IndexOf(variableName);
|
---|
121 | }
|
---|
122 |
|
---|
123 | public override bool VariableHasType<T>(int columnIndex) {
|
---|
124 | return columnIndex >= variableValues.Count || variableValues[columnIndex] is List<T>;
|
---|
125 | }
|
---|
126 |
|
---|
127 | [Obsolete("use the index based variant, is faster")]
|
---|
128 | public override IList<T> GetValues<T>(string variableName, bool considerSelection) {
|
---|
129 | return GetValues<T>(GetColumnIndex(variableName), considerSelection);
|
---|
130 | }
|
---|
131 |
|
---|
132 | public override IList<T> GetValues<T>(int columnIndex, bool considerSelection) {
|
---|
133 | if (considerSelection) {
|
---|
134 | var list = new List<T>();
|
---|
135 | foreach (var rowIdx in selection[columnIndex]) {
|
---|
136 | list.Add((T)variableValues[columnIndex][rowIdx]);
|
---|
137 | }
|
---|
138 | return list;
|
---|
139 | } else {
|
---|
140 | return (IList<T>)variableValues[columnIndex];
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | public override void SetValues<T>(int columnIndex, IList<T> values) {
|
---|
145 | SaveSnapshot(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1);
|
---|
146 | if (VariableHasType<T>(columnIndex)) {
|
---|
147 | variableValues[columnIndex] = (IList)values;
|
---|
148 | } else {
|
---|
149 | throw new ArgumentException("The datatype of column " + columnIndex + " must be of type " + variableValues[columnIndex].GetType().Name + " but was " + typeof(T).Name);
|
---|
150 | }
|
---|
151 | if (!IsInTransaction)
|
---|
152 | OnChanged(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1);
|
---|
153 | }
|
---|
154 |
|
---|
155 | public override bool SetValue(string value, int columnIndex, int rowIndex) {
|
---|
156 | bool valid = false;
|
---|
157 | if (VariableHasType<double>(columnIndex)) {
|
---|
158 | double val;
|
---|
159 | valid = double.TryParse(value, out val);
|
---|
160 | SetValueIfValid(columnIndex, rowIndex, valid, val);
|
---|
161 | } else if (VariableHasType<string>(columnIndex)) {
|
---|
162 | valid = value != null;
|
---|
163 | SetValueIfValid(columnIndex, rowIndex, valid, value);
|
---|
164 | } else if (VariableHasType<DateTime>(columnIndex)) {
|
---|
165 | DateTime date;
|
---|
166 | valid = DateTime.TryParse(value, out date);
|
---|
167 | SetValueIfValid(columnIndex, rowIndex, valid, date);
|
---|
168 | } else {
|
---|
169 | throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (!IsInTransaction)
|
---|
173 | OnChanged(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1);
|
---|
174 |
|
---|
175 | return valid;
|
---|
176 | }
|
---|
177 |
|
---|
178 | public override bool Validate(string value, out string errorMessage, int columnIndex) {
|
---|
179 | if (columnIndex < 0 || columnIndex > VariableNames.Count()) {
|
---|
180 | throw new ArgumentOutOfRangeException("column index is out of range");
|
---|
181 | }
|
---|
182 |
|
---|
183 | bool valid = false;
|
---|
184 | errorMessage = string.Empty;
|
---|
185 | if (VariableHasType<double>(columnIndex)) {
|
---|
186 | double val;
|
---|
187 | valid = double.TryParse(value, out val);
|
---|
188 | if (!valid) {
|
---|
189 | errorMessage = "Invalid Value (Valid Value Format: \"" + FormatPatterns.GetDoubleFormatPattern() + "\")";
|
---|
190 | }
|
---|
191 | } else if (VariableHasType<string>(columnIndex)) {
|
---|
192 | valid = value != null;
|
---|
193 | if (!valid) {
|
---|
194 | errorMessage = "Invalid Value (string must not be null)";
|
---|
195 | }
|
---|
196 | } else if (VariableHasType<DateTime>(columnIndex)) {
|
---|
197 | DateTime date;
|
---|
198 | valid = DateTime.TryParse(value, out date);
|
---|
199 | if (!valid) {
|
---|
200 | errorMessage = "Invalid Value (Valid Value Format: \"" + CultureInfo.CurrentCulture.DateTimeFormat + "\"";
|
---|
201 | }
|
---|
202 | } else {
|
---|
203 | throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
|
---|
204 | }
|
---|
205 |
|
---|
206 | return valid;
|
---|
207 | }
|
---|
208 |
|
---|
209 | private void SetValueIfValid<T>(int columnIndex, int rowIndex, bool valid, T value) {
|
---|
210 | if (valid)
|
---|
211 | SetCell<T>(columnIndex, rowIndex, value);
|
---|
212 | }
|
---|
213 |
|
---|
214 | public override bool AreAllStringColumns(IEnumerable<int> columnIndices) {
|
---|
215 | return columnIndices.All(x => VariableHasType<string>(x));
|
---|
216 | }
|
---|
217 |
|
---|
218 | public override void DeleteRowsWithIndices(IEnumerable<int> rows) {
|
---|
219 | SaveSnapshot(DataPreprocessingChangedEventType.AddRow, -1, -1);
|
---|
220 | foreach (int rowIndex in rows.OrderByDescending(x => x)) {
|
---|
221 | foreach (IList column in variableValues) {
|
---|
222 | column.RemoveAt(rowIndex);
|
---|
223 | }
|
---|
224 | }
|
---|
225 | if (!IsInTransaction)
|
---|
226 | OnChanged(DataPreprocessingChangedEventType.DeleteRow, -1, -1);
|
---|
227 | ResetPartitions();
|
---|
228 | }
|
---|
229 |
|
---|
230 | public override void InsertRow(int rowIndex) {
|
---|
231 | SaveSnapshot(DataPreprocessingChangedEventType.DeleteRow, -1, rowIndex);
|
---|
232 | foreach (IList column in variableValues) {
|
---|
233 | Type type = column.GetType().GetGenericArguments()[0];
|
---|
234 | column.Insert(rowIndex, type.IsValueType ? Activator.CreateInstance(type) : null);
|
---|
235 | }
|
---|
236 | if (!IsInTransaction)
|
---|
237 | OnChanged(DataPreprocessingChangedEventType.AddRow, -1, rowIndex);
|
---|
238 | ResetPartitions();
|
---|
239 | }
|
---|
240 |
|
---|
241 | public override void DeleteRow(int rowIndex) {
|
---|
242 | SaveSnapshot(DataPreprocessingChangedEventType.AddRow, -1, rowIndex);
|
---|
243 | foreach (IList column in variableValues) {
|
---|
244 | column.RemoveAt(rowIndex);
|
---|
245 | }
|
---|
246 | if (!IsInTransaction)
|
---|
247 | OnChanged(DataPreprocessingChangedEventType.DeleteRow, -1, rowIndex);
|
---|
248 | ResetPartitions();
|
---|
249 | }
|
---|
250 |
|
---|
251 | public override void InsertColumn<T>(string variableName, int columnIndex) {
|
---|
252 | SaveSnapshot(DataPreprocessingChangedEventType.DeleteColumn, columnIndex, -1);
|
---|
253 | variableValues.Insert(columnIndex, new List<T>(Enumerable.Repeat(default(T), Rows)));
|
---|
254 | variableNames.Insert(columnIndex, variableName);
|
---|
255 | if (!IsInTransaction)
|
---|
256 | OnChanged(DataPreprocessingChangedEventType.AddColumn, columnIndex, -1);
|
---|
257 | }
|
---|
258 |
|
---|
259 | public override void DeleteColumn(int columnIndex) {
|
---|
260 | SaveSnapshot(DataPreprocessingChangedEventType.AddColumn, columnIndex, -1);
|
---|
261 | variableValues.RemoveAt(columnIndex);
|
---|
262 | variableNames.RemoveAt(columnIndex);
|
---|
263 | if (!IsInTransaction)
|
---|
264 | OnChanged(DataPreprocessingChangedEventType.DeleteColumn, columnIndex, -1);
|
---|
265 | }
|
---|
266 |
|
---|
267 | public override Dataset ExportToDataset() {
|
---|
268 | IList<IList> values = new List<IList>();
|
---|
269 |
|
---|
270 | for (int i = 0; i < Columns; ++i) {
|
---|
271 | values.Add(variableValues[i]);
|
---|
272 | }
|
---|
273 |
|
---|
274 | var dataset = new Dataset(variableNames, values);
|
---|
275 | return dataset;
|
---|
276 | }
|
---|
277 |
|
---|
278 | public override void ClearSelection() {
|
---|
279 | Selection = new Dictionary<int, IList<int>>();
|
---|
280 | }
|
---|
281 |
|
---|
282 | public override event EventHandler SelectionChanged;
|
---|
283 |
|
---|
284 | protected override void OnSelectionChanged() {
|
---|
285 | var listeners = SelectionChanged;
|
---|
286 | if (listeners != null) listeners(this, EventArgs.Empty);
|
---|
287 | }
|
---|
288 |
|
---|
289 |
|
---|
290 | private void ResetPartitions() {
|
---|
291 | TrainingPartition = new IntRange();
|
---|
292 | TestPartition = new IntRange();
|
---|
293 | }
|
---|
294 |
|
---|
295 | #endregion
|
---|
296 |
|
---|
297 | #region TransactionalPreprocessingData members
|
---|
298 |
|
---|
299 | public bool IsUndoAvailable {
|
---|
300 | get { return undoHistory.Count > 0; }
|
---|
301 | }
|
---|
302 |
|
---|
303 | public void Undo() {
|
---|
304 | if (IsUndoAvailable) {
|
---|
305 | Snapshot previousSnapshot = undoHistory[undoHistory.Count - 1];
|
---|
306 | variableValues = previousSnapshot.VariableValues;
|
---|
307 | variableNames = previousSnapshot.VariableNames;
|
---|
308 | TrainingPartition = previousSnapshot.TrainingPartition;
|
---|
309 | TestPartition = previousSnapshot.TestPartition;
|
---|
310 | transformations = previousSnapshot.Transformations;
|
---|
311 | undoHistory.Remove(previousSnapshot);
|
---|
312 | OnChanged(previousSnapshot.ChangedType,
|
---|
313 | previousSnapshot.ChangedColumn,
|
---|
314 | previousSnapshot.ChangedRow);
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) {
|
---|
319 | BeginTransaction(type);
|
---|
320 | action();
|
---|
321 | EndTransaction();
|
---|
322 | }
|
---|
323 |
|
---|
324 | public void BeginTransaction(DataPreprocessingChangedEventType type) {
|
---|
325 | SaveSnapshot(type, -1, -1);
|
---|
326 | eventStack.Push(type);
|
---|
327 | }
|
---|
328 |
|
---|
329 | public void EndTransaction() {
|
---|
330 | if (eventStack.Count == 0)
|
---|
331 | throw new InvalidOperationException("There is no open transaction that can be ended.");
|
---|
332 |
|
---|
333 | var @event = eventStack.Pop();
|
---|
334 | OnChanged(@event, -1, -1);
|
---|
335 | }
|
---|
336 |
|
---|
337 | #endregion
|
---|
338 | }
|
---|
339 | }
|
---|