Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeValues/PercentalChangeCommand.cs @ 14243

Last change on this file since 14243 was 9615, checked in by mkommend, 12 years ago

#1734: Updated copyright information in all DataImporter classes.

File size: 3.8 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.DataImporter.Data;
25using HeuristicLab.DataImporter.Data.CommandBase;
26using HeuristicLab.DataImporter.Data.Model;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.DataImporter.Command {
30  [StorableClass]
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(bool deserializing)
38      : base(deserializing) {
39      oldColumns = new Dictionary<int, DoubleColumn>();
40    }
41
42    public PercentalChangeCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
43      : base(dataSet, columnGroupName, affectedColumns) {
44      oldColumns = new Dictionary<int, DoubleColumn>();
45    }
46
47    public override string Description {
48      get { return "Calculate the percental change from last to the current value"; }
49    }
50
51    public override void Execute() {
52      base.Execute();
53      foreach (int col in AffectedColumns) {
54        DoubleColumn doubleCol = ColumnGroup.GetColumn(col) as DoubleColumn;
55        if (doubleCol == null) throw new CommandExecutionException("Filtering is only supported for double columns.", this);
56      }
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));
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) {
84      DoubleColumn newCol = (DoubleColumn)oldColumn.CreateCopyOfColumnWithoutValues();
85      var data = oldColumn.ValuesEnumerable.Cast<double?>();
86
87      var ans = new double?[] { null } // insert an empty value at the beginning
88      .Concat(data)
89      .Zip(data, (prev, cur) => (cur.HasValue && prev.HasValue) ? (cur - prev) / prev : null)
90      .ToArray();
91
92      foreach (var e in ans) newCol.AddValue(e);
93
94      newCol.SortOrder = oldColumn.SortOrder;
95      return newCol;
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.