Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeValues/BoxCoxTransformationCommand.cs @ 6133

Last change on this file since 6133 was 6133, checked in by gkronber, 14 years ago

#1471: imported generic parts of DataImporter from private code base

File size: 2.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Xml;
6using HeuristicLab.DataImporter.Data;
7using HeuristicLab.DataImporter.Data.CommandBase;
8using HeuristicLab.DataImporter.Data.Model;
9using HeuristicLab.DataImporter.Command.View;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11
12namespace HeuristicLab.DataImporter.Command {
13  [StorableClass]
14  [ViewableCommandInfoAttribute("Box-Cox Transformation", 1, ColumnGroupState.DoubleColumnSelected, "Change Values",
15  Position = 6, OptionsView = typeof(BoxCoxTransformationCommandView))]
16  public class BoxCoxTransformationCommand : ColumnGroupCommandWithAffectedColumnsBase {
17    private BoxCoxTransformationCommand()
18      : base(null, string.Empty, null) {
19    }
20
21    public BoxCoxTransformationCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
22      : base(dataSet, columnGroupName, affectedColumns) {
23    }
24
25    [Storable]
26    private double lambda;
27    public double Lambda {
28      get { return this.lambda; }
29      set { this.lambda = value; }
30    }
31    [Storable]
32    private double c;
33    public double C {
34      get { return this.c; }
35      set { this.c = value; }
36    }
37
38    public override string Description {
39      get { return "Box-Cox Transformation"; }
40    }
41
42    public override void Execute() {
43      base.Execute();
44      DoubleColumn column;
45      foreach (int col in AffectedColumns) {
46        if (ColumnGroup.GetColumn(col) is DoubleColumn) {
47          column = (DoubleColumn)ColumnGroup.GetColumn(col);
48          for (int i = 0; i < column.TotalValuesCount; i++)
49            if (column.GetValue(i) != null) {
50              if (lambda != 0.0) {
51                column.ChangeValue(i, Math.Pow((double)column.GetValue(i) + c, lambda) - 1);
52              } else {
53                column.ChangeValue(i, Math.Log((double)column.GetValue(i) + c));
54              }
55            }
56        }
57      }
58      ColumnGroup.FireChanged();
59      ColumnGroup = null;
60    }
61
62    public override void UndoExecute() {
63      base.UndoExecute();
64      DoubleColumn column;
65      foreach (int col in AffectedColumns) {
66        if (ColumnGroup.GetColumn(col) is DoubleColumn) {
67          column = (DoubleColumn)ColumnGroup.GetColumn(col);
68          for (int i = 0; i < column.TotalValuesCount; i++)
69            if (column.GetValue(i) != null) {
70              if (lambda != 0.0) {
71                column.ChangeValue(i, Math.Pow((double)column.GetValue(i) + 1, 1.0 / lambda) - c);
72              } else {
73                column.ChangeValue(i, Math.Exp((double)column.GetValue(i)) - c);
74              }
75            }
76        }
77      }
78      ColumnGroup.FireChanged();
79      ColumnGroup = null;
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.