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