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