[8578] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.ComponentModel;
|
---|
| 25 | using System.Linq;
|
---|
[14005] | 26 | using System.Threading.Tasks;
|
---|
[8578] | 27 | using HeuristicLab.PluginInfrastructure;
|
---|
| 28 |
|
---|
[8729] | 29 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
| 30 | [NonDiscoverableType]
|
---|
[14005] | 31 | public sealed class FeatureCorrelationCalculator : AbstractFeatureCorrelationCalculator {
|
---|
| 32 | public FeatureCorrelationCalculator() : base() { }
|
---|
[8578] | 33 |
|
---|
[14005] | 34 | public void CalculateElements(IDataAnalysisProblemData problemData, IDependencyCalculator calc, string partition, bool ignoreMissingValues) {
|
---|
[8880] | 35 | var indices = GetRelevantIndices(problemData, partition);
|
---|
[14005] | 36 | var info = new BackgroundWorkerInfo {
|
---|
| 37 | Dataset = problemData.Dataset, Calculator = calc, Partition = partition, Indices = indices, IgnoreMissingValues = ignoreMissingValues
|
---|
[8880] | 38 | };
|
---|
[8578] | 39 |
|
---|
[14005] | 40 | StartCalculation(info);
|
---|
[8880] | 41 | }
|
---|
| 42 |
|
---|
[14005] | 43 | protected override void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
|
---|
| 44 | BackgroundWorker worker = (BackgroundWorker)sender;
|
---|
[8578] | 45 | BackgroundWorkerInfo bwInfo = (BackgroundWorkerInfo)e.Argument;
|
---|
| 46 |
|
---|
[12702] | 47 | var dataset = bwInfo.Dataset;
|
---|
[14005] | 48 | var indices = bwInfo.Indices.ToArray();
|
---|
[8833] | 49 | IDependencyCalculator calc = bwInfo.Calculator;
|
---|
[8578] | 50 |
|
---|
| 51 | IList<string> doubleVariableNames = dataset.DoubleVariables.ToList();
|
---|
[14567] | 52 |
|
---|
[8578] | 53 | int length = doubleVariableNames.Count;
|
---|
| 54 | double[,] elements = new double[length, length];
|
---|
| 55 |
|
---|
| 56 | worker.ReportProgress(0);
|
---|
| 57 |
|
---|
[14005] | 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 |
|
---|
[14005] | 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 |
|
---|
[14567] | 70 | OnlineCalculatorError error = OnlineCalculatorError.None;
|
---|
[14005] | 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];
|
---|
[14005] | 81 | });
|
---|
| 82 | worker.ReportProgress((int)(((double)counter) / length * 100));
|
---|
[8578] | 83 | }
|
---|
[14567] | 84 |
|
---|
[8578] | 85 | e.Result = elements;
|
---|
[8833] | 86 | worker.ReportProgress(100);
|
---|
[8578] | 87 | }
|
---|
| 88 |
|
---|
| 89 |
|
---|
[14005] | 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 |
|
---|
[14005] | 94 | while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
|
---|
| 95 | var firstValue = firstEnumerator.Current;
|
---|
| 96 | var secondValue = secondEnumerator.Current;
|
---|
[8578] | 97 |
|
---|
[14005] | 98 | if (double.IsNaN(firstValue)) continue;
|
---|
| 99 | if (double.IsNaN(secondValue)) continue;
|
---|
[8578] | 100 |
|
---|
[14005] | 101 | yield return Tuple.Create(firstValue, secondValue);
|
---|
[8578] | 102 | }
|
---|
| 103 |
|
---|
[14005] | 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 | }
|
---|