Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10992 was 10992, checked in by rstoll, 10 years ago
  • removed ChartLogic and

moved logic accordingly to PreprocessingChartContent, ScatterPlotContent
modified views etc. to use IFilteredPreprocessingData instead of ChartLogic

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