Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Data/FilteredPreprocessingData.cs @ 15309

Last change on this file since 15309 was 15309, checked in by pfleck, 7 years ago

#2809 Worked on type-save PreprocessingDataColumns.

File size: 10.5 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.DataPreprocessing {
31  [Item("FilteredPreprocessingData", "Represents filtered data used for preprocessing.")]
32  [StorableClass]
33  public sealed class FilteredPreprocessingData : NamedItem, IFilteredPreprocessingData {
34
35    [Storable]
36    private readonly IPreprocessingData originalData;
37    [Storable]
38    private IPreprocessingData filteredData;
39
40    public IList<PreprocessingDataColumn> DataColumns {
41      get { return ActiveData.DataColumns; }
42    }
43
44    public IPreprocessingData ActiveData {
45      get { return IsFiltered ? filteredData : originalData; }
46    }
47
48    #region Constructor, Cloning & Persistence
49    public FilteredPreprocessingData(IPreprocessingData preporcessingData)
50      : base() {
51      originalData = preporcessingData;
52      filteredData = null;
53    }
54
55    private FilteredPreprocessingData(FilteredPreprocessingData original, Cloner cloner)
56      : base(original, cloner) {
57      originalData = original.originalData;
58      filteredData = original.filteredData;
59    }
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new FilteredPreprocessingData(this, cloner);
62    }
63
64    [StorableConstructor]
65    private FilteredPreprocessingData(bool deserializing)
66      : base(deserializing) { }
67    #endregion
68
69    #region Cells
70    public bool IsCellEmpty(int columnIndex, int rowIndex) {
71      return ActiveData.IsCellEmpty(columnIndex, rowIndex);
72    }
73
74    public T GetCell<T>(int columnIndex, int rowIndex) {
75      return ActiveData.GetCell<T>(columnIndex, rowIndex);
76    }
77
78    public void SetCell<T>(int columnIndex, int rowIndex, T value) {
79      if (IsFiltered)
80        throw new InvalidOperationException("SetValues not possible while data is filtered");
81      originalData.SetCell<T>(columnIndex, rowIndex, value);
82    }
83
84    public string GetCellAsString(int columnIndex, int rowIndex) {
85      return ActiveData.GetCellAsString(columnIndex, rowIndex);
86    }
87
88    public IEnumerable<T> GetValues<T>(int columnIndex, bool considerSelection) {
89      return ActiveData.GetValues<T>(columnIndex, considerSelection);
90    }
91
92    public void SetValues<T>(int columnIndex, IEnumerable<T> values) {
93      if (IsFiltered)
94        throw new InvalidOperationException("SetValues not possible while data is filtered");
95
96      originalData.SetValues<T>(columnIndex, values);
97    }
98
99    public bool SetValue(string value, int columnIndex, int rowIndex) {
100      if (IsFiltered)
101        throw new InvalidOperationException("SetValue not possible while data is filtered");
102      return originalData.SetValue(value, columnIndex, rowIndex);
103    }
104
105    public int Columns {
106      get { return ActiveData.Columns; }
107    }
108
109    public int Rows {
110      get { return ActiveData.Rows; }
111    }
112    #endregion
113
114    #region Rows
115    public void InsertRow(int rowIndex) {
116      if (IsFiltered)
117        throw new InvalidOperationException("InsertRow not possible while data is filtered");
118
119      originalData.InsertRow(rowIndex);
120    }
121
122    public void DeleteRow(int rowIndex) {
123      if (IsFiltered)
124        throw new InvalidOperationException("DeleteRow not possible while data is filtered");
125
126      originalData.DeleteRow(rowIndex);
127    }
128
129    public void DeleteRows(IEnumerable<int> rows) {
130      if (IsFiltered)
131        throw new InvalidOperationException("DeleteRowsWithIndices not possible while data is filtered");
132
133      originalData.DeleteRows(rows);
134    }
135
136    public void InsertColumn<T>(string variableName, int columnIndex) {
137      if (IsFiltered)
138        throw new InvalidOperationException("InsertColumn not possible while data is filtered");
139
140      originalData.InsertColumn<T>(variableName, columnIndex);
141    }
142
143    public void DeleteColumn(int columnIndex) {
144      if (IsFiltered)
145        throw new InvalidOperationException("DeleteColumn not possible while data is filtered");
146      originalData.DeleteColumn(columnIndex);
147    }
148
149    public void RenameColumn(int columnIndex, string name) {
150      if (IsFiltered)
151        throw new InvalidOperationException("RenameColumn not possible while data is filtered");
152      originalData.RenameColumn(columnIndex, name);
153    }
154
155    public void RenameColumns(IList<string> names) {
156      if (IsFiltered)
157        throw new InvalidOperationException("RenameColumns not possible while data is filtered");
158      originalData.RenameColumns(names);
159    }
160
161    public bool AreAllStringColumns(IEnumerable<int> columnIndices) {
162      return originalData.AreAllStringColumns(columnIndices);
163    }
164    #endregion
165
166    #region Variables
167    public IEnumerable<string> VariableNames {
168      get { return ActiveData.VariableNames; }
169    }
170    public IEnumerable<string> GetDoubleVariableNames() {
171      return originalData.GetDoubleVariableNames();
172    }
173    public string GetVariableName(int columnIndex) {
174      return ActiveData.GetVariableName(columnIndex);
175    }
176
177    public int GetColumnIndex(string variableName) {
178      return ActiveData.GetColumnIndex(variableName);
179    }
180
181    public bool VariableHasType<T>(int columnIndex) {
182      return originalData.VariableHasType<T>(columnIndex);
183    }
184
185    public Type GetVariableType(int columnIndex) {
186      return ActiveData.GetVariableType(columnIndex);
187    }
188
189    public IList<string> InputVariables {
190      get { return ActiveData.InputVariables; }
191    }
192
193    public string TargetVariable {
194      get { return ActiveData.TargetVariable; }
195    } // optional
196    #endregion
197
198    #region Partitions
199    public IntRange TrainingPartition {
200      get { return originalData.TrainingPartition; }
201    }
202
203    public IntRange TestPartition {
204      get { return originalData.TestPartition; }
205    }
206    #endregion
207
208    #region Transformations
209    public IList<ITransformation> Transformations {
210      get { return originalData.Transformations; }
211    }
212    #endregion
213
214    #region Validation
215    public bool Validate(string value, out string errorMessage, int columnIndex) {
216      return originalData.Validate(value, out errorMessage, columnIndex);
217    }
218    #endregion
219
220    #region Import & Export
221    public void Import(IDataAnalysisProblemData problemData) {
222      if (IsFiltered)
223        throw new InvalidOperationException("Import not possible while data is filtered");
224      originalData.Import(problemData);
225    }
226
227    public Dataset ExportToDataset() {
228      return originalData.ExportToDataset();
229    }
230    #endregion
231
232    #region Selection
233    public IDictionary<int, IList<int>> Selection {
234      get { return originalData.Selection; }
235      set { originalData.Selection = value; }
236    }
237
238    public void ClearSelection() {
239      originalData.ClearSelection();
240    }
241
242    public event EventHandler SelectionChanged {
243      add { originalData.SelectionChanged += value; }
244      remove { originalData.SelectionChanged -= value; }
245    }
246    #endregion
247
248    #region Transactions
249    public event DataPreprocessingChangedEventHandler Changed {
250      add { originalData.Changed += value; }
251      remove { originalData.Changed -= value; }
252    }
253
254    public bool IsUndoAvailable {
255      get { return IsFiltered ? false : originalData.IsUndoAvailable; }
256    }
257
258    public void Undo() {
259      if (IsFiltered)
260        throw new InvalidOperationException("Undo not possible while data is filtered");
261
262      originalData.Undo();
263    }
264
265    public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) {
266      if (IsFiltered)
267        throw new InvalidOperationException("Transaction not possible while data is filtered");
268      originalData.InTransaction(action, type);
269    }
270
271    public void BeginTransaction(DataPreprocessingChangedEventType type) {
272      if (IsFiltered)
273        throw new InvalidOperationException("Transaction not possible while data is filtered");
274      originalData.BeginTransaction(type);
275    }
276
277    public void EndTransaction() {
278      originalData.EndTransaction();
279    }
280    #endregion
281
282    #region Filters
283    public void SetFilter(bool[] rowFilters) {
284      filteredData = (IPreprocessingData)originalData.Clone();
285      filteredData.InTransaction(() => {
286        for (int row = (rowFilters.Length - 1); row >= 0; --row) {
287          if (rowFilters[row]) {
288            filteredData.DeleteRow(row);
289          }
290        }
291      });
292      OnFilterChanged();
293    }
294
295    public void PersistFilter() {
296      originalData.InTransaction(() => {
297        for (int i = 0; i < filteredData.Columns; ++i) {
298          if (filteredData.VariableHasType<double>(i)) {
299            originalData.SetValues<double>(i, filteredData.GetValues<double>(i));
300          } else if (filteredData.VariableHasType<string>(i)) {
301            originalData.SetValues<string>(i, filteredData.GetValues<string>(i));
302          } else if (filteredData.VariableHasType<DateTime>(i)) {
303            originalData.SetValues<DateTime>(i, filteredData.GetValues<DateTime>(i));
304          } else {
305            throw new ArgumentException("Data types of columns do not match");
306          }
307        }
308      });
309      ResetFilter();
310    }
311
312    public void ResetFilter() {
313      filteredData = null;
314      OnFilterChanged();
315    }
316
317    public bool IsFiltered {
318      get { return filteredData != null; }
319    }
320
321    public event EventHandler FilterChanged;
322
323    private void OnFilterChanged() {
324      if (FilterChanged != null) {
325        FilterChanged(this, new EventArgs());
326      }
327    }
328    #endregion
329  }
330}
Note: See TracBrowser for help on using the repository browser.