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