#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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.Windows.Forms; 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("Scaling between Min/Max", 1, ColumnGroupState.DoubleColumnSelected, "Change Values", Position = 4, OptionsView = typeof(MinMaxCommandView))] public class ScalingBetweenMinAndMaxCommand : ColumnGroupCommandWithAffectedColumnsBase { private List oldMinValues; private List oldMaxValues; private ScalingBetweenMinAndMaxCommand() : base(null, string.Empty, null) { oldMinValues = new List(); oldMaxValues = new List(); } public ScalingBetweenMinAndMaxCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns) : base(dataSet, columnGroupName, affectedColumns) { oldMinValues = new List(); oldMaxValues = new List(); } public override string Description { get { return "Scale Values between minimum and maximum"; } } [Storable] private double minValue; public double MinValue { get { return this.minValue; } set { this.minValue = value; } } [Storable] private double maxValue; public double MaxValue { get { return maxValue; } set { this.maxValue = value; } } public override void Execute() { base.Execute(); if (maxValue < minValue) throw new CommandExecutionException("Minimum must be smaller or equal than Maximum.", this); DoubleColumn column; 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.ContainsNotNullValues) { oldMinValues.Add((double)column.Minimum); oldMaxValues.Add((double)column.Maximum); ScaleLinear(column, oldMinValues[j], oldMaxValues[j], minValue, maxValue); j++; } } } ColumnGroup.FireChanged(); ColumnGroup = null; } public override void UndoExecute() { base.UndoExecute(); DoubleColumn column; int j = 0; for (int i = 0; i < AffectedColumns.Length; i++) { if (ColumnGroup.GetColumn(i) is DoubleColumn) { column = (DoubleColumn)ColumnGroup.GetColumn(AffectedColumns[i]); if (column.ContainsNotNullValues) { oldMinValues.Add((double)column.Minimum); oldMaxValues.Add((double)column.Maximum); ScaleLinear(column, minValue, maxValue, oldMinValues[j], oldMaxValues[j]); j++; } } } oldMinValues.Clear(); oldMaxValues.Clear(); ColumnGroup.FireChanged(); ColumnGroup = null; } private void ScaleLinear(DoubleColumn column, double oldMin, double oldMax, double newMin, double newMax) { double newRange = newMax - newMin; double? actValue; double oldRange = oldMax - oldMin; if (oldRange == 0) oldRange = 1; for (int i = 0; i < column.TotalValuesCount; i++) { actValue = (double?)column.GetValue(i); if (actValue != null) column.ChangeValue(i, newMin + newRange * (((double)actValue) - oldMin) / oldRange); } } } }