Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/MissingValues/DeleteRowsWithMissingValuesCommand.cs @ 8661

Last change on this file since 8661 was 7968, checked in by sforsten, 13 years ago

#1867:

  • Delete command for columns and for rows has been added, which have less data than a specified threshold
  • threshold can be set in a view
File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.DataImporter.Data;
25using HeuristicLab.DataImporter.Data.CommandBase;
26using HeuristicLab.DataImporter.Data.Model;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.DataImporter.Command {
30  [StorableClass]
31  [ViewableCommandInfoAttribute("Delete Rows", 1, ColumnGroupState.AnySelectedColumnContainsNull,
32    "Handle Missing Values", Position = 4)]
33  public class DeleteRowsWithMissingValuesCommand : ColumnGroupCommandWithAffectedColumnsBase {
34    private List<ColumnBase> oldColumns;
35    private ICollection<int> oldSortedColumnIndices;
36
37    private DeleteRowsWithMissingValuesCommand()
38      : base(null, string.Empty, null) {
39      oldColumns = new List<ColumnBase>();
40    }
41
42    public DeleteRowsWithMissingValuesCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
43      : base(dataSet, columnGroupName, affectedColumns) {
44      oldColumns = new List<ColumnBase>();
45    }
46
47    public override void Execute() {
48      base.Execute();
49      ColumnGroup tempColumnGroup = new ColumnGroup();
50      HashSet<int> positions = new HashSet<int>();
51      ColumnBase column;
52
53      for (int col = 0; col < AffectedColumns.Length; col++) {
54        column = ColumnGroup.GetColumn(AffectedColumns[col]);
55        for (int row = 0; row < ColumnGroup.RowCount; row++) {
56          if (column.GetValue(row) == null && !positions.Contains(row))
57            positions.Add(row);
58        }
59      }
60
61      foreach (ColumnBase x in ColumnGroup.Columns) {
62        oldColumns.Add(x);
63        tempColumnGroup.AddColumn(x.CreateCopyOfColumnWithoutValues());
64      }
65
66      for (int i = 0; i < ColumnGroup.RowCount; i++)
67        if (!positions.Contains(i))
68          tempColumnGroup.AddRow(ColumnGroup.GetRow(i));
69
70      oldSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
71      for (int i = 0; i < ColumnGroup.Columns.Count(); i++) {
72        tempColumnGroup.Columns.ElementAt(i).SortOrder = ColumnGroup.GetColumn(i).SortOrder;
73        ColumnGroup.ReplaceColumn(i, tempColumnGroup.Columns.ElementAt(i));
74      }
75
76      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
77      ColumnGroup.FireChanged();
78      ColumnGroup = null;
79    }
80
81    public override void UndoExecute() {
82      base.UndoExecute();
83      for (int i = 0; i < ColumnGroup.Columns.Count(); i++)
84        ColumnGroup.ReplaceColumn(i, oldColumns[i]);
85      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
86      oldColumns.Clear();
87      oldSortedColumnIndices = null;
88      ColumnGroup.FireChanged();
89      ColumnGroup = null;
90    }
91
92    public override string Description {
93      get { return "Delete rows with missing values"; }
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.