Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2875: Removed years in copyrights

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