[8578] | 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;
|
---|
[8833] | 23 | using System.Collections.Generic;
|
---|
[8578] | 24 | using System.Linq;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.MainForm;
|
---|
[8833] | 28 | using HeuristicLab.PluginInfrastructure;
|
---|
[8578] | 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) {
|
---|
[8833] | 45 | dataView.RowVisibility = SetInitialVariableVisibility();
|
---|
[8578] | 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 | }
|
---|
[8833] | 54 | protected void TimeframeTextbox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
|
---|
| 55 | if (e.KeyCode == Keys.Enter) {
|
---|
| 56 | CalculateCorrelation();
|
---|
| 57 | }
|
---|
[8578] | 58 | }
|
---|
| 59 |
|
---|
| 60 | protected override void CalculateCorrelation() {
|
---|
[8833] | 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;
|
---|
[8578] | 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 {
|
---|
[8833] | 75 | fcc.TryCancelCalculation();
|
---|
[8578] | 76 | SetNewCorrelation(corr, calc, frames);
|
---|
[8833] | 77 | UpdateDataView();
|
---|
[8578] | 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[8833] | 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) {
|
---|
[8578] | 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 | }
|
---|
[8861] | 102 | SetNewCorrelation(neededValues);
|
---|
[8578] | 103 | }
|
---|
| 104 |
|
---|
[8861] | 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);
|
---|
[8578] | 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);
|
---|
[8861] | 116 | SetNewCorrelation(e.Correlation);
|
---|
[8833] | 117 | UpdateDataView();
|
---|
[8578] | 118 | }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[8833] | 121 | [NonDiscoverableType]
|
---|
| 122 | private class FeatureCorrelationTimeframeCache : Object {
|
---|
| 123 | private Dictionary<Tuple<IDependencyCalculator, string, string>, double[,]> timeFrameCorrelationsCache;
|
---|
| 124 |
|
---|
| 125 | public FeatureCorrelationTimeframeCache()
|
---|
| 126 | : base() {
|
---|
| 127 | InitializeCaches();
|
---|
[8578] | 128 | }
|
---|
[8689] | 129 |
|
---|
[8833] | 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 | }
|
---|
[8689] | 149 | }
|
---|
[8578] | 150 | }
|
---|
| 151 | } |
---|