Changeset 10185


Ignore:
Timestamp:
12/04/13 13:51:07 (6 years ago)
Author:
pfleck
Message:

Implemented constructors of PreprocessingData.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/PreprocessingData.cs

    r10182 r10185  
    2323using System.Collections;
    2424using System.Collections.Generic;
     25using System.Linq;
     26using HeuristicLab.Common;
    2527using HeuristicLab.Core;
    2628using HeuristicLab.DataPreprocessing;
     
    3133  public class PreprocessingData : NamedItem, IPreprocessingData {
    3234
    33     private Dictionary<string, IList> variableValues;
     35    private IDictionary<string, IList> variableValues;
    3436
     37    private IDictionary<string, int> variableNameIndices;
     38
     39    private double trainingToTestRatio;
     40
     41    private PreprocessingData(PreprocessingData original, Cloner cloner)
     42      : base(original, cloner) {
     43      variableValues = new Dictionary<string, IList>(variableValues);
     44      variableNameIndices = new Dictionary<string, int>(variableNameIndices);
     45    }
     46   
    3547    public PreprocessingData(IDataAnalysisProblemData problemData)
    3648      : base() {
    3749      Name = "-";
    38       foreach (var s in problemData.Dataset.VariableNames) {
    39         if (problemData.Dataset.IsType<double>(s)) {
    4050
    41         } else if (problemData.Dataset.IsType<string>(s)) {
    42 
    43         } else if (problemData.Dataset.IsType<DateTime>(s)) {
    44 
     51      // create dictionary from variable name to index
     52      variableNameIndices = new Dictionary<string, int>();
     53      var variableNamesList = problemData.Dataset.VariableNames.ToList();
     54      for (int i = 0; i < variableNamesList.Count; i++) {
     55        variableNameIndices.Add(variableNamesList[i], i);
     56      }
     57     
     58      // copy values
     59      variableValues = new Dictionary<string, IList>();
     60      foreach (var variableName in problemData.Dataset.VariableNames) {
     61        if (problemData.Dataset.IsType<double>(variableName)) {
     62          variableValues[variableName] = problemData.Dataset.GetDoubleValues(variableName).ToList();
     63        } else if (problemData.Dataset.IsType<string>(variableName)) {
     64          variableValues[variableName] = CreateColumn<string>(problemData.Dataset, variableNameIndices[variableName], x => x);
     65        } else if (problemData.Dataset.IsType<DateTime>(variableName)) {
     66          variableValues[variableName] = CreateColumn<DateTime>(problemData.Dataset, variableNameIndices[variableName], x => DateTime.Parse(x));
    4567        } else {
    46           throw new ArgumentException("The datatype of column " + s + "is of TODO");
     68          throw new ArgumentException("The datatype of column " + variableName + " must be of type List<double>, List<string> or List<DateTime>");
    4769        }
    4870      }
    49       throw new NotImplementedException();
     71
     72      trainingToTestRatio = (double)problemData.TrainingPartition.Size / problemData.TestPartition.Size;
     73    }
     74
     75    private static IList CreateColumn<T>(Dataset ds, int column, Func<string, T> selector) {
     76      var list = new List<T>(ds.Rows);
     77      for (int row = 0; row < ds.Rows; row++) {
     78        list[row] = selector(ds.GetValue(row, column));
     79      }
     80      return list;
    5081    }
    5182
    5283    #region NamedItem abstract Member Implementations
    5384
    54     public override Common.IDeepCloneable Clone(Common.Cloner cloner) {
    55       throw new NotImplementedException();
     85    public override IDeepCloneable Clone(Cloner cloner) {
     86      return new PreprocessingData(this, cloner);
    5687    }
    5788
Note: See TracChangeset for help on using the changeset viewer.