Free cookie consent management tool by TermsFeed Policy Generator

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

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