Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataPreprocessing/3.4/Data/FilteredPreprocessingData.cs @ 14075

Last change on this file since 14075 was 13508, checked in by pfleck, 8 years ago

#2559

  • Enabled type selection for creating/importing/exporting/applying.
  • Deleted unnecessary interfaces.
  • Reorganized source files of DataPreprocessing.
File size: 9.1 KB
RevLine 
[13502]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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;
[10783]23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Problems.DataAnalysis;
28
[13502]29namespace HeuristicLab.DataPreprocessing {
[10804]30  public class FilteredPreprocessingData : NamedItem, IFilteredPreprocessingData {
[10978]31    private readonly ITransactionalPreprocessingData originalData;
32    private ITransactionalPreprocessingData filteredData;
[10783]33
[10999]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
[10992]50    public IDictionary<int, IList<int>> Selection {
[10978]51      get { return originalData.Selection; }
52      set { originalData.Selection = value; }
53    }
54
[10999]55    public int Columns {
56      get { return ActiveData.Columns; }
[10783]57    }
58
[10999]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
[10783]76    public FilteredPreprocessingData(ITransactionalPreprocessingData preporcessingData)
77      : base() {
[10804]78      originalData = preporcessingData;
79      filteredData = null;
[10783]80    }
81
[10999]82    protected FilteredPreprocessingData(FilteredPreprocessingData original, Cloner cloner)
83      : base(original, cloner) {
84      originalData = original.originalData;
85      filteredData = original.filteredData;
86    }
[10804]87    public override IDeepCloneable Clone(Cloner cloner) {
88      return new FilteredPreprocessingData(this, cloner);
89    }
[10783]90
[10804]91    public T GetCell<T>(int columnIndex, int rowIndex) {
92      return ActiveData.GetCell<T>(columnIndex, rowIndex);
93    }
[10783]94
[10804]95    public void SetCell<T>(int columnIndex, int rowIndex, T value) {
[10978]96      if (IsFiltered)
[11003]97        throw new InvalidOperationException("SetValues not possible while data is filtered");
[10978]98      originalData.SetCell<T>(columnIndex, rowIndex, value);
[10804]99    }
[10783]100
[10804]101    public string GetCellAsString(int columnIndex, int rowIndex) {
102      return ActiveData.GetCellAsString(columnIndex, rowIndex);
103    }
[10783]104
[10809]105    public IList<T> GetValues<T>(int columnIndex, bool considerSelection) {
106      return ActiveData.GetValues<T>(columnIndex, considerSelection);
[10804]107    }
[10783]108
[10804]109    public void SetValues<T>(int columnIndex, IList<T> values) {
[10978]110      if (IsFiltered)
111        throw new InvalidOperationException("SetValues not possible while data is filtered");
112
113      originalData.SetValues<T>(columnIndex, values);
[10804]114    }
[10783]115
[10804]116    public void InsertRow(int rowIndex) {
[10978]117      if (IsFiltered)
118        throw new InvalidOperationException("InsertRow not possible while data is filtered");
119
120      originalData.InsertRow(rowIndex);
[10804]121    }
[10783]122
[10804]123    public void DeleteRow(int rowIndex) {
[10978]124      if (IsFiltered)
125        throw new InvalidOperationException("DeleteRow not possible while data is filtered");
126
127      originalData.DeleteRow(rowIndex);
[10804]128    }
[10783]129
[10804]130    public void InsertColumn<T>(string variableName, int columnIndex) {
[10978]131      if (IsFiltered)
132        throw new InvalidOperationException("InsertColumn not possible while data is filtered");
133
134      originalData.InsertColumn<T>(variableName, columnIndex);
[10804]135    }
[10783]136
[10804]137    public void DeleteColumn(int columnIndex) {
[10978]138      if (IsFiltered)
139        throw new InvalidOperationException("DeleteColumn not possible while data is filtered");
140      originalData.DeleteColumn(columnIndex);
[10804]141    }
[10783]142
[13252]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
[10804]155    public string GetVariableName(int columnIndex) {
156      return ActiveData.GetVariableName(columnIndex);
157    }
[10783]158
[10804]159    public int GetColumnIndex(string variableName) {
160      return ActiveData.GetColumnIndex(variableName);
161    }
[10783]162
[11156]163    public bool VariableHasType<T>(int columnIndex) {
164      return originalData.VariableHasType<T>(columnIndex);
[10804]165    }
[10783]166
[10804]167    public Dataset ExportToDataset() {
168      return originalData.ExportToDataset();
169    }
[10783]170
[10804]171    public void SetFilter(bool[] rowFilters) {
172      filteredData = (ITransactionalPreprocessingData)originalData.Clone();
[10939]173      filteredData.InTransaction(() => {
174        for (int row = (rowFilters.Length - 1); row >= 0; --row) {
175          if (rowFilters[row]) {
176            filteredData.DeleteRow(row);
177          }
[10804]178        }
[10939]179      });
[10900]180      OnFilterChanged();
[10804]181    }
[10783]182
[10804]183    public void PersistFilter() {
[10900]184      originalData.InTransaction(() => {
185        for (int i = 0; i < filteredData.Columns; ++i) {
[11156]186          if (filteredData.VariableHasType<double>(i)) {
[10900]187            originalData.SetValues<double>(i, filteredData.GetValues<double>(i));
[11156]188          } else if (filteredData.VariableHasType<string>(i)) {
[10900]189            originalData.SetValues<string>(i, filteredData.GetValues<string>(i));
[11156]190          } else if (filteredData.VariableHasType<DateTime>(i)) {
[10900]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      });
[10804]197      ResetFilter();
198    }
[10783]199
[10804]200    public void ResetFilter() {
201      filteredData = null;
[10900]202      OnFilterChanged();
[10804]203    }
[10783]204
[10900]205    private void OnFilterChanged() {
206      if (FilterChanged != null) {
207        FilterChanged(this, new EventArgs());
208      }
209    }
210
[11003]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
[10804]233    public void Undo() {
[10978]234      if (IsFiltered)
235        throw new InvalidOperationException("Undo not possible while data is filtered");
236
237      originalData.Undo();
[10804]238    }
[10783]239
[10804]240    public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) {
[10978]241      if (IsFiltered)
242        throw new InvalidOperationException("Transaction not possible while data is filtered");
243      originalData.InTransaction(action, type);
[10804]244    }
[10783]245
[10804]246    public void BeginTransaction(DataPreprocessingChangedEventType type) {
[10978]247      if (IsFiltered)
248        throw new InvalidOperationException("Transaction not possible while data is filtered");
249      originalData.BeginTransaction(type);
[10804]250    }
251
252    public void EndTransaction() {
253      originalData.EndTransaction();
254    }
255
[10999]256    public IEnumerable<string> GetDoubleVariableNames() {
257      return originalData.GetDoubleVariableNames();
258    }
[10900]259
[10804]260    public void ClearSelection() {
261      originalData.ClearSelection();
262    }
263
[10847]264    public event EventHandler SelectionChanged {
265      add { originalData.SelectionChanged += value; }
266      remove { originalData.SelectionChanged -= value; }
267    }
[10804]268
[11003]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
[10999]275    public event EventHandler FilterChanged;
[11003]276    #endregion
[10783]277  }
278}
Note: See TracBrowser for help on using the repository browser.