Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Views/3.4/FeatureCorrelation/FeatureCorrelationView.cs @ 14005

Last change on this file since 14005 was 14005, checked in by mkommend, 8 years ago

#2619: Merged r13938 and r14001 into stable.

File size: 4.7 KB
RevLine 
[8578]1#region License Information
2/* HeuristicLab
[12009]3 * Copyright (C) 2002-2015 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
[8833]22using System;
23using System.Collections.Generic;
[8578]24using System.Windows.Forms;
25using HeuristicLab.Data;
26using HeuristicLab.MainForm;
27using HeuristicLab.MainForm.WindowsForms;
[8833]28using HeuristicLab.PluginInfrastructure;
[8578]29
30namespace HeuristicLab.Problems.DataAnalysis.Views {
31  [View("Feature Correlation View")]
32  [Content(typeof(DataAnalysisProblemData), false)]
33  public partial class FeatureCorrelationView : AbstractFeatureCorrelationView {
34
35    private FeatureCorrelationCache correlationCache;
[14005]36    private new FeatureCorrelationCalculator CorrelationCalculator {
37      get { return (FeatureCorrelationCalculator)base.CorrelationCalculator; }
38      set { base.CorrelationCalculator = value; }
39    }
[8578]40
41    public FeatureCorrelationView()
42      : base() {
43      InitializeComponent();
[14005]44      CorrelationCalculator = new FeatureCorrelationCalculator();
[8578]45      correlationCache = new FeatureCorrelationCache();
46    }
47
48    protected override void OnContentChanged() {
[8833]49      if (Content != null) {
[8874]50        dataView.ColumnVisibility = dataView.RowVisibility = SetInitialVariableVisibility();
[8833]51      }
[8578]52      correlationCache.Reset();
53      base.OnContentChanged();
54    }
55
[14005]56    private void ignoreMissingValuesCheckbox_CheckedChanged(object sender, EventArgs e) {
57      CalculateCorrelation();
58    }
59
[8578]60    protected override void CalculateCorrelation() {
[8880]61      if (correlationCalcComboBox.SelectedItem == null) return;
62      if (partitionComboBox.SelectedItem == null) return;
[8874]63
[8880]64      IDependencyCalculator calc = (IDependencyCalculator)correlationCalcComboBox.SelectedValue;
65      string partition = (string)partitionComboBox.SelectedValue;
[8874]66      dataView.Enabled = false;
[14005]67      double[,] corr = correlationCache.GetCorrelation(calc, partition, ignoreMissingValuesCheckbox.Checked);
[8874]68      if (corr == null) {
[14005]69        CorrelationCalculator.CalculateElements(Content, calc, partition, ignoreMissingValuesCheckbox.Checked);
[8874]70      } else {
[14005]71        CorrelationCalculator.TryCancelCalculation();
[8874]72        var correlation = new DoubleMatrix(corr, Content.Dataset.DoubleVariables, Content.Dataset.DoubleVariables);
73        UpdateDataView(correlation);
[8578]74      }
75    }
76
[14005]77    protected override void FeatureCorrelation_CalculationFinished(object sender, AbstractFeatureCorrelationCalculator.CorrelationCalculationFinishedArgs e) {
[8578]78      if (InvokeRequired) {
[14005]79        Invoke(new AbstractFeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(FeatureCorrelation_CalculationFinished), sender, e);
[8874]80        return;
[8578]81      }
[14005]82      correlationCache.SetCorrelation(e.Calculcator, e.Partition, e.IgnoreMissingValues, e.Correlation);
83
[8874]84      var correlation = new DoubleMatrix(e.Correlation, Content.Dataset.DoubleVariables, Content.Dataset.DoubleVariables);
85      UpdateDataView(correlation);
[8578]86    }
[8689]87
[8833]88    [NonDiscoverableType]
89    private class FeatureCorrelationCache : Object {
[14005]90      private Dictionary<Tuple<IDependencyCalculator, string, bool>, double[,]> correlationsCache;
[8833]91
92      public FeatureCorrelationCache()
93        : base() {
94        InitializeCaches();
95      }
96
97      private void InitializeCaches() {
[14005]98        correlationsCache = new Dictionary<Tuple<IDependencyCalculator, string, bool>, double[,]>();
[8833]99      }
100
101      public void Reset() {
102        InitializeCaches();
103      }
104
[14005]105      public double[,] GetCorrelation(IDependencyCalculator calc, string partition, bool ignoreMissingValues) {
[8833]106        double[,] corr;
[14005]107        var key = Tuple.Create(calc, partition, ignoreMissingValues);
[8833]108        correlationsCache.TryGetValue(key, out corr);
109        return corr;
110      }
111
[14005]112      public void SetCorrelation(IDependencyCalculator calc, string partition, bool ignoreMissingValues, double[,] correlation) {
113        var key = Tuple.Create(calc, partition, ignoreMissingValues);
[8833]114        correlationsCache[key] = correlation;
115      }
[8689]116    }
[8578]117  }
118}
Note: See TracBrowser for help on using the repository browser.