Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/20/18 13:52:40 (5 years ago)
Author:
pfleck
Message:

#2845 reverted the last merge (r16307) because some revisions were missing

Location:
branches/2845_EnhancedProgress
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/2845_EnhancedProgress

  • branches/2845_EnhancedProgress/HeuristicLab.DataPreprocessing

  • branches/2845_EnhancedProgress/HeuristicLab.DataPreprocessing/3.4

  • branches/2845_EnhancedProgress/HeuristicLab.DataPreprocessing/3.4/Data/FilteredPreprocessingData.cs

    r16307 r16308  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Linq;
    2524using HeuristicLab.Common;
    2625using HeuristicLab.Core;
    2726using HeuristicLab.Data;
    28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2927using HeuristicLab.Problems.DataAnalysis;
    3028
    3129namespace HeuristicLab.DataPreprocessing {
    32   [Item("FilteredPreprocessingData", "Represents filtered data used for preprocessing.")]
    33   [StorableClass]
    34   public sealed class FilteredPreprocessingData : NamedItem, IFilteredPreprocessingData {
    35 
    36     [Storable]
    37     private readonly IPreprocessingData originalData;
    38     [Storable]
    39     private IPreprocessingData filteredData;
    40 
    41     public IPreprocessingData ActiveData {
     30  public class FilteredPreprocessingData : NamedItem, IFilteredPreprocessingData {
     31    private readonly ITransactionalPreprocessingData originalData;
     32    private ITransactionalPreprocessingData filteredData;
     33
     34    public IntRange TrainingPartition {
     35      get { return originalData.TrainingPartition; }
     36    }
     37
     38    public IntRange TestPartition {
     39      get { return originalData.TestPartition; }
     40    }
     41
     42    public IList<ITransformation> Transformations {
     43      get { return originalData.Transformations; }
     44    }
     45
     46    public IEnumerable<string> VariableNames {
     47      get { return ActiveData.VariableNames; }
     48    }
     49
     50    public IList<string> InputVariables { get { return ActiveData.InputVariables; } }
     51    public string TargetVariable { get { return ActiveData.TargetVariable; } } // optional
     52
     53    public IDictionary<int, IList<int>> Selection {
     54      get { return originalData.Selection; }
     55      set { originalData.Selection = value; }
     56    }
     57
     58    public int Columns {
     59      get { return ActiveData.Columns; }
     60    }
     61
     62    public int Rows {
     63      get { return ActiveData.Rows; }
     64    }
     65
     66    public ITransactionalPreprocessingData ActiveData {
    4267      get { return IsFiltered ? filteredData : originalData; }
    4368    }
    4469
    45     #region Constructor, Cloning & Persistence
    46     public FilteredPreprocessingData(IPreprocessingData preprocessingData)
     70    public bool IsUndoAvailable {
     71      get { return IsFiltered ? false : originalData.IsUndoAvailable; }
     72    }
     73
     74    public bool IsFiltered {
     75      get { return filteredData != null; }
     76    }
     77
     78
     79    public FilteredPreprocessingData(ITransactionalPreprocessingData preporcessingData)
    4780      : base() {
    48       originalData = preprocessingData;
     81      originalData = preporcessingData;
    4982      filteredData = null;
    5083    }
    5184
    52     private FilteredPreprocessingData(FilteredPreprocessingData original, Cloner cloner)
     85    protected FilteredPreprocessingData(FilteredPreprocessingData original, Cloner cloner)
    5386      : base(original, cloner) {
    5487      originalData = original.originalData;
     
    5992    }
    6093
    61     [StorableConstructor]
    62     private FilteredPreprocessingData(bool deserializing)
    63       : base(deserializing) { }
    64     #endregion
    65 
    66     #region Cells
    67     public bool IsCellEmpty(int columnIndex, int rowIndex) {
    68       return ActiveData.IsCellEmpty(columnIndex, rowIndex);
    69     }
    70 
    7194    public T GetCell<T>(int columnIndex, int rowIndex) {
    7295      return ActiveData.GetCell<T>(columnIndex, rowIndex);
     
    94117    }
    95118
    96     public bool SetValue(string value, int columnIndex, int rowIndex) {
    97       if (IsFiltered)
    98         throw new InvalidOperationException("SetValue not possible while data is filtered");
    99       return originalData.SetValue(value, columnIndex, rowIndex);
    100     }
    101 
    102     public int Columns {
    103       get { return ActiveData.Columns; }
    104     }
    105 
    106     public int Rows {
    107       get { return ActiveData.Rows; }
    108     }
    109     #endregion
    110 
    111     #region Rows
    112119    public void InsertRow(int rowIndex) {
    113120      if (IsFiltered)
     
    122129
    123130      originalData.DeleteRow(rowIndex);
    124     }
    125 
    126     public void DeleteRowsWithIndices(IEnumerable<int> rows) {
    127       if (IsFiltered)
    128         throw new InvalidOperationException("DeleteRowsWithIndices not possible while data is filtered");
    129 
    130       originalData.DeleteRowsWithIndices(rows);
    131131    }
    132132
     
    156156    }
    157157
    158     public bool AreAllStringColumns(IEnumerable<int> columnIndices) {
    159       return originalData.AreAllStringColumns(columnIndices);
    160     }
    161     #endregion
    162 
    163     #region Variables
    164     public IEnumerable<string> VariableNames {
    165       get { return ActiveData.VariableNames; }
    166     }
    167     public IEnumerable<string> GetDoubleVariableNames() {
    168       return originalData.GetDoubleVariableNames();
    169     }
    170158    public string GetVariableName(int columnIndex) {
    171159      return ActiveData.GetVariableName(columnIndex);
     
    180168    }
    181169
    182     public Type GetVariableType(int columnIndex) {
    183       return ActiveData.GetVariableType(columnIndex);
    184     }
    185 
    186     public IList<string> InputVariables {
    187       get { return ActiveData.InputVariables; }
    188     }
    189 
    190     public string TargetVariable {
    191       get { return ActiveData.TargetVariable; }
    192     } // optional
    193     #endregion
    194 
    195     #region Partitions
    196     public IntRange TrainingPartition {
    197       get { return originalData.TrainingPartition; }
    198     }
    199 
    200     public IntRange TestPartition {
    201       get { return originalData.TestPartition; }
    202     }
    203     #endregion
    204 
    205     #region Transformations
    206     public IList<ITransformation> Transformations {
    207       get { return originalData.Transformations; }
    208     }
    209     #endregion
    210 
    211     #region Validation
    212     public bool Validate(string value, out string errorMessage, int columnIndex) {
    213       return originalData.Validate(value, out errorMessage, columnIndex);
    214     }
    215     #endregion
    216 
    217     #region Import & Export
    218     public void Import(IDataAnalysisProblemData problemData) {
    219       if (IsFiltered)
    220         throw new InvalidOperationException("Import not possible while data is filtered");
    221       originalData.Import(problemData);
    222     }
    223 
    224170    public Dataset ExportToDataset() {
    225171      return originalData.ExportToDataset();
    226172    }
    227     #endregion
    228 
    229     #region Selection
    230     public IDictionary<int, IList<int>> Selection {
    231       get { return originalData.Selection; }
    232       set { originalData.Selection = value; }
    233     }
    234 
    235     public void ClearSelection() {
    236       originalData.ClearSelection();
    237     }
    238 
    239     public event EventHandler SelectionChanged {
    240       add { originalData.SelectionChanged += value; }
    241       remove { originalData.SelectionChanged -= value; }
    242     }
    243     #endregion
    244 
    245     #region Transactions
    246     public event DataPreprocessingChangedEventHandler Changed {
    247       add { originalData.Changed += value; }
    248       remove { originalData.Changed -= value; }
    249     }
    250 
    251     public bool IsUndoAvailable {
    252       get { return IsFiltered ? false : originalData.IsUndoAvailable; }
    253     }
    254 
    255     public void Undo() {
    256       if (IsFiltered)
    257         throw new InvalidOperationException("Undo not possible while data is filtered");
    258 
    259       originalData.Undo();
    260     }
    261 
    262     public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) {
    263       if (IsFiltered)
    264         throw new InvalidOperationException("Transaction not possible while data is filtered");
    265       originalData.InTransaction(action, type);
    266     }
    267 
    268     public void BeginTransaction(DataPreprocessingChangedEventType type) {
    269       if (IsFiltered)
    270         throw new InvalidOperationException("Transaction not possible while data is filtered");
    271       originalData.BeginTransaction(type);
    272     }
    273 
    274     public void EndTransaction() {
    275       originalData.EndTransaction();
    276     }
    277     #endregion
    278 
    279     #region Statistics
    280     public T GetMin<T>(int columnIndex, bool considerSelection = false, T emptyValue = default(T)) {
    281       return ActiveData.GetMin<T>(columnIndex, considerSelection, emptyValue);
    282     }
    283     public T GetMax<T>(int columnIndex, bool considerSelection = false, T emptyValue = default(T)) {
    284       return ActiveData.GetMax<T>(columnIndex, considerSelection, emptyValue);
    285     }
    286     public T GetMean<T>(int columnIndex, bool considerSelection = false, T emptyValue = default(T)) {
    287       return ActiveData.GetMean<T>(columnIndex, considerSelection, emptyValue);
    288     }
    289     public T GetMedian<T>(int columnIndex, bool considerSelection = false, T emptyValue = default(T)) where T : IComparable<T> {
    290       return ActiveData.GetMedian<T>(columnIndex, considerSelection, emptyValue);
    291     }
    292     public T GetMode<T>(int columnIndex, bool considerSelection = false, T emptyValue = default(T)) where T : IEquatable<T> {
    293       return ActiveData.GetMode<T>(columnIndex, considerSelection, emptyValue);
    294     }
    295     public T GetStandardDeviation<T>(int columnIndex, bool considerSelection = false, T emptyValue = default(T)) {
    296       return ActiveData.GetStandardDeviation<T>(columnIndex, considerSelection, emptyValue);
    297     }
    298     public T GetVariance<T>(int columnIndex, bool considerSelection = false, T emptyValue = default(T)) {
    299       return ActiveData.GetVariance<T>(columnIndex, considerSelection, emptyValue);
    300     }
    301     public T GetQuantile<T>(double alpha, int columnIndex, bool considerSelection = false, T emptyValue = default(T)) where T : IComparable<T> {
    302       return ActiveData.GetQuantile<T>(alpha, columnIndex, considerSelection, emptyValue);
    303     }
    304     public int GetDistinctValues<T>(int columnIndex, bool considerSelection = false) {
    305       return ActiveData.GetDistinctValues<T>(columnIndex, considerSelection);
    306     }
    307 
    308     public int GetMissingValueCount() {
    309       return ActiveData.GetMissingValueCount();
    310     }
    311     public int GetMissingValueCount(int columnIndex) {
    312       return ActiveData.GetMissingValueCount(columnIndex);
    313     }
    314     public int GetRowMissingValueCount(int rowIndex) {
    315       return ActiveData.GetRowMissingValueCount(rowIndex);
    316     }
    317     #endregion
    318 
    319     #region Filters
    320     public void SetFilter(bool[] remainingRows) {
    321       filteredData = (IPreprocessingData)originalData.Clone();
     173
     174    public void SetFilter(bool[] rowFilters) {
     175      filteredData = (ITransactionalPreprocessingData)originalData.Clone();
    322176      filteredData.InTransaction(() => {
    323         var remainingIndices = Enumerable.Range(0, remainingRows.Length).Where(x => remainingRows[x]);
    324 
    325         foreach (var v in filteredData.VariableNames) {
    326           var ci = filteredData.GetColumnIndex(v);
    327           if (filteredData.VariableHasType<double>(ci)) {
    328             var values = filteredData.GetValues<double>(ci);
    329             var filteredValues = remainingIndices.Select(x => values[x]).ToList();
    330             filteredData.SetValues(ci, filteredValues);
    331           } else if (filteredData.VariableHasType<DateTime>(ci)) {
    332             var values = filteredData.GetValues<DateTime>(ci);
    333             var filteredValues = remainingIndices.Select(x => values[x]).ToList();
    334             filteredData.SetValues(ci, filteredValues);
    335           } else if (filteredData.VariableHasType<string>(ci)) {
    336             var values = filteredData.GetValues<string>(ci);
    337             var filteredValues = remainingIndices.Select(x => values[x]).ToList();
    338             filteredData.SetValues(ci, filteredValues);
     177        for (int row = (rowFilters.Length - 1); row >= 0; --row) {
     178          if (rowFilters[row]) {
     179            filteredData.DeleteRow(row);
    339180          }
    340181        }
     
    365206    }
    366207
    367     public bool IsFiltered {
    368       get { return filteredData != null; }
    369     }
    370 
    371     public event EventHandler FilterChanged;
    372 
    373208    private void OnFilterChanged() {
    374209      if (FilterChanged != null) {
     
    376211      }
    377212    }
     213
     214    public event DataPreprocessingChangedEventHandler Changed {
     215      add { originalData.Changed += value; }
     216      remove { originalData.Changed -= value; }
     217    }
     218
     219    public bool SetValue(string value, int columnIndex, int rowIndex) {
     220      if (IsFiltered)
     221        throw new InvalidOperationException("SetValue not possible while data is filtered");
     222      return originalData.SetValue(value, columnIndex, rowIndex);
     223    }
     224
     225    public bool AreAllStringColumns(IEnumerable<int> columnIndices) {
     226      return originalData.AreAllStringColumns(columnIndices);
     227    }
     228
     229    public void DeleteRowsWithIndices(IEnumerable<int> rows) {
     230      if (IsFiltered)
     231        throw new InvalidOperationException("DeleteRowsWithIndices not possible while data is filtered");
     232
     233      originalData.DeleteRowsWithIndices(rows);
     234    }
     235
     236    public void Undo() {
     237      if (IsFiltered)
     238        throw new InvalidOperationException("Undo not possible while data is filtered");
     239
     240      originalData.Undo();
     241    }
     242
     243    public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) {
     244      if (IsFiltered)
     245        throw new InvalidOperationException("Transaction not possible while data is filtered");
     246      originalData.InTransaction(action, type);
     247    }
     248
     249    public void BeginTransaction(DataPreprocessingChangedEventType type) {
     250      if (IsFiltered)
     251        throw new InvalidOperationException("Transaction not possible while data is filtered");
     252      originalData.BeginTransaction(type);
     253    }
     254
     255    public void EndTransaction() {
     256      originalData.EndTransaction();
     257    }
     258
     259    public IEnumerable<string> GetDoubleVariableNames() {
     260      return originalData.GetDoubleVariableNames();
     261    }
     262
     263    public void ClearSelection() {
     264      originalData.ClearSelection();
     265    }
     266
     267    public event EventHandler SelectionChanged {
     268      add { originalData.SelectionChanged += value; }
     269      remove { originalData.SelectionChanged -= value; }
     270    }
     271
     272    #region IPreprocessingData Members
     273    public bool Validate(string value, out string errorMessage, int columnIndex) {
     274      return originalData.Validate(value, out errorMessage, columnIndex);
     275    }
     276
     277    public event EventHandler FilterChanged;
    378278    #endregion
    379279  }
Note: See TracChangeset for help on using the changeset viewer.