Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7267 was 7267, checked in by gkronber, 12 years ago

#1734 updated copyright year in all files of the DataImporter branch

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