Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10367 was 10367, checked in by rstoll, 11 years ago
  • modified PreprocessingData, uses columnIndex now instead of variableName (is faster and more convenient), set variabelName based methods to Obsolete
  • Already changed SearchLogic, DataGridLogic, StatisticLogic as well as PreprocessingDataManipulation

*

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