Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeValues/NormalDistributionScalingCommand.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: 4.6 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 HeuristicLab.DataImporter.Command.View;
24using HeuristicLab.DataImporter.Data;
25using HeuristicLab.DataImporter.Data.CommandBase;
26using HeuristicLab.DataImporter.Data.Model;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HEAL.Attic;
29
30namespace HeuristicLab.DataImporter.Command {
31  [StorableType("44C1FF7C-84EA-4BBA-B8AE-17D8D1B5DC17")]
32  [ViewableCommandInfoAttribute("Scale to normal distribution", 1, ColumnGroupState.DoubleColumnSelected, "Change Values",
33   Position = 5, OptionsView = typeof(NormalDistributionCommandView))]
34  public class NormalDistributionScalingCommand : ColumnGroupCommandWithAffectedColumnsBase {
35    private List<KeyValuePair<double, double>> oldNormalDistribution;
36
37    [StorableConstructor]
38    protected NormalDistributionScalingCommand(bool deserializing)
39      : base(deserializing) {
40      oldNormalDistribution = new List<KeyValuePair<double, double>>();
41    }
42
43    public NormalDistributionScalingCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
44      : base(dataSet, columnGroupName, affectedColumns) {
45      oldNormalDistribution = new List<KeyValuePair<double, double>>();
46    }
47
48    public override string Description {
49      get { return "Scale to normal distribution"; }
50    }
51
52    [Storable]
53    private double newMean;
54    public double Mean {
55      get { return this.newMean; }
56      set { this.newMean = value; }
57    }
58
59    [Storable]
60    private double newStddev;
61    public double StandardDeviation {
62      get { return this.newStddev; }
63      set { this.newStddev = value; }
64    }
65
66    public override void Execute() {
67      base.Execute();
68      DoubleColumn column;
69      double oldMean;
70      double oldStddev;
71      for (int i = 0; i < AffectedColumns.Length; i++) {
72        if (ColumnGroup.GetColumn(AffectedColumns[i]) is DoubleColumn) {
73          column = (DoubleColumn)ColumnGroup.GetColumn(AffectedColumns[i]);
74          if (column.NonNullValuesCount == 0)
75            continue;
76          oldMean = (double)column.Mean;
77          oldStddev = (double)column.StandardDeviation;
78          CalculateNewColumn(column, newMean, newStddev, oldMean, oldStddev);
79          oldNormalDistribution.Add(new KeyValuePair<double, double>(oldMean, oldStddev));
80        }
81      }
82      ColumnGroup.FireChanged();
83      ColumnGroup = null;
84    }
85
86    public override void UndoExecute() {
87      base.UndoExecute();
88      DoubleColumn column;
89      double oldMean;
90      double oldStddev;
91      int j = 0;
92      for (int i = 0; i < AffectedColumns.Length; i++) {
93        if (ColumnGroup.GetColumn(AffectedColumns[i]) is DoubleColumn) {
94          column = (DoubleColumn)ColumnGroup.GetColumn(AffectedColumns[i]);
95          if (column.NonNullValuesCount == 0)
96            continue;
97          oldMean = oldNormalDistribution[j].Key;
98          oldStddev = oldNormalDistribution[j].Value;
99          CalculateNewColumn(column, oldMean, oldStddev, newMean, newStddev);
100          j++;
101        }
102      }
103      oldNormalDistribution.Clear();
104      ColumnGroup.FireChanged();
105      ColumnGroup = null;
106
107    }
108
109    private void CalculateNewColumn(DoubleColumn column, double newMean, double newStddev, double oldMean, double oldStddev) {
110      double newValue;
111      double oldValue;
112      for (int val = 0; val < column.TotalValuesCount; val++) {
113        if (column.GetValue(val) == null)
114          continue;
115        oldValue = (double)column.GetValue(val);
116        if (oldStddev == 0)
117          newValue = oldValue - oldMean + newMean;
118        else
119          newValue = ((oldValue - oldMean) / oldStddev) * newStddev + newMean;
120        column.ChangeValue(val, newValue);
121      }
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.