Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeValues/FilterMovingAverageCommand.cs @ 12693

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

#1734: Updated copyright information in all DataImporter classes.

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;
29
30namespace HeuristicLab.DataImporter.Command {
31  [StorableClass]
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(bool deserializing)
40      : base(deserializing) {
41      oldColumns = new Dictionary<int, DoubleColumn>();
42    }
43
44    public FilterMovingAverageCommand(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 Average"; }
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      DoubleColumn column;
58      oldSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
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
76      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
77      oldSortedColumnIndices = null;
78      oldColumns.Clear();
79      ColumnGroup.FireChanged();
80      ColumnGroup = null;
81    }
82
83    private DoubleColumn CalcNewColumn(DoubleColumn oldColumn, int windowSize) {
84      DoubleColumn newCol = (DoubleColumn)oldColumn.CreateCopyOfColumnWithoutValues();
85      int left = windowSize / 2;
86      int right = windowSize / 2;
87      if (windowSize % 2 == 0) left--;
88
89      double sum = 0;
90      double? val;
91      int cnt = 0;
92      //initialize sum and count for iterative moving average
93      for (int i = 0; i <= right; i++) {
94        val = (double?)oldColumn.GetValue(i);
95        if (val != null) {
96          sum += val.Value;
97          cnt++;
98        }
99      }
100      //first value newColumn[0]
101      newCol.AddValue(cnt == 0 ? null : (IComparable)(sum / cnt));
102      for (int i = 1; i < oldColumn.TotalValuesCount; i++) {
103        val = (double?)oldColumn.GetValue(i - left - 1);
104        if (val != null) {
105          sum -= val.Value;
106          cnt--;
107        }
108        val = (double?)oldColumn.GetValue(i + right);
109        if (val != null) {
110          sum += val.Value;
111          cnt++;
112        }
113        newCol.AddValue(cnt == 0 ? null : (IComparable)(sum / cnt));
114      }
115      newCol.SortOrder = oldColumn.SortOrder;
116      return newCol;
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.