Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14185 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

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();
[8689]52      OnlineCalculatorError error = OnlineCalculatorError.None;
[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
[13938]70          if (bwInfo.IgnoreMissingValues) {
71            var filtered = FilterNaNValues(var1, var2);
72            elements[i, j] = calc.Calculate(filtered, out error);
73          } else
74            elements[i, j] = calc.Calculate(var1, var2, out error);
75
[8578]76          if (!error.Equals(OnlineCalculatorError.None)) {
[8689]77            elements[i, j] = double.NaN;
[8578]78          }
[8689]79          elements[j, i] = elements[i, j];
[13938]80
81        });
82        worker.ReportProgress((int)(((double)counter) / length * 100));
[8578]83      }
84      e.Result = elements;
[8833]85      worker.ReportProgress(100);
[8578]86    }
87
88
[13938]89    private static IEnumerable<Tuple<double, double>> FilterNaNValues(IEnumerable<double> first, IEnumerable<double> second) {
90      var firstEnumerator = first.GetEnumerator();
91      var secondEnumerator = second.GetEnumerator();
[8578]92
[13938]93      while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
94        var firstValue = firstEnumerator.Current;
95        var secondValue = secondEnumerator.Current;
[8578]96
[13938]97        if (double.IsNaN(firstValue)) continue;
98        if (double.IsNaN(secondValue)) continue;
[8578]99
[13938]100        yield return Tuple.Create(firstValue, secondValue);
[8578]101      }
102
[13938]103      if (firstEnumerator.MoveNext() || secondEnumerator.MoveNext()) {
104        throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
[8578]105      }
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.