Free cookie consent management tool by TermsFeed Policy Generator

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