Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10665 was 10586, checked in by tsteinre, 11 years ago
  • divided/refactored PreprocessingData into TransactionalPreprocessingData and preprocessingData
File size: 6.7 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
[10586]42    protected PreprocessingData(PreprocessingData original, Cloner cloner)
[10185]43      : base(original, cloner) {
[10550]44      variableValues = CopyVariableValues(original.variableValues);
[10548]45      variableNames = new List<string>(original.variableNames);
46      trainingToTestRatio = original.trainingToTestRatio;
[10185]47    }
[10187]48
[10168]49    public PreprocessingData(IDataAnalysisProblemData problemData)
50      : base() {
51      Name = "-";
52
[10187]53      variableNames = new List<string>(problemData.Dataset.VariableNames);
[10185]54      // create dictionary from variable name to index
[10187]55
[10367]56      int columnIndex = 0;
57      variableValues = new Dictionary<int, IList>();
[10185]58      foreach (var variableName in problemData.Dataset.VariableNames) {
59        if (problemData.Dataset.IsType<double>(variableName)) {
[10367]60          variableValues[columnIndex] = problemData.Dataset.GetDoubleValues(variableName).ToList();
[10185]61        } else if (problemData.Dataset.IsType<string>(variableName)) {
[10367]62          variableValues[columnIndex] = CreateColumn<string>(problemData.Dataset, columnIndex, x => x);
[10185]63        } else if (problemData.Dataset.IsType<DateTime>(variableName)) {
[10367]64          variableValues[columnIndex] = CreateColumn<DateTime>(problemData.Dataset, columnIndex, x => DateTime.Parse(x));
[10168]65        } else {
[10185]66          throw new ArgumentException("The datatype of column " + variableName + " must be of type List<double>, List<string> or List<DateTime>");
[10168]67        }
[10367]68        ++columnIndex;
[10168]69      }
[10185]70
[10235]71      trainingToTestRatio = (double)problemData.TrainingPartition.Size / Math.Max(problemData.Dataset.Rows, double.Epsilon);
[10163]72    }
73
[10185]74    private static IList CreateColumn<T>(Dataset ds, int column, Func<string, T> selector) {
75      var list = new List<T>(ds.Rows);
[10341]76      for (int row = 0; row < ds.Rows; ++row) {
[10367]77        list.Add(selector(ds.GetValue(row, column)));
[10185]78      }
79      return list;
80    }
81
[10586]82    protected IDictionary<int, IList> CopyVariableValues(IDictionary<int, IList> original) {
[10550]83      var copy = new Dictionary<int, IList>(variableValues);
84      for (int i = 0; i < original.Count; i++) {
[10554]85        variableValues[i] = (IList)Activator.CreateInstance(original[i].GetType(), original[i]);
[10550]86      }
87      return copy;
88    }
89
[10163]90    #region NamedItem abstract Member Implementations
91
[10185]92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new PreprocessingData(this, cloner);
[10163]94    }
95
96    #endregion
97
98    #region IPreprocessingData Members
99
[10367]100    public T GetCell<T>(int columnIndex, int rowIndex) {
101      return (T)variableValues[columnIndex][rowIndex];
[10181]102    }
103
[10236]104
[10586]105    public virtual void SetCell<T>(int columnIndex, int rowIndex, T value) {
[10367]106      variableValues[columnIndex][rowIndex] = value;
107    }
108
109
110    public string GetCellAsString(int columnIndex, int rowIndex) {
111      return variableValues[columnIndex][rowIndex].ToString();
112    }
113
[10547]114
[10367]115    [Obsolete("use the index based variant, is faster")]
[10243]116    public IList<T> GetValues<T>(string variableName) {
[10367]117      return GetValues<T>(GetColumnIndex(variableName));
[10181]118    }
119
[10367]120    public IList<T> GetValues<T>(int columnIndex) {
121      return (IList<T>)variableValues[columnIndex];
122    }
123
[10586]124    public virtual void SetValues<T>(int columnIndex, IList<T> values) {
[10367]125      if (IsType<T>(columnIndex)) {
126        variableValues[columnIndex] = (IList)values;
127      } else {
128        throw new ArgumentException("The datatype of column " + columnIndex + " must be of type " + variableValues[columnIndex].GetType().Name + " but was " + typeof(T).Name);
[10311]129      }
[10181]130    }
131
[10586]132    public virtual void InsertRow(int rowIndex) {
[10194]133      foreach (IList column in variableValues.Values) {
134        Type type = column.GetType().GetGenericArguments()[0];
135        column.Insert(rowIndex, type.IsValueType ? Activator.CreateInstance(type) : null);
136      }
[10163]137    }
138
[10586]139    public virtual void DeleteRow(int rowIndex) {
[10194]140      foreach (IList column in variableValues.Values) {
141        column.RemoveAt(rowIndex);
142      }
[10163]143    }
144
[10586]145    public virtual void InsertColumn<T>(string variableName, int columnIndex) {
[10367]146      variableValues.Add(columnIndex, new List<T>(Rows));
[10547]147      variableNames.Insert(columnIndex, variableName);
[10163]148    }
149
[10586]150    public virtual void DeleteColumn(int columnIndex) {
[10367]151      variableValues.Remove(columnIndex);
152      variableNames.RemoveAt(columnIndex);
153    }
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
198    #endregion
[10163]199  }
200}
Note: See TracBrowser for help on using the repository browser.