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