[10709] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10709] | 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 |
|
---|
[15535] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[10709] | 24 | using System.Drawing;
|
---|
[15535] | 25 | using System.Linq;
|
---|
[10709] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[15535] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[10709] | 29 |
|
---|
| 30 | namespace HeuristicLab.DataPreprocessing {
|
---|
| 31 | [Item("Manipulation", "Represents the available manipulations on a data set.")]
|
---|
[15535] | 32 | [StorableClass]
|
---|
| 33 | public class ManipulationContent : PreprocessingContent, IViewShortcut {
|
---|
[10970] | 34 | public static new Image StaticItemImage {
|
---|
| 35 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Method; }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[15535] | 38 | #region Constructor, Cloning & Persistence
|
---|
| 39 | public ManipulationContent(IFilteredPreprocessingData preprocessingData)
|
---|
| 40 | : base(preprocessingData) {
|
---|
| 41 | }
|
---|
[15242] | 42 |
|
---|
[15535] | 43 | public ManipulationContent(ManipulationContent original, Cloner cloner) :
|
---|
| 44 | base(original, cloner) {
|
---|
[10709] | 45 | }
|
---|
| 46 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 47 | return new ManipulationContent(this, cloner);
|
---|
| 48 | }
|
---|
[15535] | 49 |
|
---|
| 50 | [StorableConstructor]
|
---|
| 51 | protected ManipulationContent(bool deserializing)
|
---|
| 52 | : base(deserializing) { }
|
---|
| 53 | #endregion
|
---|
| 54 |
|
---|
| 55 | public List<int> RowsWithMissingValuesGreater(double percent) {
|
---|
| 56 | List<int> rows = new List<int>();
|
---|
| 57 |
|
---|
| 58 | for (int i = 0; i < PreprocessingData.Rows; ++i) {
|
---|
| 59 | int missingCount = PreprocessingData.GetRowMissingValueCount(i);
|
---|
| 60 | if (100f / PreprocessingData.Columns * missingCount > percent) {
|
---|
| 61 | rows.Add(i);
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | return rows;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public List<int> ColumnsWithMissingValuesGreater(double percent) {
|
---|
| 69 | List<int> columns = new List<int>();
|
---|
| 70 | for (int i = 0; i < PreprocessingData.Columns; ++i) {
|
---|
| 71 | int missingCount = PreprocessingData.GetMissingValueCount(i);
|
---|
| 72 | if (100f / PreprocessingData.Rows * missingCount > percent) {
|
---|
| 73 | columns.Add(i);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | return columns;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | public List<int> ColumnsWithVarianceSmaller(double variance) {
|
---|
| 81 | List<int> columns = new List<int>();
|
---|
| 82 | for (int i = 0; i < PreprocessingData.Columns; ++i) {
|
---|
| 83 | if (PreprocessingData.VariableHasType<double>(i)) {
|
---|
| 84 | double columnVariance = PreprocessingData.GetVariance<double>(i);
|
---|
| 85 | if (columnVariance < variance) {
|
---|
| 86 | columns.Add(i);
|
---|
| 87 | }
|
---|
| 88 | } else if (PreprocessingData.VariableHasType<DateTime>(i)) {
|
---|
| 89 | double columnVariance = (double)PreprocessingData.GetVariance<DateTime>(i).Ticks / TimeSpan.TicksPerSecond;
|
---|
| 90 | if (columnVariance < variance) {
|
---|
| 91 | columns.Add(i);
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | return columns;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | public void DeleteRowsWithMissingValuesGreater(double percent) {
|
---|
| 99 | DeleteRows(RowsWithMissingValuesGreater(percent));
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | public void DeleteColumnsWithMissingValuesGreater(double percent) {
|
---|
| 103 | DeleteColumns(ColumnsWithMissingValuesGreater(percent));
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | public void DeleteColumnsWithVarianceSmaller(double variance) {
|
---|
| 107 | DeleteColumns(ColumnsWithVarianceSmaller(variance));
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | private void DeleteRows(List<int> rows) {
|
---|
| 111 | PreprocessingData.InTransaction(() => {
|
---|
| 112 | foreach (int row in rows.OrderByDescending(x => x)) {
|
---|
| 113 | PreprocessingData.DeleteRow(row);
|
---|
| 114 | }
|
---|
| 115 | });
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | private void DeleteColumns(List<int> columns) {
|
---|
| 119 | PreprocessingData.InTransaction(() => {
|
---|
| 120 | foreach (int column in columns.OrderByDescending(x => x)) {
|
---|
| 121 | PreprocessingData.DeleteColumn(column);
|
---|
| 122 | }
|
---|
| 123 | });
|
---|
| 124 | }
|
---|
[10709] | 125 | }
|
---|
| 126 | }
|
---|