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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.DataImporter.Command.View;
|
---|
26 | using HeuristicLab.DataImporter.Data;
|
---|
27 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
28 | using HeuristicLab.DataImporter.Data.Model;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HEAL.Attic;
|
---|
31 |
|
---|
32 | namespace 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(StorableConstructorFlag _) : base(_) {
|
---|
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 | }
|
---|