Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.4/PreprocessingFeatureCorrelationView.cs @ 10947

Last change on this file since 10947 was 10934, checked in by mleitner, 10 years ago

Update correlationmatrix on preprocessing data change - improve performance on datagrid selection.

File size: 8.0 KB
Line 
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
22using System.Collections.Generic;
23using System.ComponentModel;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Data;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29using HeuristicLab.PluginInfrastructure;
30using System;
31using HeuristicLab.DataPreprocessing;
32
33namespace 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
69    protected override void RegisterContentEvents() {
70      base.RegisterContentEvents();
71      Content.PreprocessingData.Changed += Data_Changed;
72      fcc.ProgressCalculation += new FeatureCorrelationCalculator.ProgressCalculationHandler(Content_ProgressCalculation);
73      fcc.CorrelationCalculationFinished += new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished);
74    }
75
76    protected override void DeregisterContentEvents() {
77      Content.PreprocessingData.Changed -= Data_Changed;
78      fcc.CorrelationCalculationFinished -= new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished);
79      fcc.ProgressCalculation -= new FeatureCorrelationCalculator.ProgressCalculationHandler(Content_ProgressCalculation);
80      base.DeregisterContentEvents();
81    }
82
83    private void Data_Changed(object sender, DataPreprocessingChangedEventArgs e) {
84      OnContentChanged();
85    }
86
87    protected override void OnContentChanged() {
88      base.OnContentChanged();
89      fcc.TryCancelCalculation();
90      if (Content != null) {
91        fcc.ProblemData = Content.ProblemData;
92        CalculateCorrelation();
93      } else {
94        dataView.Maximum = 0;
95        dataView.Minimum = 0;
96        dataView.Content = null;
97        dataView.ResetVisibility();
98      }
99    }
100
101    protected virtual bool[] SetInitialVariableVisibility() {
102      bool[] initialVisibility = new bool[Content.ProblemData.Dataset.DoubleVariables.Count()];
103      int i = 0;
104      foreach (var variable in Content.ProblemData.Dataset.DoubleVariables) {
105        initialVisibility[i] = Content.ProblemData.AllowedInputVariables.Contains(variable);
106        i++;
107      }
108      return initialVisibility;
109    }
110
111    protected void CorrelationMeasureComboBox_SelectedChangeCommitted(object sender, System.EventArgs e) {
112      CalculateCorrelation();
113    }
114    protected void PartitionComboBox_SelectedChangeCommitted(object sender, System.EventArgs e) {
115      CalculateCorrelation();
116    }
117
118     protected void CalculateCorrelation() {
119      if (correlationCalcComboBox.SelectedItem == null) return;
120      if (partitionComboBox.SelectedItem == null) return;
121
122      IDependencyCalculator calc = (IDependencyCalculator)correlationCalcComboBox.SelectedValue;
123      string partition = (string)partitionComboBox.SelectedValue;
124      dataView.Enabled = false;
125      double[,] corr = correlationCache.GetCorrelation(calc, partition);
126      if (corr == null) {
127        fcc.CalculateElements(calc, partition);
128      } else {
129        fcc.TryCancelCalculation();
130        var correlation = new DoubleMatrix(corr, Content.ProblemData.Dataset.DoubleVariables, Content.ProblemData.Dataset.DoubleVariables);
131        UpdateDataView(correlation);
132      }
133    }
134
135    protected void Content_CorrelationCalculationFinished(object sender, FeatureCorrelationCalculator.CorrelationCalculationFinishedArgs e) {
136      if (InvokeRequired) {
137        Invoke(new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished), sender, e);
138        return;
139      }
140      correlationCache.SetCorrelation(e.Calculcator, e.Partition, e.Correlation);
141      var correlation = new DoubleMatrix(e.Correlation, Content.ProblemData.Dataset.DoubleVariables, Content.ProblemData.Dataset.DoubleVariables);
142      UpdateDataView(correlation);
143    }
144
145    protected void UpdateDataView(DoubleMatrix correlation) {
146      IDependencyCalculator calc = (IDependencyCalculator)correlationCalcComboBox.SelectedValue;
147      maximumLabel.Text = calc.Maximum.ToString();
148      minimumLabel.Text = calc.Minimum.ToString();
149
150      correlation.SortableView = true;
151      dataView.Maximum = calc.Maximum;
152      dataView.Minimum = calc.Minimum;
153      dataView.Content = correlation;
154      dataView.Enabled = true;
155    }
156
157    protected void Content_ProgressCalculation(object sender, ProgressChangedEventArgs e) {
158      if (!progressPanel.Visible && e.ProgressPercentage != progressBar.Maximum) {
159        progressPanel.Show();
160      } else if (e.ProgressPercentage == progressBar.Maximum) {
161        progressPanel.Hide();
162      }
163      progressBar.Value = e.ProgressPercentage;
164    }
165
166      [NonDiscoverableType]
167      private class FeatureCorrelationCache : Object {
168        private Dictionary<Tuple<IDependencyCalculator, string>, double[,]> correlationsCache;
169
170        public FeatureCorrelationCache()
171          : base() {
172          InitializeCaches();
173        }
174
175        private void InitializeCaches() {
176          correlationsCache = new Dictionary<Tuple<IDependencyCalculator, string>, double[,]>();
177        }
178
179        public void Reset() {
180          InitializeCaches();
181        }
182
183        public double[,] GetCorrelation(IDependencyCalculator calc, string partition) {
184          double[,] corr;
185          var key = new Tuple<IDependencyCalculator, string>(calc, partition);
186          correlationsCache.TryGetValue(key, out corr);
187          return corr;
188        }
189
190        public void SetCorrelation(IDependencyCalculator calc, string partition, double[,] correlation) {
191          var key = new Tuple<IDependencyCalculator, string>(calc, partition);
192          correlationsCache[key] = correlation;
193        }
194      }
195  }
196}
Note: See TracBrowser for help on using the repository browser.