[8578] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8578] | 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;
|
---|
[8880] | 36 | private string lastFramesValue;
|
---|
[8578] | 37 |
|
---|
[14005] | 38 | private new TimeframeFeatureCorrelationCalculator CorrelationCalculator {
|
---|
| 39 | get { return (TimeframeFeatureCorrelationCalculator)base.CorrelationCalculator; }
|
---|
| 40 | set { base.CorrelationCalculator = value; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 |
|
---|
[8578] | 44 | public TimeframeFeatureCorrelationView() {
|
---|
| 45 | InitializeComponent();
|
---|
[14005] | 46 | CorrelationCalculator = new TimeframeFeatureCorrelationCalculator();
|
---|
[8578] | 47 | correlationTimeframCache = new FeatureCorrelationTimeframeCache();
|
---|
[8880] | 48 | errorProvider.SetIconAlignment(timeframeTextbox, ErrorIconAlignment.MiddleRight);
|
---|
| 49 | errorProvider.SetIconPadding(timeframeTextbox, 2);
|
---|
| 50 | lastFramesValue = timeframeTextbox.Text;
|
---|
[8578] | 51 | }
|
---|
| 52 |
|
---|
| 53 | protected override void OnContentChanged() {
|
---|
| 54 | correlationTimeframCache.Reset();
|
---|
| 55 | if (Content != null) {
|
---|
[8833] | 56 | dataView.RowVisibility = SetInitialVariableVisibility();
|
---|
[8880] | 57 | SetVariableSelectionComboBox();
|
---|
[8578] | 58 | }
|
---|
| 59 | base.OnContentChanged();
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[8880] | 62 | protected virtual void SetVariableSelectionComboBox() {
|
---|
| 63 | variableSelectionComboBox.DataSource = Content.Dataset.DoubleVariables.ToList();
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[8874] | 66 | private void VariableSelectionComboBox_SelectedChangeCommitted(object sender, EventArgs e) {
|
---|
[8578] | 67 | CalculateCorrelation();
|
---|
| 68 | }
|
---|
[8874] | 69 | private void TimeframeTextbox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
|
---|
[8880] | 70 | if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return) {
|
---|
| 71 | timeFrameLabel.Select(); // select label to validate data
|
---|
[8833] | 72 | }
|
---|
[8880] | 73 |
|
---|
| 74 | if (e.KeyCode == Keys.Escape) {
|
---|
| 75 | timeframeTextbox.Text = lastFramesValue;
|
---|
| 76 | timeFrameLabel.Select(); // select label to validate data
|
---|
| 77 | }
|
---|
[8578] | 78 | }
|
---|
[8880] | 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 | }
|
---|
[8578] | 100 |
|
---|
| 101 | protected override void CalculateCorrelation() {
|
---|
[14005] | 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);
|
---|
[8578] | 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[14005] | 126 | protected override void FeatureCorrelation_CalculationFinished(object sender, AbstractFeatureCorrelationCalculator.CorrelationCalculationFinishedArgs e) {
|
---|
[8578] | 127 | if (InvokeRequired) {
|
---|
[14005] | 128 | Invoke(new AbstractFeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(FeatureCorrelation_CalculationFinished), sender, e);
|
---|
[8578] | 129 | } else {
|
---|
| 130 | correlationTimeframCache.SetTimeframeCorrelation(e.Calculcator, e.Partition, e.Variable, e.Correlation);
|
---|
[8874] | 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);
|
---|
[8578] | 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[8833] | 137 | [NonDiscoverableType]
|
---|
| 138 | private class FeatureCorrelationTimeframeCache : Object {
|
---|
| 139 | private Dictionary<Tuple<IDependencyCalculator, string, string>, double[,]> timeFrameCorrelationsCache;
|
---|
| 140 |
|
---|
| 141 | public FeatureCorrelationTimeframeCache()
|
---|
| 142 | : base() {
|
---|
| 143 | InitializeCaches();
|
---|
[8578] | 144 | }
|
---|
[8689] | 145 |
|
---|
[8833] | 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 | }
|
---|
[8689] | 165 | }
|
---|
[8578] | 166 | }
|
---|
| 167 | } |
---|