Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10951 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
RevLine 
[8578]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 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
[8880]22using System.Collections.Generic;
[8578]23using System.ComponentModel;
24using System.Linq;
25using System.Windows.Forms;
[8861]26using HeuristicLab.Data;
[8578]27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
[8833]29using HeuristicLab.PluginInfrastructure;
[10908]30using System;
31using HeuristicLab.DataPreprocessing;
[8578]32
33namespace HeuristicLab.Problems.DataAnalysis.Views {
[10908]34  [View("Preprocessing Feature Correlation View")]
35  [Content(typeof(CorrelationMatrixContent), false)]
36  public partial class PreprocessingFeatureCorrelationView : AsynchronousContentView {
[8880]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
[8578]43    protected FeatureCorrelationCalculator fcc;
44
[10908]45    public new CorrelationMatrixContent Content
46    {
47      get { return (CorrelationMatrixContent) base.Content; }
[8578]48      set { base.Content = value; }
49    }
50
[10908]51    private FeatureCorrelationCache correlationCache;
52
53    public PreprocessingFeatureCorrelationView() {
54
55      correlationCache = new FeatureCorrelationCache();
[8578]56      InitializeComponent();
57      fcc = new FeatureCorrelationCalculator();
[8833]58      var calculators = ApplicationManager.Manager.GetInstances<IDependencyCalculator>();
59      var calcList = calculators.OrderBy(c => c.Name).Select(c => new { Name = c.Name, Calculator = c }).ToList();
[8880]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;
[8578]66    }
67
[10934]68
[8578]69    protected override void RegisterContentEvents() {
70      base.RegisterContentEvents();
[10934]71      Content.PreprocessingData.Changed += Data_Changed;
[8729]72      fcc.ProgressCalculation += new FeatureCorrelationCalculator.ProgressCalculationHandler(Content_ProgressCalculation);
73      fcc.CorrelationCalculationFinished += new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished);
[8578]74    }
75
76    protected override void DeregisterContentEvents() {
[10934]77      Content.PreprocessingData.Changed -= Data_Changed;
[8880]78      fcc.CorrelationCalculationFinished -= new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished);
79      fcc.ProgressCalculation -= new FeatureCorrelationCalculator.ProgressCalculationHandler(Content_ProgressCalculation);
[8578]80      base.DeregisterContentEvents();
81    }
82
[10934]83    private void Data_Changed(object sender, DataPreprocessingChangedEventArgs e) {
84      OnContentChanged();
85    }
86
[8578]87    protected override void OnContentChanged() {
88      base.OnContentChanged();
[8833]89      fcc.TryCancelCalculation();
[8578]90      if (Content != null) {
[10908]91        fcc.ProblemData = Content.ProblemData;
[8578]92        CalculateCorrelation();
93      } else {
[8861]94        dataView.Maximum = 0;
95        dataView.Minimum = 0;
[8833]96        dataView.Content = null;
97        dataView.ResetVisibility();
[8578]98      }
99    }
100
[8833]101    protected virtual bool[] SetInitialVariableVisibility() {
[10908]102      bool[] initialVisibility = new bool[Content.ProblemData.Dataset.DoubleVariables.Count()];
[8689]103      int i = 0;
[10908]104      foreach (var variable in Content.ProblemData.Dataset.DoubleVariables) {
105        initialVisibility[i] = Content.ProblemData.AllowedInputVariables.Contains(variable);
[8689]106        i++;
107      }
108      return initialVisibility;
109    }
110
[8578]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
[10908]118     protected void CalculateCorrelation() {
119      if (correlationCalcComboBox.SelectedItem == null) return;
120      if (partitionComboBox.SelectedItem == null) return;
[8578]121
[10908]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
[8874]145    protected void UpdateDataView(DoubleMatrix correlation) {
[8880]146      IDependencyCalculator calc = (IDependencyCalculator)correlationCalcComboBox.SelectedValue;
[8861]147      maximumLabel.Text = calc.Maximum.ToString();
148      minimumLabel.Text = calc.Minimum.ToString();
[8578]149
[8874]150      correlation.SortableView = true;
[8861]151      dataView.Maximum = calc.Maximum;
152      dataView.Minimum = calc.Minimum;
[8874]153      dataView.Content = correlation;
[8833]154      dataView.Enabled = true;
[8578]155    }
156
157    protected void Content_ProgressCalculation(object sender, ProgressChangedEventArgs e) {
[8881]158      if (!progressPanel.Visible && e.ProgressPercentage != progressBar.Maximum) {
159        progressPanel.Show();
160      } else if (e.ProgressPercentage == progressBar.Maximum) {
161        progressPanel.Hide();
[8578]162      }
[8881]163      progressBar.Value = e.ProgressPercentage;
[8578]164    }
[10908]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      }
[8578]195  }
196}
Note: See TracBrowser for help on using the repository browser.