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 HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis.Transformations;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.DataPreprocessing {
|
---|
32 |
|
---|
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 | private 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 | {
|
---|
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 | {
|
---|
108 | return variableValues[columnIndex][rowIndex].ToString();
|
---|
109 | }
|
---|
110 |
|
---|
111 | public override string GetVariableName(int columnIndex)
|
---|
112 | {
|
---|
113 | return variableNames[columnIndex];
|
---|
114 | }
|
---|
115 |
|
---|
116 | public override int GetColumnIndex(string variableName)
|
---|
117 | {
|
---|
118 | return variableNames.IndexOf(variableName);
|
---|
119 | }
|
---|
120 |
|
---|
121 | public override bool IsType<T>(int columnIndex)
|
---|
122 | {
|
---|
123 | return variableValues[columnIndex] is List<T>;
|
---|
124 | }
|
---|
125 |
|
---|
126 | [Obsolete("use the index based variant, is faster")]
|
---|
127 | public override IList<T> GetValues<T>(string variableName, bool considerSelection)
|
---|
128 | {
|
---|
129 | return GetValues<T>(GetColumnIndex(variableName), considerSelection);
|
---|
130 | }
|
---|
131 |
|
---|
132 | public override IList<T> GetValues<T>(int columnIndex, bool considerSelection)
|
---|
133 | {
|
---|
134 | if (considerSelection)
|
---|
135 | {
|
---|
136 | var list = new List<T>();
|
---|
137 | foreach (var rowIdx in selection[columnIndex])
|
---|
138 | {
|
---|
139 | list.Add((T)variableValues[columnIndex][rowIdx]);
|
---|
140 | }
|
---|
141 | return list;
|
---|
142 | }
|
---|
143 | else
|
---|
144 | {
|
---|
145 | return (IList<T>)variableValues[columnIndex];
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | public override void SetValues<T>(int columnIndex, IList<T> values) {
|
---|
150 | SaveSnapshot(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1);
|
---|
151 | if (IsType<T>(columnIndex))
|
---|
152 | {
|
---|
153 | variableValues[columnIndex] = (IList)values;
|
---|
154 | }
|
---|
155 | else
|
---|
156 | {
|
---|
157 | throw new ArgumentException("The datatype of column " + columnIndex + " must be of type " + variableValues[columnIndex].GetType().Name + " but was " + typeof(T).Name);
|
---|
158 | }
|
---|
159 | if (!IsInTransaction)
|
---|
160 | OnChanged(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1);
|
---|
161 | }
|
---|
162 |
|
---|
163 | public override void InsertRow(int rowIndex) {
|
---|
164 | SaveSnapshot(DataPreprocessingChangedEventType.DeleteRow, -1, rowIndex);
|
---|
165 | foreach (IList column in variableValues)
|
---|
166 | {
|
---|
167 | Type type = column.GetType().GetGenericArguments()[0];
|
---|
168 | column.Insert(rowIndex, type.IsValueType ? Activator.CreateInstance(type) : null);
|
---|
169 | }
|
---|
170 | if (!IsInTransaction)
|
---|
171 | OnChanged(DataPreprocessingChangedEventType.AddRow, -1, rowIndex);
|
---|
172 | }
|
---|
173 |
|
---|
174 | public override void DeleteRow(int rowIndex) {
|
---|
175 | SaveSnapshot(DataPreprocessingChangedEventType.AddRow, -1, rowIndex);
|
---|
176 | foreach (IList column in variableValues)
|
---|
177 | {
|
---|
178 | column.RemoveAt(rowIndex);
|
---|
179 | }
|
---|
180 | if (!IsInTransaction)
|
---|
181 | OnChanged(DataPreprocessingChangedEventType.DeleteRow, -1, rowIndex);
|
---|
182 | }
|
---|
183 |
|
---|
184 | public override void InsertColumn<T>(string variableName, int columnIndex) {
|
---|
185 | SaveSnapshot(DataPreprocessingChangedEventType.DeleteColumn, columnIndex, -1);
|
---|
186 | variableValues.Insert(columnIndex, new List<T>(Rows));
|
---|
187 | variableNames.Insert(columnIndex, variableName);
|
---|
188 | if (!IsInTransaction)
|
---|
189 | OnChanged(DataPreprocessingChangedEventType.AddColumn, columnIndex, -1);
|
---|
190 | }
|
---|
191 |
|
---|
192 | public override void DeleteColumn(int columnIndex) {
|
---|
193 | SaveSnapshot(DataPreprocessingChangedEventType.AddColumn, columnIndex, -1);
|
---|
194 | variableValues.RemoveAt(columnIndex);
|
---|
195 | variableNames.RemoveAt(columnIndex);
|
---|
196 | if (!IsInTransaction)
|
---|
197 | OnChanged(DataPreprocessingChangedEventType.DeleteColumn, columnIndex, -1);
|
---|
198 | }
|
---|
199 |
|
---|
200 | public override Dataset ExportToDataset()
|
---|
201 | {
|
---|
202 | IList<IList> values = new List<IList>();
|
---|
203 |
|
---|
204 | for (int i = 0; i < Columns; ++i)
|
---|
205 | {
|
---|
206 | values.Add(variableValues[i]);
|
---|
207 | }
|
---|
208 |
|
---|
209 | var dataset = new Dataset(variableNames, values);
|
---|
210 | return dataset;
|
---|
211 | }
|
---|
212 |
|
---|
213 | public override void ClearSelection()
|
---|
214 | {
|
---|
215 | Selection = new Dictionary<int, IList<int>>();
|
---|
216 | }
|
---|
217 |
|
---|
218 | public override event EventHandler SelectionChanged;
|
---|
219 |
|
---|
220 | protected override void OnSelectionChanged()
|
---|
221 | {
|
---|
222 | var listeners = SelectionChanged;
|
---|
223 | if (listeners != null) listeners(this, EventArgs.Empty);
|
---|
224 | }
|
---|
225 |
|
---|
226 |
|
---|
227 | #endregion
|
---|
228 |
|
---|
229 | #region TransactionalPreprocessingData members
|
---|
230 |
|
---|
231 | public bool IsUndoAvailable {
|
---|
232 | get { return undoHistory.Count > 0; }
|
---|
233 | }
|
---|
234 |
|
---|
235 | public void Undo() {
|
---|
236 | if (IsUndoAvailable) {
|
---|
237 | Snapshot previousSnapshot = undoHistory[undoHistory.Count - 1];
|
---|
238 | variableValues = previousSnapshot.VariableValues;
|
---|
239 | variableNames = previousSnapshot.VariableNames;
|
---|
240 | TrainingPartition = previousSnapshot.TrainingPartition;
|
---|
241 | TestPartition = previousSnapshot.TestPartition;
|
---|
242 | transformations = previousSnapshot.Transformations;
|
---|
243 | undoHistory.Remove(previousSnapshot);
|
---|
244 | OnChanged(previousSnapshot.ChangedType,
|
---|
245 | previousSnapshot.ChangedColumn,
|
---|
246 | previousSnapshot.ChangedRow);
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) {
|
---|
251 | BeginTransaction(type);
|
---|
252 | action();
|
---|
253 | EndTransaction();
|
---|
254 | }
|
---|
255 |
|
---|
256 | public void BeginTransaction(DataPreprocessingChangedEventType type) {
|
---|
257 | SaveSnapshot(type, -1, -1);
|
---|
258 | eventStack.Push(type);
|
---|
259 | }
|
---|
260 |
|
---|
261 | public void EndTransaction() {
|
---|
262 | if (eventStack.Count == 0)
|
---|
263 | throw new InvalidOperationException("There is no open transaction that can be ended.");
|
---|
264 |
|
---|
265 | var @event = eventStack.Pop();
|
---|
266 | OnChanged(@event, -1, -1);
|
---|
267 | }
|
---|
268 |
|
---|
269 | #endregion
|
---|
270 | }
|
---|
271 | }
|
---|