[8578] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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;
|
---|
| 26 | using HeuristicLab.PluginInfrastructure;
|
---|
| 27 |
|
---|
[8729] | 28 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
| 29 | [NonDiscoverableType]
|
---|
[8578] | 30 | public class FeatureCorrelationCalculator : Object {
|
---|
| 31 |
|
---|
| 32 | private BackgroundWorker bw;
|
---|
| 33 | private BackgroundWorkerInfo bwInfo;
|
---|
| 34 |
|
---|
| 35 | private IDataAnalysisProblemData problemData;
|
---|
| 36 | public IDataAnalysisProblemData ProblemData {
|
---|
| 37 | set {
|
---|
| 38 | if (bw != null) {
|
---|
| 39 | bw.CancelAsync();
|
---|
| 40 | }
|
---|
| 41 | problemData = value;
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | public FeatureCorrelationCalculator()
|
---|
| 46 | : base() { }
|
---|
| 47 |
|
---|
| 48 | public FeatureCorrelationCalculator(IDataAnalysisProblemData problemData)
|
---|
| 49 | : base() {
|
---|
| 50 | this.problemData = problemData;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[8833] | 53 | public void CalculateElements(IDependencyCalculator calc, string partition) {
|
---|
[8578] | 54 | CalculateElements(problemData.Dataset, calc, partition);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[8833] | 57 | // returns true if any calculation takes place
|
---|
| 58 | public bool CalculateTimeframeElements(IDependencyCalculator calc, string partition, string variable, int frames, double[,] correlation = null) {
|
---|
[8578] | 59 | if (correlation == null || correlation.GetLength(1) <= frames) {
|
---|
| 60 | CalculateElements(problemData.Dataset, calc, partition, variable, frames, correlation);
|
---|
| 61 | return true;
|
---|
| 62 | } else {
|
---|
| 63 | return false;
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[8833] | 67 | public void TryCancelCalculation() {
|
---|
| 68 | if (bw != null && bw.IsBusy) {
|
---|
| 69 | bwInfo = null;
|
---|
| 70 | bw.CancelAsync();
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[12509] | 74 | private void CalculateElements(IDataset dataset, IDependencyCalculator calc, string partition, string variable = null, int frames = 0, double[,] alreadyCalculated = null) {
|
---|
[8880] | 75 | var indices = GetRelevantIndices(problemData, partition);
|
---|
| 76 | bwInfo = new BackgroundWorkerInfo {
|
---|
| 77 | Dataset = dataset, Calculator = calc, Partition = partition, Indices = indices,
|
---|
| 78 | Variable = variable, Frames = frames, AlreadyCalculated = alreadyCalculated
|
---|
| 79 | };
|
---|
[8578] | 80 | if (bw == null) {
|
---|
| 81 | bw = new BackgroundWorker();
|
---|
| 82 | bw.WorkerReportsProgress = true;
|
---|
| 83 | bw.WorkerSupportsCancellation = true;
|
---|
| 84 | bw.DoWork += new DoWorkEventHandler(BwDoWork);
|
---|
| 85 | bw.ProgressChanged += new ProgressChangedEventHandler(BwProgressChanged);
|
---|
| 86 | bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BwRunWorkerCompleted);
|
---|
| 87 | }
|
---|
| 88 | if (bw.IsBusy) {
|
---|
| 89 | bw.CancelAsync();
|
---|
| 90 | } else {
|
---|
| 91 | bw.RunWorkerAsync(bwInfo);
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[8880] | 95 | private IEnumerable<int> GetRelevantIndices(IDataAnalysisProblemData problemData, string partition) {
|
---|
| 96 | IEnumerable<int> var;
|
---|
| 97 | if (partition.Equals(AbstractFeatureCorrelationView.TRAININGSAMPLES))
|
---|
| 98 | var = problemData.TrainingIndices;
|
---|
| 99 | else if (partition.Equals(AbstractFeatureCorrelationView.TESTSAMPLES))
|
---|
| 100 | var = problemData.TestIndices;
|
---|
| 101 | else var = Enumerable.Range(0, problemData.Dataset.Rows);
|
---|
| 102 | return var;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[8578] | 105 | #region backgroundworker
|
---|
| 106 | private void BwDoWork(object sender, DoWorkEventArgs e) {
|
---|
| 107 | BackgroundWorkerInfo bwInfo = (BackgroundWorkerInfo)e.Argument;
|
---|
| 108 | if (bwInfo.Variable == null) {
|
---|
| 109 | BwCalculateCorrelation(sender, e);
|
---|
| 110 | } else {
|
---|
| 111 | BwCalculateTimeframeCorrelation(sender, e);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | private void BwCalculateCorrelation(object sender, DoWorkEventArgs e) {
|
---|
| 116 | BackgroundWorker worker = sender as BackgroundWorker;
|
---|
| 117 |
|
---|
| 118 | BackgroundWorkerInfo bwInfo = (BackgroundWorkerInfo)e.Argument;
|
---|
[12509] | 119 | var dataset = bwInfo.Dataset;
|
---|
[8880] | 120 | IEnumerable<int> indices = bwInfo.Indices;
|
---|
[8833] | 121 | IDependencyCalculator calc = bwInfo.Calculator;
|
---|
[8578] | 122 |
|
---|
| 123 | IList<string> doubleVariableNames = dataset.DoubleVariables.ToList();
|
---|
[8689] | 124 | OnlineCalculatorError error = OnlineCalculatorError.None;
|
---|
[8578] | 125 | int length = doubleVariableNames.Count;
|
---|
| 126 | double[,] elements = new double[length, length];
|
---|
| 127 | double calculations = (Math.Pow(length, 2) + length) / 2;
|
---|
| 128 |
|
---|
| 129 | worker.ReportProgress(0);
|
---|
| 130 |
|
---|
| 131 | for (int i = 0; i < length; i++) {
|
---|
| 132 | for (int j = 0; j < i + 1; j++) {
|
---|
| 133 | if (worker.CancellationPending) {
|
---|
[8833] | 134 | worker.ReportProgress(100);
|
---|
[8578] | 135 | e.Cancel = true;
|
---|
| 136 | return;
|
---|
| 137 | }
|
---|
[8880] | 138 | IEnumerable<double> var1 = problemData.Dataset.GetDoubleValues(doubleVariableNames[i], indices);
|
---|
| 139 | IEnumerable<double> var2 = problemData.Dataset.GetDoubleValues(doubleVariableNames[j], indices);
|
---|
[8578] | 140 |
|
---|
[12151] | 141 | elements[i, j] = calc.Calculate(var1, var2, out error);
|
---|
[8578] | 142 |
|
---|
| 143 | if (!error.Equals(OnlineCalculatorError.None)) {
|
---|
[8689] | 144 | elements[i, j] = double.NaN;
|
---|
[8578] | 145 | }
|
---|
[8689] | 146 | elements[j, i] = elements[i, j];
|
---|
[8578] | 147 | worker.ReportProgress((int)Math.Round((((Math.Pow(i, 2) + i) / 2 + j + 1.0) / calculations) * 100));
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | e.Result = elements;
|
---|
[8833] | 151 | worker.ReportProgress(100);
|
---|
[8578] | 152 | }
|
---|
| 153 |
|
---|
| 154 | private void BwCalculateTimeframeCorrelation(object sender, DoWorkEventArgs e) {
|
---|
| 155 | BackgroundWorker worker = sender as BackgroundWorker;
|
---|
| 156 |
|
---|
| 157 | BackgroundWorkerInfo bwInfo = (BackgroundWorkerInfo)e.Argument;
|
---|
[12509] | 158 | var dataset = bwInfo.Dataset;
|
---|
[8880] | 159 | IEnumerable<int> indices = bwInfo.Indices;
|
---|
[8833] | 160 | IDependencyCalculator calc = bwInfo.Calculator;
|
---|
[8578] | 161 | string variable = bwInfo.Variable;
|
---|
| 162 | int frames = bwInfo.Frames;
|
---|
| 163 | double[,] alreadyCalculated = bwInfo.AlreadyCalculated;
|
---|
| 164 |
|
---|
| 165 | IList<string> doubleVariableNames = dataset.DoubleVariables.ToList();
|
---|
[8689] | 166 | OnlineCalculatorError error = OnlineCalculatorError.None;
|
---|
[8578] | 167 | int length = doubleVariableNames.Count;
|
---|
| 168 | double[,] elements = new double[length, frames + 1];
|
---|
| 169 | double calculations = (frames + 1) * length;
|
---|
| 170 |
|
---|
| 171 | worker.ReportProgress(0);
|
---|
| 172 |
|
---|
| 173 | int start = 0;
|
---|
| 174 | if (alreadyCalculated != null) {
|
---|
| 175 | for (int i = 0; i < alreadyCalculated.GetLength(0); i++) {
|
---|
| 176 | Array.Copy(alreadyCalculated, i * alreadyCalculated.GetLength(1), elements, i * elements.GetLength(1), alreadyCalculated.GetLength(1));
|
---|
| 177 | }
|
---|
| 178 | start = alreadyCalculated.GetLength(1);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | for (int i = 0; i < length; i++) {
|
---|
| 182 | for (int j = start; j <= frames; j++) {
|
---|
| 183 | if (worker.CancellationPending) {
|
---|
[8833] | 184 | worker.ReportProgress(100);
|
---|
[8578] | 185 | e.Cancel = true;
|
---|
| 186 | return;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[8880] | 189 | IEnumerable<double> var1 = problemData.Dataset.GetDoubleValues(variable, indices);
|
---|
| 190 | IEnumerable<double> var2 = problemData.Dataset.GetDoubleValues(doubleVariableNames[i], indices);
|
---|
[8578] | 191 |
|
---|
| 192 | var valuesInFrame = var1.Take(j);
|
---|
| 193 | var help = var1.Skip(j).ToList();
|
---|
| 194 | help.AddRange(valuesInFrame);
|
---|
| 195 | var1 = help;
|
---|
| 196 |
|
---|
[8833] | 197 | elements[i, j] = calc.Calculate(var1, var2, out error);
|
---|
[8578] | 198 |
|
---|
| 199 | if (!error.Equals(OnlineCalculatorError.None)) {
|
---|
[8689] | 200 | elements[i, j] = double.NaN;
|
---|
[8578] | 201 | }
|
---|
| 202 | worker.ReportProgress((int)((100.0 / calculations) * (i * (frames + 1) + j + 1)));
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 | e.Result = elements;
|
---|
[8833] | 206 | worker.ReportProgress(100);
|
---|
[8578] | 207 | }
|
---|
| 208 |
|
---|
| 209 | private void BwRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
|
---|
| 210 | BackgroundWorker worker = sender as BackgroundWorker;
|
---|
| 211 | if (!e.Cancelled && !worker.CancellationPending) {
|
---|
[8874] | 212 | if (e.Error != null) {
|
---|
[8578] | 213 | ErrorHandling.ShowErrorDialog(e.Error);
|
---|
| 214 | } else {
|
---|
| 215 | OnCorrelationCalculationFinished((double[,])e.Result, bwInfo.Calculator, bwInfo.Partition, bwInfo.Variable);
|
---|
| 216 | }
|
---|
[8833] | 217 | } else if (bwInfo != null) {
|
---|
[8578] | 218 | bw.RunWorkerAsync(bwInfo);
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
| 221 | #endregion
|
---|
| 222 |
|
---|
| 223 | #region events
|
---|
| 224 | public class CorrelationCalculationFinishedArgs : EventArgs {
|
---|
| 225 | public double[,] Correlation { get; private set; }
|
---|
[8833] | 226 | public IDependencyCalculator Calculcator { get; private set; }
|
---|
| 227 | public string Partition { get; private set; }
|
---|
[8578] | 228 | public string Variable { get; private set; }
|
---|
| 229 |
|
---|
[8833] | 230 | public CorrelationCalculationFinishedArgs(double[,] correlation, IDependencyCalculator calculator, string partition, string variable = null) {
|
---|
[8578] | 231 | this.Correlation = correlation;
|
---|
| 232 | this.Calculcator = calculator;
|
---|
| 233 | this.Partition = partition;
|
---|
| 234 | this.Variable = variable;
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
[8729] | 237 |
|
---|
[8578] | 238 | public delegate void CorrelationCalculationFinishedHandler(object sender, CorrelationCalculationFinishedArgs e);
|
---|
| 239 | public event CorrelationCalculationFinishedHandler CorrelationCalculationFinished;
|
---|
[8833] | 240 | protected virtual void OnCorrelationCalculationFinished(double[,] correlation, IDependencyCalculator calculator, string partition, string variable = null) {
|
---|
[8578] | 241 | var handler = CorrelationCalculationFinished;
|
---|
| 242 | if (handler != null)
|
---|
| 243 | handler(this, new CorrelationCalculationFinishedArgs(correlation, calculator, partition, variable));
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | public delegate void ProgressCalculationHandler(object sender, ProgressChangedEventArgs e);
|
---|
| 247 | public event ProgressCalculationHandler ProgressCalculation;
|
---|
| 248 | protected void BwProgressChanged(object sender, ProgressChangedEventArgs e) {
|
---|
| 249 | BackgroundWorker worker = sender as BackgroundWorker;
|
---|
[8833] | 250 | if (ProgressCalculation != null) {
|
---|
[8578] | 251 | ProgressCalculation(sender, e);
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 | #endregion
|
---|
| 255 |
|
---|
[8729] | 256 | private class BackgroundWorkerInfo {
|
---|
[12509] | 257 | public IDataset Dataset { get; set; }
|
---|
[8833] | 258 | public IDependencyCalculator Calculator { get; set; }
|
---|
| 259 | public string Partition { get; set; }
|
---|
[8880] | 260 | public IEnumerable<int> Indices { get; set; }
|
---|
[8578] | 261 | public string Variable { get; set; }
|
---|
| 262 | public int Frames { get; set; }
|
---|
| 263 | public double[,] AlreadyCalculated { get; set; }
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 | }
|
---|