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

Last change on this file since 13938 was 13938, checked in by mkommend, 5 years ago

#2619:

  • Refactored and separated the different feature correlation calculations.
  • Added a checkbox to ignore missing values in the calculation.
File size: 4.0 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2016 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      for (int i = 0; i < length; i++) {
75        for (int j = start; j <= frames; j++) {
76          if (worker.CancellationPending) {
77            worker.ReportProgress(100);
78            e.Cancel = true;
79            return;
80          }
81
82          IEnumerable<double> var1 = dataset.GetDoubleValues(variable, indices);
83          IEnumerable<double> var2 = dataset.GetDoubleValues(doubleVariableNames[i], indices);
84
85          var valuesInFrame = var1.Take(j);
86          var help = var1.Skip(j).ToList();
87          help.AddRange(valuesInFrame);
88          var1 = help;
89
90          var error = OnlineCalculatorError.None;
91          elements[i, j] = calc.Calculate(var1, var2, out error);
92
93          if (!error.Equals(OnlineCalculatorError.None)) {
94            elements[i, j] = double.NaN;
95          }
96          worker.ReportProgress((int)((100.0 / calculations) * (i * (frames + 1) + j + 1)));
97        }
98      }
99      e.Result = elements;
100      worker.ReportProgress(100);
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.