Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeValues/FilterMovingMedianCommand.cs @ 9614

Last change on this file since 9614 was 9614, checked in by mkommend, 11 years ago

#1734: Added StorableConstructor to all storable DataImporter classes.

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