Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeValues/FilterMovingAverageCommand.cs @ 16994

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

#2520 Update plugin dependencies and references for HL.DataImporter for new persistence

File size: 4.3 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 HEAL.Attic;
29
30namespace HeuristicLab.DataImporter.Command {
31  [StorableType("1AF80A5F-F102-4E74-B550-ABFE9C4E17EB")]
32  [ViewableCommandInfoAttribute("Moving Average", 1, ColumnGroupState.DoubleColumnSelected, "Change Values", Position = 2,
33    OptionsView = typeof(FilterCommandView))]
34  public class FilterMovingAverageCommand : FilterCommandBase {
35    private Dictionary<int, DoubleColumn> oldColumns;
36    private ICollection<int> oldSortedColumnIndices;
37
38    [StorableConstructor]
39    protected FilterMovingAverageCommand(StorableConstructorFlag _) : base(_) {
40      oldColumns = new Dictionary<int, DoubleColumn>();
41    }
42
43    public FilterMovingAverageCommand(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 Average"; }
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      DoubleColumn column;
57      oldSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
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
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      double sum = 0;
89      double? val;
90      int cnt = 0;
91      //initialize sum and count for iterative moving average
92      for (int i = 0; i <= right; i++) {
93        val = (double?)oldColumn.GetValue(i);
94        if (val != null) {
95          sum += val.Value;
96          cnt++;
97        }
98      }
99      //first value newColumn[0]
100      newCol.AddValue(cnt == 0 ? null : (IComparable)(sum / cnt));
101      for (int i = 1; i < oldColumn.TotalValuesCount; i++) {
102        val = (double?)oldColumn.GetValue(i - left - 1);
103        if (val != null) {
104          sum -= val.Value;
105          cnt--;
106        }
107        val = (double?)oldColumn.GetValue(i + right);
108        if (val != null) {
109          sum += val.Value;
110          cnt++;
111        }
112        newCol.AddValue(cnt == 0 ? null : (IComparable)(sum / cnt));
113      }
114      newCol.SortOrder = oldColumn.SortOrder;
115      return newCol;
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.