Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeValues/ScalingBetweenMinAndMaxCommand.cs @ 10879

Last change on this file since 10879 was 9615, checked in by mkommend, 12 years ago

#1734: Updated copyright information in all DataImporter classes.

File size: 4.4 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("Scaling between Min/Max", 1, ColumnGroupState.DoubleColumnSelected, "Change Values",
32 Position = 4, OptionsView = typeof(MinMaxCommandView))]
33  public class ScalingBetweenMinAndMaxCommand : ColumnGroupCommandWithAffectedColumnsBase {
34    private List<double> oldMinValues;
35    private List<double> oldMaxValues;
36
37    [StorableConstructor]
38    protected ScalingBetweenMinAndMaxCommand(bool deserializing)
39      : base(deserializing) {
40      oldMinValues = new List<double>();
41      oldMaxValues = new List<double>();
42    }
43
44    public ScalingBetweenMinAndMaxCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
45      : base(dataSet, columnGroupName, affectedColumns) {
46      oldMinValues = new List<double>();
47      oldMaxValues = new List<double>();
48    }
49
50    public override string Description {
51      get { return "Scale Values between minimum and maximum"; }
52    }
53
54    [Storable]
55    private double minValue;
56    public double MinValue {
57      get { return this.minValue; }
58      set { this.minValue = value; }
59    }
60
61    [Storable]
62    private double maxValue;
63    public double MaxValue {
64      get { return maxValue; }
65      set { this.maxValue = value; }
66    }
67
68    public override void Execute() {
69      base.Execute();
70      if (maxValue < minValue)
71        throw new CommandExecutionException("Minimum must be smaller or equal than Maximum.", this);
72      DoubleColumn column;
73      int j = 0;
74      for (int i = 0; i < AffectedColumns.Length; i++) {
75        if (ColumnGroup.GetColumn(AffectedColumns[i]) is DoubleColumn) {
76          column = (DoubleColumn)ColumnGroup.GetColumn(AffectedColumns[i]);
77          if (column.ContainsNotNullValues) {
78            oldMinValues.Add((double)column.Minimum);
79            oldMaxValues.Add((double)column.Maximum);
80            ScaleLinear(column, oldMinValues[j], oldMaxValues[j], minValue, maxValue);
81            j++;
82          }
83        }
84      }
85      ColumnGroup.FireChanged();
86      ColumnGroup = null;
87    }
88
89    public override void UndoExecute() {
90      base.UndoExecute();
91      DoubleColumn column;
92      int j = 0;
93      for (int i = 0; i < AffectedColumns.Length; i++) {
94        if (ColumnGroup.GetColumn(i) is DoubleColumn) {
95          column = (DoubleColumn)ColumnGroup.GetColumn(AffectedColumns[i]);
96          if (column.ContainsNotNullValues) {
97            oldMinValues.Add((double)column.Minimum);
98            oldMaxValues.Add((double)column.Maximum);
99            ScaleLinear(column, minValue, maxValue, oldMinValues[j], oldMaxValues[j]);
100            j++;
101          }
102        }
103      }
104      oldMinValues.Clear();
105      oldMaxValues.Clear();
106      ColumnGroup.FireChanged();
107      ColumnGroup = null;
108    }
109
110    private void ScaleLinear(DoubleColumn column, double oldMin, double oldMax, double newMin, double newMax) {
111      double newRange = newMax - newMin;
112      double? actValue;
113      double oldRange = oldMax - oldMin;
114      if (oldRange == 0)
115        oldRange = 1;
116      for (int i = 0; i < column.TotalValuesCount; i++) {
117        actValue = (double?)column.GetValue(i);
118        if (actValue != null)
119          column.ChangeValue(i, newMin + newRange * (((double)actValue) - oldMin) / oldRange);
120      }
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.