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