#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 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 System.Linq;
using System.Text;
using System.Xml;
using HeuristicLab.DataImporter.Data;
using HeuristicLab.DataImporter.Data.CommandBase;
using HeuristicLab.DataImporter.Data.Model;
using HeuristicLab.DataImporter.Command.View;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.DataImporter.Command {
[StorableClass]
[ViewableCommandInfoAttribute("Percental Change", 1, ColumnGroupState.DoubleColumnSelected, "Change Values", Position = 3)]
public class PercentalChangeCommand : ColumnGroupCommandWithAffectedColumnsBase {
private Dictionary oldColumns;
private ICollection oldSortedColumnIndices;
private PercentalChangeCommand()
: base(null, string.Empty, null) {
oldColumns = new Dictionary();
}
public PercentalChangeCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
: base(dataSet, columnGroupName, affectedColumns) {
oldColumns = new Dictionary();
}
public override string Description {
get { return "Calculate the percental change from last to the current value"; }
}
public override void Execute() {
base.Execute();
foreach (int col in AffectedColumns) {
DoubleColumn doubleCol = ColumnGroup.GetColumn(col) as DoubleColumn;
if (doubleCol == null) throw new CommandExecutionException("Filtering is only supported for double columns.", 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));
}
}
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) {
DoubleColumn newCol = (DoubleColumn)oldColumn.CreateCopyOfColumnWithoutValues();
var data = oldColumn.ValuesEnumerable.Cast();
var ans = new double?[] { null } // insert an empty value at the beginning
.Concat(data)
.Zip(data, (prev, cur) => (cur.HasValue && prev.HasValue) ? (cur - prev) / prev : null)
.ToArray();
foreach (var e in ans) newCol.AddValue(e);
newCol.SortOrder = oldColumn.SortOrder;
return newCol;
}
}
}