Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10811 was 10809, checked in by sbreuer, 11 years ago
  • selected average and co. implemented
  • SelectionChanged NullPointer fixed
File size: 7.9 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.")]
35  public class PreprocessingData : NamedItem, IPreprocessingData {
36
[10740]37    protected IList<IList> variableValues;
[10550]38
[10586]39    protected IList<string> variableNames;
[10168]40
[10586]41    protected double trainingToTestRatio;
[10186]42
[10695]43    protected IList<ITransformation> transformations;
44
[10804]45    protected IDictionary<int, IList<int>> currentSelection;
46
[10586]47    protected PreprocessingData(PreprocessingData original, Cloner cloner)
[10185]48      : base(original, cloner) {
[10550]49      variableValues = CopyVariableValues(original.variableValues);
[10548]50      variableNames = new List<string>(original.variableNames);
51      trainingToTestRatio = original.trainingToTestRatio;
[10185]52    }
[10187]53
[10168]54    public PreprocessingData(IDataAnalysisProblemData problemData)
55      : base() {
[10786]56      Name = "Preprocessing Data";
[10168]57
[10786]58      transformations = new List<ITransformation>();
[10804]59      currentSelection = new Dictionary<int, IList<int>>();
[10786]60
[10187]61      variableNames = new List<string>(problemData.Dataset.VariableNames);
[10185]62      // create dictionary from variable name to index
[10187]63
[10367]64      int columnIndex = 0;
[10740]65      variableValues = new List<IList>();
[10185]66      foreach (var variableName in problemData.Dataset.VariableNames) {
67        if (problemData.Dataset.IsType<double>(variableName)) {
[10740]68          variableValues.Insert(columnIndex, problemData.Dataset.GetDoubleValues(variableName).ToList());
[10185]69        } else if (problemData.Dataset.IsType<string>(variableName)) {
[10740]70          variableValues.Insert(columnIndex, CreateColumn<string>(problemData.Dataset, columnIndex, x => x));
[10185]71        } else if (problemData.Dataset.IsType<DateTime>(variableName)) {
[10740]72          variableValues.Insert(columnIndex, CreateColumn<DateTime>(problemData.Dataset, columnIndex, x => DateTime.Parse(x)));
[10168]73        } else {
[10185]74          throw new ArgumentException("The datatype of column " + variableName + " must be of type List<double>, List<string> or List<DateTime>");
[10168]75        }
[10367]76        ++columnIndex;
[10168]77      }
[10185]78
[10235]79      trainingToTestRatio = (double)problemData.TrainingPartition.Size / Math.Max(problemData.Dataset.Rows, double.Epsilon);
[10163]80    }
81
[10185]82    private static IList CreateColumn<T>(Dataset ds, int column, Func<string, T> selector) {
83      var list = new List<T>(ds.Rows);
[10341]84      for (int row = 0; row < ds.Rows; ++row) {
[10367]85        list.Add(selector(ds.GetValue(row, column)));
[10185]86      }
87      return list;
88    }
89
[10740]90    protected IList<IList> CopyVariableValues(IList<IList> original) {
[10783]91      var copy = new List<IList>(original);
[10740]92      for (int i = 0; i < original.Count; ++i) {
[10783]93        copy[i] = (IList)Activator.CreateInstance(original[i].GetType(), original[i]);
[10550]94      }
95      return copy;
96    }
97
[10163]98    #region NamedItem abstract Member Implementations
99
[10185]100    public override IDeepCloneable Clone(Cloner cloner) {
101      return new PreprocessingData(this, cloner);
[10163]102    }
103
104    #endregion
105
106    #region IPreprocessingData Members
107
[10367]108    public T GetCell<T>(int columnIndex, int rowIndex) {
109      return (T)variableValues[columnIndex][rowIndex];
[10181]110    }
111
[10236]112
[10586]113    public virtual void SetCell<T>(int columnIndex, int rowIndex, T value) {
[10367]114      variableValues[columnIndex][rowIndex] = value;
115    }
116
117
118    public string GetCellAsString(int columnIndex, int rowIndex) {
119      return variableValues[columnIndex][rowIndex].ToString();
120    }
121
[10547]122
[10367]123    [Obsolete("use the index based variant, is faster")]
[10809]124    public IList<T> GetValues<T>(string variableName, bool considerSelection) {
125      return GetValues<T>(GetColumnIndex(variableName), considerSelection);
[10181]126    }
127
[10809]128    public IList<T> GetValues<T>(int columnIndex, bool considerSelection) {
129      if (considerSelection) {
130        var list = new List<T>();
131        foreach (var rowIdx in currentSelection[columnIndex]) {
132          list.Add((T)variableValues[columnIndex][rowIdx]);
133        }
134        return list;
135      } else {
136        return (IList<T>)variableValues[columnIndex];
137      }
[10367]138    }
139
[10586]140    public virtual void SetValues<T>(int columnIndex, IList<T> values) {
[10367]141      if (IsType<T>(columnIndex)) {
142        variableValues[columnIndex] = (IList)values;
143      } else {
144        throw new ArgumentException("The datatype of column " + columnIndex + " must be of type " + variableValues[columnIndex].GetType().Name + " but was " + typeof(T).Name);
[10311]145      }
[10181]146    }
147
[10586]148    public virtual void InsertRow(int rowIndex) {
[10740]149      foreach (IList column in variableValues) {
[10194]150        Type type = column.GetType().GetGenericArguments()[0];
151        column.Insert(rowIndex, type.IsValueType ? Activator.CreateInstance(type) : null);
152      }
[10163]153    }
154
[10586]155    public virtual void DeleteRow(int rowIndex) {
[10740]156      foreach (IList column in variableValues) {
[10194]157        column.RemoveAt(rowIndex);
158      }
[10163]159    }
160
[10586]161    public virtual void InsertColumn<T>(string variableName, int columnIndex) {
[10740]162      variableValues.Insert(columnIndex, new List<T>(Rows));
[10547]163      variableNames.Insert(columnIndex, variableName);
[10163]164    }
165
[10586]166    public virtual void DeleteColumn(int columnIndex) {
[10740]167      variableValues.RemoveAt(columnIndex);
[10367]168      variableNames.RemoveAt(columnIndex);
169    }
170
[10221]171    public IntRange TrainingPartition {
172      get { return new IntRange(0, (int)(Rows * trainingToTestRatio)); }
173    }
174
175    public IntRange TestPartition {
[10235]176      get { return new IntRange((int)(Rows * trainingToTestRatio), Rows); }
[10221]177    }
178
[10695]179    public IList<ITransformation> Transformations {
180      get { return transformations; }
181    }
182
[10547]183    public string GetVariableName(int columnIndex) {
184      return variableNames[columnIndex];
185    }
186
[10243]187    public IEnumerable<string> VariableNames {
[10188]188      get { return variableNames; }
[10163]189    }
190
[10367]191    public int GetColumnIndex(string variableName) {
192      return variableNames.IndexOf(variableName);
193    }
[10243]194
[10367]195    public bool IsType<T>(int columnIndex) {
196      return variableValues[columnIndex] is List<T>;
197    }
[10181]198
[10163]199    public int Columns {
[10194]200      get { return variableNames.Count; }
[10163]201    }
202
203    public int Rows {
[10367]204      get { return variableValues.Count > 0 ? variableValues[0].Count : 0; }
[10163]205    }
[10189]206
[10220]207    public Dataset ExportToDataset() {
208      IList<IList> values = new List<IList>();
[10367]209
210      for (int i = 0; i < Columns; ++i) {
211        values.Add(variableValues[i]);
[10220]212      }
213
214      var dataset = new Dataset(variableNames, values);
215      return dataset;
216    }
217
[10804]218    public void SetSelection(IDictionary<int, IList<int>> selection) {
219      currentSelection = selection;
220      if (SelectionChanged != null) {
221        SelectionChanged(this, new EventArgs());
222      }
223    }
224
225    public IDictionary<int, IList<int>> GetSelection() {
226      return currentSelection;
227    }
228
229    public void ClearSelection() {
230      currentSelection = new Dictionary<int, IList<int>>();
231    }
232
233    public event EventHandler SelectionChanged;
234
[10220]235    #endregion
[10163]236  }
237}
Note: See TracBrowser for help on using the repository browser.