#region License Information /* HeuristicLab * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using HeuristicLab.DataImporter.Data; using HeuristicLab.DataImporter.Data.CommandBase; using HeuristicLab.DataImporter.Data.Model; using HeuristicLab.DataImporter.Command.View; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.DataImporter.Command { [StorableClass] [ViewableCommandInfoAttribute("Scale to normal distribution", 1, ColumnGroupState.DoubleColumnSelected, "Change Values", Position = 5, OptionsView = typeof(NormalDistributionCommandView))] public class NormalDistributionScalingCommand : ColumnGroupCommandWithAffectedColumnsBase { private List> oldNormalDistribution; private NormalDistributionScalingCommand() : base(null, string.Empty, null) { oldNormalDistribution = new List>(); } public NormalDistributionScalingCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns) : base(dataSet, columnGroupName, affectedColumns) { oldNormalDistribution = new List>(); } public override string Description { get { return "Scale to normal distribution"; } } [Storable] private double newMean; public double Mean { get { return this.newMean; } set { this.newMean = value; } } [Storable] private double newStddev; public double StandardDeviation { get { return this.newStddev; } set { this.newStddev = value; } } public override void Execute() { base.Execute(); DoubleColumn column; double oldMean; double oldStddev; for (int i = 0; i < AffectedColumns.Length; i++) { if (ColumnGroup.GetColumn(AffectedColumns[i]) is DoubleColumn) { column = (DoubleColumn)ColumnGroup.GetColumn(AffectedColumns[i]); if (column.NonNullValuesCount == 0) continue; oldMean = (double)column.Mean; oldStddev = (double)column.StandardDeviation; CalculateNewColumn(column, newMean, newStddev, oldMean, oldStddev); oldNormalDistribution.Add(new KeyValuePair(oldMean, oldStddev)); } } ColumnGroup.FireChanged(); ColumnGroup = null; } public override void UndoExecute() { base.UndoExecute(); DoubleColumn column; double oldMean; double oldStddev; int j = 0; for (int i = 0; i < AffectedColumns.Length; i++) { if (ColumnGroup.GetColumn(AffectedColumns[i]) is DoubleColumn) { column = (DoubleColumn)ColumnGroup.GetColumn(AffectedColumns[i]); if (column.NonNullValuesCount == 0) continue; oldMean = oldNormalDistribution[j].Key; oldStddev = oldNormalDistribution[j].Value; CalculateNewColumn(column, oldMean, oldStddev, newMean, newStddev); j++; } } oldNormalDistribution.Clear(); ColumnGroup.FireChanged(); ColumnGroup = null; } private void CalculateNewColumn(DoubleColumn column, double newMean, double newStddev, double oldMean, double oldStddev) { double newValue; double oldValue; for (int val = 0; val < column.TotalValuesCount; val++) { if (column.GetValue(val) == null) continue; oldValue = (double)column.GetValue(val); if (oldStddev == 0) newValue = oldValue - oldMean + newMean; else newValue = ((oldValue - oldMean) / oldStddev) * newStddev + newMean; column.ChangeValue(val, newValue); } } } }