Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeValues/PercentalChangeCommand.cs @ 16566

Last change on this file since 16566 was 16566, checked in by gkronber, 6 years ago

#2520: updated HeuristicLab.DataImporter addon for new Persistence (introduced in 3.3.16)

File size: 3.8 KB
RevLine 
[6708]1#region License Information
2/* HeuristicLab
[9615]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6708]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;
[16566]28using HEAL.Attic;
[6708]29
30namespace HeuristicLab.DataImporter.Command {
[16566]31  [StorableType("EE8DF207-4544-486C-99EF-503CE0A09B0A")]
[6708]32  [ViewableCommandInfoAttribute("Percental Change", 1, ColumnGroupState.DoubleColumnSelected, "Change Values", Position = 3)]
33  public class PercentalChangeCommand : ColumnGroupCommandWithAffectedColumnsBase {
34    private Dictionary<int, DoubleColumn> oldColumns;
35    private ICollection<int> oldSortedColumnIndices;
36
[9614]37    [StorableConstructor]
38    protected PercentalChangeCommand(bool deserializing)
39      : base(deserializing) {
[6708]40      oldColumns = new Dictionary<int, DoubleColumn>();
41    }
42
43    public PercentalChangeCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
44      : base(dataSet, columnGroupName, affectedColumns) {
45      oldColumns = new Dictionary<int, DoubleColumn>();
46    }
47
48    public override string Description {
49      get { return "Calculate the percental change from last to the current value"; }
50    }
51
52    public override void Execute() {
53      base.Execute();
54      foreach (int col in AffectedColumns) {
55        DoubleColumn doubleCol = ColumnGroup.GetColumn(col) as DoubleColumn;
56        if (doubleCol == null) throw new CommandExecutionException("Filtering is only supported for double columns.", this);
57      }
58      DoubleColumn column;
59      oldSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
60      foreach (int col in AffectedColumns) {
61        if (ColumnGroup.GetColumn(col) is DoubleColumn) {
62          column = (DoubleColumn)ColumnGroup.GetColumn(col);
63          oldColumns.Add(col, column);
64          ColumnGroup.ReplaceColumn(col, CalcNewColumn(column));
65        }
66      }
67      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
68      ColumnGroup.FireChanged();
69      ColumnGroup = null;
70    }
71
72    public override void UndoExecute() {
73      base.UndoExecute();
74      foreach (KeyValuePair<int, DoubleColumn> pair in oldColumns)
75        ColumnGroup.ReplaceColumn(pair.Key, pair.Value);
76
77      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
78      oldSortedColumnIndices = null;
79      oldColumns.Clear();
80      ColumnGroup.FireChanged();
81      ColumnGroup = null;
82    }
83
84    private DoubleColumn CalcNewColumn(DoubleColumn oldColumn) {
85      DoubleColumn newCol = (DoubleColumn)oldColumn.CreateCopyOfColumnWithoutValues();
86      var data = oldColumn.ValuesEnumerable.Cast<double?>();
87
88      var ans = new double?[] { null } // insert an empty value at the beginning
89      .Concat(data)
90      .Zip(data, (prev, cur) => (cur.HasValue && prev.HasValue) ? (cur - prev) / prev : null)
91      .ToArray();
92
93      foreach (var e in ans) newCol.AddValue(e);
94
95      newCol.SortOrder = oldColumn.SortOrder;
96      return newCol;
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.