1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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 |
|
---|
34 | [Item("PreprocessingData", "Represents data used for preprocessing.")]
|
---|
35 | public class TransactionalPreprocessingData : PreprocessingData, ITransactionalPreprocessingData {
|
---|
36 |
|
---|
37 | private class Snapshot {
|
---|
38 | public IList<IList> VariableValues { get; set; }
|
---|
39 | public IList<string> VariableNames { get; set; }
|
---|
40 |
|
---|
41 | public IntRange TrainingPartition { get; set; }
|
---|
42 | public IntRange TestPartition { get; set; }
|
---|
43 | public IList<ITransformation> Transformations { get; set; }
|
---|
44 | public DataPreprocessingChangedEventType ChangedType { get; set; }
|
---|
45 |
|
---|
46 | public int ChangedColumn { get; set; }
|
---|
47 | public int ChangedRow { get; set; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | private const int MAX_UNDO_DEPTH = 5;
|
---|
51 |
|
---|
52 | private readonly IList<Snapshot> undoHistory = new List<Snapshot>();
|
---|
53 | private readonly Stack<DataPreprocessingChangedEventType> eventStack = new Stack<DataPreprocessingChangedEventType>();
|
---|
54 |
|
---|
55 | public bool IsInTransaction { get { return eventStack.Count > 0; } }
|
---|
56 |
|
---|
57 | public TransactionalPreprocessingData(IDataAnalysisProblemData problemData)
|
---|
58 | : base(problemData) {
|
---|
59 | }
|
---|
60 |
|
---|
61 | private TransactionalPreprocessingData(TransactionalPreprocessingData original, Cloner cloner)
|
---|
62 | : base(original, cloner) {
|
---|
63 | }
|
---|
64 |
|
---|
65 | private void SaveSnapshot(DataPreprocessingChangedEventType changedType, int column, int row) {
|
---|
66 | if (IsInTransaction) return;
|
---|
67 |
|
---|
68 | var currentSnapshot = new Snapshot {
|
---|
69 | VariableValues = CopyVariableValues(variableValues),
|
---|
70 | VariableNames = new List<string>(variableNames),
|
---|
71 | TrainingPartition = new IntRange(TrainingPartition.Start, TrainingPartition.End),
|
---|
72 | TestPartition = new IntRange(TestPartition.Start, TestPartition.End),
|
---|
73 | Transformations = new List<ITransformation>(transformations),
|
---|
74 | ChangedType = changedType,
|
---|
75 | ChangedColumn = column,
|
---|
76 | ChangedRow = row
|
---|
77 | };
|
---|
78 |
|
---|
79 | if (undoHistory.Count >= MAX_UNDO_DEPTH)
|
---|
80 | undoHistory.RemoveAt(0);
|
---|
81 |
|
---|
82 | undoHistory.Add(currentSnapshot);
|
---|
83 | }
|
---|
84 |
|
---|
85 | #region NamedItem abstract Member Implementations
|
---|
86 |
|
---|
87 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
88 | return new TransactionalPreprocessingData(this, cloner);
|
---|
89 | }
|
---|
90 |
|
---|
91 | #endregion
|
---|
92 |
|
---|
93 | #region Overridden IPreprocessingData Members
|
---|
94 |
|
---|
95 | public override T GetCell<T>(int columnIndex, int rowIndex) {
|
---|
96 | return (T)variableValues[columnIndex][rowIndex];
|
---|
97 | }
|
---|
98 |
|
---|
99 | public override void SetCell<T>(int columnIndex, int rowIndex, T value) {
|
---|
100 | SaveSnapshot(DataPreprocessingChangedEventType.ChangeItem, columnIndex, rowIndex);
|
---|
101 | variableValues[columnIndex][rowIndex] = value;
|
---|
102 | if (!IsInTransaction)
|
---|
103 | OnChanged(DataPreprocessingChangedEventType.ChangeItem, columnIndex, rowIndex);
|
---|
104 | }
|
---|
105 |
|
---|
106 | public override string GetCellAsString(int columnIndex, int rowIndex) {
|
---|
107 | return variableValues[columnIndex][rowIndex].ToString();
|
---|
108 | }
|
---|
109 |
|
---|
110 | public override string GetVariableName(int columnIndex) {
|
---|
111 | return variableNames[columnIndex];
|
---|
112 | }
|
---|
113 |
|
---|
114 | public override int GetColumnIndex(string variableName) {
|
---|
115 | return variableNames.IndexOf(variableName);
|
---|
116 | }
|
---|
117 |
|
---|
118 | public override bool IsType<T>(int columnIndex) {
|
---|
119 | return variableValues[columnIndex] is List<T>;
|
---|
120 | }
|
---|
121 |
|
---|
122 | [Obsolete("use the index based variant, is faster")]
|
---|
123 | public override IList<T> GetValues<T>(string variableName, bool considerSelection) {
|
---|
124 | return GetValues<T>(GetColumnIndex(variableName), considerSelection);
|
---|
125 | }
|
---|
126 |
|
---|
127 | public override IList<T> GetValues<T>(int columnIndex, bool considerSelection) {
|
---|
128 | if (considerSelection) {
|
---|
129 | var list = new List<T>();
|
---|
130 | foreach (var rowIdx in selection[columnIndex]) {
|
---|
131 | list.Add((T)variableValues[columnIndex][rowIdx]);
|
---|
132 | }
|
---|
133 | return list;
|
---|
134 | } else {
|
---|
135 | return (IList<T>)variableValues[columnIndex];
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | public override void SetValues<T>(int columnIndex, IList<T> values) {
|
---|
140 | SaveSnapshot(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1);
|
---|
141 | if (IsType<T>(columnIndex)) {
|
---|
142 | variableValues[columnIndex] = (IList)values;
|
---|
143 | } else {
|
---|
144 | throw new ArgumentException("The datatype of column " + columnIndex + " must be of type " + variableValues[columnIndex].GetType().Name + " but was " + typeof(T).Name);
|
---|
145 | }
|
---|
146 | if (!IsInTransaction)
|
---|
147 | OnChanged(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1);
|
---|
148 | }
|
---|
149 |
|
---|
150 | public override bool SetValue(string value, int columnIndex, int rowIndex) {
|
---|
151 | bool valid = false;
|
---|
152 | if (IsType<double>(columnIndex)) {
|
---|
153 | double val;
|
---|
154 | valid = double.TryParse(value, out val);
|
---|
155 | SetValueIfValid(columnIndex, rowIndex, valid, val);
|
---|
156 | } else if (IsType<string>(columnIndex)) {
|
---|
157 | valid = value != null;
|
---|
158 | SetValueIfValid(columnIndex, rowIndex, valid, value);
|
---|
159 | } else if (IsType<DateTime>(columnIndex)) {
|
---|
160 | DateTime date;
|
---|
161 | valid = DateTime.TryParse(value, out date);
|
---|
162 | SetValueIfValid(columnIndex, rowIndex, valid, date);
|
---|
163 | } else {
|
---|
164 | throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
|
---|
165 | }
|
---|
166 |
|
---|
167 | if (!IsInTransaction)
|
---|
168 | OnChanged(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1);
|
---|
169 |
|
---|
170 | return valid;
|
---|
171 | }
|
---|
172 |
|
---|
173 | public override bool Validate(string value, out string errorMessage, int columnIndex) {
|
---|
174 | if (columnIndex < 0 || columnIndex > VariableNames.Count()) {
|
---|
175 | throw new ArgumentOutOfRangeException("column index is out of range");
|
---|
176 | }
|
---|
177 |
|
---|
178 | bool valid = false;
|
---|
179 | errorMessage = string.Empty;
|
---|
180 | if (IsType<double>(columnIndex)) {
|
---|
181 | double val;
|
---|
182 | valid = double.TryParse(value, out val);
|
---|
183 | if (!valid) {
|
---|
184 | errorMessage = "Invalid Value (Valid Value Format: \"" + FormatPatterns.GetDoubleFormatPattern() + "\")";
|
---|
185 | }
|
---|
186 | } else if (IsType<string>(columnIndex)) {
|
---|
187 | valid = value != null;
|
---|
188 | if (!valid) {
|
---|
189 | errorMessage = "Invalid Value (string must not be null)";
|
---|
190 | }
|
---|
191 | } else if (IsType<DateTime>(columnIndex)) {
|
---|
192 | DateTime date;
|
---|
193 | valid = DateTime.TryParse(value, out date);
|
---|
194 | if (!valid) {
|
---|
195 | errorMessage = "Invalid Value (Valid Value Format: \"" + CultureInfo.CurrentCulture.DateTimeFormat + "\"";
|
---|
196 | }
|
---|
197 | } else {
|
---|
198 | throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
|
---|
199 | }
|
---|
200 |
|
---|
201 | return valid;
|
---|
202 | }
|
---|
203 |
|
---|
204 | private void SetValueIfValid<T>(int columnIndex, int rowIndex, bool valid, T value) {
|
---|
205 | if (valid)
|
---|
206 | SetCell<T>(columnIndex, rowIndex, value);
|
---|
207 | }
|
---|
208 |
|
---|
209 | public override bool AreAllStringColumns(IEnumerable<int> columnIndices) {
|
---|
210 | return columnIndices.All(x => IsType<string>(x));
|
---|
211 | }
|
---|
212 |
|
---|
213 | public override void DeleteRowsWithIndices(IEnumerable<int> rows) {
|
---|
214 | foreach (int rowIndex in rows) {
|
---|
215 | DeleteRow(rowIndex);
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | public override void InsertRow(int rowIndex) {
|
---|
220 | SaveSnapshot(DataPreprocessingChangedEventType.DeleteRow, -1, rowIndex);
|
---|
221 | foreach (IList column in variableValues) {
|
---|
222 | Type type = column.GetType().GetGenericArguments()[0];
|
---|
223 | column.Insert(rowIndex, type.IsValueType ? Activator.CreateInstance(type) : null);
|
---|
224 | }
|
---|
225 | if (!IsInTransaction)
|
---|
226 | OnChanged(DataPreprocessingChangedEventType.AddRow, -1, rowIndex);
|
---|
227 | }
|
---|
228 |
|
---|
229 | public override void DeleteRow(int rowIndex) {
|
---|
230 | SaveSnapshot(DataPreprocessingChangedEventType.AddRow, -1, rowIndex);
|
---|
231 | foreach (IList column in variableValues) {
|
---|
232 | column.RemoveAt(rowIndex);
|
---|
233 | }
|
---|
234 | if (!IsInTransaction)
|
---|
235 | OnChanged(DataPreprocessingChangedEventType.DeleteRow, -1, rowIndex);
|
---|
236 | }
|
---|
237 |
|
---|
238 | public override void InsertColumn<T>(string variableName, int columnIndex) {
|
---|
239 | SaveSnapshot(DataPreprocessingChangedEventType.DeleteColumn, columnIndex, -1);
|
---|
240 | variableValues.Insert(columnIndex, new List<T>(Rows));
|
---|
241 | variableNames.Insert(columnIndex, variableName);
|
---|
242 | if (!IsInTransaction)
|
---|
243 | OnChanged(DataPreprocessingChangedEventType.AddColumn, columnIndex, -1);
|
---|
244 | }
|
---|
245 |
|
---|
246 | public override void DeleteColumn(int columnIndex) {
|
---|
247 | SaveSnapshot(DataPreprocessingChangedEventType.AddColumn, columnIndex, -1);
|
---|
248 | variableValues.RemoveAt(columnIndex);
|
---|
249 | variableNames.RemoveAt(columnIndex);
|
---|
250 | if (!IsInTransaction)
|
---|
251 | OnChanged(DataPreprocessingChangedEventType.DeleteColumn, columnIndex, -1);
|
---|
252 | }
|
---|
253 |
|
---|
254 | public override Dataset ExportToDataset() {
|
---|
255 | IList<IList> values = new List<IList>();
|
---|
256 |
|
---|
257 | for (int i = 0; i < Columns; ++i) {
|
---|
258 | values.Add(variableValues[i]);
|
---|
259 | }
|
---|
260 |
|
---|
261 | var dataset = new Dataset(variableNames, values);
|
---|
262 | return dataset;
|
---|
263 | }
|
---|
264 |
|
---|
265 | public override void ClearSelection() {
|
---|
266 | Selection = new Dictionary<int, IList<int>>();
|
---|
267 | }
|
---|
268 |
|
---|
269 | public override event EventHandler SelectionChanged;
|
---|
270 |
|
---|
271 | protected override void OnSelectionChanged() {
|
---|
272 | var listeners = SelectionChanged;
|
---|
273 | if (listeners != null) listeners(this, EventArgs.Empty);
|
---|
274 | }
|
---|
275 |
|
---|
276 |
|
---|
277 | #endregion
|
---|
278 |
|
---|
279 | #region TransactionalPreprocessingData members
|
---|
280 |
|
---|
281 | public bool IsUndoAvailable {
|
---|
282 | get { return undoHistory.Count > 0; }
|
---|
283 | }
|
---|
284 |
|
---|
285 | public void Undo() {
|
---|
286 | if (IsUndoAvailable) {
|
---|
287 | Snapshot previousSnapshot = undoHistory[undoHistory.Count - 1];
|
---|
288 | variableValues = previousSnapshot.VariableValues;
|
---|
289 | variableNames = previousSnapshot.VariableNames;
|
---|
290 | TrainingPartition = previousSnapshot.TrainingPartition;
|
---|
291 | TestPartition = previousSnapshot.TestPartition;
|
---|
292 | transformations = previousSnapshot.Transformations;
|
---|
293 | undoHistory.Remove(previousSnapshot);
|
---|
294 | OnChanged(previousSnapshot.ChangedType,
|
---|
295 | previousSnapshot.ChangedColumn,
|
---|
296 | previousSnapshot.ChangedRow);
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) {
|
---|
301 | BeginTransaction(type);
|
---|
302 | action();
|
---|
303 | EndTransaction();
|
---|
304 | }
|
---|
305 |
|
---|
306 | public void BeginTransaction(DataPreprocessingChangedEventType type) {
|
---|
307 | SaveSnapshot(type, -1, -1);
|
---|
308 | eventStack.Push(type);
|
---|
309 | }
|
---|
310 |
|
---|
311 | public void EndTransaction() {
|
---|
312 | if (eventStack.Count == 0)
|
---|
313 | throw new InvalidOperationException("There is no open transaction that can be ended.");
|
---|
314 |
|
---|
315 | var @event = eventStack.Pop();
|
---|
316 | OnChanged(@event, -1, -1);
|
---|
317 | }
|
---|
318 |
|
---|
319 | #endregion
|
---|
320 | }
|
---|
321 | }
|
---|