#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using HeuristicLab.Data;
using HeuristicLab.MainForm;
using HeuristicLab.MainForm.WindowsForms;
using HeuristicLab.PluginInfrastructure;
using System;
using HeuristicLab.DataPreprocessing;
namespace HeuristicLab.Problems.DataAnalysis.Views {
[View("Preprocessing Feature Correlation View")]
[Content(typeof(CorrelationMatrixContent), false)]
public partial class PreprocessingFeatureCorrelationView : AsynchronousContentView {
public const string ALLSAMPLES = "All Samples";
public const string TRAININGSAMPLES = "Training Samples";
public const string TESTSAMPLES = "Test Samples";
public static readonly IList Partitions = new List() { ALLSAMPLES, TRAININGSAMPLES, TESTSAMPLES };
protected FeatureCorrelationCalculator fcc;
public new CorrelationMatrixContent Content
{
get { return (CorrelationMatrixContent) base.Content; }
set { base.Content = value; }
}
private FeatureCorrelationCache correlationCache;
public PreprocessingFeatureCorrelationView() {
correlationCache = new FeatureCorrelationCache();
InitializeComponent();
fcc = new FeatureCorrelationCalculator();
var calculators = ApplicationManager.Manager.GetInstances();
var calcList = calculators.OrderBy(c => c.Name).Select(c => new { Name = c.Name, Calculator = c }).ToList();
correlationCalcComboBox.ValueMember = "Calculator";
correlationCalcComboBox.DisplayMember = "Name";
correlationCalcComboBox.DataSource = calcList;
correlationCalcComboBox.SelectedItem = calcList.First(c => c.Calculator.GetType().Equals(typeof(PearsonsRDependenceCalculator)));
partitionComboBox.DataSource = Partitions;
partitionComboBox.SelectedItem = TRAININGSAMPLES;
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.PreprocessingData.Changed += Data_Changed;
fcc.ProgressCalculation += new FeatureCorrelationCalculator.ProgressCalculationHandler(Content_ProgressCalculation);
fcc.CorrelationCalculationFinished += new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished);
}
protected override void DeregisterContentEvents() {
Content.PreprocessingData.Changed -= Data_Changed;
fcc.CorrelationCalculationFinished -= new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished);
fcc.ProgressCalculation -= new FeatureCorrelationCalculator.ProgressCalculationHandler(Content_ProgressCalculation);
base.DeregisterContentEvents();
}
private void Data_Changed(object sender, DataPreprocessingChangedEventArgs e) {
OnContentChanged();
}
protected override void OnContentChanged() {
base.OnContentChanged();
fcc.TryCancelCalculation();
if (Content != null) {
fcc.ProblemData = Content.ProblemData;
CalculateCorrelation();
} else {
dataView.Maximum = 0;
dataView.Minimum = 0;
dataView.Content = null;
dataView.ResetVisibility();
}
}
protected virtual bool[] SetInitialVariableVisibility() {
bool[] initialVisibility = new bool[Content.ProblemData.Dataset.DoubleVariables.Count()];
int i = 0;
foreach (var variable in Content.ProblemData.Dataset.DoubleVariables) {
initialVisibility[i] = Content.ProblemData.AllowedInputVariables.Contains(variable);
i++;
}
return initialVisibility;
}
protected void CorrelationMeasureComboBox_SelectedChangeCommitted(object sender, System.EventArgs e) {
CalculateCorrelation();
}
protected void PartitionComboBox_SelectedChangeCommitted(object sender, System.EventArgs e) {
CalculateCorrelation();
}
protected void CalculateCorrelation() {
if (correlationCalcComboBox.SelectedItem == null) return;
if (partitionComboBox.SelectedItem == null) return;
IDependencyCalculator calc = (IDependencyCalculator)correlationCalcComboBox.SelectedValue;
string partition = (string)partitionComboBox.SelectedValue;
dataView.Enabled = false;
double[,] corr = correlationCache.GetCorrelation(calc, partition);
if (corr == null) {
fcc.CalculateElements(calc, partition);
} else {
fcc.TryCancelCalculation();
var correlation = new DoubleMatrix(corr, Content.ProblemData.Dataset.DoubleVariables, Content.ProblemData.Dataset.DoubleVariables);
UpdateDataView(correlation);
}
}
protected void Content_CorrelationCalculationFinished(object sender, FeatureCorrelationCalculator.CorrelationCalculationFinishedArgs e) {
if (InvokeRequired) {
Invoke(new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished), sender, e);
return;
}
correlationCache.SetCorrelation(e.Calculcator, e.Partition, e.Correlation);
var correlation = new DoubleMatrix(e.Correlation, Content.ProblemData.Dataset.DoubleVariables, Content.ProblemData.Dataset.DoubleVariables);
UpdateDataView(correlation);
}
protected void UpdateDataView(DoubleMatrix correlation) {
IDependencyCalculator calc = (IDependencyCalculator)correlationCalcComboBox.SelectedValue;
maximumLabel.Text = calc.Maximum.ToString();
minimumLabel.Text = calc.Minimum.ToString();
correlation.SortableView = true;
dataView.Maximum = calc.Maximum;
dataView.Minimum = calc.Minimum;
dataView.Content = correlation;
dataView.Enabled = true;
}
protected void Content_ProgressCalculation(object sender, ProgressChangedEventArgs e) {
if (!progressPanel.Visible && e.ProgressPercentage != progressBar.Maximum) {
progressPanel.Show();
} else if (e.ProgressPercentage == progressBar.Maximum) {
progressPanel.Hide();
}
progressBar.Value = e.ProgressPercentage;
}
[NonDiscoverableType]
private class FeatureCorrelationCache : Object {
private Dictionary, double[,]> correlationsCache;
public FeatureCorrelationCache()
: base() {
InitializeCaches();
}
private void InitializeCaches() {
correlationsCache = new Dictionary, double[,]>();
}
public void Reset() {
InitializeCaches();
}
public double[,] GetCorrelation(IDependencyCalculator calc, string partition) {
double[,] corr;
var key = new Tuple(calc, partition);
correlationsCache.TryGetValue(key, out corr);
return corr;
}
public void SetCorrelation(IDependencyCalculator calc, string partition, double[,] correlation) {
var key = new Tuple(calc, partition);
correlationsCache[key] = correlation;
}
}
}
}