Free cookie consent management tool by TermsFeed Policy Generator

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

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