Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataPreprocessing/3.4/Data/PreprocessingData.cs @ 15296

Last change on this file since 15296 was 15110, checked in by pfleck, 7 years ago

#2709: merged branch to trunk

File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
33  [Item("PreprocessingData", "Represents data used for preprocessing.")]
34  public abstract class PreprocessingData : NamedItem, IPreprocessingData {
35    public IntRange TrainingPartition { get; set; }
36    public IntRange TestPartition { get; set; }
37
38    public IList<ITransformation> Transformations { get; protected set; }
39
40    protected IList<IList> variableValues;
41    protected IList<string> variableNames;
42
43    public IEnumerable<string> VariableNames {
44      get { return variableNames; }
45    }
46
47    public IEnumerable<string> GetDoubleVariableNames() {
48      var doubleVariableNames = new List<string>();
49      for (int i = 0; i < Columns; ++i) {
50        if (VariableHasType<double>(i)) {
51          doubleVariableNames.Add(variableNames[i]);
52        }
53      }
54      return doubleVariableNames;
55    }
56
57    public IList<string> InputVariables { get; private set; }
58    public string TargetVariable { get; private set; } // optional
59
60    public int Columns {
61      get { return variableNames.Count; }
62    }
63
64    public int Rows {
65      get { return variableValues.Count > 0 ? variableValues[0].Count : 0; }
66    }
67
68    protected IDictionary<int, IList<int>> selection;
69    public IDictionary<int, IList<int>> Selection {
70      get { return selection; }
71      set {
72        selection = value;
73        OnSelectionChanged();
74      }
75    }
76
77    protected PreprocessingData(PreprocessingData original, Cloner cloner)
78      : base(original, cloner) {
79      variableValues = CopyVariableValues(original.variableValues);
80      variableNames = new List<string>(original.variableNames);
81      TrainingPartition = (IntRange)original.TrainingPartition.Clone(cloner);
82      TestPartition = (IntRange)original.TestPartition.Clone(cloner);
83      Transformations = new List<ITransformation>(original.Transformations.Select(cloner.Clone));
84
85      InputVariables = new List<string>(original.InputVariables);
86      TargetVariable = original.TargetVariable;
87
88      RegisterEventHandler();
89    }
90
91    protected PreprocessingData(IDataAnalysisProblemData problemData)
92      : base() {
93      Name = "Preprocessing Data";
94
95      Transformations = new List<ITransformation>();
96      selection = new Dictionary<int, IList<int>>();
97
98      Import(problemData);
99
100      RegisterEventHandler();
101    }
102
103    public void Import(IDataAnalysisProblemData problemData) {
104      Dataset dataset = (Dataset)problemData.Dataset;
105      variableNames = new List<string>(problemData.Dataset.VariableNames);
106      InputVariables = new List<string>(problemData.AllowedInputVariables);
107      TargetVariable = (problemData is IRegressionProblemData) ? ((IRegressionProblemData)problemData).TargetVariable
108        : (problemData is IClassificationProblemData) ? ((IClassificationProblemData)problemData).TargetVariable
109        : null;
110
111      int columnIndex = 0;
112      variableValues = new List<IList>();
113      foreach (var variableName in problemData.Dataset.VariableNames) {
114        if (dataset.VariableHasType<double>(variableName)) {
115          variableValues.Insert(columnIndex, dataset.GetDoubleValues(variableName).ToList());
116        } else if (dataset.VariableHasType<string>(variableName)) {
117          variableValues.Insert(columnIndex, dataset.GetStringValues(variableName).ToList());
118        } else if (dataset.VariableHasType<DateTime>(variableName)) {
119          variableValues.Insert(columnIndex, dataset.GetDateTimeValues(variableName).ToList());
120        } else {
121          throw new ArgumentException("The datatype of column " + variableName + " must be of type double, string or DateTime");
122        }
123        ++columnIndex;
124      }
125
126      TrainingPartition = new IntRange(problemData.TrainingPartition.Start, problemData.TrainingPartition.End);
127      TestPartition = new IntRange(problemData.TestPartition.Start, problemData.TestPartition.End);
128    }
129
130    private void RegisterEventHandler() {
131      Changed += (s, e) => {
132        switch (e.Type) {
133          case DataPreprocessingChangedEventType.DeleteRow:
134            CheckPartitionRanges();
135            break;
136          case DataPreprocessingChangedEventType.Any:
137            CheckPartitionRanges();
138            break;
139          case DataPreprocessingChangedEventType.Transformation:
140            CheckPartitionRanges();
141            break;
142        }
143      };
144    }
145
146    private void CheckPartitionRanges() {
147      int maxRowIndex = Math.Max(0, Rows);
148      TrainingPartition.Start = Math.Min(TrainingPartition.Start, maxRowIndex);
149      TrainingPartition.End = Math.Min(TrainingPartition.End, maxRowIndex);
150      TestPartition.Start = Math.Min(TestPartition.Start, maxRowIndex);
151      TestPartition.End = Math.Min(TestPartition.End, maxRowIndex);
152    }
153
154    protected IList<IList> CopyVariableValues(IList<IList> original) {
155      var copy = new List<IList>(original);
156      for (int i = 0; i < original.Count; ++i) {
157        copy[i] = (IList)Activator.CreateInstance(original[i].GetType(), original[i]);
158      }
159      return copy;
160    }
161
162
163    #region IPreprocessingData Members
164    public abstract T GetCell<T>(int columnIndex, int rowIndex);
165
166    public abstract void SetCell<T>(int columnIndex, int rowIndex, T value);
167
168    public abstract string GetCellAsString(int columnIndex, int rowIndex);
169
170    public abstract string GetVariableName(int columnIndex);
171
172    public abstract int GetColumnIndex(string variableName);
173
174    public abstract bool VariableHasType<T>(int columnIndex);
175
176    [Obsolete("use the index based variant, is faster")]
177    public abstract IList<T> GetValues<T>(string variableName, bool considerSelection);
178
179    public abstract IList<T> GetValues<T>(int columnIndex, bool considerSelection);
180
181    public abstract void SetValues<T>(int columnIndex, IList<T> values);
182
183    public abstract bool SetValue(string value, int columnIndex, int rowIndex);
184
185    public abstract bool Validate(string value, out string errorMessage, int columnIndex);
186
187    public abstract bool AreAllStringColumns(IEnumerable<int> columnIndices);
188
189    public abstract void DeleteRowsWithIndices(IEnumerable<int> rows);
190
191    public abstract void InsertRow(int rowIndex);
192
193    public abstract void DeleteRow(int rowIndex);
194
195    public abstract void InsertColumn<T>(string variableName, int columnIndex);
196
197    public abstract void DeleteColumn(int columnIndex);
198
199    public abstract void RenameColumn(int columnIndex, string name);
200    public abstract void RenameColumns(IList<string> list);
201
202    public abstract Dataset ExportToDataset();
203
204    public abstract void ClearSelection();
205
206    public abstract event EventHandler SelectionChanged;
207    protected abstract void OnSelectionChanged();
208
209    public event DataPreprocessingChangedEventHandler Changed;
210    protected virtual void OnChanged(DataPreprocessingChangedEventType type, int column, int row) {
211      var listeners = Changed;
212      if (listeners != null) listeners(this, new DataPreprocessingChangedEventArgs(type, column, row));
213    }
214    #endregion
215  }
216}
Note: See TracBrowser for help on using the repository browser.