Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeValues/FilterMovingAverageCommand.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.4 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;
23using System.Collections.Generic;
24using HeuristicLab.DataImporter.Command.View;
25using HeuristicLab.DataImporter.Data;
26using HeuristicLab.DataImporter.Data.CommandBase;
27using HeuristicLab.DataImporter.Data.Model;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HEAL.Attic;
30
31namespace HeuristicLab.DataImporter.Command {
32  [StorableType("1AF80A5F-F102-4E74-B550-ABFE9C4E17EB")]
33  [ViewableCommandInfoAttribute("Moving Average", 1, ColumnGroupState.DoubleColumnSelected, "Change Values", Position = 2,
34    OptionsView = typeof(FilterCommandView))]
35  public class FilterMovingAverageCommand : FilterCommandBase {
36    private Dictionary<int, DoubleColumn> oldColumns;
37    private ICollection<int> oldSortedColumnIndices;
38
39    [StorableConstructor]
40    protected FilterMovingAverageCommand(bool deserializing)
41      : base(deserializing) {
42      oldColumns = new Dictionary<int, DoubleColumn>();
43    }
44
45    public FilterMovingAverageCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
46      : base(dataSet, columnGroupName, affectedColumns) {
47      oldColumns = new Dictionary<int, DoubleColumn>();
48    }
49
50    public override string Description {
51      get { return "Filter Column - Moving Average"; }
52    }
53
54    public override void Execute() {
55      base.Execute();
56      if (this.WindowSize < 1)
57        throw new CommandExecutionException("Window size for filter commands must no be smaller than 1.", this);
58      DoubleColumn column;
59      oldSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
60      foreach (int col in AffectedColumns) {
61        if (ColumnGroup.GetColumn(col) is DoubleColumn) {
62          column = (DoubleColumn)ColumnGroup.GetColumn(col);
63          oldColumns.Add(col, column);
64          ColumnGroup.ReplaceColumn(col, CalcNewColumn(column, this.WindowSize));
65        }
66      }
67      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
68      ColumnGroup.FireChanged();
69      ColumnGroup = null;
70    }
71
72    public override void UndoExecute() {
73      base.UndoExecute();
74      foreach (KeyValuePair<int, DoubleColumn> pair in oldColumns)
75        ColumnGroup.ReplaceColumn(pair.Key, pair.Value);
76
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      double sum = 0;
91      double? val;
92      int cnt = 0;
93      //initialize sum and count for iterative moving average
94      for (int i = 0; i <= right; i++) {
95        val = (double?)oldColumn.GetValue(i);
96        if (val != null) {
97          sum += val.Value;
98          cnt++;
99        }
100      }
101      //first value newColumn[0]
102      newCol.AddValue(cnt == 0 ? null : (IComparable)(sum / cnt));
103      for (int i = 1; i < oldColumn.TotalValuesCount; i++) {
104        val = (double?)oldColumn.GetValue(i - left - 1);
105        if (val != null) {
106          sum -= val.Value;
107          cnt--;
108        }
109        val = (double?)oldColumn.GetValue(i + right);
110        if (val != null) {
111          sum += val.Value;
112          cnt++;
113        }
114        newCol.AddValue(cnt == 0 ? null : (IComparable)(sum / cnt));
115      }
116      newCol.SortOrder = oldColumn.SortOrder;
117      return newCol;
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.