Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10978 was 10978, checked in by mleitner, 10 years ago

Refactoring

File size: 8.0 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    protected double trainingToTestRatio;
38    public IntRange TrainingPartition {
39      get { return new IntRange(0, (int)(Rows * trainingToTestRatio)); }
40    }
41    public IntRange TestPartition {
42      get { return new IntRange((int)(Rows * trainingToTestRatio), Rows); }
43    }
44
45    protected IList<ITransformation> transformations;
46    public IList<ITransformation> Transformations {
47      get { return transformations; }
48    }
49
50    protected IList<IList> variableValues;
51    protected IList<string> variableNames;
52
53    public IEnumerable<string> VariableNames {
54      get { return variableNames; }
55    }
56
57    public int Columns {
58      get { return variableNames.Count; }
59    }
60
61    public int Rows {
62      get { return variableValues.Count > 0 ? variableValues[0].Count : 0; }
63    }
64
65    protected IDictionary<int, IList<int>> selection;
66    public IDictionary<int, IList<int>> Selection {
67      get { return selection; }
68      set {
69          selection = value;
70          OnSelectionChanged();
71        }
72      }   
73
74    protected PreprocessingData(PreprocessingData original, Cloner cloner)
75      : base(original, cloner) {
76      variableValues = CopyVariableValues(original.variableValues);
77      variableNames = new List<string>(original.variableNames);
78      trainingToTestRatio = original.trainingToTestRatio;
79      transformations = new List<ITransformation>();
80    }
81
82    protected PreprocessingData(IDataAnalysisProblemData problemData)
83      : base() {
84      Name = "Preprocessing Data";
85
86      transformations = new List<ITransformation>();
87      selection = new Dictionary<int, IList<int>>();
88
89      variableNames = new List<string>(problemData.Dataset.VariableNames);
90
91      int columnIndex = 0;
92      variableValues = new List<IList>();
93      foreach (var variableName in problemData.Dataset.VariableNames) {
94        if (problemData.Dataset.IsType<double>(variableName)) {
95          variableValues.Insert(columnIndex, problemData.Dataset.GetDoubleValues(variableName).ToList());
96        } else if (problemData.Dataset.IsType<string>(variableName)) {
97          variableValues.Insert(columnIndex, CreateColumn<string>(problemData.Dataset, columnIndex, x => x));
98        } else if (problemData.Dataset.IsType<DateTime>(variableName)) {
99          variableValues.Insert(columnIndex, CreateColumn<DateTime>(problemData.Dataset, columnIndex, x => DateTime.Parse(x)));
100        } else {
101          throw new ArgumentException("The datatype of column " + variableName + " must be of type double, string or DateTime");
102        }
103        ++columnIndex;
104      }
105
106      trainingToTestRatio = (double)problemData.TrainingPartition.Size / Math.Max(problemData.Dataset.Rows, double.Epsilon);
107    }
108
109    private static IList CreateColumn<T>(Dataset ds, int column, Func<string, T> selector) {
110      var list = new List<T>(ds.Rows);
111      for (int row = 0; row < ds.Rows; ++row) {
112        list.Add(selector(ds.GetValue(row, column)));
113      }
114      return list;
115    }
116
117    protected IList<IList> CopyVariableValues(IList<IList> original) {
118      var copy = new List<IList>(original);
119      for (int i = 0; i < original.Count; ++i) {
120        copy[i] = (IList)Activator.CreateInstance(original[i].GetType(), original[i]);
121      }
122      return copy;
123    }
124
125
126    #region IPreprocessingData Members
127
128    public T GetCell<T>(int columnIndex, int rowIndex) {
129      return (T)variableValues[columnIndex][rowIndex];
130    }
131
132    public virtual void SetCell<T>(int columnIndex, int rowIndex, T value) {
133      variableValues[columnIndex][rowIndex] = value;
134    }
135
136    public string GetCellAsString(int columnIndex, int rowIndex) {
137      return variableValues[columnIndex][rowIndex].ToString();
138    }
139
140    public string GetVariableName(int columnIndex) {
141      return variableNames[columnIndex];
142    }
143
144    public int GetColumnIndex(string variableName) {
145      return variableNames.IndexOf(variableName);
146    }
147
148    public bool IsType<T>(int columnIndex) {
149      return variableValues[columnIndex] is List<T>;
150    }
151
152    [Obsolete("use the index based variant, is faster")]
153    public IList<T> GetValues<T>(string variableName, bool considerSelection) {
154      return GetValues<T>(GetColumnIndex(variableName), considerSelection);
155    }
156
157    public IList<T> GetValues<T>(int columnIndex, bool considerSelection) {
158      if (considerSelection) {
159        var list = new List<T>();
160        foreach (var rowIdx in selection[columnIndex]) {
161          list.Add((T)variableValues[columnIndex][rowIdx]);
162        }
163        return list;
164      } else {
165        return (IList<T>)variableValues[columnIndex];
166      }
167    }
168
169    public virtual void SetValues<T>(int columnIndex, IList<T> values) {
170      if (IsType<T>(columnIndex)) {
171        variableValues[columnIndex] = (IList)values;
172      } else {
173        throw new ArgumentException("The datatype of column " + columnIndex + " must be of type " + variableValues[columnIndex].GetType().Name + " but was " + typeof(T).Name);
174      }
175    }
176
177    public virtual void InsertRow(int rowIndex) {
178      foreach (IList column in variableValues) {
179        Type type = column.GetType().GetGenericArguments()[0];
180        column.Insert(rowIndex, type.IsValueType ? Activator.CreateInstance(type) : null);
181      }
182    }
183
184    public virtual void DeleteRow(int rowIndex) {
185      foreach (IList column in variableValues) {
186        column.RemoveAt(rowIndex);
187      }
188    }
189
190    public virtual void InsertColumn<T>(string variableName, int columnIndex) {
191      variableValues.Insert(columnIndex, new List<T>(Rows));
192      variableNames.Insert(columnIndex, variableName);
193    }
194
195    public virtual void DeleteColumn(int columnIndex) {
196      variableValues.RemoveAt(columnIndex);
197      variableNames.RemoveAt(columnIndex);
198    }
199
200    public Dataset ExportToDataset() {
201      IList<IList> values = new List<IList>();
202
203      for (int i = 0; i < Columns; ++i) {
204        values.Add(variableValues[i]);
205      }
206
207      var dataset = new Dataset(variableNames, values);
208      return dataset;
209    }
210
211    public void ClearSelection() {
212      Selection = new Dictionary<int, IList<int>>();
213    }
214
215    public event EventHandler SelectionChanged;
216    protected virtual void OnSelectionChanged() {
217       var listeners = SelectionChanged;
218       if (listeners != null) listeners(this, EventArgs.Empty);
219    }
220
221    public event DataPreprocessingChangedEventHandler Changed;
222    protected virtual void OnChanged(DataPreprocessingChangedEventType type, int column, int row) {
223      var listeners = Changed;
224      if (listeners != null) listeners(this, new DataPreprocessingChangedEventArgs(type, column, row));
225    }
226    #endregion
227  }
228}
Note: See TracBrowser for help on using the repository browser.