Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/RegressionSolutionTargetResponseGradientView.cs @ 13840

Last change on this file since 13840 was 13840, checked in by pfleck, 9 years ago

#2597 Implemented cancellation of chart update when new update is called during calculation.

File size: 5.3 KB
RevLine 
[13817]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
[13828]22using System;
[13817]23using System.Collections.Generic;
[13808]24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.Problems.DataAnalysis.Views {
30  [View("Target Response Gradient View")]
31  [Content(typeof(IRegressionSolution))]
32  public partial class RegressionSolutionTargetResponseGradientView : DataAnalysisSolutionEvaluationView {
[13831]33    private readonly Dictionary<string, GradientChart> charts;
34    private const int Points = 200;
[13808]35
36    public RegressionSolutionTargetResponseGradientView() {
37      InitializeComponent();
[13831]38      charts = new Dictionary<string, GradientChart>();
[13808]39    }
40
41    public new IRegressionSolution Content {
42      get { return (IRegressionSolution)base.Content; }
[13831]43      set { base.Content = value; }
[13808]44    }
45
46    protected override void RegisterContentEvents() {
47      base.RegisterContentEvents();
48      variableListView.ItemChecked += variableListView_ItemChecked;
49    }
50
51    protected override void DeregisterContentEvents() {
52      variableListView.ItemChecked -= variableListView_ItemChecked;
53      base.DeregisterContentEvents();
54    }
55
56    protected override void OnContentChanged() {
57      base.OnContentChanged();
[13817]58      if (Content == null) return;
[13831]59      var problemData = Content.ProblemData;
[13828]60      // create dataset
61      var variableNames = Content.GetUsedVariablesForPrediction().ToList();
[13831]62      var variableValues = variableNames.Select(x => new List<double> { problemData.Dataset.GetDoubleValues(x, problemData.TrainingIndices).Median() });
63      var sharedFixedVariables = new ModifiableDataset(variableNames, variableValues);
[13828]64      // create charts
[13831]65      charts.Clear();
66      foreach (var variableName in variableNames) {
[13820]67        var gradientChart = new GradientChart {
68          Dock = DockStyle.Fill,
69          Margin = Padding.Empty,
70          ShowLegend = false,
71          ShowCursor = true,
72          ShowXAxisLabel = true,
[13831]73          ShowYAxisLabel = true,
[13820]74        };
[13840]75        gradientChart.VariableValueChanged += (o, e) => {
[13817]76          foreach (var chart in gradientChartTableLayout.Controls.Cast<GradientChart>()) {
77            if (chart == (GradientChart)o) continue;
[13840]78            chart.UpdateChart();
[13817]79          }
80        };
[13831]81        gradientChart.Configure(new[] { Content }, sharedFixedVariables, variableName, Points);
82        charts[variableName] = gradientChart;
[13808]83      }
[13828]84      // update variable list
85      variableListView.Items.Clear();
86      variableListView.Items.AddRange(variableNames.Select(x => new ListViewItem(x, 0)).ToArray());
[13808]87    }
[13828]88    // sort chart controls so that they always appear in the same order as in the list view
89    // the gradient chart layout should be suspended before calling this method
90    private void SortControls() {
91      var tl = gradientChartTableLayout;
92      var indices = variableListView.CheckedItems.Cast<ListViewItem>().ToDictionary(checkedItem => checkedItem.Text, checkedItem => checkedItem.Index);
93      var count = tl.Controls.Count;
[13831]94      var charts = new GradientChart[count];
95      for (int i = 0; i < count; ++i) charts[i] = (GradientChart)tl.Controls[i];
96      Array.Sort(charts, (a, b) => indices[a.FreeVariable].CompareTo(indices[b.FreeVariable]));
[13828]97      tl.Controls.Clear();
[13831]98      tl.Controls.AddRange(charts);
[13828]99    }
[13808]100
[13840]101    private void variableListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
[13808]102      if (charts == null) return;
103      var item = e.Item;
104      var variable = item.Text;
105      var chart = charts[variable];
[13817]106      var tl = gradientChartTableLayout;
107      tl.SuspendLayout();
108      var columnWidth = tl.GetColumnWidths()[0]; // all columns have the same width
109      var rowHeight = 0.8f * columnWidth;
110      tl.RowStyles.Clear();
[13808]111      if (item.Checked) {
[13817]112        tl.Controls.Add(chart);
[13840]113        chart.UpdateChart();
[13808]114      } else {
[13817]115        tl.Controls.Remove(chart);
[13808]116      }
[13828]117      var count = tl.Controls.Count;
118      SortControls();
119      tl.RowCount = count / tl.ColumnCount + (count % tl.ColumnCount == 0 ? 0 : 1);
120      for (int i = 0; i < count; ++i) {
121        var control = tl.Controls[i];
122        int rowIndex = i / tl.ColumnCount;
123        int columnIndex = i % tl.ColumnCount;
124        tl.SetRow(control, rowIndex);
125        tl.SetColumn(control, columnIndex);
126      }
[13817]127      for (int i = 0; i < tl.RowCount; ++i) {
128        tl.RowStyles.Add(new RowStyle(SizeType.Absolute, rowHeight));
129      }
130      tl.ResumeLayout();
[13808]131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.