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

Last change on this file since 14021 was 14021, checked in by bburlacu, 6 years ago

#2597: Fixed some additional issues with the density charts (which are now turned on by default).

File size: 15.2 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.Drawing;
25using System.Linq;
26using System.Threading.Tasks;
27using System.Windows.Forms;
28using HeuristicLab.Common;
29using HeuristicLab.MainForm;
30using HeuristicLab.Visualization.ChartControlsExtensions;
31
32namespace HeuristicLab.Problems.DataAnalysis.Views {
33  [View("Target Response Gradients")]
34  [Content(typeof(IRegressionSolution))]
35  public partial class RegressionSolutionTargetResponseGradientView : DataAnalysisSolutionEvaluationView {
36    private readonly Dictionary<string, GradientChart> gradientCharts;
37    private readonly Dictionary<string, DensityChart> densityCharts;
38    private readonly Dictionary<string, Panel> groupingPanels;
39
40    private const int Points = 200;
41    private int MaxColumns = 4;
42
43    private IEnumerable<string> VisibleVariables {
44      get {
45        foreach (ListViewItem item in variableListView.CheckedItems)
46          yield return item.Text;
47      }
48    }
49    private IEnumerable<GradientChart> VisibleGradientCharts {
50      get { return VisibleVariables.Select(v => gradientCharts[v]); }
51    }
52    private IEnumerable<DensityChart> VisibleDensityCharts {
53      get { return VisibleVariables.Select(v => densityCharts[v]); }
54    }
55    private IEnumerable<Panel> VisibleChartsPanels {
56      get { return VisibleVariables.Select(v => groupingPanels[v]); }
57    }
58
59    public RegressionSolutionTargetResponseGradientView() {
60      InitializeComponent();
61      gradientCharts = new Dictionary<string, GradientChart>();
62      densityCharts = new Dictionary<string, DensityChart>();
63      groupingPanels = new Dictionary<string, Panel>();
64
65      limitView.Content = new DoubleLimit(0, 1);
66      limitView.Content.ValueChanged += limit_ValueChanged;
67
68      densityComboBox.SelectedIndex = 1; // select Training
69
70      columnsTextBox.Text = "4";
71
72      // Avoid additional horizontal scrollbar
73      var vertScrollWidth = SystemInformation.VerticalScrollBarWidth;
74      scrollPanel.Padding = new Padding(0, 0, vertScrollWidth, 0);
75      scrollPanel.AutoScroll = true;
76    }
77
78    public new IRegressionSolution Content {
79      get { return (IRegressionSolution)base.Content; }
80      set { base.Content = value; }
81    }
82
83    protected override void RegisterContentEvents() {
84      base.RegisterContentEvents();
85      Content.ModelChanged += solution_ModelChanged;
86    }
87
88    protected override void DeregisterContentEvents() {
89      Content.ModelChanged -= solution_ModelChanged;
90      base.DeregisterContentEvents();
91    }
92
93    protected override void OnContentChanged() {
94      base.OnContentChanged();
95      if (Content == null) return;
96      var problemData = Content.ProblemData;
97
98      // Init Y-axis range
99      double min = double.MaxValue, max = double.MinValue;
100      var trainingTarget = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);
101      foreach (var t in trainingTarget) {
102        if (t < min) min = t;
103        if (t > max) max = t;
104      }
105      double range = max - min;
106      const double scale = 1.0 / 3.0;
107      double axisMin, axisMax, axisInterval;
108      ChartUtil.CalculateAxisInterval(min - scale * range, max + scale * range, 5, out axisMin, out axisMax, out axisInterval);
109      automaticYAxisCheckBox.Checked = false;
110      limitView.ReadOnly = false;
111      limitView.Content.Lower = axisMin;
112      limitView.Content.Upper = axisMax;
113
114      // create dataset
115      var allowedInputVariables = Content.ProblemData.AllowedInputVariables;
116      var variableValues = allowedInputVariables.Select(x => new List<double> { problemData.Dataset.GetDoubleValues(x, problemData.TrainingIndices).Median() });
117      var sharedFixedVariables = new ModifiableDataset(allowedInputVariables, variableValues);
118
119      // create controls
120      gradientCharts.Clear();
121      densityCharts.Clear();
122      groupingPanels.Clear();
123      foreach (var variableName in allowedInputVariables) {
124        var gradientChart = CreateGradientChart(variableName, sharedFixedVariables);
125        gradientCharts.Add(variableName, gradientChart);
126
127        var densityChart = new DensityChart() {
128          Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right,
129          Margin = Padding.Empty,
130          Height = 12,
131          Visible = false
132        };
133        densityCharts.Add(variableName, densityChart);
134
135        var panel = new Panel() {
136          Dock = DockStyle.Fill,
137          Margin = Padding.Empty,
138          BackColor = Color.White
139        };
140
141        panel.Controls.Add(densityChart);
142        panel.Controls.Add(gradientChart);
143        groupingPanels.Add(variableName, panel);
144      }
145
146      // update variable list
147      variableListView.ItemChecked -= variableListView_ItemChecked;
148      variableListView.Items.Clear();
149      foreach (var variable in allowedInputVariables)
150        variableListView.Items.Add(key: variable, text: variable, imageIndex: 0);
151
152      foreach (var variable in Content.Model.VariablesUsedForPrediction)
153        variableListView.Items[variable].Checked = true;
154      variableListView.ItemChecked += variableListView_ItemChecked;
155
156      RecalculateAndRelayoutCharts();
157    }
158
159    private async void RecalculateAndRelayoutCharts() {
160      foreach (var variable in VisibleVariables) {
161        var gradientChart = gradientCharts[variable];
162        await gradientChart.RecalculateAsync();
163      }
164      gradientChartTableLayout.SuspendLayout();
165      SetupYAxis();
166      ReOrderControls();
167      SetStyles();
168      gradientChartTableLayout.ResumeLayout();
169      gradientChartTableLayout.Refresh();
170      foreach (var variable in VisibleVariables) {
171        var densityChart = densityCharts[variable];
172        UpdateDensityChart(densityChart, variable);
173      }
174    }
175    private GradientChart CreateGradientChart(string variableName, ModifiableDataset sharedFixedVariables) {
176      var gradientChart = new GradientChart {
177        Dock = DockStyle.Fill,
178        Margin = Padding.Empty,
179        ShowLegend = false,
180        ShowCursor = true,
181        ShowConfigButton = false,
182        YAxisTicks = 5,
183      };
184      gradientChart.VariableValueChanged += async (o, e) => {
185        var recalculations = VisibleGradientCharts.Except(new[] { (GradientChart)o }).Select(async chart => {
186          await chart.RecalculateAsync(updateOnFinish: false, resetYAxis: false);
187        }).ToList();
188        await Task.WhenAll(recalculations);
189
190        if (recalculations.All(t => t.IsCompleted))
191          SetupYAxis();
192      };
193      gradientChart.Configure(new[] { Content }, sharedFixedVariables, variableName, Points);
194      gradientChart.SolutionAdded += gradientChart_SolutionAdded;
195      gradientChart.SolutionRemoved += gradientChart_SolutionRemoved;
196      return gradientChart;
197    }
198
199    private void SetupYAxis() {
200      double axisMin, axisMax;
201      if (automaticYAxisCheckBox.Checked) {
202        double min = double.MaxValue, max = double.MinValue;
203        foreach (var chart in VisibleGradientCharts) {
204          if (chart.YMin < min) min = chart.YMin;
205          if (chart.YMax > max) max = chart.YMax;
206        }
207
208        double axisInterval;
209        ChartUtil.CalculateAxisInterval(min, max, 5, out axisMin, out axisMax, out axisInterval);
210      } else {
211        axisMin = limitView.Content.Lower;
212        axisMax = limitView.Content.Upper;
213      }
214
215      foreach (var chart in VisibleGradientCharts) {
216        chart.FixedYAxisMin = axisMin;
217        chart.FixedYAxisMax = axisMax;
218      }
219    }
220
221    // reorder chart controls so that they always appear in the same order as in the list view
222    // the table layout containing the controls should be suspended before calling this method
223    private void ReOrderControls() {
224      var tl = gradientChartTableLayout;
225      tl.Controls.Clear();
226      int row = 0, column = 0;
227      foreach (var v in VisibleVariables) {
228        var chartsPanel = groupingPanels[v];
229        tl.Controls.Add(chartsPanel, column, row);
230
231        var chart = gradientCharts[v];
232        chart.YAxisTitle = column == 0 ? Content.ProblemData.TargetVariable : string.Empty;
233        column++;
234
235        if (column == MaxColumns) {
236          row++;
237          column = 0;
238        }
239      }
240    }
241
242    private void SetStyles() {
243      var tl = gradientChartTableLayout;
244      tl.RowStyles.Clear();
245      tl.ColumnStyles.Clear();
246      int numVariables = VisibleVariables.Count();
247      if (numVariables == 0)
248        return;
249
250      // set column styles
251      tl.ColumnCount = Math.Min(numVariables, MaxColumns);
252      for (int c = 0; c < tl.ColumnCount; c++)
253        tl.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100.0f / tl.ColumnCount));
254
255      // set row styles
256      tl.RowCount = (int)Math.Ceiling((double)numVariables / tl.ColumnCount);
257      var columnWidth = tl.Width / tl.ColumnCount; // assume all columns have the same width
258      var rowHeight = (int)(0.8 * columnWidth);
259      for (int r = 0; r < tl.RowCount; r++)
260        tl.RowStyles.Add(new RowStyle(SizeType.Absolute, rowHeight));
261    }
262
263    private async void gradientChart_SolutionAdded(object sender, EventArgs<IRegressionSolution> e) {
264      var solution = e.Value;
265      foreach (var chart in gradientCharts.Values) {
266        if (sender == chart) continue;
267        await chart.AddSolutionAsync(solution);
268      }
269    }
270
271    private async void gradientChart_SolutionRemoved(object sender, EventArgs<IRegressionSolution> e) {
272      var solution = e.Value;
273      foreach (var chart in gradientCharts.Values) {
274        if (sender == chart) continue;
275        await chart.RemoveSolutionAsync(solution);
276      }
277    }
278
279    private async void variableListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
280      var item = e.Item;
281      var variable = item.Text;
282      var gradientChart = gradientCharts[variable];
283      var chartsPanel = groupingPanels[variable];
284      var tl = gradientChartTableLayout;
285
286      tl.SuspendLayout();
287      if (item.Checked) {
288        tl.Controls.Add(chartsPanel);
289        await gradientChart.RecalculateAsync();
290      } else {
291        tl.Controls.Remove(chartsPanel);
292      }
293
294      if (tl.Controls.Count > 0) {
295        SetupYAxis();
296        ReOrderControls();
297        SetStyles();
298      }
299      tl.ResumeLayout();
300      tl.Refresh();
301      densityComboBox_SelectedIndexChanged(this, EventArgs.Empty);
302    }
303
304    private void automaticYAxisCheckBox_CheckedChanged(object sender, EventArgs e) {
305      limitView.ReadOnly = automaticYAxisCheckBox.Checked;
306      SetupYAxis();
307      gradientChartTableLayout.Refresh();
308      densityComboBox_SelectedIndexChanged(this, EventArgs.Empty); // necessary to realign the density plots
309    }
310
311    private void limit_ValueChanged(object sender, EventArgs e) {
312      if (automaticYAxisCheckBox.Checked)
313        return;
314      SetupYAxis();
315      gradientChartTableLayout.Refresh();
316      densityComboBox_SelectedIndexChanged(this, EventArgs.Empty); // necessary to realign the density plots
317    }
318
319    private void densityComboBox_SelectedIndexChanged(object sender, EventArgs e) {
320      if (Content == null)
321        return;
322
323      int si = densityComboBox.SelectedIndex;
324      if (si == 0) {
325        foreach (var densityChart in densityCharts.Values)
326          densityChart.Visible = false;
327      } else {
328        var indices = GetDensityIndices(si).ToList();
329
330        foreach (var entry in densityCharts) {
331          var variableName = entry.Key;
332          var densityChart = entry.Value;
333          if (!VisibleVariables.Contains(variableName))
334            continue;
335          UpdateDensityChart(densityChart, variableName, indices);
336
337        }
338      }
339    }
340    private IEnumerable<int> GetDensityIndices(int selectedIndex) {
341      var problemData = Content.ProblemData;
342      return
343        selectedIndex == 1 ? problemData.TrainingIndices :
344        selectedIndex == 2 ? problemData.TestIndices :
345        problemData.AllIndices;
346    }
347    private void UpdateDensityChart(DensityChart densityChart, string variable, IList<int> indices = null) {
348      if (densityComboBox.SelectedIndex == 0)
349        return;
350      if (indices == null) {
351        indices = GetDensityIndices(densityComboBox.SelectedIndex).ToList();
352      }
353      var data = Content.ProblemData.Dataset.GetDoubleValues(variable, indices).ToList();
354      var gradientChart = gradientCharts[variable];
355      var min = gradientChart.FixedXAxisMin;
356      var max = gradientChart.FixedXAxisMax;
357      var buckets = gradientChart.DrawingSteps;
358      if (min.HasValue && max.HasValue) {
359        densityChart.UpdateChart(data, min.Value, max.Value, buckets);
360        densityChart.Width = gradientChart.Width;
361
362        var gcPlotPosition = gradientChart.InnerPlotPosition;
363        densityChart.Left = (int)(gcPlotPosition.X / 100.0 * gradientChart.Width);
364        densityChart.Width = (int)(gcPlotPosition.Width / 100.0 * gradientChart.Width);
365
366        densityChart.Visible = true;
367      }
368    }
369
370    private void columnsTextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
371      int columns;
372      if (!int.TryParse(columnsTextBox.Text, out columns)) {
373        e.Cancel = true;
374        columnsTextBox.Select();
375        var textBox = (TextBox)sender;
376        errorProvider.SetError(columnsTextBox, "Columns number must be a positive integer.");
377        errorProvider.SetIconPadding(textBox, -20);
378      }
379    }
380
381    private void columnsTextBox_Validated(object sender, EventArgs e) {
382      errorProvider.SetError(columnsTextBox, "");
383      MaxColumns = int.Parse(columnsTextBox.Text);
384      int columns = Math.Min(VisibleVariables.Count(), MaxColumns);
385      if (columns > 0) {
386        var tl = gradientChartTableLayout;
387        MaxColumns = columns;
388        tl.SuspendLayout();
389        ReOrderControls();
390        SetStyles();
391        tl.ResumeLayout();
392        tl.Refresh();
393        densityComboBox_SelectedIndexChanged(this, EventArgs.Empty);
394      }
395    }
396
397    private async void solution_ModelChanged(object sender, EventArgs e) {
398      foreach (var variable in VisibleVariables) {
399        var gradientChart = gradientCharts[variable];
400        var densityChart = densityCharts[variable];
401        // recalculate and refresh
402        await gradientChart.RecalculateAsync();
403        gradientChart.Refresh();
404        UpdateDensityChart(densityChart, variable);
405      }
406    }
407  }
408}
Note: See TracBrowser for help on using the repository browser.