Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeValues/ScalingBetweenMinAndMaxCommand.cs @ 16566

Last change on this file since 16566 was 16566, checked in by gkronber, 5 years ago

#2520: updated HeuristicLab.DataImporter addon for new Persistence (introduced in 3.3.16)

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