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