Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Content/ManipulationContent.cs @ 15309

Last change on this file since 15309 was 15309, checked in by pfleck, 7 years ago

#2809 Worked on type-save PreprocessingDataColumns.

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Drawing;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.DataPreprocessing {
31  [Item("Manipulation", "Represents the available manipulations on a data set.")]
32  [StorableClass]
33  public class ManipulationContent : PreprocessingContent, IViewShortcut {
34    public static new Image StaticItemImage {
35      get { return HeuristicLab.Common.Resources.VSImageLibrary.Method; }
36    }
37
38    #region Constructor, Cloning & Persistence
39    public ManipulationContent(IFilteredPreprocessingData preprocessingData)
40      : base(preprocessingData) {
41    }
42
43    public ManipulationContent(ManipulationContent original, Cloner cloner) :
44      base(original, cloner) {
45    }
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new ManipulationContent(this, cloner);
48    }
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 = 0;
60        for (var col = 0; col < PreprocessingData.DataColumns.Count; col++) {
61          if (!PreprocessingData.DataColumns[col].IsValidValue(i))
62            missingCount++;
63        }
64        if (100f / PreprocessingData.Columns * missingCount > percent)
65          rows.Add(i);
66      }
67
68      return rows;
69    }
70
71    public List<int> ColumnsWithMissingValuesGreater(double percent) {
72      List<int> columns = new List<int>();
73      for (int i = 0; i < PreprocessingData.Columns; ++i) {
74        int missingCount = PreprocessingData.DataColumns[i].GetNumberOfMissingValues();
75        if (100f / PreprocessingData.Rows * missingCount > percent) {
76          columns.Add(i);
77        }
78      }
79
80      return columns;
81    }
82
83    public List<int> ColumnsWithVarianceSmaller(double variance) {
84      List<int> columns = new List<int>();
85
86      for (int i = 0; i < PreprocessingData.Columns; i++) {
87        if (PreprocessingData.DataColumns[i].TypeSwitch<bool>(
88          c => c.GetVariance() < variance,
89          c => false,
90          c => c.GetVariance().Ticks / TimeSpan.TicksPerSecond < variance
91        ))
92          columns.Add(i);
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.DataColumns.RemoveAt(column);
122        }
123      });
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.