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