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