source: trunk/sources/HeuristicLab.DataPreprocessing/3.4/Implementations/FilteredPreprocessingData.cs @ 11156

Last change on this file since 11156 was 11156, checked in by gkronber, 8 years ago

#2206: made several changes / improvements to the data-preprocessing code while reviewing the code

File size: 8.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.DataPreprocessing.Interfaces;
7using HeuristicLab.Problems.DataAnalysis;
8
9namespace 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>(string variableName, bool considerSelection) {
86      return ActiveData.GetValues<T>(variableName, considerSelection);
87    }
88
89    public IList<T> GetValues<T>(int columnIndex, bool considerSelection) {
90      return ActiveData.GetValues<T>(columnIndex, considerSelection);
91    }
92
93    public void SetValues<T>(int columnIndex, IList<T> values) {
94      if (IsFiltered)
95        throw new InvalidOperationException("SetValues not possible while data is filtered");
96
97      originalData.SetValues<T>(columnIndex, values);
98    }
99
100    public void InsertRow(int rowIndex) {
101      if (IsFiltered)
102        throw new InvalidOperationException("InsertRow not possible while data is filtered");
103
104      originalData.InsertRow(rowIndex);
105    }
106
107    public void DeleteRow(int rowIndex) {
108      if (IsFiltered)
109        throw new InvalidOperationException("DeleteRow not possible while data is filtered");
110
111      originalData.DeleteRow(rowIndex);
112    }
113
114    public void InsertColumn<T>(string variableName, int columnIndex) {
115      if (IsFiltered)
116        throw new InvalidOperationException("InsertColumn not possible while data is filtered");
117
118      originalData.InsertColumn<T>(variableName, columnIndex);
119    }
120
121    public void DeleteColumn(int columnIndex) {
122      if (IsFiltered)
123        throw new InvalidOperationException("DeleteColumn not possible while data is filtered");
124      originalData.DeleteColumn(columnIndex);
125    }
126
127    public string GetVariableName(int columnIndex) {
128      return ActiveData.GetVariableName(columnIndex);
129    }
130
131    public int GetColumnIndex(string variableName) {
132      return ActiveData.GetColumnIndex(variableName);
133    }
134
135    public bool VariableHasType<T>(int columnIndex) {
136      return originalData.VariableHasType<T>(columnIndex);
137    }
138
139    public Dataset ExportToDataset() {
140      return originalData.ExportToDataset();
141    }
142
143    public void SetFilter(bool[] rowFilters) {
144      filteredData = (ITransactionalPreprocessingData)originalData.Clone();
145      filteredData.InTransaction(() => {
146        for (int row = (rowFilters.Length - 1); row >= 0; --row) {
147          if (rowFilters[row]) {
148            filteredData.DeleteRow(row);
149          }
150        }
151      });
152      OnFilterChanged();
153    }
154
155    public void PersistFilter() {
156      originalData.InTransaction(() => {
157        for (int i = 0; i < filteredData.Columns; ++i) {
158          if (filteredData.VariableHasType<double>(i)) {
159            originalData.SetValues<double>(i, filteredData.GetValues<double>(i));
160          } else if (filteredData.VariableHasType<string>(i)) {
161            originalData.SetValues<string>(i, filteredData.GetValues<string>(i));
162          } else if (filteredData.VariableHasType<DateTime>(i)) {
163            originalData.SetValues<DateTime>(i, filteredData.GetValues<DateTime>(i));
164          } else {
165            throw new ArgumentException("Data types of columns do not match");
166          }
167        }
168      });
169      ResetFilter();
170    }
171
172    public void ResetFilter() {
173      filteredData = null;
174      OnFilterChanged();
175    }
176
177    private void OnFilterChanged() {
178      if (FilterChanged != null) {
179        FilterChanged(this, new EventArgs());
180      }
181    }
182
183    public event DataPreprocessingChangedEventHandler Changed {
184      add { originalData.Changed += value; }
185      remove { originalData.Changed -= value; }
186    }
187
188    public bool SetValue(string value, int columnIndex, int rowIndex) {
189      if (IsFiltered)
190        throw new InvalidOperationException("SetValue not possible while data is filtered");
191      return originalData.SetValue(value, columnIndex, rowIndex);
192    }
193
194    public bool AreAllStringColumns(IEnumerable<int> columnIndices) {
195      return originalData.AreAllStringColumns(columnIndices);
196    }
197
198    public void DeleteRowsWithIndices(IEnumerable<int> rows) {
199      if (IsFiltered)
200        throw new InvalidOperationException("DeleteRowsWithIndices not possible while data is filtered");
201
202      originalData.DeleteRowsWithIndices(rows);
203    }
204
205    public void Undo() {
206      if (IsFiltered)
207        throw new InvalidOperationException("Undo not possible while data is filtered");
208
209      originalData.Undo();
210    }
211
212    public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) {
213      if (IsFiltered)
214        throw new InvalidOperationException("Transaction not possible while data is filtered");
215      originalData.InTransaction(action, type);
216    }
217
218    public void BeginTransaction(DataPreprocessingChangedEventType type) {
219      if (IsFiltered)
220        throw new InvalidOperationException("Transaction not possible while data is filtered");
221      originalData.BeginTransaction(type);
222    }
223
224    public void EndTransaction() {
225      originalData.EndTransaction();
226    }
227
228    public IEnumerable<string> GetDoubleVariableNames() {
229      return originalData.GetDoubleVariableNames();
230    }
231
232    public void ClearSelection() {
233      originalData.ClearSelection();
234    }
235
236    public event EventHandler SelectionChanged {
237      add { originalData.SelectionChanged += value; }
238      remove { originalData.SelectionChanged -= value; }
239    }
240
241    #region IPreprocessingData Members
242
243    public bool Validate(string value, out string errorMessage, int columnIndex) {
244      return originalData.Validate(value, out errorMessage, columnIndex);
245    }
246
247    public event EventHandler FilterChanged;
248    #endregion
249  }
250}
Note: See TracBrowser for help on using the repository browser.