#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using HeuristicLab.DataImporter.Command.View;
using HeuristicLab.DataImporter.Data;
using HeuristicLab.DataImporter.Data.CommandBase;
using HeuristicLab.DataImporter.Data.Model;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.DataImporter.Command {
[StorableClass]
[ViewableCommandInfoAttribute("Moving Average", 1, ColumnGroupState.DoubleColumnSelected, "Change Values", Position = 2,
OptionsView = typeof(FilterCommandView))]
public class FilterMovingAverageCommand : FilterCommandBase {
private Dictionary oldColumns;
private ICollection oldSortedColumnIndices;
[StorableConstructor]
protected FilterMovingAverageCommand(bool deserializing)
: base(deserializing) {
oldColumns = new Dictionary();
}
public FilterMovingAverageCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
: base(dataSet, columnGroupName, affectedColumns) {
oldColumns = new Dictionary();
}
public override string Description {
get { return "Filter Column - Moving Average"; }
}
public override void Execute() {
base.Execute();
if (this.WindowSize < 1)
throw new CommandExecutionException("Window size for filter commands must no be smaller than 1.", this);
DoubleColumn column;
oldSortedColumnIndices = new List(ColumnGroup.SortedColumnIndexes);
foreach (int col in AffectedColumns) {
if (ColumnGroup.GetColumn(col) is DoubleColumn) {
column = (DoubleColumn)ColumnGroup.GetColumn(col);
oldColumns.Add(col, column);
ColumnGroup.ReplaceColumn(col, CalcNewColumn(column, this.WindowSize));
}
}
ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
ColumnGroup.FireChanged();
ColumnGroup = null;
}
public override void UndoExecute() {
base.UndoExecute();
foreach (KeyValuePair pair in oldColumns)
ColumnGroup.ReplaceColumn(pair.Key, pair.Value);
ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
oldSortedColumnIndices = null;
oldColumns.Clear();
ColumnGroup.FireChanged();
ColumnGroup = null;
}
private DoubleColumn CalcNewColumn(DoubleColumn oldColumn, int windowSize) {
DoubleColumn newCol = (DoubleColumn)oldColumn.CreateCopyOfColumnWithoutValues();
int left = windowSize / 2;
int right = windowSize / 2;
if (windowSize % 2 == 0) left--;
double sum = 0;
double? val;
int cnt = 0;
//initialize sum and count for iterative moving average
for (int i = 0; i <= right; i++) {
val = (double?)oldColumn.GetValue(i);
if (val != null) {
sum += val.Value;
cnt++;
}
}
//first value newColumn[0]
newCol.AddValue(cnt == 0 ? null : (IComparable)(sum / cnt));
for (int i = 1; i < oldColumn.TotalValuesCount; i++) {
val = (double?)oldColumn.GetValue(i - left - 1);
if (val != null) {
sum -= val.Value;
cnt--;
}
val = (double?)oldColumn.GetValue(i + right);
if (val != null) {
sum += val.Value;
cnt++;
}
newCol.AddValue(cnt == 0 ? null : (IComparable)(sum / cnt));
}
newCol.SortOrder = oldColumn.SortOrder;
return newCol;
}
}
}