Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.DataPreprocessing/3.4/Implementations/PreprocessingData.cs @ 13881

Last change on this file since 13881 was 13881, checked in by mkommend, 8 years ago

#2536: Merged r13427 and r13539 into stable.

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