Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18082 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

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