Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeValues/NormalDistributionScalingCommand.cs @ 7267

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

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

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