Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/TimeSeries/InterpolateMissingValues.cs @ 6689

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

#1471: added plugin for DbExplorer interfaces, deleted .resx files, set svn:ignore properties, and added license header

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