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 |
|
---|
31 | namespace 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[row] = 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, IEnumerable<T> values) {
|
---|
114 | variableValues[variableName] = values.ToList();
|
---|
115 | }
|
---|
116 |
|
---|
117 | public void InsertRow(int rowIndex) {
|
---|
118 | foreach (IList column in variableValues.Values) {
|
---|
119 | Type type = column.GetType().GetGenericArguments()[0];
|
---|
120 |
|
---|
121 | column.Insert(rowIndex, type.IsValueType ? Activator.CreateInstance(type) : null);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | public void DeleteRow(int rowIndex) {
|
---|
126 | foreach (IList column in variableValues.Values) {
|
---|
127 | column.RemoveAt(rowIndex);
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | public void InsertColumn<T>(string variableName, int columnIndex) {
|
---|
132 | variableValues.Add(variableName, new List<T>(Rows));
|
---|
133 | variableNameIndices.Add(variableName, columnIndex);
|
---|
134 | variableNames.Insert(columnIndex, variableName);
|
---|
135 | }
|
---|
136 |
|
---|
137 | public void DeleteColumn(string variableName) {
|
---|
138 | variableValues.Remove(variableName);
|
---|
139 | variableNames.RemoveAt(variableNameIndices[variableName]);
|
---|
140 | variableNameIndices.Remove(variableName);
|
---|
141 | }
|
---|
142 |
|
---|
143 | public IntRange TrainingPartition {
|
---|
144 | get { return new IntRange(0, (int)(Rows * trainingToTestRatio)); }
|
---|
145 | }
|
---|
146 |
|
---|
147 | public IntRange TestPartition {
|
---|
148 | get { return new IntRange((int)(Rows * trainingToTestRatio), Rows); }
|
---|
149 | }
|
---|
150 |
|
---|
151 | public IEnumerable<string> VariableNames {
|
---|
152 | get { return variableNames; }
|
---|
153 | }
|
---|
154 |
|
---|
155 | public string GetVariableName(int columnIndex) {
|
---|
156 | return variableNames[columnIndex];
|
---|
157 | }
|
---|
158 |
|
---|
159 | public bool IsType<T>(string variableName) {
|
---|
160 | return variableValues[variableName] is List<T>;
|
---|
161 | }
|
---|
162 |
|
---|
163 | public int Columns {
|
---|
164 | get { return variableNames.Count; }
|
---|
165 | }
|
---|
166 |
|
---|
167 | public int Rows {
|
---|
168 | get { return variableValues[variableNames[0]].Count; }
|
---|
169 | }
|
---|
170 |
|
---|
171 | public Dataset ExportToDataset() {
|
---|
172 | IList<IList> values = new List<IList>();
|
---|
173 | foreach (var variable in VariableNames) {
|
---|
174 | values.Add(variableValues[variable]);
|
---|
175 | }
|
---|
176 |
|
---|
177 | var dataset = new Dataset(variableNames, values);
|
---|
178 | return dataset;
|
---|
179 | }
|
---|
180 |
|
---|
181 | #endregion
|
---|
182 | }
|
---|
183 | }
|
---|