Free cookie consent management tool by TermsFeed Policy Generator

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

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