Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.DataPreprocessing/3.4/Data/FilteredPreprocessingData.cs @ 14186

Last change on this file since 14186 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 9.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Problems.DataAnalysis;
28
29namespace HeuristicLab.DataPreprocessing {
30  public class FilteredPreprocessingData : NamedItem, IFilteredPreprocessingData {
31    private readonly ITransactionalPreprocessingData originalData;
32    private ITransactionalPreprocessingData filteredData;
33
34    public IntRange TrainingPartition {
35      get { return originalData.TrainingPartition; }
36    }
37
38    public IntRange TestPartition {
39      get { return originalData.TestPartition; }
40    }
41
42    public IList<ITransformation> Transformations {
43      get { return originalData.Transformations; }
44    }
45
46    public IEnumerable<string> VariableNames {
47      get { return ActiveData.VariableNames; }
48    }
49
50    public IDictionary<int, IList<int>> Selection {
51      get { return originalData.Selection; }
52      set { originalData.Selection = value; }
53    }
54
55    public int Columns {
56      get { return ActiveData.Columns; }
57    }
58
59    public int Rows {
60      get { return ActiveData.Rows; }
61    }
62
63    public ITransactionalPreprocessingData ActiveData {
64      get { return IsFiltered ? filteredData : originalData; }
65    }
66
67    public bool IsUndoAvailable {
68      get { return IsFiltered ? false : originalData.IsUndoAvailable; }
69    }
70
71    public bool IsFiltered {
72      get { return filteredData != null; }
73    }
74
75
76    public FilteredPreprocessingData(ITransactionalPreprocessingData preporcessingData)
77      : base() {
78      originalData = preporcessingData;
79      filteredData = null;
80    }
81
82    protected FilteredPreprocessingData(FilteredPreprocessingData original, Cloner cloner)
83      : base(original, cloner) {
84      originalData = original.originalData;
85      filteredData = original.filteredData;
86    }
87    public override IDeepCloneable Clone(Cloner cloner) {
88      return new FilteredPreprocessingData(this, cloner);
89    }
90
91    public T GetCell<T>(int columnIndex, int rowIndex) {
92      return ActiveData.GetCell<T>(columnIndex, rowIndex);
93    }
94
95    public void SetCell<T>(int columnIndex, int rowIndex, T value) {
96      if (IsFiltered)
97        throw new InvalidOperationException("SetValues not possible while data is filtered");
98      originalData.SetCell<T>(columnIndex, rowIndex, value);
99    }
100
101    public string GetCellAsString(int columnIndex, int rowIndex) {
102      return ActiveData.GetCellAsString(columnIndex, rowIndex);
103    }
104
105    public IList<T> GetValues<T>(int columnIndex, bool considerSelection) {
106      return ActiveData.GetValues<T>(columnIndex, considerSelection);
107    }
108
109    public void SetValues<T>(int columnIndex, IList<T> values) {
110      if (IsFiltered)
111        throw new InvalidOperationException("SetValues not possible while data is filtered");
112
113      originalData.SetValues<T>(columnIndex, values);
114    }
115
116    public void InsertRow(int rowIndex) {
117      if (IsFiltered)
118        throw new InvalidOperationException("InsertRow not possible while data is filtered");
119
120      originalData.InsertRow(rowIndex);
121    }
122
123    public void DeleteRow(int rowIndex) {
124      if (IsFiltered)
125        throw new InvalidOperationException("DeleteRow not possible while data is filtered");
126
127      originalData.DeleteRow(rowIndex);
128    }
129
130    public void InsertColumn<T>(string variableName, int columnIndex) {
131      if (IsFiltered)
132        throw new InvalidOperationException("InsertColumn not possible while data is filtered");
133
134      originalData.InsertColumn<T>(variableName, columnIndex);
135    }
136
137    public void DeleteColumn(int columnIndex) {
138      if (IsFiltered)
139        throw new InvalidOperationException("DeleteColumn not possible while data is filtered");
140      originalData.DeleteColumn(columnIndex);
141    }
142
143    public void RenameColumn(int columnIndex, string name) {
144      if (IsFiltered)
145        throw new InvalidOperationException("RenameColumn not possible while data is filtered");
146      originalData.RenameColumn(columnIndex, name);
147    }
148
149    public void RenameColumns(IList<string> names) {
150      if (IsFiltered)
151        throw new InvalidOperationException("RenameColumns not possible while data is filtered");
152      originalData.RenameColumns(names);
153    }
154
155    public string GetVariableName(int columnIndex) {
156      return ActiveData.GetVariableName(columnIndex);
157    }
158
159    public int GetColumnIndex(string variableName) {
160      return ActiveData.GetColumnIndex(variableName);
161    }
162
163    public bool VariableHasType<T>(int columnIndex) {
164      return originalData.VariableHasType<T>(columnIndex);
165    }
166
167    public Dataset ExportToDataset() {
168      return originalData.ExportToDataset();
169    }
170
171    public void SetFilter(bool[] rowFilters) {
172      filteredData = (ITransactionalPreprocessingData)originalData.Clone();
173      filteredData.InTransaction(() => {
174        for (int row = (rowFilters.Length - 1); row >= 0; --row) {
175          if (rowFilters[row]) {
176            filteredData.DeleteRow(row);
177          }
178        }
179      });
180      OnFilterChanged();
181    }
182
183    public void PersistFilter() {
184      originalData.InTransaction(() => {
185        for (int i = 0; i < filteredData.Columns; ++i) {
186          if (filteredData.VariableHasType<double>(i)) {
187            originalData.SetValues<double>(i, filteredData.GetValues<double>(i));
188          } else if (filteredData.VariableHasType<string>(i)) {
189            originalData.SetValues<string>(i, filteredData.GetValues<string>(i));
190          } else if (filteredData.VariableHasType<DateTime>(i)) {
191            originalData.SetValues<DateTime>(i, filteredData.GetValues<DateTime>(i));
192          } else {
193            throw new ArgumentException("Data types of columns do not match");
194          }
195        }
196      });
197      ResetFilter();
198    }
199
200    public void ResetFilter() {
201      filteredData = null;
202      OnFilterChanged();
203    }
204
205    private void OnFilterChanged() {
206      if (FilterChanged != null) {
207        FilterChanged(this, new EventArgs());
208      }
209    }
210
211    public event DataPreprocessingChangedEventHandler Changed {
212      add { originalData.Changed += value; }
213      remove { originalData.Changed -= value; }
214    }
215
216    public bool SetValue(string value, int columnIndex, int rowIndex) {
217      if (IsFiltered)
218        throw new InvalidOperationException("SetValue not possible while data is filtered");
219      return originalData.SetValue(value, columnIndex, rowIndex);
220    }
221
222    public bool AreAllStringColumns(IEnumerable<int> columnIndices) {
223      return originalData.AreAllStringColumns(columnIndices);
224    }
225
226    public void DeleteRowsWithIndices(IEnumerable<int> rows) {
227      if (IsFiltered)
228        throw new InvalidOperationException("DeleteRowsWithIndices not possible while data is filtered");
229
230      originalData.DeleteRowsWithIndices(rows);
231    }
232
233    public void Undo() {
234      if (IsFiltered)
235        throw new InvalidOperationException("Undo not possible while data is filtered");
236
237      originalData.Undo();
238    }
239
240    public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) {
241      if (IsFiltered)
242        throw new InvalidOperationException("Transaction not possible while data is filtered");
243      originalData.InTransaction(action, type);
244    }
245
246    public void BeginTransaction(DataPreprocessingChangedEventType type) {
247      if (IsFiltered)
248        throw new InvalidOperationException("Transaction not possible while data is filtered");
249      originalData.BeginTransaction(type);
250    }
251
252    public void EndTransaction() {
253      originalData.EndTransaction();
254    }
255
256    public IEnumerable<string> GetDoubleVariableNames() {
257      return originalData.GetDoubleVariableNames();
258    }
259
260    public void ClearSelection() {
261      originalData.ClearSelection();
262    }
263
264    public event EventHandler SelectionChanged {
265      add { originalData.SelectionChanged += value; }
266      remove { originalData.SelectionChanged -= value; }
267    }
268
269    #region IPreprocessingData Members
270
271    public bool Validate(string value, out string errorMessage, int columnIndex) {
272      return originalData.Validate(value, out errorMessage, columnIndex);
273    }
274
275    public event EventHandler FilterChanged;
276    #endregion
277  }
278}
Note: See TracBrowser for help on using the repository browser.