Ignore:
Timestamp:
06/11/14 12:36:39 (8 years ago)
Author:
mleitner
Message:

Refactoring

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.4/PreprocessingFeatureCorrelationView.cs

    r10934 r10978  
    3030using System;
    3131using HeuristicLab.DataPreprocessing;
     32using HeuristicLab.Problems.DataAnalysis.Views;
    3233
    3334namespace HeuristicLab.Problems.DataAnalysis.Views {
     
    3536  [Content(typeof(CorrelationMatrixContent), false)]
    3637  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;
    4438
    4539    public new CorrelationMatrixContent Content
     
    4943    }
    5044
    51     private FeatureCorrelationCache correlationCache;
     45    FeatureCorrelationView correlationView;
    5246
    5347    public PreprocessingFeatureCorrelationView() {
    54 
    55       correlationCache = new FeatureCorrelationCache();
    5648      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;
     49      correlationView = new FeatureCorrelationView();
     50      correlationView.Dock = DockStyle.Fill;
     51      this.Controls.Add(correlationView);
    6652    }
    6753
     
    6955    protected override void RegisterContentEvents() {
    7056      base.RegisterContentEvents();
    71       Content.PreprocessingData.Changed += Data_Changed;
    72       fcc.ProgressCalculation += new FeatureCorrelationCalculator.ProgressCalculationHandler(Content_ProgressCalculation);
    73       fcc.CorrelationCalculationFinished += new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished);
     57      Content.Changed += Data_Changed;     
    7458    }
    7559
    7660    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);
     61      Content.Changed -= Data_Changed;     
    8062      base.DeregisterContentEvents();
    8163    }
     
    8769    protected override void OnContentChanged() {
    8870      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);
     71      if (Content == null) {
     72        correlationView.Content = null;
    13873        return;
    13974      }
    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);
     75
     76      correlationView.Content = Content.ProblemData;
    14377    }
    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       }
    19578  }
    19679}
Note: See TracChangeset for help on using the changeset viewer.