Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/Implementations/FilteredPreprocessingData.cs @ 10939

Last change on this file since 10939 was 10939, checked in by sbreuer, 10 years ago
  • do filtering in transaction, so the content changed event is only fired once
  • fix bug with apply filter
File size: 6.5 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;
8using HeuristicLab.Problems.DataAnalysis.Transformations;
9
10namespace HeuristicLab.DataPreprocessing.Implementations {
11  public class FilteredPreprocessingData : NamedItem, IFilteredPreprocessingData {
12    protected ITransactionalPreprocessingData originalData;
13    protected ITransactionalPreprocessingData filteredData;
14
15    protected FilteredPreprocessingData(FilteredPreprocessingData original, Cloner cloner)
16      : base(original, cloner) {
17      originalData = (ITransactionalPreprocessingData)original.originalData;
18      filteredData = (ITransactionalPreprocessingData)original.filteredData;
19    }
20
21    public FilteredPreprocessingData(ITransactionalPreprocessingData preporcessingData)
22      : base() {
23      originalData = preporcessingData;
24      filteredData = null;
25    }
26
27    public override IDeepCloneable Clone(Cloner cloner) {
28      return new FilteredPreprocessingData(this, cloner);
29    }
30
31    public T GetCell<T>(int columnIndex, int rowIndex) {
32      return ActiveData.GetCell<T>(columnIndex, rowIndex);
33    }
34
35    public void SetCell<T>(int columnIndex, int rowIndex, T value) {
36      ReadOnlyOnFilterData.SetCell<T>(columnIndex, rowIndex, value);
37    }
38
39    public string GetCellAsString(int columnIndex, int rowIndex) {
40      return ActiveData.GetCellAsString(columnIndex, rowIndex);
41    }
42
43    public IList<T> GetValues<T>(string variableName, bool considerSelection) {
44      return ActiveData.GetValues<T>(variableName, considerSelection);
45    }
46
47    public IList<T> GetValues<T>(int columnIndex, bool considerSelection) {
48      return ActiveData.GetValues<T>(columnIndex, considerSelection);
49    }
50
51    public void SetValues<T>(int columnIndex, IList<T> values) {
52      ReadOnlyOnFilterData.SetValues<T>(columnIndex, values);
53    }
54
55    public void InsertRow(int rowIndex) {
56      ReadOnlyOnFilterData.InsertRow(rowIndex);
57    }
58
59    public void DeleteRow(int rowIndex) {
60      ReadOnlyOnFilterData.DeleteRow(rowIndex);
61    }
62
63    public void InsertColumn<T>(string variableName, int columnIndex) {
64      ReadOnlyOnFilterData.InsertColumn<T>(variableName, columnIndex);
65    }
66
67    public void DeleteColumn(int columnIndex) {
68      ReadOnlyOnFilterData.DeleteColumn(columnIndex);
69    }
70
71    public IntRange TrainingPartition {
72      get { return originalData.TrainingPartition; }
73    }
74
75    public IntRange TestPartition {
76      get { return originalData.TestPartition; }
77    }
78
79    public IList<ITransformation> Transformations {
80      get { return originalData.Transformations; }
81    }
82
83    public IEnumerable<string> VariableNames {
84      get { return ActiveData.VariableNames; }
85    }
86
87    public string GetVariableName(int columnIndex) {
88      return ActiveData.GetVariableName(columnIndex);
89    }
90
91    public int GetColumnIndex(string variableName) {
92      return ActiveData.GetColumnIndex(variableName);
93    }
94
95    public bool IsType<T>(int columnIndex) {
96      return originalData.IsType<T>(columnIndex);
97    }
98
99    public int Columns {
100      get { return ActiveData.Columns; }
101    }
102
103    public int Rows {
104      get { return ActiveData.Rows; }
105    }
106
107    public Dataset ExportToDataset() {
108      return originalData.ExportToDataset();
109    }
110
111    public void SetFilter(bool[] rowFilters) {
112      filteredData = (ITransactionalPreprocessingData)originalData.Clone();
113      filteredData.InTransaction(() => {
114        for (int row = (rowFilters.Length - 1); row >= 0; --row) {
115          if (rowFilters[row]) {
116            filteredData.DeleteRow(row);
117          }
118        }
119      });
120      OnFilterChanged();
121    }
122
123    public void PersistFilter() {
124      originalData.InTransaction(() => {
125        for (int i = 0; i < filteredData.Columns; ++i) {
126          if (filteredData.IsType<double>(i)) {
127            originalData.SetValues<double>(i, filteredData.GetValues<double>(i));
128          } else if (filteredData.IsType<string>(i)) {
129            originalData.SetValues<string>(i, filteredData.GetValues<string>(i));
130          } else if (filteredData.IsType<DateTime>(i)) {
131            originalData.SetValues<DateTime>(i, filteredData.GetValues<DateTime>(i));
132          } else {
133            throw new ArgumentException("Data types of columns do not match");
134          }
135        }
136      });
137      ResetFilter();
138    }
139
140    public void ResetFilter() {
141      filteredData = null;
142      OnFilterChanged();
143    }
144
145    private void OnFilterChanged() {
146      if (FilterChanged != null) {
147        FilterChanged(this, new EventArgs());
148      }
149    }
150
151    public ITransactionalPreprocessingData ActiveData {
152      get { return IsFiltered ? filteredData : originalData; }
153    }
154
155    public ITransactionalPreprocessingData ReadOnlyOnFilterData {
156      get {
157        if (IsFiltered)
158          throw new InvalidOperationException();
159
160        return originalData;
161      }
162    }
163
164    public bool IsFiltered {
165      get { return filteredData != null; }
166    }
167
168    public event DataPreprocessingChangedEventHandler Changed {
169      add { originalData.Changed += value; }
170      remove { originalData.Changed -= value; }
171    }
172
173    public bool IsUndoAvailable {
174      get { return IsFiltered ? false : originalData.IsUndoAvailable; }
175    }
176
177    public void Undo() {
178      ReadOnlyOnFilterData.Undo();
179    }
180
181    public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) {
182      ReadOnlyOnFilterData.InTransaction(action, type);
183    }
184
185    public void BeginTransaction(DataPreprocessingChangedEventType type) {
186      ReadOnlyOnFilterData.BeginTransaction(type);
187    }
188
189    public void EndTransaction() {
190      originalData.EndTransaction();
191    }
192
193    public event EventHandler FilterChanged;
194
195    #region IPreprocessingData Members
196
197
198    public void SetSelection(IDictionary<int, IList<int>> selection) {
199      originalData.SetSelection(selection);
200    }
201
202    public IDictionary<int, IList<int>> GetSelection() {
203      return originalData.GetSelection();
204    }
205
206    public void ClearSelection() {
207      originalData.ClearSelection();
208    }
209
210    public event EventHandler SelectionChanged {
211      add { originalData.SelectionChanged += value; }
212      remove { originalData.SelectionChanged -= value; }
213    }
214
215    #endregion
216  }
217}
Note: See TracBrowser for help on using the repository browser.