Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2520 Update plugin dependencies and references for HL.DataImporter for new persistence

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