Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/PreprocessingData.cs @ 10718

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