Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2619:

  • Refactored and separated the different feature correlation calculations.
  • Added a checkbox to ignore missing values in the calculation.
File size: 5.3 KB
RevLine 
[13938]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 abstract class AbstractFeatureCorrelationCalculator {
33    private readonly BackgroundWorker backgroundWorker;
34
35    public AbstractFeatureCorrelationCalculator() {
36      backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
37
38      backgroundWorker.DoWork += BackgroundWorker_DoWork;
39      backgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged;
40      backgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
41    }
42
43    private BackgroundWorkerInfo bwInfo;
44    public void TryCancelCalculation() {
45      if (!backgroundWorker.IsBusy) return;
46
47      bwInfo = null;
48      backgroundWorker.CancelAsync();
49    }
50
51    protected static IEnumerable<int> GetRelevantIndices(IDataAnalysisProblemData problemData, string partition) {
52      IEnumerable<int> var;
53      if (partition.Equals(AbstractFeatureCorrelationView.TRAININGSAMPLES))
54        var = problemData.TrainingIndices;
55      else if (partition.Equals(AbstractFeatureCorrelationView.TESTSAMPLES))
56        var = problemData.TestIndices;
57      else var = Enumerable.Range(0, problemData.Dataset.Rows);
58      return var;
59    }
60
61    #region backgroundworker
62
63    protected void StartCalculation(BackgroundWorkerInfo info) {
64      bwInfo = info;
65      if (backgroundWorker.IsBusy) {
66        backgroundWorker.CancelAsync();
67        return;
68      }
69
70      backgroundWorker.RunWorkerAsync(bwInfo);
71    }
72
73    protected abstract void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e);
74
75    private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
76      BackgroundWorker worker = (BackgroundWorker)sender;
77      if (!e.Cancelled && !worker.CancellationPending) {
78        if (e.Error != null) {
79          ErrorHandling.ShowErrorDialog(e.Error);
80        } else {
81          OnCorrelationCalculationFinished((double[,])e.Result, bwInfo.Calculator, bwInfo.Partition, bwInfo.IgnoreMissingValues, bwInfo.Variable);
82        }
83      } else if (bwInfo != null) {
84        backgroundWorker.RunWorkerAsync(bwInfo);
85      }
86    }
87
88    #endregion
89
90    #region events
91    public class CorrelationCalculationFinishedArgs : EventArgs {
92      public double[,] Correlation { get; private set; }
93      public IDependencyCalculator Calculcator { get; private set; }
94      public string Partition { get; private set; }
95      public bool IgnoreMissingValues { get; private set; }
96      public string Variable { get; private set; }
97
98
99      public CorrelationCalculationFinishedArgs(double[,] correlation, IDependencyCalculator calculator, string partition, bool ignoreMissingValues, string variable = null) {
100        this.Correlation = correlation;
101        this.Calculcator = calculator;
102        this.Partition = partition;
103        this.IgnoreMissingValues = ignoreMissingValues;
104        this.Variable = variable;
105      }
106    }
107
108    public delegate void CorrelationCalculationFinishedHandler(object sender, CorrelationCalculationFinishedArgs e);
109    public event CorrelationCalculationFinishedHandler CorrelationCalculationFinished;
110    protected virtual void OnCorrelationCalculationFinished(double[,] correlation, IDependencyCalculator calculator, string partition, bool ignoreMissingValues, string variable = null) {
111      var handler = CorrelationCalculationFinished;
112      if (handler != null)
113        handler(this, new CorrelationCalculationFinishedArgs(correlation, calculator, partition, ignoreMissingValues, variable));
114    }
115
116    public event ProgressChangedEventHandler ProgressChanged;
117    protected virtual void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
118      var handler = ProgressChanged;
119      if (handler != null) {
120        handler(sender, e);
121      }
122    }
123    #endregion
124
125    protected class BackgroundWorkerInfo {
126      public IDataset Dataset { get; set; }
127      public IDependencyCalculator Calculator { get; set; }
128      public string Partition { get; set; }
129      public IEnumerable<int> Indices { get; set; }
130
131      public bool IgnoreMissingValues { get; set; }
132
133      public string Variable { get; set; }
134      public int Frames { get; set; }
135      public double[,] AlreadyCalculated { get; set; }
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.