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