Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/FeatureCorrelation/TimeframeFeatureCorrelationCalculator.cs @ 17991

Last change on this file since 17991 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 3.8 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Collections.Generic;
26using System.ComponentModel;
27using System.Linq;
28using HeuristicLab.PluginInfrastructure;
29
30namespace HeuristicLab.Problems.DataAnalysis.Views {
31  [NonDiscoverableType]
32  public class TimeframeFeatureCorrelationCalculator : AbstractFeatureCorrelationCalculator {
33    public TimeframeFeatureCorrelationCalculator() : base() { }
34
35    // returns true if any calculation takes place
36    public bool CalculateTimeframeElements(IDataAnalysisProblemData problemData, IDependencyCalculator calc, string partition, string variable, int frames, double[,] correlation = null) {
37      if (correlation != null && correlation.GetLength(1) > frames) return false;
38
39      var indices = GetRelevantIndices(problemData, partition);
40      var info = new BackgroundWorkerInfo {
41        Dataset = problemData.Dataset, Calculator = calc, Partition = partition, Indices = indices, Variable = variable, Frames = frames, AlreadyCalculated = correlation
42      };
43
44      StartCalculation(info);
45      return true;
46    }
47
48    protected override void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
49      BackgroundWorker worker = (BackgroundWorker)sender;
50      BackgroundWorkerInfo bwInfo = (BackgroundWorkerInfo)e.Argument;
51
52      var dataset = bwInfo.Dataset;
53      var indices = bwInfo.Indices.ToArray();
54      IDependencyCalculator calc = bwInfo.Calculator;
55      string variable = bwInfo.Variable;
56      int frames = bwInfo.Frames;
57      double[,] alreadyCalculated = bwInfo.AlreadyCalculated;
58
59      IList<string> doubleVariableNames = dataset.DoubleVariables.ToList();
60      int length = doubleVariableNames.Count;
61      double[,] elements = new double[length, frames + 1];
62      double calculations = (frames + 1) * length;
63
64      worker.ReportProgress(0);
65
66      int start = 0;
67      if (alreadyCalculated != null) {
68        for (int i = 0; i < alreadyCalculated.GetLength(0); i++) {
69          Array.Copy(alreadyCalculated, i * alreadyCalculated.GetLength(1), elements, i * elements.GetLength(1), alreadyCalculated.GetLength(1));
70        }
71        start = alreadyCalculated.GetLength(1);
72      }
73
74      var var1 = dataset.GetDoubleValues(variable, indices).ToArray();
75
76      for (int i = 0; i < length; i++) {
77        for (int j = start; j <= frames; j++) {
78          if (worker.CancellationPending) {
79            worker.ReportProgress(100);
80            e.Cancel = true;
81            return;
82          }
83
84          IEnumerable<double> var2 = dataset.GetDoubleValues(doubleVariableNames[i], indices);
85
86          var error = OnlineCalculatorError.None;
87          elements[i, j] = calc.Calculate(var1.Skip(j), var2.Take(var1.Length-j), out error);
88
89          if (!error.Equals(OnlineCalculatorError.None)) {
90            elements[i, j] = double.NaN;
91          }
92          worker.ReportProgress((int)((100.0 / calculations) * (i * (frames + 1) + j + 1)));
93        }
94      }
95      e.Result = elements;
96      worker.ReportProgress(100);
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.