Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/11/14 12:36:39 (10 years ago)
Author:
mleitner
Message:

Refactoring

File:
1 edited

Legend:

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

    r10842 r10978  
    3333
    3434  [Item("PreprocessingData", "Represents data used for preprocessing.")]
    35   public class PreprocessingData : NamedItem, IPreprocessingData {
     35  public abstract class PreprocessingData : NamedItem, IPreprocessingData {
     36
     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    }
    3649
    3750    protected IList<IList> variableValues;
    38 
    3951    protected IList<string> variableNames;
    4052
    41     protected double trainingToTestRatio;
    42 
    43     protected IList<ITransformation> transformations;
    44 
    45     protected IDictionary<int, IList<int>> currentSelection;
     53    public IEnumerable<string> VariableNames {
     54      get { return variableNames; }
     55    }
     56
     57    public int Columns {
     58      get { return variableNames.Count; }
     59    }
     60
     61    public int Rows {
     62      get { return variableValues.Count > 0 ? variableValues[0].Count : 0; }
     63    }
     64
     65    protected IDictionary<int, IList<int>> selection;
     66    public IDictionary<int, IList<int>> Selection {
     67      get { return selection; }
     68      set {
     69          selection = value;
     70          OnSelectionChanged();
     71        }
     72      }   
    4673
    4774    protected PreprocessingData(PreprocessingData original, Cloner cloner)
     
    5380    }
    5481
    55     public PreprocessingData(IDataAnalysisProblemData problemData)
     82    protected PreprocessingData(IDataAnalysisProblemData problemData)
    5683      : base() {
    5784      Name = "Preprocessing Data";
    5885
    5986      transformations = new List<ITransformation>();
    60       currentSelection = new Dictionary<int, IList<int>>();
     87      selection = new Dictionary<int, IList<int>>();
    6188
    6289      variableNames = new List<string>(problemData.Dataset.VariableNames);
    63       // create dictionary from variable name to index
    6490
    6591      int columnIndex = 0;
     
    7399          variableValues.Insert(columnIndex, CreateColumn<DateTime>(problemData.Dataset, columnIndex, x => DateTime.Parse(x)));
    74100        } else {
    75           throw new ArgumentException("The datatype of column " + variableName + " must be of type List<double>, List<string> or List<DateTime>");
     101          throw new ArgumentException("The datatype of column " + variableName + " must be of type double, string or DateTime");
    76102        }
    77103        ++columnIndex;
     
    97123    }
    98124
    99     #region NamedItem abstract Member Implementations
    100 
    101     public override IDeepCloneable Clone(Cloner cloner) {
    102       return new PreprocessingData(this, cloner);
    103     }
    104 
    105     #endregion
    106125
    107126    #region IPreprocessingData Members
     
    111130    }
    112131
    113 
    114132    public virtual void SetCell<T>(int columnIndex, int rowIndex, T value) {
    115133      variableValues[columnIndex][rowIndex] = value;
    116134    }
    117135
    118 
    119136    public string GetCellAsString(int columnIndex, int rowIndex) {
    120137      return variableValues[columnIndex][rowIndex].ToString();
    121138    }
    122139
     140    public string GetVariableName(int columnIndex) {
     141      return variableNames[columnIndex];
     142    }
     143
     144    public int GetColumnIndex(string variableName) {
     145      return variableNames.IndexOf(variableName);
     146    }
     147
     148    public bool IsType<T>(int columnIndex) {
     149      return variableValues[columnIndex] is List<T>;
     150    }
    123151
    124152    [Obsolete("use the index based variant, is faster")]
     
    130158      if (considerSelection) {
    131159        var list = new List<T>();
    132         foreach (var rowIdx in currentSelection[columnIndex]) {
     160        foreach (var rowIdx in selection[columnIndex]) {
    133161          list.Add((T)variableValues[columnIndex][rowIdx]);
    134162        }
     
    170198    }
    171199
    172     public IntRange TrainingPartition {
    173       get { return new IntRange(0, (int)(Rows * trainingToTestRatio)); }
    174     }
    175 
    176     public IntRange TestPartition {
    177       get { return new IntRange((int)(Rows * trainingToTestRatio), Rows); }
    178     }
    179 
    180     public IList<ITransformation> Transformations {
    181       get { return transformations; }
    182     }
    183 
    184     public string GetVariableName(int columnIndex) {
    185       return variableNames[columnIndex];
    186     }
    187 
    188     public IEnumerable<string> VariableNames {
    189       get { return variableNames; }
    190     }
    191 
    192     public int GetColumnIndex(string variableName) {
    193       return variableNames.IndexOf(variableName);
    194     }
    195 
    196     public bool IsType<T>(int columnIndex) {
    197       return variableValues[columnIndex] is List<T>;
    198     }
    199 
    200     public int Columns {
    201       get { return variableNames.Count; }
    202     }
    203 
    204     public int Rows {
    205       get { return variableValues.Count > 0 ? variableValues[0].Count : 0; }
    206     }
    207 
    208200    public Dataset ExportToDataset() {
    209201      IList<IList> values = new List<IList>();
     
    217209    }
    218210
    219     public void SetSelection(IDictionary<int, IList<int>> selection) {
    220       currentSelection = selection;
    221       if (SelectionChanged != null) {
    222         SelectionChanged(this, new EventArgs());
    223       }
    224     }
    225 
    226     public IDictionary<int, IList<int>> GetSelection() {
    227       return currentSelection;
    228     }
    229 
    230211    public void ClearSelection() {
    231       currentSelection = new Dictionary<int, IList<int>>();
     212      Selection = new Dictionary<int, IList<int>>();
    232213    }
    233214
    234215    public event EventHandler SelectionChanged;
    235 
     216    protected virtual void OnSelectionChanged() {
     217       var listeners = SelectionChanged;
     218       if (listeners != null) listeners(this, EventArgs.Empty);
     219    }
     220
     221    public event DataPreprocessingChangedEventHandler Changed;
     222    protected virtual void OnChanged(DataPreprocessingChangedEventType type, int column, int row) {
     223      var listeners = Changed;
     224      if (listeners != null) listeners(this, new DataPreprocessingChangedEventArgs(type, column, row));
     225    }
    236226    #endregion
    237227  }
Note: See TracChangeset for help on using the changeset viewer.