Free cookie consent management tool by TermsFeed Policy Generator

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

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