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

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