Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Data/FilteredPreprocessingData.cs @ 15269

Last change on this file since 15269 was 15269, checked in by pfleck, 7 years ago

#2809: Removed SearchLogic

File size: 9.4 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 sealed 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 IList<string> InputVariables { get { return ActiveData.InputVariables; } }
51    public string TargetVariable { get { return ActiveData.TargetVariable; } } // optional
52
53    public IDictionary<int, IList<int>> Selection {
54      get { return originalData.Selection; }
55      set { originalData.Selection = value; }
56    }
57
58    public int Columns {
59      get { return ActiveData.Columns; }
60    }
61
62    public int Rows {
63      get { return ActiveData.Rows; }
64    }
65
66    public ITransactionalPreprocessingData ActiveData {
67      get { return IsFiltered ? filteredData : originalData; }
68    }
69
70    public bool IsUndoAvailable {
71      get { return IsFiltered ? false : originalData.IsUndoAvailable; }
72    }
73
74    public bool IsFiltered {
75      get { return filteredData != null; }
76    }
77
78
79    public FilteredPreprocessingData(ITransactionalPreprocessingData preporcessingData)
80      : base() {
81      originalData = preporcessingData;
82      filteredData = null;
83    }
84
85    private FilteredPreprocessingData(FilteredPreprocessingData original, Cloner cloner)
86      : base(original, cloner) {
87      originalData = original.originalData;
88      filteredData = original.filteredData;
89    }
90    public override IDeepCloneable Clone(Cloner cloner) {
91      return new FilteredPreprocessingData(this, cloner);
92    }
93
94    public bool IsCellEmpty(int columnIndex, int rowIndex) {
95      return ActiveData.IsCellEmpty(columnIndex, rowIndex);
96    }
97
98    public T GetCell<T>(int columnIndex, int rowIndex) {
99      return ActiveData.GetCell<T>(columnIndex, rowIndex);
100    }
101
102    public void SetCell<T>(int columnIndex, int rowIndex, T value) {
103      if (IsFiltered)
104        throw new InvalidOperationException("SetValues not possible while data is filtered");
105      originalData.SetCell<T>(columnIndex, rowIndex, value);
106    }
107
108    public string GetCellAsString(int columnIndex, int rowIndex) {
109      return ActiveData.GetCellAsString(columnIndex, rowIndex);
110    }
111
112    public IList<T> GetValues<T>(int columnIndex, bool considerSelection) {
113      return ActiveData.GetValues<T>(columnIndex, considerSelection);
114    }
115
116    public void SetValues<T>(int columnIndex, IList<T> values) {
117      if (IsFiltered)
118        throw new InvalidOperationException("SetValues not possible while data is filtered");
119
120      originalData.SetValues<T>(columnIndex, values);
121    }
122
123    public void InsertRow(int rowIndex) {
124      if (IsFiltered)
125        throw new InvalidOperationException("InsertRow not possible while data is filtered");
126
127      originalData.InsertRow(rowIndex);
128    }
129
130    public void DeleteRow(int rowIndex) {
131      if (IsFiltered)
132        throw new InvalidOperationException("DeleteRow not possible while data is filtered");
133
134      originalData.DeleteRow(rowIndex);
135    }
136
137    public void InsertColumn<T>(string variableName, int columnIndex) {
138      if (IsFiltered)
139        throw new InvalidOperationException("InsertColumn not possible while data is filtered");
140
141      originalData.InsertColumn<T>(variableName, columnIndex);
142    }
143
144    public void DeleteColumn(int columnIndex) {
145      if (IsFiltered)
146        throw new InvalidOperationException("DeleteColumn not possible while data is filtered");
147      originalData.DeleteColumn(columnIndex);
148    }
149
150    public void RenameColumn(int columnIndex, string name) {
151      if (IsFiltered)
152        throw new InvalidOperationException("RenameColumn not possible while data is filtered");
153      originalData.RenameColumn(columnIndex, name);
154    }
155
156    public void RenameColumns(IList<string> names) {
157      if (IsFiltered)
158        throw new InvalidOperationException("RenameColumns not possible while data is filtered");
159      originalData.RenameColumns(names);
160    }
161
162    public string GetVariableName(int columnIndex) {
163      return ActiveData.GetVariableName(columnIndex);
164    }
165
166    public int GetColumnIndex(string variableName) {
167      return ActiveData.GetColumnIndex(variableName);
168    }
169
170    public bool VariableHasType<T>(int columnIndex) {
171      return originalData.VariableHasType<T>(columnIndex);
172    }
173
174    public Dataset ExportToDataset() {
175      return originalData.ExportToDataset();
176    }
177
178    public void SetFilter(bool[] rowFilters) {
179      filteredData = (ITransactionalPreprocessingData)originalData.Clone();
180      filteredData.InTransaction(() => {
181        for (int row = (rowFilters.Length - 1); row >= 0; --row) {
182          if (rowFilters[row]) {
183            filteredData.DeleteRow(row);
184          }
185        }
186      });
187      OnFilterChanged();
188    }
189
190    public void PersistFilter() {
191      originalData.InTransaction(() => {
192        for (int i = 0; i < filteredData.Columns; ++i) {
193          if (filteredData.VariableHasType<double>(i)) {
194            originalData.SetValues<double>(i, filteredData.GetValues<double>(i));
195          } else if (filteredData.VariableHasType<string>(i)) {
196            originalData.SetValues<string>(i, filteredData.GetValues<string>(i));
197          } else if (filteredData.VariableHasType<DateTime>(i)) {
198            originalData.SetValues<DateTime>(i, filteredData.GetValues<DateTime>(i));
199          } else {
200            throw new ArgumentException("Data types of columns do not match");
201          }
202        }
203      });
204      ResetFilter();
205    }
206
207    public void ResetFilter() {
208      filteredData = null;
209      OnFilterChanged();
210    }
211
212    private void OnFilterChanged() {
213      if (FilterChanged != null) {
214        FilterChanged(this, new EventArgs());
215      }
216    }
217
218    public event DataPreprocessingChangedEventHandler Changed {
219      add { originalData.Changed += value; }
220      remove { originalData.Changed -= value; }
221    }
222
223    public bool SetValue(string value, int columnIndex, int rowIndex) {
224      if (IsFiltered)
225        throw new InvalidOperationException("SetValue not possible while data is filtered");
226      return originalData.SetValue(value, columnIndex, rowIndex);
227    }
228
229    public bool AreAllStringColumns(IEnumerable<int> columnIndices) {
230      return originalData.AreAllStringColumns(columnIndices);
231    }
232
233    public void DeleteRowsWithIndices(IEnumerable<int> rows) {
234      if (IsFiltered)
235        throw new InvalidOperationException("DeleteRowsWithIndices not possible while data is filtered");
236
237      originalData.DeleteRowsWithIndices(rows);
238    }
239
240    public void Undo() {
241      if (IsFiltered)
242        throw new InvalidOperationException("Undo not possible while data is filtered");
243
244      originalData.Undo();
245    }
246
247    public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) {
248      if (IsFiltered)
249        throw new InvalidOperationException("Transaction not possible while data is filtered");
250      originalData.InTransaction(action, type);
251    }
252
253    public void BeginTransaction(DataPreprocessingChangedEventType type) {
254      if (IsFiltered)
255        throw new InvalidOperationException("Transaction not possible while data is filtered");
256      originalData.BeginTransaction(type);
257    }
258
259    public void EndTransaction() {
260      originalData.EndTransaction();
261    }
262
263    public IEnumerable<string> GetDoubleVariableNames() {
264      return originalData.GetDoubleVariableNames();
265    }
266
267    public void ClearSelection() {
268      originalData.ClearSelection();
269    }
270
271    public event EventHandler SelectionChanged {
272      add { originalData.SelectionChanged += value; }
273      remove { originalData.SelectionChanged -= value; }
274    }
275
276    #region IPreprocessingData Members
277    public bool Validate(string value, out string errorMessage, int columnIndex) {
278      return originalData.Validate(value, out errorMessage, columnIndex);
279    }
280
281    public event EventHandler FilterChanged;
282    #endregion
283  }
284}
Note: See TracBrowser for help on using the repository browser.