[6134] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7267] | 3 | * Copyright (C) 2002-2012 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 |
|
---|
| 22 | using System;
|
---|
[6133] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using System.Windows.Forms;
|
---|
| 27 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
| 28 | using HeuristicLab.DataImporter.Data.Model;
|
---|
| 29 | using HeuristicLab.DataImporter.Data;
|
---|
| 30 | using HeuristicLab.DataImporter.Command.View;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 |
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.DataImporter.Command {
|
---|
| 35 | [StorableClass]
|
---|
| 36 | [ViewableCommandInfoAttribute("Interpolate Missing Values", 1, ColumnGroupState.Sorted | ColumnGroupState.DoubleColumnSelected, "Time Series",
|
---|
| 37 | Position = 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 | }
|
---|