1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.DataPreprocessing.Interfaces;
|
---|
7 | using HeuristicLab.Problems.DataAnalysis;
|
---|
8 | using HeuristicLab.Problems.DataAnalysis.Transformations;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.DataPreprocessing.Implementations {
|
---|
11 | public class FilteredPreprocessingData : NamedItem, IFilteredPreprocessingData {
|
---|
12 | private readonly ITransactionalPreprocessingData originalData;
|
---|
13 | private ITransactionalPreprocessingData filteredData;
|
---|
14 |
|
---|
15 | public IntRange TrainingPartition {
|
---|
16 | get { return originalData.TrainingPartition; }
|
---|
17 | }
|
---|
18 |
|
---|
19 | public IntRange TestPartition {
|
---|
20 | get { return originalData.TestPartition; }
|
---|
21 | }
|
---|
22 |
|
---|
23 | public IList<ITransformation> Transformations {
|
---|
24 | get { return originalData.Transformations; }
|
---|
25 | }
|
---|
26 |
|
---|
27 | public IEnumerable<string> VariableNames {
|
---|
28 | get { return ActiveData.VariableNames; }
|
---|
29 | }
|
---|
30 |
|
---|
31 | public IDictionary<int, IList<int>> Selection {
|
---|
32 | get { return originalData.Selection; }
|
---|
33 | set { originalData.Selection = value; }
|
---|
34 | }
|
---|
35 |
|
---|
36 | public int Columns {
|
---|
37 | get { return ActiveData.Columns; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public int Rows {
|
---|
41 | get { return ActiveData.Rows; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public ITransactionalPreprocessingData ActiveData {
|
---|
45 | get { return IsFiltered ? filteredData : originalData; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public bool IsUndoAvailable {
|
---|
49 | get { return IsFiltered ? false : originalData.IsUndoAvailable; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public bool IsFiltered {
|
---|
53 | get { return filteredData != null; }
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | public FilteredPreprocessingData(ITransactionalPreprocessingData preporcessingData)
|
---|
58 | : base() {
|
---|
59 | originalData = preporcessingData;
|
---|
60 | filteredData = null;
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected FilteredPreprocessingData(FilteredPreprocessingData original, Cloner cloner)
|
---|
64 | : base(original, cloner) {
|
---|
65 | originalData = original.originalData;
|
---|
66 | filteredData = original.filteredData;
|
---|
67 | }
|
---|
68 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
69 | return new FilteredPreprocessingData(this, cloner);
|
---|
70 | }
|
---|
71 |
|
---|
72 | public T GetCell<T>(int columnIndex, int rowIndex) {
|
---|
73 | return ActiveData.GetCell<T>(columnIndex, rowIndex);
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void SetCell<T>(int columnIndex, int rowIndex, T value) {
|
---|
77 | if (IsFiltered)
|
---|
78 | throw new InvalidOperationException("SetValues not possible while data is filtered");
|
---|
79 | originalData.SetCell<T>(columnIndex, rowIndex, value);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public string GetCellAsString(int columnIndex, int rowIndex) {
|
---|
83 | return ActiveData.GetCellAsString(columnIndex, rowIndex);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public IList<T> GetValues<T>(string variableName, bool considerSelection) {
|
---|
87 | return ActiveData.GetValues<T>(variableName, considerSelection);
|
---|
88 | }
|
---|
89 |
|
---|
90 | public IList<T> GetValues<T>(int columnIndex, bool considerSelection) {
|
---|
91 | return ActiveData.GetValues<T>(columnIndex, considerSelection);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public void SetValues<T>(int columnIndex, IList<T> values) {
|
---|
95 | if (IsFiltered)
|
---|
96 | throw new InvalidOperationException("SetValues not possible while data is filtered");
|
---|
97 |
|
---|
98 | originalData.SetValues<T>(columnIndex, values);
|
---|
99 | }
|
---|
100 |
|
---|
101 | public void InsertRow(int rowIndex) {
|
---|
102 | if (IsFiltered)
|
---|
103 | throw new InvalidOperationException("InsertRow not possible while data is filtered");
|
---|
104 |
|
---|
105 | originalData.InsertRow(rowIndex);
|
---|
106 | }
|
---|
107 |
|
---|
108 | public void DeleteRow(int rowIndex) {
|
---|
109 | if (IsFiltered)
|
---|
110 | throw new InvalidOperationException("DeleteRow not possible while data is filtered");
|
---|
111 |
|
---|
112 | originalData.DeleteRow(rowIndex);
|
---|
113 | }
|
---|
114 |
|
---|
115 | public void InsertColumn<T>(string variableName, int columnIndex) {
|
---|
116 | if (IsFiltered)
|
---|
117 | throw new InvalidOperationException("InsertColumn not possible while data is filtered");
|
---|
118 |
|
---|
119 | originalData.InsertColumn<T>(variableName, columnIndex);
|
---|
120 | }
|
---|
121 |
|
---|
122 | public void DeleteColumn(int columnIndex) {
|
---|
123 | if (IsFiltered)
|
---|
124 | throw new InvalidOperationException("DeleteColumn not possible while data is filtered");
|
---|
125 | originalData.DeleteColumn(columnIndex);
|
---|
126 | }
|
---|
127 |
|
---|
128 | public string GetVariableName(int columnIndex) {
|
---|
129 | return ActiveData.GetVariableName(columnIndex);
|
---|
130 | }
|
---|
131 |
|
---|
132 | public int GetColumnIndex(string variableName) {
|
---|
133 | return ActiveData.GetColumnIndex(variableName);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public bool IsType<T>(int columnIndex) {
|
---|
137 | return originalData.IsType<T>(columnIndex);
|
---|
138 | }
|
---|
139 |
|
---|
140 | public Dataset ExportToDataset() {
|
---|
141 | return originalData.ExportToDataset();
|
---|
142 | }
|
---|
143 |
|
---|
144 | public void SetFilter(bool[] rowFilters) {
|
---|
145 | filteredData = (ITransactionalPreprocessingData)originalData.Clone();
|
---|
146 | filteredData.InTransaction(() => {
|
---|
147 | for (int row = (rowFilters.Length - 1); row >= 0; --row) {
|
---|
148 | if (rowFilters[row]) {
|
---|
149 | filteredData.DeleteRow(row);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | });
|
---|
153 | OnFilterChanged();
|
---|
154 | }
|
---|
155 |
|
---|
156 | public void PersistFilter() {
|
---|
157 | originalData.InTransaction(() => {
|
---|
158 | for (int i = 0; i < filteredData.Columns; ++i) {
|
---|
159 | if (filteredData.IsType<double>(i)) {
|
---|
160 | originalData.SetValues<double>(i, filteredData.GetValues<double>(i));
|
---|
161 | } else if (filteredData.IsType<string>(i)) {
|
---|
162 | originalData.SetValues<string>(i, filteredData.GetValues<string>(i));
|
---|
163 | } else if (filteredData.IsType<DateTime>(i)) {
|
---|
164 | originalData.SetValues<DateTime>(i, filteredData.GetValues<DateTime>(i));
|
---|
165 | } else {
|
---|
166 | throw new ArgumentException("Data types of columns do not match");
|
---|
167 | }
|
---|
168 | }
|
---|
169 | });
|
---|
170 | ResetFilter();
|
---|
171 | }
|
---|
172 |
|
---|
173 | public void ResetFilter() {
|
---|
174 | filteredData = null;
|
---|
175 | OnFilterChanged();
|
---|
176 | }
|
---|
177 |
|
---|
178 | private void OnFilterChanged() {
|
---|
179 | if (FilterChanged != null) {
|
---|
180 | FilterChanged(this, new EventArgs());
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | public event DataPreprocessingChangedEventHandler Changed {
|
---|
185 | add { originalData.Changed += value; }
|
---|
186 | remove { originalData.Changed -= value; }
|
---|
187 | }
|
---|
188 |
|
---|
189 | public bool SetValue(string value, int columnIndex, int rowIndex) {
|
---|
190 | if (IsFiltered)
|
---|
191 | throw new InvalidOperationException("SetValue not possible while data is filtered");
|
---|
192 | return originalData.SetValue(value, columnIndex, rowIndex);
|
---|
193 | }
|
---|
194 |
|
---|
195 | public bool AreAllStringColumns(IEnumerable<int> columnIndices) {
|
---|
196 | return originalData.AreAllStringColumns(columnIndices);
|
---|
197 | }
|
---|
198 |
|
---|
199 | public void DeleteRowsWithIndices(IEnumerable<int> rows) {
|
---|
200 | if (IsFiltered)
|
---|
201 | throw new InvalidOperationException("DeleteRowsWithIndices not possible while data is filtered");
|
---|
202 |
|
---|
203 | originalData.DeleteRowsWithIndices(rows);
|
---|
204 | }
|
---|
205 |
|
---|
206 | public void Undo() {
|
---|
207 | if (IsFiltered)
|
---|
208 | throw new InvalidOperationException("Undo not possible while data is filtered");
|
---|
209 |
|
---|
210 | originalData.Undo();
|
---|
211 | }
|
---|
212 |
|
---|
213 | public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) {
|
---|
214 | if (IsFiltered)
|
---|
215 | throw new InvalidOperationException("Transaction not possible while data is filtered");
|
---|
216 | originalData.InTransaction(action, type);
|
---|
217 | }
|
---|
218 |
|
---|
219 | public void BeginTransaction(DataPreprocessingChangedEventType type) {
|
---|
220 | if (IsFiltered)
|
---|
221 | throw new InvalidOperationException("Transaction not possible while data is filtered");
|
---|
222 | originalData.BeginTransaction(type);
|
---|
223 | }
|
---|
224 |
|
---|
225 | public void EndTransaction() {
|
---|
226 | originalData.EndTransaction();
|
---|
227 | }
|
---|
228 |
|
---|
229 | public IEnumerable<string> GetDoubleVariableNames() {
|
---|
230 | return originalData.GetDoubleVariableNames();
|
---|
231 | }
|
---|
232 |
|
---|
233 | public void ClearSelection() {
|
---|
234 | originalData.ClearSelection();
|
---|
235 | }
|
---|
236 |
|
---|
237 | public event EventHandler SelectionChanged {
|
---|
238 | add { originalData.SelectionChanged += value; }
|
---|
239 | remove { originalData.SelectionChanged -= value; }
|
---|
240 | }
|
---|
241 |
|
---|
242 | #region IPreprocessingData Members
|
---|
243 |
|
---|
244 | public bool Validate(string value, out string errorMessage, int columnIndex) {
|
---|
245 | return originalData.Validate(value, out errorMessage, columnIndex);
|
---|
246 | }
|
---|
247 |
|
---|
248 | public event EventHandler FilterChanged;
|
---|
249 | #endregion
|
---|
250 | }
|
---|
251 | }
|
---|