Free cookie consent management tool by TermsFeed Policy Generator

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