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 |
|
---|
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 |
|
---|
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 | protected void VariableSelectionComboBox_SelectedChangeCommitted(object sender, EventArgs e) {
|
---|
52 | CalculateCorrelation();
|
---|
53 | }
|
---|
54 | protected void TimeframeTextbox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
|
---|
55 | if (e.KeyCode == Keys.Enter) {
|
---|
56 | CalculateCorrelation();
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | protected override void CalculateCorrelation() {
|
---|
61 | if (CorrelationCalcComboBox.SelectedItem != null && PartitionComboBox.SelectedItem != null
|
---|
62 | && VariableSelectionComboBox.SelectedItem != null && ValidateTimeframeTextbox()) {
|
---|
63 | string variable = (string)VariableSelectionComboBox.SelectedItem;
|
---|
64 | IDependencyCalculator calc = (IDependencyCalculator)CorrelationCalcComboBox.SelectedValue;
|
---|
65 | string partition = (string)PartitionComboBox.SelectedValue;
|
---|
66 | int frames;
|
---|
67 | int.TryParse(TimeframeTextbox.Text, out frames);
|
---|
68 | dataView.Enabled = false;
|
---|
69 | double[,] corr = correlationTimeframCache.GetTimeframeCorrelation(calc, partition, variable);
|
---|
70 | if (corr == null) {
|
---|
71 | fcc.CalculateTimeframeElements(calc, partition, variable, frames);
|
---|
72 | } else if (corr.GetLength(1) <= frames) {
|
---|
73 | fcc.CalculateTimeframeElements(calc, partition, variable, frames, corr);
|
---|
74 | } else {
|
---|
75 | fcc.TryCancelCalculation();
|
---|
76 | SetNewCorrelation(corr, calc, frames);
|
---|
77 | UpdateDataView();
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected bool ValidateTimeframeTextbox() {
|
---|
83 | int help;
|
---|
84 | if (!int.TryParse(TimeframeTextbox.Text, out help)) {
|
---|
85 | MessageBox.Show("Timeframe couldn't be parsed. Enter a valid integer value.", "Parse Error", MessageBoxButtons.OK);
|
---|
86 | return false;
|
---|
87 | } else {
|
---|
88 | if (help > 50) {
|
---|
89 | DialogResult dr = MessageBox.Show("The entered value is bigger than 50. Are you sure you want to calculate? " +
|
---|
90 | "The calculation could take some time.", "Huge Value Warning", MessageBoxButtons.YesNo);
|
---|
91 | return dr.Equals(DialogResult.Yes);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | return true;
|
---|
95 | }
|
---|
96 |
|
---|
97 | private void SetNewCorrelation(double[,] elements, IDependencyCalculator calc, int frames) {
|
---|
98 | double[,] neededValues = new double[elements.GetLength(0), frames + 1];
|
---|
99 | for (int i = 0; i < elements.GetLength(0); i++) {
|
---|
100 | Array.Copy(elements, i * elements.GetLength(1), neededValues, i * neededValues.GetLength(1), frames + 1);
|
---|
101 | }
|
---|
102 | SetNewCorrelation(neededValues);
|
---|
103 | }
|
---|
104 |
|
---|
105 | private void SetNewCorrelation(double[,] elements) {
|
---|
106 | currentCorrelation = new DoubleMatrix(elements,
|
---|
107 | Enumerable.Range(0, elements.GetLength(1)).Select(x => x.ToString()),
|
---|
108 | Content.Dataset.DoubleVariables);
|
---|
109 | }
|
---|
110 |
|
---|
111 | protected override void Content_CorrelationCalculationFinished(object sender, FeatureCorrelationCalculator.CorrelationCalculationFinishedArgs e) {
|
---|
112 | if (InvokeRequired) {
|
---|
113 | Invoke(new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished), sender, e);
|
---|
114 | } else {
|
---|
115 | correlationTimeframCache.SetTimeframeCorrelation(e.Calculcator, e.Partition, e.Variable, e.Correlation);
|
---|
116 | SetNewCorrelation(e.Correlation);
|
---|
117 | UpdateDataView();
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | [NonDiscoverableType]
|
---|
122 | private class FeatureCorrelationTimeframeCache : Object {
|
---|
123 | private Dictionary<Tuple<IDependencyCalculator, string, string>, double[,]> timeFrameCorrelationsCache;
|
---|
124 |
|
---|
125 | public FeatureCorrelationTimeframeCache()
|
---|
126 | : base() {
|
---|
127 | InitializeCaches();
|
---|
128 | }
|
---|
129 |
|
---|
130 | private void InitializeCaches() {
|
---|
131 | timeFrameCorrelationsCache = new Dictionary<Tuple<IDependencyCalculator, string, string>, double[,]>();
|
---|
132 | }
|
---|
133 |
|
---|
134 | public void Reset() {
|
---|
135 | InitializeCaches();
|
---|
136 | }
|
---|
137 |
|
---|
138 | public double[,] GetTimeframeCorrelation(IDependencyCalculator calc, string partition, string variable) {
|
---|
139 | double[,] corr;
|
---|
140 | var key = new Tuple<IDependencyCalculator, string, string>(calc, partition, variable);
|
---|
141 | timeFrameCorrelationsCache.TryGetValue(key, out corr);
|
---|
142 | return corr;
|
---|
143 | }
|
---|
144 |
|
---|
145 | public void SetTimeframeCorrelation(IDependencyCalculator calc, string partition, string variable, double[,] correlation) {
|
---|
146 | var key = new Tuple<IDependencyCalculator, string, string>(calc, partition, variable);
|
---|
147 | timeFrameCorrelationsCache[key] = correlation;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 | } |
---|