Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/TimeSeries/InterpolateMissingValues.cs @ 16994

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

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

File size: 4.8 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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.DataImporter.Data;
27using HeuristicLab.DataImporter.Data.CommandBase;
28using HeuristicLab.DataImporter.Data.Model;
29using HEAL.Attic;
30
31namespace HeuristicLab.DataImporter.Command {
32  [StorableType("3975A950-D715-48C8-91FB-C982DA6887CA")]
33  [ViewableCommandInfoAttribute("Interpolate Missing Values", 1, ColumnGroupState.Sorted | ColumnGroupState.DoubleColumnSelected, "Time Series",
34Position = 2)]
35  public class InterpolateMissingValues : FillMissingValueCommandBase {
36    private DateTimeColumn columnToSample;
37
38    [StorableConstructor]
39    protected InterpolateMissingValues(StorableConstructorFlag _) : base(_) { }
40    public InterpolateMissingValues(DataSet dataSet, string columnGroupName, int[] affectedColumns)
41      : base(dataSet, columnGroupName, affectedColumns) {
42    }
43
44    public override string Description {
45      get { return "Interpolate Missing Values"; }
46    }
47
48    public override void Execute() {
49      base.Execute();
50      if (!(ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.ElementAt(0)) is DateTimeColumn)
51   || ColumnGroup.SortedColumnIndexes.Count != 1)
52        throw new CommandExecutionException("ColumnGroup must be sorted after a datetime column.", this);
53      columnToSample = (DateTimeColumn)ColumnGroup.Columns.ElementAt(ColumnGroup.SortedColumnIndexes.ElementAt(0));
54      if (columnToSample.SortOrder != SortOrder.Ascending)
55        throw new CommandExecutionException("ColumnGroup must be sorted ascending by the datetime column.", this);
56      if (columnToSample.ContainsNullValues)
57        throw new CommandExecutionException("The datetime column must not contain null values.", this);
58
59      oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList();
60      oldSortedColumnIndexes = new List<int>(ColumnGroup.SortedColumnIndexes);
61      DoubleColumn column;
62      int startIndex = -1;
63      int endIndex = -1;
64      foreach (int col in AffectedColumns) {
65        if (ColumnGroup.GetColumn(col) is DoubleColumn && ColumnGroup.GetColumn(col).ContainsNullValues) {
66          column = (DoubleColumn)ColumnGroup.Columns.ElementAt(col);
67          nullValues.Add(col, new List<int>());
68          startIndex = FindNextNotNullValueIndex(column, startIndex);
69          endIndex = FindNextNotNullValueIndex(column, startIndex);
70          while (endIndex != -1) {
71            if (endIndex - startIndex != 1) {
72              nullValues[col].AddRange(Enumerable.Range(startIndex + 1, endIndex - startIndex - 1));
73              InterpolateValues(column, startIndex, endIndex);
74            }
75            startIndex = endIndex;
76            endIndex = FindNextNotNullValueIndex(column, endIndex);
77          }
78          startIndex = -1;
79        }
80      }
81      ColumnGroup.FireChanged();
82      columnToSample = null;
83      this.ColumnGroup = null;
84    }
85
86    private int FindNextNotNullValueIndex(DoubleColumn column, int startIndex) {
87      int endIndex = -1;
88      for (int i = startIndex + 1; i < column.TotalValuesCount && endIndex == -1; i++)
89        if (column.GetValue(i) != null)
90          endIndex = i;
91      return endIndex;
92    }
93
94    private void InterpolateValues(DoubleColumn column, int startIndex, int endIndex) {
95      double startValue = (double)column.GetValue(startIndex);
96      double endValue = (double)column.GetValue(endIndex);
97      DateTime start = (DateTime)columnToSample.GetValue(startIndex);
98      DateTime end = (DateTime)columnToSample.GetValue(endIndex);
99
100      for (int i = startIndex + 1; i < endIndex; i++) {
101        if (column.GetValue(i) != null)
102          throw new CommandExecutionException("Tried to manipulate non null value.", this);
103        column.ChangeValue(i, ((double)(((DateTime)columnToSample.GetValue(i) - start).TotalSeconds / (end - start).TotalSeconds)) *
104          (endValue - startValue) + startValue);
105      }
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.