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