Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/PreprocessingData.cs @ 10842

Last change on this file since 10842 was 10842, checked in by psteiner, 10 years ago

BugFix transactions null

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