Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/MissingValues/DeleteRowsWithMissingValuesThresholdCommand.cs @ 16566

Last change on this file since 16566 was 16566, checked in by gkronber, 6 years ago

#2520: updated HeuristicLab.DataImporter addon for new Persistence (introduced in 3.3.16)

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;
30using HEAL.Attic;
31
32namespace HeuristicLab.DataImporter.Command {
33  [StorableType("DD29CB5F-0878-4CEC-B8C2-F2E7AA357210")]
34  [ViewableCommandInfoAttribute("Delete Rows beneath Threshold", 1, ColumnGroupState.Active,
35    "Handle Missing Values", Position = 5, OptionsView = typeof(DeleteWithThresholdView))]
36  public class DeleteRowsWithMissingValuesThresholdCommand : ColumnGroupCommandBase, IThresholdCommand {
37    private Dictionary<int, IComparable[]> deletedRows;
38
39    [Storable]
40    private double threshold = 0.8;
41    public double Threshold { get { return threshold; } set { threshold = value; } }
42
43    [StorableConstructor]
44    protected DeleteRowsWithMissingValuesThresholdCommand(bool deserializing)
45      : base(deserializing) {
46      deletedRows = new Dictionary<int, IComparable[]>();
47    }
48
49    public DeleteRowsWithMissingValuesThresholdCommand(DataSet dataSet, string columnGroupName)
50      : base(dataSet, columnGroupName) {
51      deletedRows = new Dictionary<int, IComparable[]>();
52    }
53
54    public override void Execute() {
55      base.Execute();
56      IComparable[] row;
57
58      double columnsCount = ColumnGroup.Columns.Count();
59      for (int rowInd = 0; rowInd < ColumnGroup.RowCount; rowInd++) {
60        row = ColumnGroup.GetRow(rowInd);
61        double setValues = ColumnGroup.GetRow(rowInd).Where(x => x != null).Count();
62        if (setValues / columnsCount < Threshold) {
63          deletedRows[rowInd] = row;
64        }
65      }
66
67      foreach (int rowInd in deletedRows.Select(x => x.Key).Reverse()) {
68        ColumnGroup.DeleteRow(rowInd);
69      }
70
71      ColumnGroup.FireChanged();
72      ColumnGroup = null;
73    }
74
75    public override void UndoExecute() {
76      base.UndoExecute();
77      foreach (KeyValuePair<int, IComparable[]> pair in deletedRows) {
78        ColumnGroup.InsertRow(pair.Key, pair.Value);
79      }
80      deletedRows.Clear();
81      ColumnGroup.FireChanged();
82      ColumnGroup = null;
83    }
84
85    public override string Description {
86      get { return "Delete rows with a missing value percentage beneath a certain threshold"; }
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.