Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10245 was 10243, checked in by rstoll, 11 years ago
  • removed NotImplementedException in property Rows since it is invoked by an event
  • set fields number of columns / rows to read only
File size: 6.3 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 {
[10163]32  [Item("PreprocessingData", "Represents data used for preprocessing.")]
33  public class PreprocessingData : NamedItem, IPreprocessingData {
34
[10185]35    private IDictionary<string, IList> variableValues;
[10168]36
[10186]37    private IList<string> variableNames;
38
[10185]39    private IDictionary<string, int> variableNameIndices;
40
41    private double trainingToTestRatio;
[10220]42
[10185]43    private PreprocessingData(PreprocessingData original, Cloner cloner)
44      : base(original, cloner) {
45      variableValues = new Dictionary<string, IList>(variableValues);
46      variableNameIndices = new Dictionary<string, int>(variableNameIndices);
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
55      variableNameIndices = new Dictionary<string, int>();
56      var variableNamesList = problemData.Dataset.VariableNames.ToList();
57      for (int i = 0; i < variableNamesList.Count; i++) {
58        variableNameIndices.Add(variableNamesList[i], i);
59      }
[10187]60
[10185]61      // copy values
62      variableValues = new Dictionary<string, IList>();
63      foreach (var variableName in problemData.Dataset.VariableNames) {
64        if (problemData.Dataset.IsType<double>(variableName)) {
65          variableValues[variableName] = problemData.Dataset.GetDoubleValues(variableName).ToList();
66        } else if (problemData.Dataset.IsType<string>(variableName)) {
67          variableValues[variableName] = CreateColumn<string>(problemData.Dataset, variableNameIndices[variableName], x => x);
68        } else if (problemData.Dataset.IsType<DateTime>(variableName)) {
69          variableValues[variableName] = CreateColumn<DateTime>(problemData.Dataset, variableNameIndices[variableName], x => DateTime.Parse(x));
[10168]70        } else {
[10185]71          throw new ArgumentException("The datatype of column " + variableName + " must be of type List<double>, List<string> or List<DateTime>");
[10168]72        }
73      }
[10185]74
[10235]75      trainingToTestRatio = (double)problemData.TrainingPartition.Size / Math.Max(problemData.Dataset.Rows, double.Epsilon);
[10163]76    }
77
[10185]78    private static IList CreateColumn<T>(Dataset ds, int column, Func<string, T> selector) {
79      var list = new List<T>(ds.Rows);
80      for (int row = 0; row < ds.Rows; row++) {
81        list[row] = selector(ds.GetValue(row, column));
82      }
83      return list;
84    }
85
[10163]86    #region NamedItem abstract Member Implementations
87
[10185]88    public override IDeepCloneable Clone(Cloner cloner) {
89      return new PreprocessingData(this, cloner);
[10163]90    }
91
92    #endregion
93
94    #region IPreprocessingData Members
95
[10181]96    public T GetCell<T>(string variableName, int row) {
[10187]97      return (T)variableValues[variableName][row];
[10163]98    }
99
[10181]100    public void SetCell<T>(string variableName, int row, T value) {
[10187]101      variableValues[variableName][row] = value;
[10181]102    }
103
[10236]104    public string GetCellAsString(string variableName, int row) {
105      return variableValues[variableName][row].ToString();
106    }
107
[10243]108    public IList<T> GetValues<T>(string variableName) {
[10194]109      // TODO: test if cast is valid
[10243]110      return (IList<T>) variableValues[variableName];
[10181]111    }
112
113    public void SetValues<T>(string variableName, IEnumerable<T> values) {
[10187]114      variableValues[variableName] = values.ToList();
[10181]115    }
116
[10163]117    public void InsertRow(int rowIndex) {
[10194]118      foreach (IList column in variableValues.Values) {
119        Type type = column.GetType().GetGenericArguments()[0];
120
121        column.Insert(rowIndex, type.IsValueType ? Activator.CreateInstance(type) : null);
122      }
[10163]123    }
124
125    public void DeleteRow(int rowIndex) {
[10194]126      foreach (IList column in variableValues.Values) {
127        column.RemoveAt(rowIndex);
128      }
[10163]129    }
130
[10194]131    public void InsertColumn<T>(string variableName, int columnIndex) {
132      variableValues.Add(variableName, new List<T>(Rows));
133      variableNameIndices.Add(variableName, columnIndex);
134      variableNames.Insert(columnIndex, variableName);
[10163]135    }
136
[10181]137    public void DeleteColumn(string variableName) {
[10194]138      variableValues.Remove(variableName);
139      variableNames.RemoveAt(variableNameIndices[variableName]);
140      variableNameIndices.Remove(variableName);
[10181]141    }
142
[10221]143    public IntRange TrainingPartition {
144      get { return new IntRange(0, (int)(Rows * trainingToTestRatio)); }
145    }
146
147    public IntRange TestPartition {
[10235]148      get { return new IntRange((int)(Rows * trainingToTestRatio), Rows); }
[10221]149    }
150
[10243]151    public IEnumerable<string> VariableNames {
[10188]152      get { return variableNames; }
[10163]153    }
154
[10243]155    public string GetVariableName(int columnIndex) {
156      return variableNames[columnIndex];
157    }
158
[10181]159    public bool IsType<T>(string variableName) {
[10188]160      return variableValues[variableName] is List<T>;
[10181]161    }
162
[10163]163    public int Columns {
[10194]164      get { return variableNames.Count; }
[10163]165    }
166
167    public int Rows {
[10221]168      get { return variableValues[variableNames[0]].Count; }
[10163]169    }
[10189]170
[10220]171    public Dataset ExportToDataset() {
172      IList<IList> values = new List<IList>();
173      foreach (var variable in VariableNames) {
174        values.Add(variableValues[variable]);
175      }
176
177      var dataset = new Dataset(variableNames, values);
178      return dataset;
179    }
180
181    #endregion
[10163]182  }
183}
Note: See TracBrowser for help on using the repository browser.