Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13845 was 13845, checked in by pfleck, 8 years ago

#2597

  • All used variables of a model are initially checked when opening the Target Response Gradient View.
  • Fixed a bug on axis scaling.
File size: 6.5 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Threading.Tasks;
26using System.Windows.Forms;
27using HeuristicLab.Common;
28using HeuristicLab.MainForm;
29using HeuristicLab.Visualization.ChartControlsExtensions;
30
31namespace HeuristicLab.Problems.DataAnalysis.Views {
32  [View("Target Response Gradient View")]
33  [Content(typeof(IRegressionSolution))]
34  public partial class RegressionSolutionTargetResponseGradientView : DataAnalysisSolutionEvaluationView {
35    private readonly Dictionary<string, GradientChart> charts;
36    private const int Points = 200;
37
38    private IEnumerable<GradientChart> VisibleCharts {
39      get { return gradientChartTableLayout.Controls.OfType<GradientChart>(); }
40    }
41
42    public RegressionSolutionTargetResponseGradientView() {
43      InitializeComponent();
44      charts = new Dictionary<string, GradientChart>();
45    }
46
47    public new IRegressionSolution Content {
48      get { return (IRegressionSolution)base.Content; }
49      set { base.Content = value; }
50    }
51
52    protected override void RegisterContentEvents() {
53      base.RegisterContentEvents();
54      variableListView.ItemChecked += variableListView_ItemChecked;
55    }
56
57    protected override void DeregisterContentEvents() {
58      variableListView.ItemChecked -= variableListView_ItemChecked;
59      base.DeregisterContentEvents();
60    }
61
62    protected override void OnContentChanged() {
63      base.OnContentChanged();
64      if (Content == null) return;
65      var problemData = Content.ProblemData;
66      // create dataset
67      var allowedInputVariables = Content.ProblemData.AllowedInputVariables;
68      var variableValues = allowedInputVariables.Select(x => new List<double> { problemData.Dataset.GetDoubleValues(x, problemData.TrainingIndices).Median() });
69      var sharedFixedVariables = new ModifiableDataset(allowedInputVariables, variableValues);
70      // create charts
71      charts.Clear();
72      foreach (var variableName in allowedInputVariables) {
73        var gradientChart = new GradientChart {
74          Dock = DockStyle.Fill,
75          Margin = Padding.Empty,
76          ShowLegend = false,
77          ShowCursor = true,
78          ShowXAxisLabel = true,
79          ShowYAxisLabel = true,
80          YAxisTicks = 5,
81        };
82        gradientChart.VariableValueChanged += async (o, e) => {
83          var recalculations = VisibleCharts.Except(new[] { (GradientChart)o }).Select(async chart => {
84            await chart.RecalculateAsync(updateOnFinish: false, resetYAxis: false);
85          }).ToList();
86          await Task.WhenAll(recalculations);
87
88          if (recalculations.All(t => t.IsCompleted))
89            SyncYAxis();
90        };
91        gradientChart.Configure(new[] { Content }, sharedFixedVariables, variableName, Points);
92        charts[variableName] = gradientChart;
93      }
94      // update variable list
95      variableListView.Items.Clear();
96      foreach (var variable in allowedInputVariables)
97        variableListView.Items.Add(key: variable, text: variable, imageIndex: 0);
98
99      foreach (var variable in Content.GetUsedVariablesForPrediction())
100        variableListView.Items[variable].Checked = true;
101    }
102
103    private void SyncYAxis() {
104      double min = double.MaxValue, max = double.MinValue;
105      foreach (var chart in VisibleCharts) {
106        if (chart.YMin < min) min = chart.YMin;
107        if (chart.YMax > max) max = chart.YMax;
108      }
109      double axisMin, axisMax, axisInterval;
110      ChartUtil.CalculateAxisInterval(min, max, 5, out axisMin, out axisMax, out axisInterval);
111
112      foreach (var chart in VisibleCharts) {
113        chart.FixedYAxisMin = axisMin;
114        chart.FixedYAxisMax = axisMax;
115        chart.Update();
116      }
117    }
118
119    // sort chart controls so that they always appear in the same order as in the list view
120    // the gradient chart layout should be suspended before calling this method
121    private void SortControls() {
122      var tl = gradientChartTableLayout;
123      var indices = variableListView.CheckedItems.Cast<ListViewItem>().ToDictionary(checkedItem => checkedItem.Text, checkedItem => checkedItem.Index);
124      var count = tl.Controls.Count;
125      var charts = new GradientChart[count];
126      for (int i = 0; i < count; ++i) charts[i] = (GradientChart)tl.Controls[i];
127      Array.Sort(charts, (a, b) => indices[a.FreeVariable].CompareTo(indices[b.FreeVariable]));
128      tl.Controls.Clear();
129      tl.Controls.AddRange(charts);
130    }
131
132    private async void variableListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
133      if (charts == null) return;
134      var item = e.Item;
135      var variable = item.Text;
136      var chart = charts[variable];
137      var tl = gradientChartTableLayout;
138      tl.SuspendLayout();
139      var columnWidth = tl.GetColumnWidths()[0]; // all columns have the same width
140      var rowHeight = 0.8f * columnWidth;
141      tl.RowStyles.Clear();
142      if (item.Checked) {
143        tl.Controls.Add(chart);
144        await chart.RecalculateAsync();
145      } else {
146        tl.Controls.Remove(chart);
147      }
148      SyncYAxis();
149
150      var count = tl.Controls.Count;
151      SortControls();
152      tl.RowCount = count / tl.ColumnCount + (count % tl.ColumnCount == 0 ? 0 : 1);
153      for (int i = 0; i < count; ++i) {
154        var control = tl.Controls[i];
155        int rowIndex = i / tl.ColumnCount;
156        int columnIndex = i % tl.ColumnCount;
157        tl.SetRow(control, rowIndex);
158        tl.SetColumn(control, columnIndex);
159      }
160      for (int i = 0; i < tl.RowCount; ++i) {
161        tl.RowStyles.Add(new RowStyle(SizeType.Absolute, rowHeight));
162      }
163      tl.ResumeLayout();
164    }
165  }
166}
Note: See TracBrowser for help on using the repository browser.