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