Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/12/17 16:32:35 (6 years ago)
Author:
pfleck
Message:

#2809: merged branch to trunk

Location:
trunk/sources/HeuristicLab.DataPreprocessing
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.DataPreprocessing

  • trunk/sources/HeuristicLab.DataPreprocessing/3.4

  • trunk/sources/HeuristicLab.DataPreprocessing/3.4/Data/FilteredPreprocessingData.cs

    r15110 r15518  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
    2627using HeuristicLab.Data;
     28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2729using HeuristicLab.Problems.DataAnalysis;
    2830
    2931namespace HeuristicLab.DataPreprocessing {
    30   public class FilteredPreprocessingData : NamedItem, IFilteredPreprocessingData {
    31     private readonly ITransactionalPreprocessingData originalData;
    32     private ITransactionalPreprocessingData filteredData;
    33 
     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 {
     42      get { return IsFiltered ? filteredData : originalData; }
     43    }
     44
     45    #region Constructor, Cloning & Persistence
     46    public FilteredPreprocessingData(IPreprocessingData preprocessingData)
     47      : base() {
     48      originalData = preprocessingData;
     49      filteredData = null;
     50    }
     51
     52    private FilteredPreprocessingData(FilteredPreprocessingData original, Cloner cloner)
     53      : base(original, cloner) {
     54      originalData = original.originalData;
     55      filteredData = original.filteredData;
     56    }
     57    public override IDeepCloneable Clone(Cloner cloner) {
     58      return new FilteredPreprocessingData(this, cloner);
     59    }
     60
     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
     71    public T GetCell<T>(int columnIndex, int rowIndex) {
     72      return ActiveData.GetCell<T>(columnIndex, rowIndex);
     73    }
     74
     75    public void SetCell<T>(int columnIndex, int rowIndex, T value) {
     76      if (IsFiltered)
     77        throw new InvalidOperationException("SetValues not possible while data is filtered");
     78      originalData.SetCell<T>(columnIndex, rowIndex, value);
     79    }
     80
     81    public string GetCellAsString(int columnIndex, int rowIndex) {
     82      return ActiveData.GetCellAsString(columnIndex, rowIndex);
     83    }
     84
     85    public IList<T> GetValues<T>(int columnIndex, bool considerSelection) {
     86      return ActiveData.GetValues<T>(columnIndex, considerSelection);
     87    }
     88
     89    public void SetValues<T>(int columnIndex, IList<T> values) {
     90      if (IsFiltered)
     91        throw new InvalidOperationException("SetValues not possible while data is filtered");
     92
     93      originalData.SetValues<T>(columnIndex, values);
     94    }
     95
     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
     112    public void InsertRow(int rowIndex) {
     113      if (IsFiltered)
     114        throw new InvalidOperationException("InsertRow not possible while data is filtered");
     115
     116      originalData.InsertRow(rowIndex);
     117    }
     118
     119    public void DeleteRow(int rowIndex) {
     120      if (IsFiltered)
     121        throw new InvalidOperationException("DeleteRow not possible while data is filtered");
     122
     123      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);
     131    }
     132
     133    public void InsertColumn<T>(string variableName, int columnIndex) {
     134      if (IsFiltered)
     135        throw new InvalidOperationException("InsertColumn not possible while data is filtered");
     136
     137      originalData.InsertColumn<T>(variableName, columnIndex);
     138    }
     139
     140    public void DeleteColumn(int columnIndex) {
     141      if (IsFiltered)
     142        throw new InvalidOperationException("DeleteColumn not possible while data is filtered");
     143      originalData.DeleteColumn(columnIndex);
     144    }
     145
     146    public void RenameColumn(int columnIndex, string name) {
     147      if (IsFiltered)
     148        throw new InvalidOperationException("RenameColumn not possible while data is filtered");
     149      originalData.RenameColumn(columnIndex, name);
     150    }
     151
     152    public void RenameColumns(IList<string> names) {
     153      if (IsFiltered)
     154        throw new InvalidOperationException("RenameColumns not possible while data is filtered");
     155      originalData.RenameColumns(names);
     156    }
     157
     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    }
     170    public string GetVariableName(int columnIndex) {
     171      return ActiveData.GetVariableName(columnIndex);
     172    }
     173
     174    public int GetColumnIndex(string variableName) {
     175      return ActiveData.GetColumnIndex(variableName);
     176    }
     177
     178    public bool VariableHasType<T>(int columnIndex) {
     179      return originalData.VariableHasType<T>(columnIndex);
     180    }
     181
     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
    34196    public IntRange TrainingPartition {
    35197      get { return originalData.TrainingPartition; }
     
    39201      get { return originalData.TestPartition; }
    40202    }
    41 
     203    #endregion
     204
     205    #region Transformations
    42206    public IList<ITransformation> Transformations {
    43207      get { return originalData.Transformations; }
    44208    }
    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 
     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
     224    public Dataset ExportToDataset() {
     225      return originalData.ExportToDataset();
     226    }
     227    #endregion
     228
     229    #region Selection
    53230    public IDictionary<int, IList<int>> Selection {
    54231      get { return originalData.Selection; }
     
    56233    }
    57234
    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 {
    67       get { return IsFiltered ? filteredData : originalData; }
     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; }
    68249    }
    69250
     
    72253    }
    73254
    74     public bool IsFiltered {
    75       get { return filteredData != null; }
    76     }
    77 
    78 
    79     public FilteredPreprocessingData(ITransactionalPreprocessingData preporcessingData)
    80       : base() {
    81       originalData = preporcessingData;
    82       filteredData = null;
    83     }
    84 
    85     protected FilteredPreprocessingData(FilteredPreprocessingData original, Cloner cloner)
    86       : base(original, cloner) {
    87       originalData = original.originalData;
    88       filteredData = original.filteredData;
    89     }
    90     public override IDeepCloneable Clone(Cloner cloner) {
    91       return new FilteredPreprocessingData(this, cloner);
    92     }
    93 
    94     public T GetCell<T>(int columnIndex, int rowIndex) {
    95       return ActiveData.GetCell<T>(columnIndex, rowIndex);
    96     }
    97 
    98     public void SetCell<T>(int columnIndex, int rowIndex, T value) {
    99       if (IsFiltered)
    100         throw new InvalidOperationException("SetValues not possible while data is filtered");
    101       originalData.SetCell<T>(columnIndex, rowIndex, value);
    102     }
    103 
    104     public string GetCellAsString(int columnIndex, int rowIndex) {
    105       return ActiveData.GetCellAsString(columnIndex, rowIndex);
    106     }
    107 
    108     public IList<T> GetValues<T>(int columnIndex, bool considerSelection) {
    109       return ActiveData.GetValues<T>(columnIndex, considerSelection);
    110     }
    111 
    112     public void SetValues<T>(int columnIndex, IList<T> values) {
    113       if (IsFiltered)
    114         throw new InvalidOperationException("SetValues not possible while data is filtered");
    115 
    116       originalData.SetValues<T>(columnIndex, values);
    117     }
    118 
    119     public void InsertRow(int rowIndex) {
    120       if (IsFiltered)
    121         throw new InvalidOperationException("InsertRow not possible while data is filtered");
    122 
    123       originalData.InsertRow(rowIndex);
    124     }
    125 
    126     public void DeleteRow(int rowIndex) {
    127       if (IsFiltered)
    128         throw new InvalidOperationException("DeleteRow not possible while data is filtered");
    129 
    130       originalData.DeleteRow(rowIndex);
    131     }
    132 
    133     public void InsertColumn<T>(string variableName, int columnIndex) {
    134       if (IsFiltered)
    135         throw new InvalidOperationException("InsertColumn not possible while data is filtered");
    136 
    137       originalData.InsertColumn<T>(variableName, columnIndex);
    138     }
    139 
    140     public void DeleteColumn(int columnIndex) {
    141       if (IsFiltered)
    142         throw new InvalidOperationException("DeleteColumn not possible while data is filtered");
    143       originalData.DeleteColumn(columnIndex);
    144     }
    145 
    146     public void RenameColumn(int columnIndex, string name) {
    147       if (IsFiltered)
    148         throw new InvalidOperationException("RenameColumn not possible while data is filtered");
    149       originalData.RenameColumn(columnIndex, name);
    150     }
    151 
    152     public void RenameColumns(IList<string> names) {
    153       if (IsFiltered)
    154         throw new InvalidOperationException("RenameColumns not possible while data is filtered");
    155       originalData.RenameColumns(names);
    156     }
    157 
    158     public string GetVariableName(int columnIndex) {
    159       return ActiveData.GetVariableName(columnIndex);
    160     }
    161 
    162     public int GetColumnIndex(string variableName) {
    163       return ActiveData.GetColumnIndex(variableName);
    164     }
    165 
    166     public bool VariableHasType<T>(int columnIndex) {
    167       return originalData.VariableHasType<T>(columnIndex);
    168     }
    169 
    170     public Dataset ExportToDataset() {
    171       return originalData.ExportToDataset();
    172     }
    173 
    174     public void SetFilter(bool[] rowFilters) {
    175       filteredData = (ITransactionalPreprocessingData)originalData.Clone();
     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.GetMean<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();
    176322      filteredData.InTransaction(() => {
    177         for (int row = (rowFilters.Length - 1); row >= 0; --row) {
    178           if (rowFilters[row]) {
    179             filteredData.DeleteRow(row);
     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);
    180339          }
    181340        }
     
    206365    }
    207366
     367    public bool IsFiltered {
     368      get { return filteredData != null; }
     369    }
     370
     371    public event EventHandler FilterChanged;
     372
    208373    private void OnFilterChanged() {
    209374      if (FilterChanged != null) {
     
    211376      }
    212377    }
    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;
    278378    #endregion
    279379  }
Note: See TracChangeset for help on using the changeset viewer.