[6134] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9615] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6134] | 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 |
|
---|
[6133] | 22 | using System.Collections.Generic;
|
---|
[9614] | 23 | using HeuristicLab.DataImporter.Command.View;
|
---|
[6133] | 24 | using HeuristicLab.DataImporter.Data;
|
---|
| 25 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
| 26 | using HeuristicLab.DataImporter.Data.Model;
|
---|
[16566] | 27 | using HEAL.Attic;
|
---|
[6133] | 28 |
|
---|
| 29 | namespace HeuristicLab.DataImporter.Command {
|
---|
[16566] | 30 | [StorableType("B1E2D0F2-A927-4D6E-952E-8CF349E0CB6E")]
|
---|
[6133] | 31 | [ViewableCommandInfoAttribute("Moving Median", 1, ColumnGroupState.DoubleColumnSelected, "Change Values", Position = 3,
|
---|
| 32 | OptionsView = typeof(FilterCommandView))]
|
---|
| 33 | public class FilterMovingMedianCommand : FilterCommandBase {
|
---|
| 34 | private Dictionary<int, DoubleColumn> oldColumns;
|
---|
| 35 | private ICollection<int> oldSortedColumnIndices;
|
---|
| 36 |
|
---|
[9614] | 37 | [StorableConstructor]
|
---|
[16567] | 38 | protected FilterMovingMedianCommand(StorableConstructorFlag _) : base(_) {
|
---|
[6133] | 39 | oldColumns = new Dictionary<int, DoubleColumn>();
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public FilterMovingMedianCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
|
---|
| 43 | : base(dataSet, columnGroupName, affectedColumns) {
|
---|
| 44 | oldColumns = new Dictionary<int, DoubleColumn>();
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public override string Description {
|
---|
| 48 | get { return "Filter Column - Moving Median"; }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public override void Execute() {
|
---|
| 52 | base.Execute();
|
---|
| 53 | if (this.WindowSize < 1)
|
---|
| 54 | throw new CommandExecutionException("Window size for filter commands must no be smaller than 1.", this);
|
---|
| 55 | oldSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
|
---|
| 56 | DoubleColumn column;
|
---|
| 57 | foreach (int col in AffectedColumns) {
|
---|
| 58 | if (ColumnGroup.GetColumn(col) is DoubleColumn) {
|
---|
| 59 | column = (DoubleColumn)ColumnGroup.GetColumn(col);
|
---|
| 60 | oldColumns.Add(col, column);
|
---|
| 61 | ColumnGroup.ReplaceColumn(col, CalcNewColumn(column, this.WindowSize));
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
|
---|
| 65 | ColumnGroup.FireChanged();
|
---|
| 66 | ColumnGroup = null;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public override void UndoExecute() {
|
---|
| 70 | base.UndoExecute();
|
---|
| 71 | foreach (KeyValuePair<int, DoubleColumn> pair in oldColumns)
|
---|
| 72 | ColumnGroup.ReplaceColumn(pair.Key, pair.Value);
|
---|
| 73 | ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
|
---|
| 74 | oldSortedColumnIndices = null;
|
---|
| 75 | oldColumns.Clear();
|
---|
| 76 | ColumnGroup.FireChanged();
|
---|
| 77 | ColumnGroup = null;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | private DoubleColumn CalcNewColumn(DoubleColumn oldColumn, int windowSize) {
|
---|
| 81 | DoubleColumn newCol = (DoubleColumn)oldColumn.CreateCopyOfColumnWithoutValues();
|
---|
| 82 | int left = windowSize / 2;
|
---|
| 83 | int right = windowSize / 2;
|
---|
| 84 | if (windowSize % 2 == 0) left--;
|
---|
| 85 |
|
---|
| 86 | int startIndex = -1;
|
---|
| 87 | int endIndex = oldColumn.TotalValuesCount;
|
---|
| 88 | //first value newColumn[0]
|
---|
| 89 | newCol.AddValue(oldColumn.GetMedian(0, right >= oldColumn.TotalValuesCount ? oldColumn.TotalValuesCount : right + 1));
|
---|
| 90 | for (int i = 1; i < oldColumn.TotalValuesCount; i++) {
|
---|
| 91 | startIndex = i - left;
|
---|
| 92 | if (startIndex < 0)
|
---|
| 93 | startIndex = 0;
|
---|
| 94 | endIndex = i + right;
|
---|
| 95 | if (endIndex >= oldColumn.TotalValuesCount)
|
---|
| 96 | endIndex = oldColumn.TotalValuesCount - 1;
|
---|
| 97 | newCol.AddValue(oldColumn.GetMedian(startIndex, endIndex + 1));
|
---|
| 98 | }
|
---|
| 99 | newCol.SortOrder = oldColumn.SortOrder;
|
---|
| 100 | return newCol;
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|