Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeValues/NormalDistributionScalingCommand.cs @ 15694

Last change on this file since 15694 was 9615, checked in by mkommend, 11 years ago

#1734: Updated copyright information in all DataImporter classes.

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