Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/TimeSeries/DeleteNotEquidistantRowsCommand.cs @ 16566

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

#2520: updated HeuristicLab.DataImporter addon for new Persistence (introduced in 3.3.16)

File size: 4.6 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("6849AAF1-2FD7-4755-84A6-DEDF44BDC97C")]
35  [ViewableCommandInfoAttribute("Delete not equidistant rows", 1, ColumnGroupState.Sorted, "Time Series",
36Position = 1, OptionsView = typeof(TimeBasedCommandView))]
37  public class DeleteNotEquidistantRowsCommand : EquidistantTimeSeriesCommandBase {
38    private DateTimeColumn columnToSample;
39    private List<ColumnBase> oldColumns;
40    private ICollection<int> oldSortedColumnIndices;
41    private IEnumerable<SortOrder> oldSortOrder;
42
43    [StorableConstructor]
44    protected DeleteNotEquidistantRowsCommand(bool deserializing)
45      : base(deserializing) {
46      oldColumns = new List<ColumnBase>();
47    }
48
49    public DeleteNotEquidistantRowsCommand(DataSet dataSet, string columnGroupName)
50      : base(dataSet, columnGroupName) {
51      oldColumns = new List<ColumnBase>();
52    }
53
54    public override string Description {
55      get { return "Delete not equidistant rows"; }
56    }
57
58    public override void Execute() {
59      base.Execute();
60      if (!(ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.ElementAt(0)) is DateTimeColumn)
61        || ColumnGroup.SortedColumnIndexes.Count != 1)
62        throw new CommandExecutionException("ColumnGroup must be sorted after a datetime column.", this);
63      columnToSample = (DateTimeColumn)ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.ElementAt(0));
64      if (columnToSample.SortOrder != SortOrder.Ascending)
65        throw new CommandExecutionException("ColumnGroup must be sorted ascending after the selected datetime column.", this);
66      if (columnToSample.ContainsNullValues)
67        throw new CommandExecutionException("The datetime column must not contain null values.", this);
68
69      oldSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
70      oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList();
71
72      DateTime value;
73      DateTime actPoint = StartTime;
74      ColumnGroup tempColumnGroup = new ColumnGroup();
75      List<int> positions = new List<int>();
76
77      for (int i = 0; i < ColumnGroup.RowCount; i++) {
78        value = (DateTime)columnToSample.GetValue(i);
79        while (actPoint < value)
80          actPoint += SampleFrequency;
81        if (actPoint == value) {
82          positions.Add(i);
83        }
84      }
85
86      foreach (ColumnBase x in ColumnGroup.Columns) {
87        oldColumns.Add(x);
88        tempColumnGroup.AddColumn(x.CreateCopyOfColumnWithoutValues());
89      }
90
91      for (int i = 0; i < positions.Count; i++)
92        tempColumnGroup.AddRow(ColumnGroup.GetRow(positions[i]));
93
94      for (int i = 0; i < ColumnGroup.Columns.Count(); i++)
95        ColumnGroup.ReplaceColumn(i, tempColumnGroup.Columns.ElementAt(i));
96
97      ColumnGroup.SortOrdersForColumns = oldSortOrder;
98      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
99      ColumnGroup.FireChanged();
100      this.ColumnGroup = null;
101    }
102
103    public override void UndoExecute() {
104      base.UndoExecute();
105      for (int i = 0; i < ColumnGroup.Columns.Count(); i++)
106        ColumnGroup.ReplaceColumn(i, oldColumns[i]);
107      ColumnGroup.SortOrdersForColumns = oldSortOrder;
108      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
109      oldSortedColumnIndices = null;
110      oldSortOrder = null;
111      oldColumns.Clear();
112      ColumnGroup.FireChanged();
113      this.ColumnGroup = null;
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.