1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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 | #endregion
|
---|
21 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.ComponentModel;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.MainForm;
|
---|
28 | using HeuristicLab.MainForm.WindowsForms;
|
---|
29 | using HeuristicLab.PluginInfrastructure;
|
---|
30 | using System;
|
---|
31 | using HeuristicLab.DataPreprocessing;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
34 | [View("Preprocessing Feature Correlation View")]
|
---|
35 | [Content(typeof(CorrelationMatrixContent), false)]
|
---|
36 | public partial class PreprocessingFeatureCorrelationView : AsynchronousContentView {
|
---|
37 | public const string ALLSAMPLES = "All Samples";
|
---|
38 | public const string TRAININGSAMPLES = "Training Samples";
|
---|
39 | public const string TESTSAMPLES = "Test Samples";
|
---|
40 |
|
---|
41 | public static readonly IList<string> Partitions = new List<string>() { ALLSAMPLES, TRAININGSAMPLES, TESTSAMPLES };
|
---|
42 |
|
---|
43 | protected FeatureCorrelationCalculator fcc;
|
---|
44 |
|
---|
45 | public new CorrelationMatrixContent Content
|
---|
46 | {
|
---|
47 | get { return (CorrelationMatrixContent) base.Content; }
|
---|
48 | set { base.Content = value; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | private FeatureCorrelationCache correlationCache;
|
---|
52 |
|
---|
53 | public PreprocessingFeatureCorrelationView() {
|
---|
54 |
|
---|
55 | correlationCache = new FeatureCorrelationCache();
|
---|
56 | InitializeComponent();
|
---|
57 | fcc = new FeatureCorrelationCalculator();
|
---|
58 | var calculators = ApplicationManager.Manager.GetInstances<IDependencyCalculator>();
|
---|
59 | var calcList = calculators.OrderBy(c => c.Name).Select(c => new { Name = c.Name, Calculator = c }).ToList();
|
---|
60 | correlationCalcComboBox.ValueMember = "Calculator";
|
---|
61 | correlationCalcComboBox.DisplayMember = "Name";
|
---|
62 | correlationCalcComboBox.DataSource = calcList;
|
---|
63 | correlationCalcComboBox.SelectedItem = calcList.First(c => c.Calculator.GetType().Equals(typeof(PearsonsRDependenceCalculator)));
|
---|
64 | partitionComboBox.DataSource = Partitions;
|
---|
65 | partitionComboBox.SelectedItem = TRAININGSAMPLES;
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected override void RegisterContentEvents() {
|
---|
69 | base.RegisterContentEvents();
|
---|
70 | fcc.ProgressCalculation += new FeatureCorrelationCalculator.ProgressCalculationHandler(Content_ProgressCalculation);
|
---|
71 | fcc.CorrelationCalculationFinished += new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished);
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected override void DeregisterContentEvents() {
|
---|
75 | fcc.CorrelationCalculationFinished -= new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished);
|
---|
76 | fcc.ProgressCalculation -= new FeatureCorrelationCalculator.ProgressCalculationHandler(Content_ProgressCalculation);
|
---|
77 | base.DeregisterContentEvents();
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected override void OnContentChanged() {
|
---|
81 | base.OnContentChanged();
|
---|
82 | fcc.TryCancelCalculation();
|
---|
83 | if (Content != null) {
|
---|
84 | fcc.ProblemData = Content.ProblemData;
|
---|
85 | CalculateCorrelation();
|
---|
86 | } else {
|
---|
87 | dataView.Maximum = 0;
|
---|
88 | dataView.Minimum = 0;
|
---|
89 | dataView.Content = null;
|
---|
90 | dataView.ResetVisibility();
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected virtual bool[] SetInitialVariableVisibility() {
|
---|
95 | bool[] initialVisibility = new bool[Content.ProblemData.Dataset.DoubleVariables.Count()];
|
---|
96 | int i = 0;
|
---|
97 | foreach (var variable in Content.ProblemData.Dataset.DoubleVariables) {
|
---|
98 | initialVisibility[i] = Content.ProblemData.AllowedInputVariables.Contains(variable);
|
---|
99 | i++;
|
---|
100 | }
|
---|
101 | return initialVisibility;
|
---|
102 | }
|
---|
103 |
|
---|
104 | protected void CorrelationMeasureComboBox_SelectedChangeCommitted(object sender, System.EventArgs e) {
|
---|
105 | CalculateCorrelation();
|
---|
106 | }
|
---|
107 | protected void PartitionComboBox_SelectedChangeCommitted(object sender, System.EventArgs e) {
|
---|
108 | CalculateCorrelation();
|
---|
109 | }
|
---|
110 |
|
---|
111 | protected void CalculateCorrelation() {
|
---|
112 | if (correlationCalcComboBox.SelectedItem == null) return;
|
---|
113 | if (partitionComboBox.SelectedItem == null) return;
|
---|
114 |
|
---|
115 | IDependencyCalculator calc = (IDependencyCalculator)correlationCalcComboBox.SelectedValue;
|
---|
116 | string partition = (string)partitionComboBox.SelectedValue;
|
---|
117 | dataView.Enabled = false;
|
---|
118 | double[,] corr = correlationCache.GetCorrelation(calc, partition);
|
---|
119 | if (corr == null) {
|
---|
120 | fcc.CalculateElements(calc, partition);
|
---|
121 | } else {
|
---|
122 | fcc.TryCancelCalculation();
|
---|
123 | var correlation = new DoubleMatrix(corr, Content.ProblemData.Dataset.DoubleVariables, Content.ProblemData.Dataset.DoubleVariables);
|
---|
124 | UpdateDataView(correlation);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | protected void Content_CorrelationCalculationFinished(object sender, FeatureCorrelationCalculator.CorrelationCalculationFinishedArgs e) {
|
---|
129 | if (InvokeRequired) {
|
---|
130 | Invoke(new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished), sender, e);
|
---|
131 | return;
|
---|
132 | }
|
---|
133 | correlationCache.SetCorrelation(e.Calculcator, e.Partition, e.Correlation);
|
---|
134 | var correlation = new DoubleMatrix(e.Correlation, Content.ProblemData.Dataset.DoubleVariables, Content.ProblemData.Dataset.DoubleVariables);
|
---|
135 | UpdateDataView(correlation);
|
---|
136 | }
|
---|
137 |
|
---|
138 | protected void UpdateDataView(DoubleMatrix correlation) {
|
---|
139 | IDependencyCalculator calc = (IDependencyCalculator)correlationCalcComboBox.SelectedValue;
|
---|
140 | maximumLabel.Text = calc.Maximum.ToString();
|
---|
141 | minimumLabel.Text = calc.Minimum.ToString();
|
---|
142 |
|
---|
143 | correlation.SortableView = true;
|
---|
144 | dataView.Maximum = calc.Maximum;
|
---|
145 | dataView.Minimum = calc.Minimum;
|
---|
146 | dataView.Content = correlation;
|
---|
147 | dataView.Enabled = true;
|
---|
148 | }
|
---|
149 |
|
---|
150 | protected void Content_ProgressCalculation(object sender, ProgressChangedEventArgs e) {
|
---|
151 | if (!progressPanel.Visible && e.ProgressPercentage != progressBar.Maximum) {
|
---|
152 | progressPanel.Show();
|
---|
153 | } else if (e.ProgressPercentage == progressBar.Maximum) {
|
---|
154 | progressPanel.Hide();
|
---|
155 | }
|
---|
156 | progressBar.Value = e.ProgressPercentage;
|
---|
157 | }
|
---|
158 |
|
---|
159 | [NonDiscoverableType]
|
---|
160 | private class FeatureCorrelationCache : Object {
|
---|
161 | private Dictionary<Tuple<IDependencyCalculator, string>, double[,]> correlationsCache;
|
---|
162 |
|
---|
163 | public FeatureCorrelationCache()
|
---|
164 | : base() {
|
---|
165 | InitializeCaches();
|
---|
166 | }
|
---|
167 |
|
---|
168 | private void InitializeCaches() {
|
---|
169 | correlationsCache = new Dictionary<Tuple<IDependencyCalculator, string>, double[,]>();
|
---|
170 | }
|
---|
171 |
|
---|
172 | public void Reset() {
|
---|
173 | InitializeCaches();
|
---|
174 | }
|
---|
175 |
|
---|
176 | public double[,] GetCorrelation(IDependencyCalculator calc, string partition) {
|
---|
177 | double[,] corr;
|
---|
178 | var key = new Tuple<IDependencyCalculator, string>(calc, partition);
|
---|
179 | correlationsCache.TryGetValue(key, out corr);
|
---|
180 | return corr;
|
---|
181 | }
|
---|
182 |
|
---|
183 | public void SetCorrelation(IDependencyCalculator calc, string partition, double[,] correlation) {
|
---|
184 | var key = new Tuple<IDependencyCalculator, string>(calc, partition);
|
---|
185 | correlationsCache[key] = correlation;
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 | } |
---|