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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.DataImporter.Data;
|
---|
25 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
26 | using HeuristicLab.DataImporter.Data.Model;
|
---|
27 | using HEAL.Attic;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.DataImporter.Command {
|
---|
30 | [StorableType("EE8DF207-4544-486C-99EF-503CE0A09B0A")]
|
---|
31 | [ViewableCommandInfoAttribute("Percental Change", 1, ColumnGroupState.DoubleColumnSelected, "Change Values", Position = 3)]
|
---|
32 | public class PercentalChangeCommand : ColumnGroupCommandWithAffectedColumnsBase {
|
---|
33 | private Dictionary<int, DoubleColumn> oldColumns;
|
---|
34 | private ICollection<int> oldSortedColumnIndices;
|
---|
35 |
|
---|
36 | [StorableConstructor]
|
---|
37 | protected PercentalChangeCommand(StorableConstructorFlag _) : base(_) {
|
---|
38 | oldColumns = new Dictionary<int, DoubleColumn>();
|
---|
39 | }
|
---|
40 |
|
---|
41 | public PercentalChangeCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
|
---|
42 | : base(dataSet, columnGroupName, affectedColumns) {
|
---|
43 | oldColumns = new Dictionary<int, DoubleColumn>();
|
---|
44 | }
|
---|
45 |
|
---|
46 | public override string Description {
|
---|
47 | get { return "Calculate the percental change from last to the current value"; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public override void Execute() {
|
---|
51 | base.Execute();
|
---|
52 | foreach (int col in AffectedColumns) {
|
---|
53 | DoubleColumn doubleCol = ColumnGroup.GetColumn(col) as DoubleColumn;
|
---|
54 | if (doubleCol == null) throw new CommandExecutionException("Filtering is only supported for double columns.", this);
|
---|
55 | }
|
---|
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));
|
---|
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) {
|
---|
83 | DoubleColumn newCol = (DoubleColumn)oldColumn.CreateCopyOfColumnWithoutValues();
|
---|
84 | var data = oldColumn.ValuesEnumerable.Cast<double?>();
|
---|
85 |
|
---|
86 | var ans = new double?[] { null } // insert an empty value at the beginning
|
---|
87 | .Concat(data)
|
---|
88 | .Zip(data, (prev, cur) => (cur.HasValue && prev.HasValue) ? (cur - prev) / prev : null)
|
---|
89 | .ToArray();
|
---|
90 |
|
---|
91 | foreach (var e in ans) newCol.AddValue(e);
|
---|
92 |
|
---|
93 | newCol.SortOrder = oldColumn.SortOrder;
|
---|
94 | return newCol;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|