Free cookie consent management tool by TermsFeed Policy Generator

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

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