Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10186 was 10186, checked in by pfleck, 10 years ago

Added variableName list.
Implemented GetCell, Columns and Rows.

File size: 5.9 KB
Line 
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;
23using System.Collections;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.DataPreprocessing;
29using HeuristicLab.Problems.DataAnalysis;
30
31namespace HeuristicLab.DataPreprocessing {
32  [Item("PreprocessingData", "Represents data used for preprocessing.")]
33  public class PreprocessingData : NamedItem, IPreprocessingData {
34
35    private IDictionary<string, IList> variableValues;
36
37    private IList<string> variableNames;
38
39    private IDictionary<string, int> variableNameIndices;
40
41    private double trainingToTestRatio;
42
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    }
48   
49    public PreprocessingData(IDataAnalysisProblemData problemData)
50      : base() {
51      Name = "-";
52
53      variableNames = new List<string>(problemData.Dataset.VariableNames));
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      }
60     
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));
70        } else {
71          throw new ArgumentException("The datatype of column " + variableName + " must be of type List<double>, List<string> or List<DateTime>");
72        }
73      }
74
75      trainingToTestRatio = (double)problemData.TrainingPartition.Size / problemData.TestPartition.Size;
76      Columns = problemData.Dataset.Columns;
77      Rows = problemData.Dataset.Rows;
78    }
79
80    private static IList CreateColumn<T>(Dataset ds, int column, Func<string, T> selector) {
81      var list = new List<T>(ds.Rows);
82      for (int row = 0; row < ds.Rows; row++) {
83        list[row] = selector(ds.GetValue(row, column));
84      }
85      return list;
86    }
87
88    #region NamedItem abstract Member Implementations
89
90    public override IDeepCloneable Clone(Cloner cloner) {
91      return new PreprocessingData(this, cloner);
92    }
93
94    #endregion
95
96    #region IPreprocessingData Members
97
98    public T GetCell<T>(int columnIndex, int rowIndex) {
99      return (T)variableValues[variableNames[columnIndex]][rowIndex];
100    }
101
102    public void SetCell<T>(int columnIndex, int rowIndex, T value) {
103      throw new NotImplementedException();
104    }
105
106    public T GetCell<T>(string variableName, int row) {
107      throw new NotImplementedException();
108    }
109
110    public void SetCell<T>(string variableName, int row, T value) {
111      throw new NotImplementedException();
112    }
113
114    public IEnumerable<T> GetValues<T>(int columnIndex) {
115      throw new NotImplementedException();
116    }
117
118    public IEnumerable<T> GetValues<T>(string variableName) {
119      throw new NotImplementedException();
120    }
121
122    public void SetValues<T>(int columnIndex, IEnumerable<T> values) {
123      throw new NotImplementedException();
124    }
125
126    public void SetValues<T>(string variableName, IEnumerable<T> values) {
127      throw new NotImplementedException();
128    }
129
130    public void InsertRow(int rowIndex) {
131      throw new NotImplementedException();
132    }
133
134    public void DeleteRow(int rowIndex) {
135      throw new NotImplementedException();
136    }
137
138    public void InsertColumn(string variableName, int columnIndex) {
139      throw new NotImplementedException();
140    }
141
142    public void DeleteColumn(int columnIndex) {
143      throw new NotImplementedException();
144    }
145
146    public void DeleteColumn(string variableName) {
147      throw new NotImplementedException();
148    }
149
150    IEnumerable<string> IPreprocessingData.VariableNames {
151      get { throw new NotImplementedException(); }
152    }
153
154    public bool IsType<T>(int columnIndex) {
155      throw new NotImplementedException();
156    }
157
158    public bool IsType<T>(string variableName) {
159      throw new NotImplementedException();
160    }
161
162    public int Columns {
163      get;
164      private set;
165    }
166
167    public int Rows {
168      get;
169      private set;
170    }
171
172    public void ExportTo(IDataAnalysisProblemData problemData) {
173      throw new NotImplementedException();
174    }
175
176    #endregion
177  }
178}
Note: See TracBrowser for help on using the repository browser.