Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Views/3.4/FeatureCorrelation/TimeframeFeatureCorrelationView.cs @ 9844

Last change on this file since 9844 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 6.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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    private string lastFramesValue;
37
38    public TimeframeFeatureCorrelationView() {
39      InitializeComponent();
40      correlationTimeframCache = new FeatureCorrelationTimeframeCache();
41      errorProvider.SetIconAlignment(timeframeTextbox, ErrorIconAlignment.MiddleRight);
42      errorProvider.SetIconPadding(timeframeTextbox, 2);
43      lastFramesValue = timeframeTextbox.Text;
44    }
45
46    protected override void OnContentChanged() {
47      correlationTimeframCache.Reset();
48      if (Content != null) {
49        dataView.RowVisibility = SetInitialVariableVisibility();
50        SetVariableSelectionComboBox();
51      }
52      base.OnContentChanged();
53    }
54
55    protected virtual void SetVariableSelectionComboBox() {
56      variableSelectionComboBox.DataSource = Content.Dataset.DoubleVariables.ToList();
57    }
58
59    private void VariableSelectionComboBox_SelectedChangeCommitted(object sender, EventArgs e) {
60      CalculateCorrelation();
61    }
62    private void TimeframeTextbox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
63      if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return) {
64        timeFrameLabel.Select();  // select label to validate data
65      }
66
67      if (e.KeyCode == Keys.Escape) {
68        timeframeTextbox.Text = lastFramesValue;
69        timeFrameLabel.Select();  // select label to validate data
70      }
71    }
72    private void TimeframeTextbox_Validated(object sender, System.EventArgs e) {
73      lastFramesValue = timeframeTextbox.Text;
74      errorProvider.SetError(timeframeTextbox, string.Empty);
75      CalculateCorrelation();
76    }
77    private void TimeframeTextbox_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
78      int help;
79      if (!int.TryParse(timeframeTextbox.Text, out help)) {
80        errorProvider.SetError(timeframeTextbox, "Timeframe couldn't be parsed. Enter a valid integer value.");
81        e.Cancel = true;
82      } else {
83        if (help > 50) {
84          DialogResult dr = MessageBox.Show("The entered value is bigger than 50. Are you sure you want to calculate? " +
85                                            "The calculation could take some time.", "Huge Value Warning", MessageBoxButtons.YesNo);
86          e.Cancel = !dr.Equals(DialogResult.Yes);
87        } else if (help < 0) {
88          errorProvider.SetError(timeframeTextbox, "The entered value can't be negative!");
89          e.Cancel = true;
90        }
91      }
92    }
93
94    protected override void CalculateCorrelation() {
95      if (correlationCalcComboBox.SelectedItem != null && partitionComboBox.SelectedItem != null
96        && variableSelectionComboBox.SelectedItem != null) {
97        string variable = (string)variableSelectionComboBox.SelectedItem;
98        IDependencyCalculator calc = (IDependencyCalculator)correlationCalcComboBox.SelectedValue;
99        string partition = (string)partitionComboBox.SelectedValue;
100        int frames;
101        int.TryParse(timeframeTextbox.Text, out frames);
102        dataView.Enabled = false;
103        double[,] corr = correlationTimeframCache.GetTimeframeCorrelation(calc, partition, variable);
104        if (corr == null) {
105          fcc.CalculateTimeframeElements(calc, partition, variable, frames);
106        } else if (corr.GetLength(1) <= frames) {
107          fcc.CalculateTimeframeElements(calc, partition, variable, frames, corr);
108        } else {
109          fcc.TryCancelCalculation();
110          var columnNames = Enumerable.Range(0, corr.GetLength(1)).Select(x => x.ToString());
111          var correlation = new DoubleMatrix(corr, columnNames, Content.Dataset.DoubleVariables);
112          ((IStringConvertibleMatrix)correlation).Columns = frames + 1;
113          UpdateDataView(correlation);
114        }
115      }
116    }
117
118    protected override void Content_CorrelationCalculationFinished(object sender, FeatureCorrelationCalculator.CorrelationCalculationFinishedArgs e) {
119      if (InvokeRequired) {
120        Invoke(new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished), sender, e);
121      } else {
122        correlationTimeframCache.SetTimeframeCorrelation(e.Calculcator, e.Partition, e.Variable, e.Correlation);
123        var columnNames = Enumerable.Range(0, e.Correlation.GetLength(1)).Select(x => x.ToString());
124        var correlation = new DoubleMatrix(e.Correlation, columnNames, Content.Dataset.DoubleVariables);
125        UpdateDataView(correlation);
126      }
127    }
128
129    [NonDiscoverableType]
130    private class FeatureCorrelationTimeframeCache : Object {
131      private Dictionary<Tuple<IDependencyCalculator, string, string>, double[,]> timeFrameCorrelationsCache;
132
133      public FeatureCorrelationTimeframeCache()
134        : base() {
135        InitializeCaches();
136      }
137
138      private void InitializeCaches() {
139        timeFrameCorrelationsCache = new Dictionary<Tuple<IDependencyCalculator, string, string>, double[,]>();
140      }
141
142      public void Reset() {
143        InitializeCaches();
144      }
145
146      public double[,] GetTimeframeCorrelation(IDependencyCalculator calc, string partition, string variable) {
147        double[,] corr;
148        var key = new Tuple<IDependencyCalculator, string, string>(calc, partition, variable);
149        timeFrameCorrelationsCache.TryGetValue(key, out corr);
150        return corr;
151      }
152
153      public void SetTimeframeCorrelation(IDependencyCalculator calc, string partition, string variable, double[,] correlation) {
154        var key = new Tuple<IDependencyCalculator, string, string>(calc, partition, variable);
155        timeFrameCorrelationsCache[key] = correlation;
156      }
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.