source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/Implementations/PreprocessingData.cs @ 10994

Last change on this file since 10994 was 10994, checked in by tsteinre, 8 years ago
  • Partition Ranges are not computed anymore: save the concrete values in PreprocessingData.
File size: 7.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Problems.DataAnalysis;
30using HeuristicLab.Problems.DataAnalysis.Transformations;
31
32namespace HeuristicLab.DataPreprocessing {
33
34  [Item("PreprocessingData", "Represents data used for preprocessing.")]
35  public abstract class PreprocessingData : NamedItem, IPreprocessingData {
36
37    public IntRange TrainingPartition { get; set; }
38    public IntRange TestPartition { get; set; }
39
40    protected IList<ITransformation> transformations;
41    public IList<ITransformation> Transformations {
42      get { return transformations; }
43    }
44
45    protected IList<IList> variableValues;
46    protected IList<string> variableNames;
47
48    public IEnumerable<string> VariableNames {
49      get { return variableNames; }
50    }
51
52    public IEnumerable<string> GetDoubleVariableNames() {
53      var doubleVariableNames = new List<string>();
54      for (int i = 0; i < Columns; ++i) {
55        if (IsType<double>(i)) {
56          doubleVariableNames.Add(variableNames[i]);
57        }
58      }
59      return doubleVariableNames;
60    }
61
62    public int Columns {
63      get { return variableNames.Count; }
64    }
65
66    public int Rows {
67      get { return variableValues.Count > 0 ? variableValues[0].Count : 0; }
68    }
69
70    protected IDictionary<int, IList<int>> selection;
71    public IDictionary<int, IList<int>> Selection {
72      get { return selection; }
73      set {
74        selection = value;
75        OnSelectionChanged();
76      }
77    }
78
79    protected PreprocessingData(PreprocessingData original, Cloner cloner)
80      : base(original, cloner) {
81      variableValues = CopyVariableValues(original.variableValues);
82      variableNames = new List<string>(original.variableNames);
83      TrainingPartition = (IntRange)original.TrainingPartition.Clone(cloner);
84      TestPartition = (IntRange)original.TestPartition.Clone(cloner);
85      transformations = new List<ITransformation>();
86
87      RegisterEventHandler();
88    }
89
90    protected PreprocessingData(IDataAnalysisProblemData problemData)
91      : base() {
92      Name = "Preprocessing Data";
93
94      transformations = new List<ITransformation>();
95      selection = new Dictionary<int, IList<int>>();
96
97      variableNames = new List<string>(problemData.Dataset.VariableNames);
98
99      int columnIndex = 0;
100      variableValues = new List<IList>();
101      foreach (var variableName in problemData.Dataset.VariableNames) {
102        if (problemData.Dataset.IsType<double>(variableName)) {
103          variableValues.Insert(columnIndex, problemData.Dataset.GetDoubleValues(variableName).ToList());
104        } else if (problemData.Dataset.IsType<string>(variableName)) {
105          variableValues.Insert(columnIndex, CreateColumn<string>(problemData.Dataset, columnIndex, x => x));
106        } else if (problemData.Dataset.IsType<DateTime>(variableName)) {
107          variableValues.Insert(columnIndex, CreateColumn<DateTime>(problemData.Dataset, columnIndex, x => DateTime.Parse(x)));
108        } else {
109          throw new ArgumentException("The datatype of column " + variableName + " must be of type double, string or DateTime");
110        }
111        ++columnIndex;
112      }
113
114      TrainingPartition = new IntRange(problemData.TrainingPartition.Start, problemData.TrainingPartition.End);
115      TestPartition = new IntRange(problemData.TestPartition.Start, problemData.TestPartition.End);
116     
117      RegisterEventHandler();
118    }
119
120    private void RegisterEventHandler() {
121      Changed += (s, e) => {
122        switch (e.Type) {
123          case DataPreprocessingChangedEventType.DeleteRow:
124            CheckPartitionRanges();
125            break;
126          case DataPreprocessingChangedEventType.Any:
127            CheckPartitionRanges();
128            break;
129          case DataPreprocessingChangedEventType.Transformation:
130            CheckPartitionRanges();
131            break;
132        }
133      };
134    }
135
136    private static IList CreateColumn<T>(Dataset ds, int column, Func<string, T> selector) {
137      var list = new List<T>(ds.Rows);
138      for (int row = 0; row < ds.Rows; ++row) {
139        list.Add(selector(ds.GetValue(row, column)));
140      }
141      return list;
142    }
143
144    private void CheckPartitionRanges() {
145      int maxRowIndex = Math.Max(0, Rows - 1);
146      TrainingPartition.Start = Math.Min(TrainingPartition.Start, maxRowIndex);
147      TrainingPartition.End = Math.Min(TrainingPartition.End, maxRowIndex);
148      TestPartition.Start = Math.Min(TestPartition.Start, maxRowIndex);
149      TestPartition.End = Math.Min(TestPartition.End, maxRowIndex);
150    }
151
152    protected IList<IList> CopyVariableValues(IList<IList> original) {
153      var copy = new List<IList>(original);
154      for (int i = 0; i < original.Count; ++i) {
155        copy[i] = (IList)Activator.CreateInstance(original[i].GetType(), original[i]);
156      }
157      return copy;
158    }
159
160
161    #region IPreprocessingData Members
162
163    public abstract T GetCell<T>(int columnIndex, int rowIndex);
164
165    public abstract void SetCell<T>(int columnIndex, int rowIndex, T value);
166
167    public abstract string GetCellAsString(int columnIndex, int rowIndex);
168
169    public abstract string GetVariableName(int columnIndex);
170
171    public abstract int GetColumnIndex(string variableName);
172
173    public abstract bool IsType<T>(int columnIndex);
174
175    [Obsolete("use the index based variant, is faster")]
176    public abstract IList<T> GetValues<T>(string variableName, bool considerSelection);
177
178    public abstract IList<T> GetValues<T>(int columnIndex, bool considerSelection);
179
180    public abstract void SetValues<T>(int columnIndex, IList<T> values);
181
182    public abstract void InsertRow(int rowIndex);
183
184    public abstract void DeleteRow(int rowIndex);
185
186    public abstract void InsertColumn<T>(string variableName, int columnIndex);
187
188    public abstract void DeleteColumn(int columnIndex);
189
190    public abstract Dataset ExportToDataset();
191
192    public abstract void ClearSelection();
193
194    public abstract event EventHandler SelectionChanged;
195    protected abstract void OnSelectionChanged();
196
197    public event DataPreprocessingChangedEventHandler Changed;
198    protected virtual void OnChanged(DataPreprocessingChangedEventType type, int column, int row) {
199      var listeners = Changed;
200      if (listeners != null) listeners(this, new DataPreprocessingChangedEventArgs(type, column, row));
201    }
202    #endregion
203  }
204}
Note: See TracBrowser for help on using the repository browser.