Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeValues/FilterMovingMedianCommand.cs @ 16566

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

#2520: updated HeuristicLab.DataImporter addon for new Persistence (introduced in 3.3.16)

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 HeuristicLab.DataImporter.Command.View;
24using HeuristicLab.DataImporter.Data;
25using HeuristicLab.DataImporter.Data.CommandBase;
26using HeuristicLab.DataImporter.Data.Model;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HEAL.Attic;
29
30namespace HeuristicLab.DataImporter.Command {
31  [StorableType("B1E2D0F2-A927-4D6E-952E-8CF349E0CB6E")]
32  [ViewableCommandInfoAttribute("Moving Median", 1, ColumnGroupState.DoubleColumnSelected, "Change Values", Position = 3,
33   OptionsView = typeof(FilterCommandView))]
34  public class FilterMovingMedianCommand : FilterCommandBase {
35    private Dictionary<int, DoubleColumn> oldColumns;
36    private ICollection<int> oldSortedColumnIndices;
37
38    [StorableConstructor]
39    protected FilterMovingMedianCommand(bool deserializing)
40      : base(deserializing) {
41      oldColumns = new Dictionary<int, DoubleColumn>();
42    }
43
44    public FilterMovingMedianCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
45      : base(dataSet, columnGroupName, affectedColumns) {
46      oldColumns = new Dictionary<int, DoubleColumn>();
47    }
48
49    public override string Description {
50      get { return "Filter Column - Moving Median"; }
51    }
52
53    public override void Execute() {
54      base.Execute();
55      if (this.WindowSize < 1)
56        throw new CommandExecutionException("Window size for filter commands must no be smaller than 1.", this);
57      oldSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
58      DoubleColumn column;
59      foreach (int col in AffectedColumns) {
60        if (ColumnGroup.GetColumn(col) is DoubleColumn) {
61          column = (DoubleColumn)ColumnGroup.GetColumn(col);
62          oldColumns.Add(col, column);
63          ColumnGroup.ReplaceColumn(col, CalcNewColumn(column, this.WindowSize));
64        }
65      }
66      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
67      ColumnGroup.FireChanged();
68      ColumnGroup = null;
69    }
70
71    public override void UndoExecute() {
72      base.UndoExecute();
73      foreach (KeyValuePair<int, DoubleColumn> pair in oldColumns)
74        ColumnGroup.ReplaceColumn(pair.Key, pair.Value);
75      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
76      oldSortedColumnIndices = null;
77      oldColumns.Clear();
78      ColumnGroup.FireChanged();
79      ColumnGroup = null;
80    }
81
82    private DoubleColumn CalcNewColumn(DoubleColumn oldColumn, int windowSize) {
83      DoubleColumn newCol = (DoubleColumn)oldColumn.CreateCopyOfColumnWithoutValues();
84      int left = windowSize / 2;
85      int right = windowSize / 2;
86      if (windowSize % 2 == 0) left--;
87
88      int startIndex = -1;
89      int endIndex = oldColumn.TotalValuesCount;
90      //first value newColumn[0]
91      newCol.AddValue(oldColumn.GetMedian(0, right >= oldColumn.TotalValuesCount ? oldColumn.TotalValuesCount : right + 1));
92      for (int i = 1; i < oldColumn.TotalValuesCount; i++) {
93        startIndex = i - left;
94        if (startIndex < 0)
95          startIndex = 0;
96        endIndex = i + right;
97        if (endIndex >= oldColumn.TotalValuesCount)
98          endIndex = oldColumn.TotalValuesCount - 1;
99        newCol.AddValue(oldColumn.GetMedian(startIndex, endIndex + 1));
100      }
101      newCol.SortOrder = oldColumn.SortOrder;
102      return newCol;
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.