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 |
|
---|
33 | [Item("PreprocessingData", "Represents data used for preprocessing.")]
|
---|
34 | public abstract class PreprocessingData : NamedItem, IPreprocessingData {
|
---|
35 |
|
---|
36 | public IntRange TrainingPartition { get; set; }
|
---|
37 | public IntRange TestPartition { get; set; }
|
---|
38 |
|
---|
39 | protected IList<ITransformation> transformations;
|
---|
40 | public IList<ITransformation> Transformations {
|
---|
41 | get { return transformations; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | protected IList<IList> variableValues;
|
---|
45 | protected IList<string> variableNames;
|
---|
46 |
|
---|
47 | public IEnumerable<string> VariableNames {
|
---|
48 | get { return variableNames; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public IEnumerable<string> GetDoubleVariableNames() {
|
---|
52 | var doubleVariableNames = new List<string>();
|
---|
53 | for (int i = 0; i < Columns; ++i) {
|
---|
54 | if (VariableHasType<double>(i)) {
|
---|
55 | doubleVariableNames.Add(variableNames[i]);
|
---|
56 | }
|
---|
57 | }
|
---|
58 | return doubleVariableNames;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public int Columns {
|
---|
62 | get { return variableNames.Count; }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public int Rows {
|
---|
66 | get { return variableValues.Count > 0 ? variableValues[0].Count : 0; }
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected IDictionary<int, IList<int>> selection;
|
---|
70 | public IDictionary<int, IList<int>> Selection {
|
---|
71 | get { return selection; }
|
---|
72 | set {
|
---|
73 | selection = value;
|
---|
74 | OnSelectionChanged();
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected PreprocessingData(PreprocessingData original, Cloner cloner)
|
---|
79 | : base(original, cloner) {
|
---|
80 | variableValues = CopyVariableValues(original.variableValues);
|
---|
81 | variableNames = new List<string>(original.variableNames);
|
---|
82 | TrainingPartition = (IntRange)original.TrainingPartition.Clone(cloner);
|
---|
83 | TestPartition = (IntRange)original.TestPartition.Clone(cloner);
|
---|
84 | transformations = new List<ITransformation>();
|
---|
85 |
|
---|
86 | RegisterEventHandler();
|
---|
87 | }
|
---|
88 |
|
---|
89 | protected PreprocessingData(IDataAnalysisProblemData problemData)
|
---|
90 | : base() {
|
---|
91 | Name = "Preprocessing Data";
|
---|
92 |
|
---|
93 | transformations = new List<ITransformation>();
|
---|
94 | selection = new Dictionary<int, IList<int>>();
|
---|
95 |
|
---|
96 | Dataset dataset = problemData.Dataset;
|
---|
97 | variableNames = new List<string>(problemData.Dataset.VariableNames);
|
---|
98 |
|
---|
99 | int columnIndex = 0;
|
---|
100 | variableValues = new List<IList>();
|
---|
101 | foreach (var variableName in problemData.Dataset.VariableNames) {
|
---|
102 | if (dataset.VariableHasType<double>(variableName)) {
|
---|
103 | variableValues.Insert(columnIndex, dataset.GetDoubleValues(variableName).ToList());
|
---|
104 | } else if (dataset.VariableHasType<string>(variableName)) {
|
---|
105 | variableValues.Insert(columnIndex, dataset.GetStringValues(variableName).ToList());
|
---|
106 | } else if (dataset.VariableHasType<DateTime>(variableName)) {
|
---|
107 | variableValues.Insert(columnIndex, dataset.GetDateTimeValues(variableName).ToList());
|
---|
108 | } else {
|
---|
109 | throw new ArgumentException("The datatype of column " + variableName + " must be of type double, string or DateTime");
|
---|
110 | }
|
---|
111 | ++columnIndex;
|
---|
112 | }
|
---|
113 |
|
---|
114 | TrainingPartition = new IntRange(problemData.TrainingPartition.Start, problemData.TrainingPartition.End);
|
---|
115 | TestPartition = new IntRange(problemData.TestPartition.Start, problemData.TestPartition.End);
|
---|
116 |
|
---|
117 | RegisterEventHandler();
|
---|
118 | }
|
---|
119 |
|
---|
120 | private void RegisterEventHandler() {
|
---|
121 | Changed += (s, e) => {
|
---|
122 | switch (e.Type) {
|
---|
123 | case DataPreprocessingChangedEventType.DeleteRow:
|
---|
124 | CheckPartitionRanges();
|
---|
125 | break;
|
---|
126 | case DataPreprocessingChangedEventType.Any:
|
---|
127 | CheckPartitionRanges();
|
---|
128 | break;
|
---|
129 | case DataPreprocessingChangedEventType.Transformation:
|
---|
130 | CheckPartitionRanges();
|
---|
131 | break;
|
---|
132 | }
|
---|
133 | };
|
---|
134 | }
|
---|
135 |
|
---|
136 | private static IList CreateColumn<T>(Dataset ds, int column, Func<string, T> selector) {
|
---|
137 | var list = new List<T>(ds.Rows);
|
---|
138 | for (int row = 0; row < ds.Rows; ++row) {
|
---|
139 | list.Add(selector(ds.GetValue(row, column)));
|
---|
140 | }
|
---|
141 | return list;
|
---|
142 | }
|
---|
143 |
|
---|
144 | private void CheckPartitionRanges() {
|
---|
145 | int maxRowIndex = Math.Max(0, Rows - 1);
|
---|
146 | TrainingPartition.Start = Math.Min(TrainingPartition.Start, maxRowIndex);
|
---|
147 | TrainingPartition.End = Math.Min(TrainingPartition.End, maxRowIndex);
|
---|
148 | TestPartition.Start = Math.Min(TestPartition.Start, maxRowIndex);
|
---|
149 | TestPartition.End = Math.Min(TestPartition.End, maxRowIndex);
|
---|
150 | }
|
---|
151 |
|
---|
152 | protected IList<IList> CopyVariableValues(IList<IList> original) {
|
---|
153 | var copy = new List<IList>(original);
|
---|
154 | for (int i = 0; i < original.Count; ++i) {
|
---|
155 | copy[i] = (IList)Activator.CreateInstance(original[i].GetType(), original[i]);
|
---|
156 | }
|
---|
157 | return copy;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | #region IPreprocessingData Members
|
---|
162 |
|
---|
163 | public abstract T GetCell<T>(int columnIndex, int rowIndex);
|
---|
164 |
|
---|
165 | public abstract void SetCell<T>(int columnIndex, int rowIndex, T value);
|
---|
166 |
|
---|
167 | public abstract string GetCellAsString(int columnIndex, int rowIndex);
|
---|
168 |
|
---|
169 | public abstract string GetVariableName(int columnIndex);
|
---|
170 |
|
---|
171 | public abstract int GetColumnIndex(string variableName);
|
---|
172 |
|
---|
173 | public abstract bool VariableHasType<T>(int columnIndex);
|
---|
174 |
|
---|
175 | [Obsolete("use the index based variant, is faster")]
|
---|
176 | public abstract IList<T> GetValues<T>(string variableName, bool considerSelection);
|
---|
177 |
|
---|
178 | public abstract IList<T> GetValues<T>(int columnIndex, bool considerSelection);
|
---|
179 |
|
---|
180 | public abstract void SetValues<T>(int columnIndex, IList<T> values);
|
---|
181 |
|
---|
182 | public abstract bool SetValue(string value, int columnIndex, int rowIndex);
|
---|
183 |
|
---|
184 | public abstract bool Validate(string value, out string errorMessage, int columnIndex);
|
---|
185 |
|
---|
186 | public abstract bool AreAllStringColumns(IEnumerable<int> columnIndices);
|
---|
187 |
|
---|
188 | public abstract void DeleteRowsWithIndices(IEnumerable<int> rows);
|
---|
189 |
|
---|
190 | public abstract void InsertRow(int rowIndex);
|
---|
191 |
|
---|
192 | public abstract void DeleteRow(int rowIndex);
|
---|
193 |
|
---|
194 | public abstract void InsertColumn<T>(string variableName, int columnIndex);
|
---|
195 |
|
---|
196 | public abstract void DeleteColumn(int columnIndex);
|
---|
197 |
|
---|
198 | public abstract Dataset ExportToDataset();
|
---|
199 |
|
---|
200 | public abstract void ClearSelection();
|
---|
201 |
|
---|
202 | public abstract event EventHandler SelectionChanged;
|
---|
203 | protected abstract void OnSelectionChanged();
|
---|
204 |
|
---|
205 | public event DataPreprocessingChangedEventHandler Changed;
|
---|
206 | protected virtual void OnChanged(DataPreprocessingChangedEventType type, int column, int row) {
|
---|
207 | var listeners = Changed;
|
---|
208 | if (listeners != null) listeners(this, new DataPreprocessingChangedEventArgs(type, column, row));
|
---|
209 | }
|
---|
210 | #endregion
|
---|
211 | }
|
---|
212 | }
|
---|