Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DatasetFeatureCorrelation/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/ExtendedHeatMap.cs @ 8294

Last change on this file since 8294 was 8294, checked in by sforsten, 12 years ago

#1292:

  • SpearmansRankCorrelationCoefficientCalculator now uses the alglib function
  • strings in ExtendedHeatMap have been made constant
File size: 7.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
21#endregion
22
23using System;
24using System.Collections.Generic;
25using System.ComponentModel;
26using System.Linq;
27using HeuristicLab.Analysis;
28using HeuristicLab.Core;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.PluginInfrastructure;
31
32namespace HeuristicLab.Problems.DataAnalysis {
33  [Item("HeatMap", "Represents a heat map of double values.")]
34  [StorableClass]
35  public class ExtendedHeatMap : HeatMap {
36
37    private IDataAnalysisProblemData problemData;
38
39    private BackgroundWorker bw;
40
41    public delegate void ProgressCalculationHandler(object sender, ProgressChangedEventArgs e);
42    public event ProgressCalculationHandler ProgressCalculation;
43
44    public ExtendedHeatMap()
45      : base() {
46      columnNames = Enumerable.Range(1, 2).Select(x => x.ToString()).ToList();
47      rowNames = Enumerable.Range(1, 2).Select(x => x.ToString()).ToList();
48    }
49
50    public ExtendedHeatMap(IDataAnalysisProblemData problemData) {
51      this.problemData = problemData;
52      this.Title = "Feature Correlation";
53      this.columnNames = problemData.Dataset.DoubleVariables.ToList();
54      this.rowNames = problemData.Dataset.DoubleVariables.ToList();
55
56      CalculateElements(problemData.Dataset);
57    }
58
59    private const string PrearsonsRSquared = "Pearsons R Squared";
60    private const string HoeffdingsDependence = "Hoeffdings Dependence";
61    private const string SpearmansRank = "Spearmans Rank";
62    public IEnumerable<string> CorrelationCalculators {
63      get { return new List<string>() { PrearsonsRSquared, HoeffdingsDependence, SpearmansRank }; }
64    }
65
66    private const string AllSamples = "All Samples";
67    private const string TrainingSamples = "Training Samples";
68    private const string TestSamples = "Test Samples";
69    public IEnumerable<string> Partitions {
70      get { return new List<string>() { AllSamples, TrainingSamples, TestSamples }; }
71    }
72
73    public void Recalculate(string calc, string partition) {
74      CalculateElements(problemData.Dataset, calc, partition);
75    }
76
77    private void CalculateElements(Dataset dataset) {
78      CalculateElements(dataset, CorrelationCalculators.First(), Partitions.First());
79    }
80
81    private void CalculateElements(Dataset dataset, string calc, string partition) {
82      if (bw == null || bw.IsBusy) {
83        if (bw != null) {
84          bw.CancelAsync();
85        }
86        bw = new BackgroundWorker();
87        bw.WorkerReportsProgress = true;
88        bw.WorkerSupportsCancellation = true;
89        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
90        bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
91        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
92      }
93      bw.RunWorkerAsync(new BackgroundWorkerInfo { Dataset = dataset, Calculator = calc, Partition = partition });
94    }
95
96    private void bw_DoWork(object sender, DoWorkEventArgs e) {
97      BackgroundWorker worker = sender as BackgroundWorker;
98
99      BackgroundWorkerInfo bwInfo = (BackgroundWorkerInfo)e.Argument;
100      Dataset dataset = bwInfo.Dataset;
101      string partition = bwInfo.Partition;
102      string calc = bwInfo.Calculator;
103
104      IList<string> doubleVariableNames = dataset.DoubleVariables.ToList();
105      OnlineCalculatorError error;
106      int length = doubleVariableNames.Count;
107      double[,] elements = new double[length, length];
108
109      double calculations = (Math.Pow(length, 2) + length) / 2;
110
111      for (int i = 0; i < length; i++) {
112        for (int j = 0; j < i + 1; j++) {
113          if (worker.CancellationPending) {
114            e.Cancel = true;
115            return;
116          }
117
118          IEnumerable<double> var1 = dataset.GetDoubleValues(doubleVariableNames[i]);
119          IEnumerable<double> var2 = dataset.GetDoubleValues(doubleVariableNames[j]);
120          if (partition.Equals(TrainingSamples)) {
121            var1 = var1.Skip(problemData.TrainingPartition.Start).Take(problemData.TrainingPartition.End - problemData.TrainingPartition.Start);
122            var2 = var2.Skip(problemData.TrainingPartition.Start).Take(problemData.TrainingPartition.End - problemData.TrainingPartition.Start);
123          } else if (partition.Equals(TestSamples)) {
124            var1 = var1.Skip(problemData.TestPartition.Start).Take(problemData.TestPartition.End - problemData.TestPartition.Start);
125            var2 = var2.Skip(problemData.TestPartition.Start).Take(problemData.TestPartition.End - problemData.TestPartition.Start);
126          }
127
128          if (calc.Equals(HoeffdingsDependence)) {
129            elements[i, j] = HoeffdingsDependenceCalculator.Calculate(var1, var2, out error);
130          } else if (calc.Equals(SpearmansRank)) {
131            elements[i, j] = SpearmansRankCorrelationCoefficientCalculator.Calculate(var1, var2, out error);
132          } else {
133            elements[i, j] = OnlinePearsonsRSquaredCalculator.Calculate(var1, var2, out error);
134          }
135          elements[j, i] = elements[i, j];
136          if (!error.Equals(OnlineCalculatorError.None)) {
137            throw new ArgumentException("Calculator returned " + error);
138          }
139          worker.ReportProgress((int)Math.Round((((Math.Pow(i, 2) + i) / 2 + j + 1.0) / calculations) * 100));
140        }
141      }
142      e.Result = elements;
143    }
144
145    private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e) {
146      BackgroundWorker worker = sender as BackgroundWorker;
147      if (!worker.CancellationPending && ProgressCalculation != null) {
148        ProgressCalculation(sender, e);
149      }
150    }
151
152    private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
153      BackgroundWorker worker = sender as BackgroundWorker;
154      if (!e.Cancelled && !worker.CancellationPending) {
155        if (!(e.Error == null)) {
156          ErrorHandling.ShowErrorDialog(e.Error);
157        } else {
158          matrix = (double[,])e.Result;
159          OnReset();
160        }
161      } else {
162        Console.WriteLine("Backgroundworker canceled");
163      }
164    }
165
166    private class BackgroundWorkerInfo {
167      public Dataset Dataset { get; set; }
168      public string Calculator { get; set; }
169      public string Partition { get; set; }
170    }
171  }
172}
Note: See TracBrowser for help on using the repository browser.