1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using System.Xml;
|
---|
27 | using HeuristicLab.DataImporter.Data;
|
---|
28 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
29 | using HeuristicLab.DataImporter.Data.Model;
|
---|
30 | using HeuristicLab.DataImporter.Command.View;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.DataImporter.Command {
|
---|
34 | [StorableClass]
|
---|
35 | [ViewableCommandInfoAttribute("Moving Average", 1, ColumnGroupState.DoubleColumnSelected, "Change Values", Position = 2,
|
---|
36 | OptionsView = typeof(FilterCommandView))]
|
---|
37 | public class FilterMovingAverageCommand : FilterCommandBase {
|
---|
38 | private Dictionary<int, DoubleColumn> oldColumns;
|
---|
39 | private ICollection<int> oldSortedColumnIndices;
|
---|
40 |
|
---|
41 | private FilterMovingAverageCommand()
|
---|
42 | : base(null, string.Empty, null) {
|
---|
43 | oldColumns = new Dictionary<int, DoubleColumn>();
|
---|
44 | }
|
---|
45 |
|
---|
46 | public FilterMovingAverageCommand(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 Average"; }
|
---|
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 | DoubleColumn column;
|
---|
60 | oldSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
|
---|
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 |
|
---|
78 | ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
|
---|
79 | oldSortedColumnIndices = null;
|
---|
80 | oldColumns.Clear();
|
---|
81 | ColumnGroup.FireChanged();
|
---|
82 | ColumnGroup = null;
|
---|
83 | }
|
---|
84 |
|
---|
85 | private DoubleColumn CalcNewColumn(DoubleColumn oldColumn, int windowSize) {
|
---|
86 | DoubleColumn newCol = (DoubleColumn)oldColumn.CreateCopyOfColumnWithoutValues();
|
---|
87 | int left = windowSize / 2;
|
---|
88 | int right = windowSize / 2;
|
---|
89 | if (windowSize % 2 == 0) left--;
|
---|
90 |
|
---|
91 | double sum = 0;
|
---|
92 | double? val;
|
---|
93 | int cnt = 0;
|
---|
94 | //initialize sum and count for iterative moving average
|
---|
95 | for (int i = 0; i <= right; i++) {
|
---|
96 | val = (double?)oldColumn.GetValue(i);
|
---|
97 | if (val != null) {
|
---|
98 | sum += val.Value;
|
---|
99 | cnt++;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | //first value newColumn[0]
|
---|
103 | newCol.AddValue(cnt == 0 ? null : (IComparable)(sum / cnt));
|
---|
104 | for (int i = 1; i < oldColumn.TotalValuesCount; i++) {
|
---|
105 | val = (double?)oldColumn.GetValue(i - left - 1);
|
---|
106 | if (val != null) {
|
---|
107 | sum -= val.Value;
|
---|
108 | cnt--;
|
---|
109 | }
|
---|
110 | val = (double?)oldColumn.GetValue(i + right);
|
---|
111 | if (val != null) {
|
---|
112 | sum += val.Value;
|
---|
113 | cnt++;
|
---|
114 | }
|
---|
115 | newCol.AddValue(cnt == 0 ? null : (IComparable)(sum / cnt));
|
---|
116 | }
|
---|
117 | newCol.SortOrder = oldColumn.SortOrder;
|
---|
118 | return newCol;
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|