Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/FeatureCorrelation/TimeframeFeatureCorrelationView.cs @ 8874

Last change on this file since 8874 was 8874, checked in by mkommend, 11 years ago

#1292: Minor code cleanup in feature correlation classes.

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Data;
27using HeuristicLab.MainForm;
28using HeuristicLab.PluginInfrastructure;
29
30namespace HeuristicLab.Problems.DataAnalysis.Views {
31  [View("Timeframe Feature Correlation View")]
32  [Content(typeof(DataAnalysisProblemData), false)]
33  public partial class TimeframeFeatureCorrelationView : AbstractFeatureCorrelationView {
34
35    private FeatureCorrelationTimeframeCache correlationTimeframCache;
36
37    public TimeframeFeatureCorrelationView() {
38      InitializeComponent();
39      correlationTimeframCache = new FeatureCorrelationTimeframeCache();
40    }
41
42    protected override void OnContentChanged() {
43      correlationTimeframCache.Reset();
44      if (Content != null) {
45        dataView.RowVisibility = SetInitialVariableVisibility();
46        VariableSelectionComboBox.DataSource = Content.Dataset.DoubleVariables.ToList();
47      }
48      base.OnContentChanged();
49    }
50
51    private void VariableSelectionComboBox_SelectedChangeCommitted(object sender, EventArgs e) {
52      CalculateCorrelation();
53    }
54
55    private void TimeframeTextbox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
56      if (e.KeyCode == Keys.Enter) {
57        CalculateCorrelation();
58      }
59    }
60
61    protected override void CalculateCorrelation() {
62      if (CorrelationCalcComboBox.SelectedItem != null && PartitionComboBox.SelectedItem != null
63        && VariableSelectionComboBox.SelectedItem != null && ValidateTimeframeTextbox()) {
64        string variable = (string)VariableSelectionComboBox.SelectedItem;
65        IDependencyCalculator calc = (IDependencyCalculator)CorrelationCalcComboBox.SelectedValue;
66        string partition = (string)PartitionComboBox.SelectedValue;
67        int frames;
68        int.TryParse(TimeframeTextbox.Text, out frames);
69        dataView.Enabled = false;
70        double[,] corr = correlationTimeframCache.GetTimeframeCorrelation(calc, partition, variable);
71        if (corr == null) {
72          fcc.CalculateTimeframeElements(calc, partition, variable, frames);
73        } else if (corr.GetLength(1) <= frames) {
74          fcc.CalculateTimeframeElements(calc, partition, variable, frames, corr);
75        } else {
76          fcc.TryCancelCalculation();
77          var columnNames = Enumerable.Range(0, corr.GetLength(1)).Select(x => x.ToString());
78          var correlation = new DoubleMatrix(corr, columnNames, Content.Dataset.DoubleVariables);
79          ((IStringConvertibleMatrix)correlation).Columns = frames + 1;
80          UpdateDataView(correlation);
81        }
82      }
83    }
84
85    private bool ValidateTimeframeTextbox() {
86      int help;
87      if (!int.TryParse(TimeframeTextbox.Text, out help)) {
88        MessageBox.Show("Timeframe couldn't be parsed. Enter a valid integer value.", "Parse Error", MessageBoxButtons.OK);
89        return false;
90      } else {
91        if (help > 50) {
92          DialogResult dr = MessageBox.Show("The entered value is bigger than 50. Are you sure you want to calculate? " +
93                                            "The calculation could take some time.", "Huge Value Warning", MessageBoxButtons.YesNo);
94          return dr.Equals(DialogResult.Yes);
95        }
96      }
97      return true;
98    }
99
100
101    protected override void Content_CorrelationCalculationFinished(object sender, FeatureCorrelationCalculator.CorrelationCalculationFinishedArgs e) {
102      if (InvokeRequired) {
103        Invoke(new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished), sender, e);
104      } else {
105        correlationTimeframCache.SetTimeframeCorrelation(e.Calculcator, e.Partition, e.Variable, e.Correlation);
106        var columnNames = Enumerable.Range(0, e.Correlation.GetLength(1)).Select(x => x.ToString());
107        var correlation = new DoubleMatrix(e.Correlation, columnNames, Content.Dataset.DoubleVariables);
108        UpdateDataView(correlation);
109      }
110    }
111
112    [NonDiscoverableType]
113    private class FeatureCorrelationTimeframeCache : Object {
114      private Dictionary<Tuple<IDependencyCalculator, string, string>, double[,]> timeFrameCorrelationsCache;
115
116      public FeatureCorrelationTimeframeCache()
117        : base() {
118        InitializeCaches();
119      }
120
121      private void InitializeCaches() {
122        timeFrameCorrelationsCache = new Dictionary<Tuple<IDependencyCalculator, string, string>, double[,]>();
123      }
124
125      public void Reset() {
126        InitializeCaches();
127      }
128
129      public double[,] GetTimeframeCorrelation(IDependencyCalculator calc, string partition, string variable) {
130        double[,] corr;
131        var key = new Tuple<IDependencyCalculator, string, string>(calc, partition, variable);
132        timeFrameCorrelationsCache.TryGetValue(key, out corr);
133        return corr;
134      }
135
136      public void SetTimeframeCorrelation(IDependencyCalculator calc, string partition, string variable, double[,] correlation) {
137        var key = new Tuple<IDependencyCalculator, string, string>(calc, partition, variable);
138        timeFrameCorrelationsCache[key] = correlation;
139      }
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.