Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.DataPreprocessing/3.4/Implementations/FilteredPreprocessingData.cs @ 13289

Last change on this file since 13289 was 13289, checked in by mkommend, 8 years ago

#2486: Merged r12983, r12986, r13252 and r13271 into stable.

File size: 8.3 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>(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 void RenameColumn(int columnIndex, string name) {
124      if (IsFiltered)
125        throw new InvalidOperationException("RenameColumn not possible while data is filtered");
126      originalData.RenameColumn(columnIndex, name);
127    }
128
129    public void RenameColumns(IList<string> names) {
130      if (IsFiltered)
131        throw new InvalidOperationException("RenameColumns not possible while data is filtered");
132      originalData.RenameColumns(names);
133    }
134
135    public string GetVariableName(int columnIndex) {
136      return ActiveData.GetVariableName(columnIndex);
137    }
138
139    public int GetColumnIndex(string variableName) {
140      return ActiveData.GetColumnIndex(variableName);
141    }
142
143    public bool VariableHasType<T>(int columnIndex) {
144      return originalData.VariableHasType<T>(columnIndex);
145    }
146
147    public Dataset ExportToDataset() {
148      return originalData.ExportToDataset();
149    }
150
151    public void SetFilter(bool[] rowFilters) {
152      filteredData = (ITransactionalPreprocessingData)originalData.Clone();
153      filteredData.InTransaction(() => {
154        for (int row = (rowFilters.Length - 1); row >= 0; --row) {
155          if (rowFilters[row]) {
156            filteredData.DeleteRow(row);
157          }
158        }
159      });
160      OnFilterChanged();
161    }
162
163    public void PersistFilter() {
164      originalData.InTransaction(() => {
165        for (int i = 0; i < filteredData.Columns; ++i) {
166          if (filteredData.VariableHasType<double>(i)) {
167            originalData.SetValues<double>(i, filteredData.GetValues<double>(i));
168          } else if (filteredData.VariableHasType<string>(i)) {
169            originalData.SetValues<string>(i, filteredData.GetValues<string>(i));
170          } else if (filteredData.VariableHasType<DateTime>(i)) {
171            originalData.SetValues<DateTime>(i, filteredData.GetValues<DateTime>(i));
172          } else {
173            throw new ArgumentException("Data types of columns do not match");
174          }
175        }
176      });
177      ResetFilter();
178    }
179
180    public void ResetFilter() {
181      filteredData = null;
182      OnFilterChanged();
183    }
184
185    private void OnFilterChanged() {
186      if (FilterChanged != null) {
187        FilterChanged(this, new EventArgs());
188      }
189    }
190
191    public event DataPreprocessingChangedEventHandler Changed {
192      add { originalData.Changed += value; }
193      remove { originalData.Changed -= value; }
194    }
195
196    public bool SetValue(string value, int columnIndex, int rowIndex) {
197      if (IsFiltered)
198        throw new InvalidOperationException("SetValue not possible while data is filtered");
199      return originalData.SetValue(value, columnIndex, rowIndex);
200    }
201
202    public bool AreAllStringColumns(IEnumerable<int> columnIndices) {
203      return originalData.AreAllStringColumns(columnIndices);
204    }
205
206    public void DeleteRowsWithIndices(IEnumerable<int> rows) {
207      if (IsFiltered)
208        throw new InvalidOperationException("DeleteRowsWithIndices not possible while data is filtered");
209
210      originalData.DeleteRowsWithIndices(rows);
211    }
212
213    public void Undo() {
214      if (IsFiltered)
215        throw new InvalidOperationException("Undo not possible while data is filtered");
216
217      originalData.Undo();
218    }
219
220    public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) {
221      if (IsFiltered)
222        throw new InvalidOperationException("Transaction not possible while data is filtered");
223      originalData.InTransaction(action, type);
224    }
225
226    public void BeginTransaction(DataPreprocessingChangedEventType type) {
227      if (IsFiltered)
228        throw new InvalidOperationException("Transaction not possible while data is filtered");
229      originalData.BeginTransaction(type);
230    }
231
232    public void EndTransaction() {
233      originalData.EndTransaction();
234    }
235
236    public IEnumerable<string> GetDoubleVariableNames() {
237      return originalData.GetDoubleVariableNames();
238    }
239
240    public void ClearSelection() {
241      originalData.ClearSelection();
242    }
243
244    public event EventHandler SelectionChanged {
245      add { originalData.SelectionChanged += value; }
246      remove { originalData.SelectionChanged -= value; }
247    }
248
249    #region IPreprocessingData Members
250
251    public bool Validate(string value, out string errorMessage, int columnIndex) {
252      return originalData.Validate(value, out errorMessage, columnIndex);
253    }
254
255    public event EventHandler FilterChanged;
256    #endregion
257  }
258}
Note: See TracBrowser for help on using the repository browser.