Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/MissingValues/FillMissingValuesWithLinearInterpolation.cs @ 16567

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

#2520: changed StorableConstructors and added StorableType attributes in HeuristicLab.DataImporter addon

File size: 4.0 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 System.Linq;
24using System.Windows.Forms;
25using HeuristicLab.DataImporter.Data;
26using HeuristicLab.DataImporter.Data.CommandBase;
27using HeuristicLab.DataImporter.Data.Model;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HEAL.Attic;
30
31namespace HeuristicLab.DataImporter.Command {
32  [StorableType("FAA97EE8-F435-4DA0-8334-0F9F98C63A55")]
33  [ViewableCommandInfoAttribute("Linear Interpolation", 1, ColumnGroupState.DoubleColumnSelected | ColumnGroupState.AnySelectedColumnContainsNull,
34    "Handle Missing Values", Position = 2)]
35  public class FillMissingValuesWithLinearInterpolation : FillMissingValueCommandBase {
36    [StorableConstructor]
37    protected FillMissingValuesWithLinearInterpolation(StorableConstructorFlag _) : base(_) { }
38    public FillMissingValuesWithLinearInterpolation(DataSet dataSet, string columnGroupName, int[] affectedColumns) :
39      base(dataSet, columnGroupName, affectedColumns) {
40    }
41
42    public override string Description {
43      get { return "Interpolate missing values"; }
44    }
45
46    public override void Execute() {
47      base.Execute();
48      DoubleColumn column;
49      int startIndex = -1;
50      int endIndex = -1;
51      oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList();
52      oldSortedColumnIndexes = new List<int>(ColumnGroup.SortedColumnIndexes);
53      foreach (int col in AffectedColumns) {
54        if (ColumnGroup.GetColumn(col) is DoubleColumn && ColumnGroup.GetColumn(col).ContainsNullValues) {
55          column = (DoubleColumn)ColumnGroup.Columns.ElementAt(col);
56          if (column.SortOrder != SortOrder.None)
57            ColumnGroup.ResetSorting();
58          nullValues.Add(col, new List<int>());
59          startIndex = FindNextNotNullValueIndex(column, startIndex);
60          endIndex = FindNextNotNullValueIndex(column, startIndex);
61          while (endIndex != -1) {
62            if (endIndex - startIndex != 1) {
63              nullValues[col].AddRange(Enumerable.Range(startIndex + 1, endIndex - startIndex - 1));
64              InterpolateValues(column, startIndex, endIndex);
65            }
66            startIndex = endIndex;
67            endIndex = FindNextNotNullValueIndex(column, endIndex);
68          }
69          startIndex = -1;
70        }
71      }
72      ColumnGroup.FireChanged();
73      ColumnGroup = null;
74    }
75
76    private int FindNextNotNullValueIndex(DoubleColumn column, int startIndex) {
77      int endIndex = -1;
78      for (int i = startIndex + 1; i < column.TotalValuesCount && endIndex == -1; i++)
79        if (column.GetValue(i) != null)
80          endIndex = i;
81      return endIndex;
82    }
83
84    private void InterpolateValues(DoubleColumn column, int startIndex, int endIndex) {
85      double startValue = (double)column.GetValue(startIndex);
86      double endValue = (double)column.GetValue(endIndex);
87      double stepwidth = (endValue - startValue) / (endIndex - startIndex);
88
89      for (int i = startIndex + 1; i < endIndex; i++) {
90        if (column.GetValue(i) != null)
91          throw new CommandExecutionException("Tried to manipulate non null value!", this);
92        column.ChangeValue(i, startValue + stepwidth * (i - startIndex));
93      }
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.