1 | #region License Information
|
---|
2 |
|
---|
3 | /* HeuristicLab
|
---|
4 | * Copyright (C) 2002-2018 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 |
|
---|
24 | using System;
|
---|
25 | using System.Collections.Generic;
|
---|
26 | using System.ComponentModel;
|
---|
27 | using System.Linq;
|
---|
28 | using HeuristicLab.MainForm;
|
---|
29 | using HeuristicLab.PluginInfrastructure;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
32 | [NonDiscoverableType]
|
---|
33 | public abstract class AbstractFeatureCorrelationCalculator {
|
---|
34 | private readonly BackgroundWorker backgroundWorker;
|
---|
35 |
|
---|
36 | public AbstractFeatureCorrelationCalculator() {
|
---|
37 | backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
|
---|
38 |
|
---|
39 | backgroundWorker.DoWork += BackgroundWorker_DoWork;
|
---|
40 | backgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged;
|
---|
41 | backgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
|
---|
42 | }
|
---|
43 |
|
---|
44 | private BackgroundWorkerInfo bwInfo;
|
---|
45 | public void TryCancelCalculation() {
|
---|
46 | if (!backgroundWorker.IsBusy) return;
|
---|
47 |
|
---|
48 | bwInfo = null;
|
---|
49 | backgroundWorker.CancelAsync();
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected static IEnumerable<int> GetRelevantIndices(IDataAnalysisProblemData problemData, string partition) {
|
---|
53 | IEnumerable<int> var;
|
---|
54 | if (partition.Equals(AbstractFeatureCorrelationView.TRAININGSAMPLES))
|
---|
55 | var = problemData.TrainingIndices;
|
---|
56 | else if (partition.Equals(AbstractFeatureCorrelationView.TESTSAMPLES))
|
---|
57 | var = problemData.TestIndices;
|
---|
58 | else var = Enumerable.Range(0, problemData.Dataset.Rows);
|
---|
59 | return var;
|
---|
60 | }
|
---|
61 |
|
---|
62 | #region backgroundworker
|
---|
63 |
|
---|
64 | protected void StartCalculation(BackgroundWorkerInfo info) {
|
---|
65 | bwInfo = info;
|
---|
66 | if (backgroundWorker.IsBusy) {
|
---|
67 | backgroundWorker.CancelAsync();
|
---|
68 | return;
|
---|
69 | }
|
---|
70 |
|
---|
71 | backgroundWorker.RunWorkerAsync(bwInfo);
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected abstract void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e);
|
---|
75 |
|
---|
76 | private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
|
---|
77 | BackgroundWorker worker = (BackgroundWorker)sender;
|
---|
78 | if (!e.Cancelled && !worker.CancellationPending) {
|
---|
79 | if (e.Error != null) {
|
---|
80 | MainFormManager.MainForm.ShowError(e.Error.Message, e.Error);
|
---|
81 | } else {
|
---|
82 | OnCorrelationCalculationFinished((double[,])e.Result, bwInfo.Calculator, bwInfo.Partition, bwInfo.IgnoreMissingValues, bwInfo.Variable);
|
---|
83 | }
|
---|
84 | } else if (bwInfo != null) {
|
---|
85 | backgroundWorker.RunWorkerAsync(bwInfo);
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | #endregion
|
---|
90 |
|
---|
91 | #region events
|
---|
92 | public class CorrelationCalculationFinishedArgs : EventArgs {
|
---|
93 | public double[,] Correlation { get; private set; }
|
---|
94 | public IDependencyCalculator Calculcator { get; private set; }
|
---|
95 | public string Partition { get; private set; }
|
---|
96 | public bool IgnoreMissingValues { get; private set; }
|
---|
97 | public string Variable { get; private set; }
|
---|
98 |
|
---|
99 |
|
---|
100 | public CorrelationCalculationFinishedArgs(double[,] correlation, IDependencyCalculator calculator, string partition, bool ignoreMissingValues, string variable = null) {
|
---|
101 | this.Correlation = correlation;
|
---|
102 | this.Calculcator = calculator;
|
---|
103 | this.Partition = partition;
|
---|
104 | this.IgnoreMissingValues = ignoreMissingValues;
|
---|
105 | this.Variable = variable;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | public delegate void CorrelationCalculationFinishedHandler(object sender, CorrelationCalculationFinishedArgs e);
|
---|
110 | public event CorrelationCalculationFinishedHandler CorrelationCalculationFinished;
|
---|
111 | protected virtual void OnCorrelationCalculationFinished(double[,] correlation, IDependencyCalculator calculator, string partition, bool ignoreMissingValues, string variable = null) {
|
---|
112 | var handler = CorrelationCalculationFinished;
|
---|
113 | if (handler != null)
|
---|
114 | handler(this, new CorrelationCalculationFinishedArgs(correlation, calculator, partition, ignoreMissingValues, variable));
|
---|
115 | }
|
---|
116 |
|
---|
117 | public event ProgressChangedEventHandler ProgressChanged;
|
---|
118 | protected virtual void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
|
---|
119 | var handler = ProgressChanged;
|
---|
120 | if (handler != null) {
|
---|
121 | handler(sender, e);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | #endregion
|
---|
125 |
|
---|
126 | protected class BackgroundWorkerInfo {
|
---|
127 | public IDataset Dataset { get; set; }
|
---|
128 | public IDependencyCalculator Calculator { get; set; }
|
---|
129 | public string Partition { get; set; }
|
---|
130 | public IEnumerable<int> Indices { get; set; }
|
---|
131 |
|
---|
132 | public bool IgnoreMissingValues { get; set; }
|
---|
133 |
|
---|
134 | public string Variable { get; set; }
|
---|
135 | public int Frames { get; set; }
|
---|
136 | public double[,] AlreadyCalculated { get; set; }
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|