Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/FeatureCorrelation/FeatureCorrelationCalculator.cs @ 14469

Last change on this file since 14469 was 14469, checked in by mkommend, 7 years ago

#2717: Readded accidentally dropped line for progress reporting.

File size: 4.1 KB
RevLine 
[8578]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8578]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.ComponentModel;
25using System.Linq;
[13938]26using System.Threading.Tasks;
[8578]27using HeuristicLab.PluginInfrastructure;
28
[8729]29namespace HeuristicLab.Problems.DataAnalysis.Views {
30  [NonDiscoverableType]
[13938]31  public sealed class FeatureCorrelationCalculator : AbstractFeatureCorrelationCalculator {
32    public FeatureCorrelationCalculator() : base() { }
[8578]33
[13938]34    public void CalculateElements(IDataAnalysisProblemData problemData, IDependencyCalculator calc, string partition, bool ignoreMissingValues) {
[8880]35      var indices = GetRelevantIndices(problemData, partition);
[13938]36      var info = new BackgroundWorkerInfo {
37        Dataset = problemData.Dataset, Calculator = calc, Partition = partition, Indices = indices, IgnoreMissingValues = ignoreMissingValues
[8880]38      };
[8578]39
[13938]40      StartCalculation(info);
[8880]41    }
42
[13938]43    protected override void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
44      BackgroundWorker worker = (BackgroundWorker)sender;
[8578]45      BackgroundWorkerInfo bwInfo = (BackgroundWorkerInfo)e.Argument;
46
[12509]47      var dataset = bwInfo.Dataset;
[13938]48      var indices = bwInfo.Indices.ToArray();
[8833]49      IDependencyCalculator calc = bwInfo.Calculator;
[8578]50
51      IList<string> doubleVariableNames = dataset.DoubleVariables.ToList();
[14468]52
[8578]53      int length = doubleVariableNames.Count;
54      double[,] elements = new double[length, length];
55
56      worker.ReportProgress(0);
57
[13938]58      for (int counter = 0; counter < length; counter++) {
59        if (worker.CancellationPending) {
60          worker.ReportProgress(100);
61          e.Cancel = true;
62          return;
63        }
[8578]64
[13938]65        var i = counter;
66        Parallel.ForEach(Enumerable.Range(i, length - i), j => {
67          var var1 = dataset.GetDoubleValues(doubleVariableNames[i], indices);
68          var var2 = dataset.GetDoubleValues(doubleVariableNames[j], indices);
[8578]69
[14468]70          OnlineCalculatorError error = OnlineCalculatorError.None;
[13938]71          if (bwInfo.IgnoreMissingValues) {
72            var filtered = FilterNaNValues(var1, var2);
73            elements[i, j] = calc.Calculate(filtered, out error);
74          } else
75            elements[i, j] = calc.Calculate(var1, var2, out error);
76
[8578]77          if (!error.Equals(OnlineCalculatorError.None)) {
[8689]78            elements[i, j] = double.NaN;
[8578]79          }
[8689]80          elements[j, i] = elements[i, j];
[14468]81        });
[14469]82        worker.ReportProgress((int)(((double)counter) / length * 100));
[8578]83      }
[14468]84
[8578]85      e.Result = elements;
[8833]86      worker.ReportProgress(100);
[8578]87    }
88
89
[13938]90    private static IEnumerable<Tuple<double, double>> FilterNaNValues(IEnumerable<double> first, IEnumerable<double> second) {
91      var firstEnumerator = first.GetEnumerator();
92      var secondEnumerator = second.GetEnumerator();
[8578]93
[13938]94      while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
95        var firstValue = firstEnumerator.Current;
96        var secondValue = secondEnumerator.Current;
[8578]97
[13938]98        if (double.IsNaN(firstValue)) continue;
99        if (double.IsNaN(secondValue)) continue;
[8578]100
[13938]101        yield return Tuple.Create(firstValue, secondValue);
[8578]102      }
103
[13938]104      if (firstEnumerator.MoveNext() || secondEnumerator.MoveNext()) {
105        throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
[8578]106      }
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.