Free cookie consent management tool by TermsFeed Policy Generator

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

Refactoring

Location:
branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4
Files:
10 edited

Legend:

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

    r10973 r10978  
    140140    public DataRow CreateSelectedDataRow(string variableName, DataRowVisualProperties.DataRowChartType chartType) {
    141141     
    142       IDictionary<int,IList<int>> selection = preprocessingData.GetSelection();
     142      IDictionary<int,IList<int>> selection = preprocessingData.Selection;
    143143      int variableIndex = preprocessingData.GetColumnIndex(variableName);
    144144
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/Implementations/CorrelationMatrixContent.cs

    r10962 r10978  
    2626
    2727namespace HeuristicLab.DataPreprocessing {
    28 
    2928  [Item("Feature Correlation Matrix", "Represents the feature correlation matrix.")]
    3029  public class CorrelationMatrixContent : Item, IViewChartShortcut {
     30    public static new Image StaticItemImage {
     31      get { return HeuristicLab.Common.Resources.VSImageLibrary.Gradient; }
     32    }
     33
     34    private IPreprocessingContext Context { get; set; }
     35    private ITransactionalPreprocessingData PreprocessingData {
     36      get { return Context.Data; }
     37    }
    3138
    3239    public DataAnalysisProblemData ProblemData {
     
    3441        var creator = new ProblemDataCreator(Context);
    3542        return (DataAnalysisProblemData)creator.CreateProblemData();
    36       } 
     43      }
    3744    }
    3845
    39     public ITransactionalPreprocessingData PreprocessingData {
    40       get {
    41         return Context.Data;
    42       }
    43     }
    44 
    45     private IPreprocessingContext Context { get; set; }
    4646    public CorrelationMatrixContent(IPreprocessingContext context) {
    4747      Context = context;
    48 
    49 
    5048    }
    5149
    5250    public CorrelationMatrixContent(CorrelationMatrixContent original, Cloner cloner)
    5351      : base(original, cloner) {
    54 
    5552    }
    56 
    57     public static new Image StaticItemImage {
    58       get { return HeuristicLab.Common.Resources.VSImageLibrary.Gradient; }
    59     }
    60 
    6153    public override IDeepCloneable Clone(Cloner cloner) {
    6254      return new CorrelationMatrixContent(this, cloner);
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/Implementations/DataGridLogic.cs

    r10900 r10978  
    144144
    145145
    146 
    147146    public void SetSelection(IDictionary<int, IList<int>> selection) {
    148       preprocessingData.SetSelection(selection);
     147      preprocessingData.Selection = selection;
    149148    }
    150149
    151150    public IDictionary<int, IList<int>> GetSelection() {
    152       return preprocessingData.GetSelection();
     151      return preprocessingData.Selection;
    153152    }
    154153    public void ClearSelection() {
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/Implementations/FilteredPreprocessingData.cs

    r10939 r10978  
    1010namespace HeuristicLab.DataPreprocessing.Implementations {
    1111  public class FilteredPreprocessingData : NamedItem, IFilteredPreprocessingData {
    12     protected ITransactionalPreprocessingData originalData;
    13     protected ITransactionalPreprocessingData filteredData;
     12    private readonly ITransactionalPreprocessingData originalData;
     13    private ITransactionalPreprocessingData filteredData;
     14
     15     public IDictionary<int, IList<int>> Selection {
     16      get { return originalData.Selection; }
     17      set { originalData.Selection = value; }
     18    }
    1419
    1520    protected FilteredPreprocessingData(FilteredPreprocessingData original, Cloner cloner)
    1621      : base(original, cloner) {
    17       originalData = (ITransactionalPreprocessingData)original.originalData;
    18       filteredData = (ITransactionalPreprocessingData)original.filteredData;
     22      originalData = original.originalData;
     23      filteredData = original.filteredData;
    1924    }
    2025
     
    3439
    3540    public void SetCell<T>(int columnIndex, int rowIndex, T value) {
    36       ReadOnlyOnFilterData.SetCell<T>(columnIndex, rowIndex, value);
     41      if (IsFiltered)
     42        throw new InvalidOperationException("SetCell not possible while data is filtered");
     43
     44      originalData.SetCell<T>(columnIndex, rowIndex, value);
    3745    }
    3846
     
    5058
    5159    public void SetValues<T>(int columnIndex, IList<T> values) {
    52       ReadOnlyOnFilterData.SetValues<T>(columnIndex, values);
     60      if (IsFiltered)
     61        throw new InvalidOperationException("SetValues not possible while data is filtered");
     62
     63      originalData.SetValues<T>(columnIndex, values);
    5364    }
    5465
    5566    public void InsertRow(int rowIndex) {
    56       ReadOnlyOnFilterData.InsertRow(rowIndex);
     67      if (IsFiltered)
     68        throw new InvalidOperationException("InsertRow not possible while data is filtered");
     69
     70      originalData.InsertRow(rowIndex);
    5771    }
    5872
    5973    public void DeleteRow(int rowIndex) {
    60       ReadOnlyOnFilterData.DeleteRow(rowIndex);
     74      if (IsFiltered)
     75        throw new InvalidOperationException("DeleteRow not possible while data is filtered");
     76
     77      originalData.DeleteRow(rowIndex);
    6178    }
    6279
    6380    public void InsertColumn<T>(string variableName, int columnIndex) {
    64       ReadOnlyOnFilterData.InsertColumn<T>(variableName, columnIndex);
     81      if (IsFiltered)
     82        throw new InvalidOperationException("InsertColumn not possible while data is filtered");
     83
     84      originalData.InsertColumn<T>(variableName, columnIndex);
    6585    }
    6686
    6787    public void DeleteColumn(int columnIndex) {
    68       ReadOnlyOnFilterData.DeleteColumn(columnIndex);
     88      if (IsFiltered)
     89        throw new InvalidOperationException("DeleteColumn not possible while data is filtered");
     90      originalData.DeleteColumn(columnIndex);
    6991    }
    7092
     
    153175    }
    154176
    155     public ITransactionalPreprocessingData ReadOnlyOnFilterData {
    156       get {
    157         if (IsFiltered)
    158           throw new InvalidOperationException();
    159 
    160         return originalData;
    161       }
    162     }
     177
     178    public bool IsUndoAvailable {
     179      get { return IsFiltered ? false : originalData.IsUndoAvailable; }
     180    }
     181
    163182
    164183    public bool IsFiltered {
     
    171190    }
    172191
    173     public bool IsUndoAvailable {
    174       get { return IsFiltered ? false : originalData.IsUndoAvailable; }
    175     }
    176192
    177193    public void Undo() {
    178       ReadOnlyOnFilterData.Undo();
     194      if (IsFiltered)
     195        throw new InvalidOperationException("Undo not possible while data is filtered");
     196
     197      originalData.Undo();
    179198    }
    180199
    181200    public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) {
    182       ReadOnlyOnFilterData.InTransaction(action, type);
     201      if (IsFiltered)
     202        throw new InvalidOperationException("Transaction not possible while data is filtered");
     203      originalData.InTransaction(action, type);
    183204    }
    184205
    185206    public void BeginTransaction(DataPreprocessingChangedEventType type) {
    186       ReadOnlyOnFilterData.BeginTransaction(type);
     207      if (IsFiltered)
     208        throw new InvalidOperationException("Transaction not possible while data is filtered");
     209      originalData.BeginTransaction(type);
    187210    }
    188211
     
    194217
    195218    #region IPreprocessingData Members
    196 
    197 
    198     public void SetSelection(IDictionary<int, IList<int>> selection) {
    199       originalData.SetSelection(selection);
    200     }
    201 
    202     public IDictionary<int, IList<int>> GetSelection() {
    203       return originalData.GetSelection();
    204     }
    205219
    206220    public void ClearSelection() {
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/Implementations/PreprocessingContext.cs

    r10786 r10978  
    3434
    3535    public IFilteredPreprocessingData Data { get; private set; }
    36 
    37     public IDataAnalysisProblemData DataAnalysisProblemData { get; private set; }
    38 
    3936    public IAlgorithm Algorithm { get; private set; }
    40 
    4137    public IDataAnalysisProblem Problem { get; private set; }
    4238
    4339    public PreprocessingContext(IDataAnalysisProblemData dataAnalysisProblemData, IAlgorithm algorithm, IDataAnalysisProblem problem) {
    44 
    4540      TransactionalPreprocessingData transactionalPreprocessingData = new TransactionalPreprocessingData(dataAnalysisProblemData);
    4641
    4742      Data = new FilteredPreprocessingData(transactionalPreprocessingData);
    48       DataAnalysisProblemData = dataAnalysisProblemData;
    4943      Algorithm = algorithm;
    5044      Problem = problem;
     
    5448      : base(original, cloner) {
    5549      Data = cloner.Clone(original.Data);
    56       DataAnalysisProblemData = original.DataAnalysisProblemData;
    5750      Algorithm = original.Algorithm;
    5851      Problem = original.Problem;
  • 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  }
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/Implementations/SearchLogic.cs

    r10817 r10978  
    114114    {
    115115      if (considerSelection) {     
    116         var selectedRows =  preprocessingData.GetSelection()[columnIndex];
     116        var selectedRows =  preprocessingData.Selection[columnIndex];
    117117       
    118118        List<T> values = new List<T>();
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/Implementations/TransactionalPreprocessingData.cs

    r10814 r10978  
    3030namespace HeuristicLab.DataPreprocessing {
    3131
    32   internal class Snapshot {
    33     public IList<IList> VariableValues { get; set; }
    34 
    35     public IList<string> VariableNames { get; set; }
    36 
    37     public double TrainingToTestRatio { get; set; }
    38 
    39     public IList<ITransformation> Transformations { get; set; }
    40 
    41     public DataPreprocessingChangedEventType ChangedType { get; set; }
    42 
    43     public int ChangedColumn { get; set; }
    44 
    45     public int ChangedRow { get; set; }
    46   }
    47 
    4832  [Item("PreprocessingData", "Represents data used for preprocessing.")]
    4933  public class TransactionalPreprocessingData : PreprocessingData, ITransactionalPreprocessingData {
    5034
     35    private class Snapshot {
     36      public IList<IList> VariableValues { get; set; }
     37      public IList<string> VariableNames { get; set; }
     38
     39      public double TrainingToTestRatio { get; set; }
     40      public IList<ITransformation> Transformations { get; set; }
     41      public DataPreprocessingChangedEventType ChangedType { get; set; }
     42
     43      public int ChangedColumn { get; set; }
     44      public int ChangedRow { get; set; }
     45    }
     46
    5147    private const int MAX_UNDO_DEPTH = 5;
    5248
    53     private IList<Snapshot> undoHistory;
    54 
    55     private Stack<DataPreprocessingChangedEventType> eventStack = new Stack<DataPreprocessingChangedEventType>();
    56 
    57     private DataPreprocessingChangedEventType lastOccuredChangedType = DataPreprocessingChangedEventType.Any;
     49    private readonly IList<Snapshot> undoHistory = new List<Snapshot>();
     50    private readonly Stack<DataPreprocessingChangedEventType> eventStack = new Stack<DataPreprocessingChangedEventType>();
    5851
    5952    public bool IsInTransaction { get { return eventStack.Count > 0; } }
     
    6154    public TransactionalPreprocessingData(IDataAnalysisProblemData problemData)
    6255      : base(problemData) {
    63       undoHistory = new List<Snapshot>();
    6456    }
    6557
    6658    private TransactionalPreprocessingData(TransactionalPreprocessingData original, Cloner cloner)
    6759      : base(original, cloner) {
    68       undoHistory = new List<Snapshot>();
    69     }
     60     }
    7061
    7162    private void SaveSnapshot(DataPreprocessingChangedEventType changedType, int column, int row) {
    72       if (eventStack.Count > 0) return;
     63      if (IsInTransaction) return;
    7364
    7465      var currentSnapshot = new Snapshot {
     
    144135    #region TransactionalPreprocessingData members
    145136
    146     public event DataPreprocessingChangedEventHandler Changed;
    147     protected virtual void OnChanged(DataPreprocessingChangedEventType type, int column, int row) {
    148       var listeners = Changed;
    149       if (listeners != null) listeners(this, new DataPreprocessingChangedEventArgs(type, column, row));
    150     }
    151 
    152137    public bool IsUndoAvailable {
    153138      get { return undoHistory.Count > 0; }
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/Interfaces/IPreprocessingContext.cs

    r10783 r10978  
    2727
    2828namespace HeuristicLab.DataPreprocessing {
    29   public interface IPreprocessingContext
    30     : IItem {
     29  public interface IPreprocessingContext : IItem {
    3130
    3231    IFilteredPreprocessingData Data { get; }
    33 
    34     [Obsolete]
    35     IDataAnalysisProblemData DataAnalysisProblemData { get; }
    36 
    3732    IAlgorithm Algorithm { get; }
    38 
    3933    IDataAnalysisProblem Problem { get; }
    4034
    4135    IItem ExportAlgorithmOrProblem();
    42 
    4336    IAlgorithm ExportAlgorithm();
    44 
    4537    IProblem ExportProblem();
    4638  }
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/Interfaces/IPreprocessingData.cs

    r10811 r10978  
    6565    Dataset ExportToDataset();
    6666
    67     void SetSelection(IDictionary<int, IList<int>> selection);
    68     IDictionary<int, IList<int>> GetSelection();
     67    IDictionary<int, IList<int>> Selection { get; set; }
    6968    void ClearSelection();
    7069
Note: See TracChangeset for help on using the changeset viewer.