#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.DataImporter.Command.View;
using HeuristicLab.DataImporter.Data;
using HeuristicLab.DataImporter.Data.CommandBase;
using HeuristicLab.DataImporter.Data.Model;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.DataImporter.Command {
[StorableClass]
[ViewableCommandInfoAttribute("Delete Rows beneath Threshold", 1, ColumnGroupState.Active,
"Handle Missing Values", Position = 5, OptionsView = typeof(DeleteWithThresholdView))]
public class DeleteRowsWithMissingValuesThresholdCommand : ColumnGroupCommandBase, IThresholdCommand {
private Dictionary deletedRows;
[Storable]
private double threshold = 0.8;
public double Threshold { get { return threshold; } set { threshold = value; } }
[StorableConstructor]
protected DeleteRowsWithMissingValuesThresholdCommand(bool deserializing)
: base(deserializing) {
deletedRows = new Dictionary();
}
public DeleteRowsWithMissingValuesThresholdCommand(DataSet dataSet, string columnGroupName)
: base(dataSet, columnGroupName) {
deletedRows = new Dictionary();
}
public override void Execute() {
base.Execute();
IComparable[] row;
double columnsCount = ColumnGroup.Columns.Count();
for (int rowInd = 0; rowInd < ColumnGroup.RowCount; rowInd++) {
row = ColumnGroup.GetRow(rowInd);
double setValues = ColumnGroup.GetRow(rowInd).Where(x => x != null).Count();
if (setValues / columnsCount < Threshold) {
deletedRows[rowInd] = row;
}
}
foreach (int rowInd in deletedRows.Select(x => x.Key).Reverse()) {
ColumnGroup.DeleteRow(rowInd);
}
ColumnGroup.FireChanged();
ColumnGroup = null;
}
public override void UndoExecute() {
base.UndoExecute();
foreach (KeyValuePair pair in deletedRows) {
ColumnGroup.InsertRow(pair.Key, pair.Value);
}
deletedRows.Clear();
ColumnGroup.FireChanged();
ColumnGroup = null;
}
public override string Description {
get { return "Delete rows with a missing value percentage beneath a certain threshold"; }
}
}
}