Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/TimeSeries/EquidistantSamplingCommand.cs @ 17603

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

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

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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.DataImporter.Command.View;
27using HeuristicLab.DataImporter.Data;
28using HeuristicLab.DataImporter.Data.CommandBase;
29using HeuristicLab.DataImporter.Data.Model;
30using HEAL.Attic;
31
32namespace HeuristicLab.DataImporter.Command {
33  [StorableType("68DF5755-DE5B-4DED-A73C-D234F503A428")]
34  [ViewableCommandInfoAttribute("Equidistant Sampling", 1, ColumnGroupState.Sorted, "Time Series",
35Position = 0, OptionsView = typeof(TimeBasedCommandView))]
36  public class EquidistantSamplingCommand : EquidistantTimeSeriesCommandBase {
37    private List<int> insertedRows;
38    private DateTimeColumn columnToSample;
39
40    [StorableConstructor]
41    protected EquidistantSamplingCommand(StorableConstructorFlag _) : base(_) { }
42    public EquidistantSamplingCommand(DataSet dataSet, string columnGroupName)
43      : base(dataSet, columnGroupName) {
44    }
45
46    public override string Description {
47      get { return "Equidistant Sampling"; }
48    }
49
50    public override void Execute() {
51      base.Execute();
52      if (!(ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.ElementAt(0)) is DateTimeColumn)
53    || ColumnGroup.SortedColumnIndexes.Count != 1)
54        throw new CommandExecutionException("ColumnGroup mus be sorted a the datetime column.", this);
55
56      columnToSample = (DateTimeColumn)ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.ElementAt(0));
57      if (columnToSample.SortOrder != SortOrder.Ascending)
58        throw new CommandExecutionException("ColumnGroup must be sorted ascending after the selected datetime column.", this);
59      if (columnToSample.ContainsNullValues)
60        throw new CommandExecutionException("The datetime column must not contain null values.", this);
61
62      for (int i = 1; i < columnToSample.TotalValuesCount; i++) {
63        if (columnToSample.GetValue(i) == columnToSample.GetValue(i - 1))
64          throw new CommandExecutionException("The datetime column must not contain duplicate values.", this);
65      }
66
67      insertedRows = new List<int>();
68      DateTime actPoint = StartTime;
69      int j = 0;
70      IComparable[] row;
71      while (j < columnToSample.TotalValuesCount &&
72        actPoint <= ((DateTime)columnToSample.GetValue(columnToSample.TotalValuesCount - 1)) + SampleFrequency) {
73        if (actPoint < (DateTime)columnToSample.GetValue(j)) {
74          row = ColumnGroup.GetEmptyRow();
75          row[ColumnGroup.SortedColumnIndexes.ElementAt(0)] = actPoint;
76          ColumnGroup.InsertRow(j, row);
77          actPoint = actPoint + SampleFrequency;
78          insertedRows.Add(j);
79        } else if (actPoint == (DateTime)columnToSample.GetValue(j))
80          actPoint = actPoint + SampleFrequency;
81        j++;
82      }
83      ColumnGroup.FireChanged();
84      this.ColumnGroup = null;
85    }
86
87    public override void UndoExecute() {
88      base.UndoExecute();
89      for (int i = insertedRows.Count - 1; i >= 0; i--)
90        ColumnGroup.DeleteRow(insertedRows[i]);
91      insertedRows.Clear();
92      ColumnGroup.FireChanged();
93      this.ColumnGroup = null;
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.