Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.DataPreprocessing/3.4/Content/ManipulationContent.cs @ 16662

Last change on this file since 16662 was 16662, checked in by gkronber, 5 years ago

#2925: merged all changes from trunk to branch (up to r16659)

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HEAL.Attic;
29
30namespace HeuristicLab.DataPreprocessing {
31  [Item("Manipulation", "Represents the available manipulations on a data set.")]
32  [StorableType("42EE7A94-807F-482D-BE64-F98B05896B16")]
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(StorableConstructorFlag _) : base(_) { }
52    #endregion
53
54    public List<int> RowsWithMissingValuesGreater(double percent) {
55      List<int> rows = new List<int>();
56
57      for (int i = 0; i < PreprocessingData.Rows; ++i) {
58        int missingCount = PreprocessingData.GetRowMissingValueCount(i);
59        if (100f / PreprocessingData.Columns * missingCount > percent) {
60          rows.Add(i);
61        }
62      }
63
64      return rows;
65    }
66
67    public List<int> ColumnsWithMissingValuesGreater(double percent) {
68      List<int> columns = new List<int>();
69      for (int i = 0; i < PreprocessingData.Columns; ++i) {
70        int missingCount = PreprocessingData.GetMissingValueCount(i);
71        if (100f / PreprocessingData.Rows * missingCount > percent) {
72          columns.Add(i);
73        }
74      }
75
76      return columns;
77    }
78
79    public List<int> ColumnsWithVarianceSmaller(double variance) {
80      List<int> columns = new List<int>();
81      for (int i = 0; i < PreprocessingData.Columns; ++i) {
82        if (PreprocessingData.VariableHasType<double>(i)) {
83          double columnVariance = PreprocessingData.GetVariance<double>(i);
84          if (columnVariance < variance) {
85            columns.Add(i);
86          }
87        } else if (PreprocessingData.VariableHasType<DateTime>(i)) {
88          double columnVariance = (double)PreprocessingData.GetVariance<DateTime>(i).Ticks / TimeSpan.TicksPerSecond;
89          if (columnVariance < variance) {
90            columns.Add(i);
91          }
92        }
93      }
94      return columns;
95    }
96
97    public void DeleteRowsWithMissingValuesGreater(double percent) {
98      DeleteRows(RowsWithMissingValuesGreater(percent));
99    }
100
101    public void DeleteColumnsWithMissingValuesGreater(double percent) {
102      DeleteColumns(ColumnsWithMissingValuesGreater(percent));
103    }
104
105    public void DeleteColumnsWithVarianceSmaller(double variance) {
106      DeleteColumns(ColumnsWithVarianceSmaller(variance));
107    }
108
109    private void DeleteRows(List<int> rows) {
110      PreprocessingData.InTransaction(() => {
111        foreach (int row in rows.OrderByDescending(x => x)) {
112          PreprocessingData.DeleteRow(row);
113        }
114      });
115    }
116
117    private void DeleteColumns(List<int> columns) {
118      PreprocessingData.InTransaction(() => {
119        foreach (int column in columns.OrderByDescending(x => x)) {
120          PreprocessingData.DeleteColumn(column);
121        }
122      });
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.