source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/Implementations/PreprocessingData.cs @ 10992

Last change on this file since 10992 was 10992, checked in by rstoll, 8 years ago
  • removed ChartLogic and

moved logic accordingly to PreprocessingChartContent, ScatterPlotContent
modified views etc. to use IFilteredPreprocessingData instead of ChartLogic

  • reordered code
File size: 6.4 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;
30using HeuristicLab.Problems.DataAnalysis.Transformations;
31
32namespace HeuristicLab.DataPreprocessing {
33
34  [Item("PreprocessingData", "Represents data used for preprocessing.")]
35  public abstract class PreprocessingData : NamedItem, IPreprocessingData {
36
37    protected double trainingToTestRatio;
38    public IntRange TrainingPartition {
39      get { return new IntRange(0, (int)(Rows * trainingToTestRatio)); }
40    }
41    public IntRange TestPartition {
42      get { return new IntRange((int)(Rows * trainingToTestRatio), Rows); }
43    }
44
45    protected IList<ITransformation> transformations;
46    public IList<ITransformation> Transformations {
47      get { return transformations; }
48    }
49
50    protected IList<IList> variableValues;
51    protected IList<string> variableNames;
52
53    public IEnumerable<string> VariableNames {
54      get { return variableNames; }
55    }
56
57    public IEnumerable<string> GetDoubleVariableNames() {
58      var doubleVariableNames = new List<string>();
59      for (int i = 0; i < Columns; ++i) {
60        if (IsType<double>(i)) {
61          doubleVariableNames.Add(variableNames[i]);
62        }
63      }
64      return doubleVariableNames;
65    }
66
67    public int Columns {
68      get { return variableNames.Count; }
69    }
70
71    public int Rows {
72      get { return variableValues.Count > 0 ? variableValues[0].Count : 0; }
73    }
74
75    protected IDictionary<int, IList<int>> selection;
76    public IDictionary<int, IList<int>> Selection {
77      get { return selection; }
78      set {
79        selection = value;
80        OnSelectionChanged();
81      }
82    }
83
84    protected PreprocessingData(PreprocessingData original, Cloner cloner)
85      : base(original, cloner) {
86      variableValues = CopyVariableValues(original.variableValues);
87      variableNames = new List<string>(original.variableNames);
88      trainingToTestRatio = original.trainingToTestRatio;
89      transformations = new List<ITransformation>();
90    }
91
92    protected PreprocessingData(IDataAnalysisProblemData problemData)
93      : base() {
94      Name = "Preprocessing Data";
95
96      transformations = new List<ITransformation>();
97      selection = new Dictionary<int, IList<int>>();
98
99      variableNames = new List<string>(problemData.Dataset.VariableNames);
100
101      int columnIndex = 0;
102      variableValues = new List<IList>();
103      foreach (var variableName in problemData.Dataset.VariableNames) {
104        if (problemData.Dataset.IsType<double>(variableName)) {
105          variableValues.Insert(columnIndex, problemData.Dataset.GetDoubleValues(variableName).ToList());
106        } else if (problemData.Dataset.IsType<string>(variableName)) {
107          variableValues.Insert(columnIndex, CreateColumn<string>(problemData.Dataset, columnIndex, x => x));
108        } else if (problemData.Dataset.IsType<DateTime>(variableName)) {
109          variableValues.Insert(columnIndex, CreateColumn<DateTime>(problemData.Dataset, columnIndex, x => DateTime.Parse(x)));
110        } else {
111          throw new ArgumentException("The datatype of column " + variableName + " must be of type double, string or DateTime");
112        }
113        ++columnIndex;
114      }
115
116      trainingToTestRatio = (double)problemData.TrainingPartition.Size / Math.Max(problemData.Dataset.Rows, double.Epsilon);
117    }
118
119    private static IList CreateColumn<T>(Dataset ds, int column, Func<string, T> selector) {
120      var list = new List<T>(ds.Rows);
121      for (int row = 0; row < ds.Rows; ++row) {
122        list.Add(selector(ds.GetValue(row, column)));
123      }
124      return list;
125    }
126
127    protected IList<IList> CopyVariableValues(IList<IList> original) {
128      var copy = new List<IList>(original);
129      for (int i = 0; i < original.Count; ++i) {
130        copy[i] = (IList)Activator.CreateInstance(original[i].GetType(), original[i]);
131      }
132      return copy;
133    }
134
135
136    #region IPreprocessingData Members
137
138    public abstract T GetCell<T>(int columnIndex, int rowIndex);
139
140    public abstract void SetCell<T>(int columnIndex, int rowIndex, T value);
141
142    public abstract string GetCellAsString(int columnIndex, int rowIndex);
143
144    public abstract string GetVariableName(int columnIndex);
145
146    public abstract int GetColumnIndex(string variableName);
147
148    public abstract bool IsType<T>(int columnIndex);
149
150    [Obsolete("use the index based variant, is faster")]
151    public abstract IList<T> GetValues<T>(string variableName, bool considerSelection);
152
153    public abstract IList<T> GetValues<T>(int columnIndex, bool considerSelection);
154
155    public abstract void SetValues<T>(int columnIndex, IList<T> values);
156
157    public abstract void InsertRow(int rowIndex);
158
159    public abstract void DeleteRow(int rowIndex);
160
161    public abstract void InsertColumn<T>(string variableName, int columnIndex);
162
163    public abstract void DeleteColumn(int columnIndex);
164
165    public abstract Dataset ExportToDataset();
166
167    public abstract void ClearSelection();
168
169    public abstract event EventHandler SelectionChanged;
170    protected abstract void OnSelectionChanged();
171
172    public event DataPreprocessingChangedEventHandler Changed;
173    protected virtual void OnChanged(DataPreprocessingChangedEventType type, int column, int row) {
174      var listeners = Changed;
175      if (listeners != null) listeners(this, new DataPreprocessingChangedEventArgs(type, column, row));
176    }
177    #endregion
178  }
179}
Note: See TracBrowser for help on using the repository browser.