Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10544 was 10544, checked in by sbreuer, 11 years ago
  • created changed event in preprocessing data
  • register to event in line chart
File size: 8.2 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.Data;
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<int, IList> variableValues;
36
37    private IList<string> variableNames;
38
39    private double trainingToTestRatio;
40
41    private PreprocessingData(PreprocessingData original, Cloner cloner)
42      : base(original, cloner) {
43      variableValues = new Dictionary<int, IList>(original.variableValues);
44    }
45
46    public PreprocessingData(IDataAnalysisProblemData problemData)
47      : base() {
48      Name = "-";
49
50      variableNames = new List<string>(problemData.Dataset.VariableNames);
51      // create dictionary from variable name to index
52
53      int columnIndex = 0;
54      variableValues = new Dictionary<int, IList>();
55      foreach (var variableName in problemData.Dataset.VariableNames) {
56        if (problemData.Dataset.IsType<double>(variableName)) {
57          variableValues[columnIndex] = problemData.Dataset.GetDoubleValues(variableName).ToList();
58        } else if (problemData.Dataset.IsType<string>(variableName)) {
59          variableValues[columnIndex] = CreateColumn<string>(problemData.Dataset, columnIndex, x => x);
60        } else if (problemData.Dataset.IsType<DateTime>(variableName)) {
61          variableValues[columnIndex] = CreateColumn<DateTime>(problemData.Dataset, columnIndex, x => DateTime.Parse(x));
62        } else {
63          throw new ArgumentException("The datatype of column " + variableName + " must be of type List<double>, List<string> or List<DateTime>");
64        }
65        ++columnIndex;
66      }
67
68      trainingToTestRatio = (double)problemData.TrainingPartition.Size / Math.Max(problemData.Dataset.Rows, double.Epsilon);
69    }
70
71    private static IList CreateColumn<T>(Dataset ds, int column, Func<string, T> selector) {
72      var list = new List<T>(ds.Rows);
73      for (int row = 0; row < ds.Rows; ++row) {
74        list.Add(selector(ds.GetValue(row, column)));
75      }
76      return list;
77    }
78
79    #region NamedItem abstract Member Implementations
80
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new PreprocessingData(this, cloner);
83    }
84
85    #endregion
86
87    #region IPreprocessingData Members
88
89    [Obsolete("use the index based variant, is faster")]
90    public T GetCell<T>(string variableName, int rowIndex) {
91      return GetCell<T>(GetColumnIndex(variableName), rowIndex);
92    }
93
94    public T GetCell<T>(int columnIndex, int rowIndex) {
95      return (T)variableValues[columnIndex][rowIndex];
96    }
97
98    [Obsolete("use the index based variant, is faster")]
99    public void SetCell<T>(string variableName, int rowIndex, T value) {
100      SetCell<T>(GetColumnIndex(variableName), rowIndex, value);
101    }
102
103    public void SetCell<T>(int columnIndex, int rowIndex, T value) {
104      variableValues[columnIndex][rowIndex] = value;
105      OnChanged(DataPreprocessingChangedEventType.ChangeItem, columnIndex, rowIndex);
106    }
107
108    [Obsolete("use the index based variant, is faster")]
109    public string GetCellAsString(string variableName, int rowIndex) {
110      return GetCellAsString(GetColumnIndex(variableName), rowIndex);
111    }
112
113    public string GetCellAsString(int columnIndex, int rowIndex) {
114      return variableValues[columnIndex][rowIndex].ToString();
115    }
116
117    [Obsolete("use the index based variant, is faster")]
118    public IList<T> GetValues<T>(string variableName) {
119      return GetValues<T>(GetColumnIndex(variableName));
120    }
121
122    public IList<T> GetValues<T>(int columnIndex) {
123      return (IList<T>)variableValues[columnIndex];
124    }
125
126    [Obsolete("use the index based variant, is faster")]
127    public void SetValues<T>(string variableName, IList<T> values) {
128      SetValues<T>(GetColumnIndex(variableName), values);
129    }
130
131    public void SetValues<T>(int columnIndex, IList<T> values) {
132      if (IsType<T>(columnIndex)) {
133        variableValues[columnIndex] = (IList)values;
134      } else {
135        throw new ArgumentException("The datatype of column " + columnIndex + " must be of type " + variableValues[columnIndex].GetType().Name + " but was " + typeof(T).Name);
136      }
137      OnChanged(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1);
138    }
139
140    public void InsertRow(int rowIndex) {
141      foreach (IList column in variableValues.Values) {
142        Type type = column.GetType().GetGenericArguments()[0];
143        column.Insert(rowIndex, type.IsValueType ? Activator.CreateInstance(type) : null);
144      }
145      OnChanged(DataPreprocessingChangedEventType.AddRow, -1, rowIndex);
146    }
147
148    public void DeleteRow(int rowIndex) {
149      foreach (IList column in variableValues.Values) {
150        column.RemoveAt(rowIndex);
151      }
152      OnChanged(DataPreprocessingChangedEventType.DeleteRow, -1, rowIndex);
153    }
154
155    public void InsertColumn<T>(string variableName, int columnIndex) {
156      variableValues.Add(columnIndex, new List<T>(Rows));
157      variableNames.Insert(columnIndex, variableName);
158      OnChanged(DataPreprocessingChangedEventType.AddColumn, columnIndex , -1);
159    }
160
161    public void DeleteColumn(int columnIndex) {
162      variableValues.Remove(columnIndex);
163      variableNames.RemoveAt(columnIndex);
164      OnChanged(DataPreprocessingChangedEventType.DeleteColumn, columnIndex, -1);
165    }
166
167    [Obsolete("use the index based variant, is faster")]
168    public void DeleteColumn(string variableName) {
169      DeleteColumn(GetColumnIndex(variableName));
170    }
171
172    public IntRange TrainingPartition {
173      get { return new IntRange(0, (int)(Rows * trainingToTestRatio)); }
174    }
175
176    public IntRange TestPartition {
177      get { return new IntRange((int)(Rows * trainingToTestRatio), Rows); }
178    }
179
180    public IEnumerable<string> VariableNames {
181      get { return variableNames; }
182    }
183
184    [Obsolete("use the index based variant, is faster")]
185    public string GetVariableName(int columnIndex) {
186      return variableNames[columnIndex];
187    }
188    public int GetColumnIndex(string variableName) {
189      return variableNames.IndexOf(variableName);
190    }
191
192    [Obsolete("use the index based variant, is faster")]
193    public bool IsType<T>(string variableName) {
194      return IsType<T>(GetColumnIndex(variableName));
195
196    }
197    public bool IsType<T>(int columnIndex) {
198      return variableValues[columnIndex] is List<T>;
199    }
200
201    public int Columns {
202      get { return variableNames.Count; }
203    }
204
205    public int Rows {
206      get { return variableValues.Count > 0 ? variableValues[0].Count : 0; }
207    }
208
209    public Dataset ExportToDataset() {
210      IList<IList> values = new List<IList>();
211
212      for (int i = 0; i < Columns; ++i) {
213        values.Add(variableValues[i]);
214      }
215
216      var dataset = new Dataset(variableNames, values);
217      return dataset;
218    }
219
220    public event DataPreprocessingChangedEventHandler Changed;
221    protected virtual void OnChanged(DataPreprocessingChangedEventType type, int column, int row) {
222      var listeners = Changed;
223      if (listeners != null) listeners(this, new DataPreprocessingChangedEventArgs(type, column, row));
224    }
225
226    #endregion
227  }
228}
Note: See TracBrowser for help on using the repository browser.