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