Changeset 15466


Ignore:
Timestamp:
11/09/17 11:51:37 (3 years ago)
Author:
pfleck
Message:

#2809: Simplified the overall filtering logic as suggested by bburlacu

  • changed parameter names to actively reflect that filter means "remaining"
  • moved filter combination logic to FilterContent
  • simplified/restructured code
Location:
branches/DataPreprocessing Cleanup
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing.Views/3.4/FilterView.Designer.cs

    r15210 r15466  
    4646    private void InitializeComponent() {
    4747      this.groupBoxFilter = new System.Windows.Forms.GroupBox();
    48       this.checkedFilterView = new HeuristicLab.DataPreprocessing.Views.CheckedFilterCollectionView();
    4948      this.groupBoxFilterInfo = new System.Windows.Forms.GroupBox();
    5049      this.lblPercentage = new System.Windows.Forms.Label();
     
    6362      this.label3 = new System.Windows.Forms.Label();
    6463      this.bottomPanel = new System.Windows.Forms.Panel();
     64      this.checkedFilterView = new HeuristicLab.DataPreprocessing.Views.CheckedFilterCollectionView();
    6565      this.groupBoxFilter.SuspendLayout();
    6666      this.groupBoxFilterInfo.SuspendLayout();
     
    8080      this.groupBoxFilter.TabStop = false;
    8181      this.groupBoxFilter.Text = "Filter";
    82       //
    83       // checkedFilterView
    84       //
    85       this.checkedFilterView.Caption = "filterView";
    86       this.checkedFilterView.Content = null;
    87       this.checkedFilterView.Dock = System.Windows.Forms.DockStyle.Fill;
    88       this.checkedFilterView.Location = new System.Drawing.Point(3, 16);
    89       this.checkedFilterView.Name = "checkedFilterView";
    90       this.checkedFilterView.ReadOnly = false;
    91       this.checkedFilterView.ShowDetails = true;
    92       this.checkedFilterView.Size = new System.Drawing.Size(652, 308);
    93       this.checkedFilterView.TabIndex = 0;
    9482      //
    9583      // groupBoxFilterInfo
     
    185173      this.rBtnOr.AutoSize = true;
    186174      this.rBtnOr.Cursor = System.Windows.Forms.Cursors.Default;
    187       this.rBtnOr.Enabled = false;
    188175      this.rBtnOr.Location = new System.Drawing.Point(53, 6);
    189176      this.rBtnOr.Name = "rBtnOr";
     
    199186      this.rBtnAnd.Checked = true;
    200187      this.rBtnAnd.Cursor = System.Windows.Forms.Cursors.Default;
    201       this.rBtnAnd.Enabled = false;
    202188      this.rBtnAnd.Location = new System.Drawing.Point(3, 6);
    203189      this.rBtnAnd.Name = "rBtnAnd";
     
    273259      this.bottomPanel.Size = new System.Drawing.Size(670, 30);
    274260      this.bottomPanel.TabIndex = 13;
     261      //
     262      // checkedFilterView
     263      //
     264      this.checkedFilterView.Caption = "filterView";
     265      this.checkedFilterView.Content = null;
     266      this.checkedFilterView.Dock = System.Windows.Forms.DockStyle.Fill;
     267      this.checkedFilterView.Location = new System.Drawing.Point(3, 16);
     268      this.checkedFilterView.Name = "checkedFilterView";
     269      this.checkedFilterView.ReadOnly = false;
     270      this.checkedFilterView.ShowDetails = true;
     271      this.checkedFilterView.Size = new System.Drawing.Size(652, 308);
     272      this.checkedFilterView.TabIndex = 0;
    275273      //
    276274      // FilterView
  • branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing.Views/3.4/FilterView.cs

    r15274 r15466  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Linq;
    2524using HeuristicLab.Collections;
     
    4443    }
    4544
    46     private void InitData() {
    47       checkedFilterView.Content = Content.Filters;
    48       checkedFilterView.Content.ItemsAdded += Content_ItemsAdded;
    49       checkedFilterView.Content.ItemsRemoved += Content_ItemsRemoved;
    50       checkedFilterView.Content.CheckedItemsChanged += Content_CheckedItemsChanged;
    51     }
    52 
    5345    protected override void OnContentChanged() {
    5446      base.OnContentChanged();
    5547      if (Content != null) {
    56         InitData();
    57         UpdateFilterInfo();
     48        checkedFilterView.Content = Content.Filters;
     49        rBtnAnd.Checked = Content.IsAndCombination;
     50        rBtnOr.Checked = !Content.IsAndCombination;
     51        UpdateFilter();
     52      } else {
     53        checkedFilterView.Content = null;
    5854      }
    5955    }
    60 
    61     private void Content_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<IFilter> e) {
    62       if (Content != null) {
    63         foreach (IFilter filter in e.Items) {
    64           filter.Active = checkedFilterView.Content.ItemChecked(filter);
    65         }
    66         UpdateFilterInfo();
    67       }
     56    protected override void RegisterContentEvents() {
     57      base.RegisterContentEvents();
     58      Content.Filters.ItemsAdded += Content_ItemsAdded;
     59      Content.Filters.ItemsRemoved += Content_ItemsRemoved;
     60      Content.Filters.CheckedItemsChanged += Content_CheckedItemsChanged;
     61    }
     62    protected override void DeregisterContentEvents() {
     63      Content.Filters.ItemsAdded -= Content_ItemsAdded;
     64      Content.Filters.ItemsRemoved -= Content_ItemsRemoved;
     65      Content.Filters.CheckedItemsChanged -= Content_CheckedItemsChanged;
     66      base.DeregisterContentEvents();
    6867    }
    6968
    70     private void UpdateFilterInfo() {
    71       List<IFilter> filters = Content.Filters.ToList();
    72       int activeFilters = filters.Count(c => c.Active);
    73       applyFilterButton.Enabled = (activeFilters > 0);
    74       rBtnAnd.Enabled = (activeFilters > 0);
    75       rBtnOr.Enabled = (activeFilters > 0);
     69    private void UpdateFilter() {
     70      bool activeFilters = Content.ActiveFilters.Any();
     71      applyFilterButton.Enabled = activeFilters;
     72
     73      int numTotal = Content.PreprocessingData.Rows;
     74      int numRemaining = numTotal;
     75
    7676      Content.PreprocessingData.ResetFilter();
    77       bool isAndCombination = rBtnAnd.Checked;
    78       bool[] ret;
    79       IList<IFilter> activeFilters1 = filters.Where(f => f.Active && f.ConstraintData != null).ToList();
    80       if (activeFilters1.Count > 0) {
    81         var result1 = Enumerable.Repeat(!isAndCombination, Content.PreprocessingData.Rows).ToArray();
     77      if (activeFilters) {
     78        var remainingRows = Content.GetRemainingRows();
     79        Content.PreprocessingData.SetFilter(remainingRows);
     80        numRemaining = remainingRows.Count(x => x);
     81      }
    8282
    83         foreach (IFilter filter in activeFilters1) {
    84           bool[] filterResult = filter.Check();
    85           for (int row = 0; row < result1.Length; ++row) {
    86             result1[row] = isAndCombination ? result1[row] || filterResult[row] : result1[row] && filterResult[row];
    87           }
    88         }
    89         Content.PreprocessingData.SetFilter(result1);
    90         ret = result1;
    91       } else {
    92         ret = Enumerable.Repeat(false, Content.PreprocessingData.Rows).ToArray();
    93       }
    94       bool[] result = ret;
    95 
    96       int filteredCnt = result.Count(c => !c);
    97 
    98       tbRemaining.Text = filteredCnt.ToString();
    99       double percentage = result.Length == 0 ? 0.0 : filteredCnt * 100 / (double)result.Length;
    100       tbPercentage.Text = String.Format("{0:0.0000}%", percentage);
    101       tbTotal.Text = result.Length.ToString();
     83      tbRemaining.Text = numRemaining.ToString();
     84      double ratio = numTotal > 0 ? numRemaining / (double)numTotal : 0.0;
     85      tbPercentage.Text = ratio.ToString("P4");
     86      tbTotal.Text = numTotal.ToString();
    10287    }
    10388
    104     private void applyFilterButton_Click(object sender, EventArgs e) {
    105       if (Content != null) {
    106         List<IFilter> filters = Content.Filters.ToList();
    107         //apply filters
    108         bool isAndCombination = rBtnAnd.Checked;
    109         Content.PreprocessingData.PersistFilter();
    110         Content.PreprocessingData.ResetFilter();
    111         //deactivate checked filters
    112         filters = checkedFilterView.Content.CheckedItems.ToList();
    113         foreach (IFilter filter in filters) {
    114           checkedFilterView.Content.SetItemCheckedState(filter, false);
    115           filter.Active = false;
    116         }
    117         UpdateFilterInfo();
    118       }
    119     }
    12089
     90    #region Content Events
    12191    //whenever a new filter is added the preprocessing data is set to the filter
    12292    private void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IFilter> e) {
     
    12797      }
    12898    }
    129 
    13099    private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IFilter> e) {
    131100      if (Content != null) {
    132         UpdateFilterInfo();
     101        UpdateFilter();
    133102      }
    134103    }
     104    private void Content_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<IFilter> e) {
     105      if (Content != null) {
     106        foreach (IFilter filter in e.Items) {
     107          filter.Active = checkedFilterView.Content.ItemChecked(filter);
     108        }
     109        UpdateFilter();
     110      }
     111    }
     112    #endregion
    135113
     114    #region Controls Events
    136115    private void rBtnAnd_CheckedChanged(object sender, EventArgs e) {
    137116      if (Content != null) {
    138         UpdateFilterInfo();
    139117        Content.IsAndCombination = rBtnAnd.Checked;
     118        UpdateFilter();
    140119      }
    141120    }
     121    private void applyFilterButton_Click(object sender, EventArgs e) {
     122      if (Content != null) {
     123        //apply filters
     124        Content.PreprocessingData.PersistFilter();
     125        Content.PreprocessingData.ResetFilter();
     126        //deactivate checked filters
     127        foreach (var filter in Content.Filters.CheckedItems) {
     128          checkedFilterView.Content.SetItemCheckedState(filter, false);
     129          filter.Active = false;
     130        }
     131        UpdateFilter();
     132      }
     133    }
     134    #endregion
    142135  }
    143136}
  • branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Content/FilterContent.cs

    r15274 r15466  
    2020#endregion
    2121
     22using System.Collections.Generic;
    2223using System.Drawing;
     24using System.Linq;
    2325using HeuristicLab.Common;
    2426using HeuristicLab.Core;
     
    3840    [Storable]
    3941    public bool IsAndCombination { get; set; }
     42
     43    public IEnumerable<IFilter> ActiveFilters {
     44      get { return Filters.Where(f => f.Active && f.ConstraintData != null); }
     45    }
     46
     47    public bool[] GetRemainingRows() {
     48      var remainingRows = new bool[PreprocessingData.Rows];
     49      if (ActiveFilters.Any()) {
     50        var filterResults = ActiveFilters.Select(f => f.Check()).ToList();
     51        var rowFilterResults = new bool[filterResults.Count];
     52        for (int row = 0; row < remainingRows.Length; row++) {
     53          for (int i = 0; i < filterResults.Count; i++)
     54            rowFilterResults[i] = filterResults[i][row];
     55
     56          remainingRows[row] = IsAndCombination
     57            ? rowFilterResults.All(x => x)
     58            : rowFilterResults.Any(x => x);
     59        }
     60      } else {
     61        // if not filters active => all rows are remaining
     62        for (int i = 0; i < remainingRows.Length; i++)
     63          remainingRows[i] = true;
     64      }
     65      return remainingRows;
     66    }
    4067
    4168    #region Constructor, Cloning & Persistence
  • branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Data/FilteredPreprocessingData.cs

    r15431 r15466  
    4343
    4444    #region Constructor, Cloning & Persistence
    45     public FilteredPreprocessingData(IPreprocessingData preporcessingData)
     45    public FilteredPreprocessingData(IPreprocessingData preprocessingData)
    4646      : base() {
    47       originalData = preporcessingData;
     47      originalData = preprocessingData;
    4848      filteredData = null;
    4949    }
     
    317317
    318318    #region Filters
    319     public void SetFilter(bool[] rowFilters) {
     319    public void SetFilter(bool[] remainingRows) {
    320320      filteredData = (IPreprocessingData)originalData.Clone();
    321321      filteredData.InTransaction(() => {
    322         for (int row = (rowFilters.Length - 1); row >= 0; --row) {
    323           if (rowFilters[row]) {
     322        for (int row = (remainingRows.Length - 1); row >= 0; --row) {
     323          if (!remainingRows[row]) {
    324324            filteredData.DeleteRow(row);
    325325          }
  • branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Data/IFilteredPreprocessingData.cs

    r15270 r15466  
    2525  public interface IFilteredPreprocessingData : IPreprocessingData {
    2626    #region Filters
    27     void SetFilter(bool[] rowFilters);
     27    void SetFilter(bool[] remainingRows);
    2828    void PersistFilter();
    2929    void ResetFilter();
  • branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Filter/ComparisonFilter.cs

    r15110 r15466  
    8686    }
    8787
     88    // return remaining rows
    8889    public new bool[] Check() {
    8990      bool[] result = new bool[ConstrainedValue.Rows];
     
    102103        }
    103104
    104         result[row] = !base.Check(item);
     105        result[row] = base.Check(item);
    105106      }
    106107
Note: See TracChangeset for help on using the changeset viewer.