Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/MissingValues/DeleteRowsWithMissingValuesThresholdCommand.cs @ 9615

Last change on this file since 9615 was 9615, checked in by mkommend, 11 years ago

#1734: Updated copyright information in all DataImporter classes.

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.DataImporter.Command.View;
26using HeuristicLab.DataImporter.Data;
27using HeuristicLab.DataImporter.Data.CommandBase;
28using HeuristicLab.DataImporter.Data.Model;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.DataImporter.Command {
32  [StorableClass]
33  [ViewableCommandInfoAttribute("Delete Rows beneath Threshold", 1, ColumnGroupState.Active,
34    "Handle Missing Values", Position = 5, OptionsView = typeof(DeleteWithThresholdView))]
35  public class DeleteRowsWithMissingValuesThresholdCommand : ColumnGroupCommandBase, IThresholdCommand {
36    private Dictionary<int, IComparable[]> deletedRows;
37
38    [Storable]
39    private double threshold = 0.8;
40    public double Threshold { get { return threshold; } set { threshold = value; } }
41
42    [StorableConstructor]
43    protected DeleteRowsWithMissingValuesThresholdCommand(bool deserializing)
44      : base(deserializing) {
45      deletedRows = new Dictionary<int, IComparable[]>();
46    }
47
48    public DeleteRowsWithMissingValuesThresholdCommand(DataSet dataSet, string columnGroupName)
49      : base(dataSet, columnGroupName) {
50      deletedRows = new Dictionary<int, IComparable[]>();
51    }
52
53    public override void Execute() {
54      base.Execute();
55      IComparable[] row;
56
57      double columnsCount = ColumnGroup.Columns.Count();
58      for (int rowInd = 0; rowInd < ColumnGroup.RowCount; rowInd++) {
59        row = ColumnGroup.GetRow(rowInd);
60        double setValues = ColumnGroup.GetRow(rowInd).Where(x => x != null).Count();
61        if (setValues / columnsCount < Threshold) {
62          deletedRows[rowInd] = row;
63        }
64      }
65
66      foreach (int rowInd in deletedRows.Select(x => x.Key).Reverse()) {
67        ColumnGroup.DeleteRow(rowInd);
68      }
69
70      ColumnGroup.FireChanged();
71      ColumnGroup = null;
72    }
73
74    public override void UndoExecute() {
75      base.UndoExecute();
76      foreach (KeyValuePair<int, IComparable[]> pair in deletedRows) {
77        ColumnGroup.InsertRow(pair.Key, pair.Value);
78      }
79      deletedRows.Clear();
80      ColumnGroup.FireChanged();
81      ColumnGroup = null;
82    }
83
84    public override string Description {
85      get { return "Delete rows with a missing value percentage beneath a certain threshold"; }
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.