Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7461 was 7267, checked in by gkronber, 13 years ago

#1734 updated copyright year in all files of the DataImporter branch

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