source: branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionTargetResponseGradientView.cs @ 14248

Last change on this file since 14248 was 14248, checked in by gkronber, 5 years ago

#2650: added support for factor variables to target variation view together with Philipp

File size: 21.4 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;
24using System.Collections.Generic;
25using System.Drawing;
26using System.Linq;
27using System.Threading.Tasks;
28using System.Windows.Forms;
29using HeuristicLab.Common;
30using HeuristicLab.MainForm;
31using HeuristicLab.Visualization.ChartControlsExtensions;
32
33namespace HeuristicLab.Problems.DataAnalysis.Views {
34  [View("Target Response Gradients")]
35  [Content(typeof(IRegressionSolution))]
36  public partial class RegressionSolutionTargetResponseGradientView : DataAnalysisSolutionEvaluationView {
37    private readonly Dictionary<string, IGradientChart> gradientCharts;
38    private readonly Dictionary<string, DensityChart> densityCharts;
39    private readonly Dictionary<string, Panel> groupingPanels;
40
41    private const int Points = 200;
42    private int MaxColumns = 4;
43
44    private IEnumerable<string> VisibleVariables {
45      get {
46        foreach (ListViewItem item in variableListView.CheckedItems)
47          yield return item.Text;
48      }
49    }
50    private IEnumerable<IGradientChart> VisibleGradientCharts {
51      get { return VisibleVariables.Select(v => gradientCharts[v]); }
52    }
53    private IEnumerable<DensityChart> VisibleDensityCharts {
54      get { return VisibleVariables.Select(v => densityCharts[v]); }
55    }
56    private IEnumerable<Panel> VisibleChartsPanels {
57      get { return VisibleVariables.Select(v => groupingPanels[v]); }
58    }
59
60    public RegressionSolutionTargetResponseGradientView() {
61      InitializeComponent();
62      gradientCharts = new Dictionary<string, IGradientChart>();
63      densityCharts = new Dictionary<string, DensityChart>();
64      groupingPanels = new Dictionary<string, Panel>();
65
66      limitView.Content = new DoubleLimit(0, 1);
67      limitView.Content.ValueChanged += limit_ValueChanged;
68
69      densityComboBox.SelectedIndex = 1; // select Training
70
71      // Avoid additional horizontal scrollbar
72      var vertScrollWidth = SystemInformation.VerticalScrollBarWidth;
73      scrollPanel.Padding = new Padding(0, 0, vertScrollWidth, 0);
74      scrollPanel.AutoScroll = true;
75    }
76
77    public new IRegressionSolution Content {
78      get { return (IRegressionSolution)base.Content; }
79      set { base.Content = value; }
80    }
81
82    protected override void RegisterContentEvents() {
83      base.RegisterContentEvents();
84      Content.ModelChanged += solution_ModelChanged;
85    }
86
87    protected override void DeregisterContentEvents() {
88      Content.ModelChanged -= solution_ModelChanged;
89      base.DeregisterContentEvents();
90    }
91
92    protected override void OnContentChanged() {
93      base.OnContentChanged();
94      if (Content == null) return;
95      var problemData = Content.ProblemData;
96
97      // Init Y-axis range
98      double min = double.MaxValue, max = double.MinValue;
99      var trainingTarget = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);
100      foreach (var t in trainingTarget) {
101        if (t < min) min = t;
102        if (t > max) max = t;
103      }
104      double range = max - min;
105      const double scale = 1.0 / 3.0;
106      double axisMin, axisMax, axisInterval;
107      ChartUtil.CalculateAxisInterval(min - scale * range, max + scale * range, 5, out axisMin, out axisMax, out axisInterval);
108      automaticYAxisCheckBox.Checked = false;
109      limitView.ReadOnly = false;
110      limitView.Content.Lower = axisMin;
111      limitView.Content.Upper = axisMax;
112
113      // create dataset
114      var allowedInputVariables = Content.ProblemData.AllowedInputVariables;
115      var doubleVariables = allowedInputVariables.Where(problemData.Dataset.VariableHasType<double>);
116      var doubleVariableValues = (IEnumerable<IList>)doubleVariables.Select(x => new List<double> { problemData.Dataset.GetDoubleValues(x, problemData.TrainingIndices).Median() });
117
118      var factorVariables = allowedInputVariables.Where(problemData.Dataset.VariableHasType<string>);
119      var factorVariableValues = (IEnumerable<IList>)factorVariables.Select(x => new List<string> {
120        problemData.Dataset.GetStringValues(x, problemData.TrainingIndices)
121        .GroupBy(val => val).OrderByDescending(g => g.Count()).First().Key // most frequent value
122      });
123
124      var sharedFixedVariables = new ModifiableDataset(doubleVariables.Concat(factorVariables), doubleVariableValues.Concat(factorVariableValues));
125
126
127      // create controls
128      gradientCharts.Clear();
129      densityCharts.Clear();
130      groupingPanels.Clear();
131      foreach (var variableName in doubleVariables) {
132        var gradientChart = CreateGradientChart(variableName, sharedFixedVariables);
133        gradientCharts.Add(variableName, gradientChart);
134
135        var densityChart = new DensityChart() {
136          Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right,
137          Margin = Padding.Empty,
138          Height = 12,
139          Visible = false,
140          Top = (int)(gradientChart.Height * 0.1),
141        };
142        densityCharts.Add(variableName, densityChart);
143
144        gradientChart.ZoomChanged += (o, e) => {
145          var gradient = (GradientChart)o;
146          var density = densityCharts[gradient.FreeVariable];
147          density.Visible = densityComboBox.SelectedIndex != 0 && !gradient.IsZoomed;
148          if (density.Visible)
149            UpdateDensityChart(density, gradient.FreeVariable);
150        };
151        gradientChart.SizeChanged += (o, e) => {
152          var gradient = (GradientChart)o;
153          var density = densityCharts[gradient.FreeVariable];
154          density.Top = (int)(gradient.Height * 0.1);
155        };
156
157        // Initially, the inner plot areas are not initialized for hidden charts (scollpanel, ...)
158        // This event handler listens for the paint event once (where everything is already initialized) to do some manual layouting.
159        gradientChart.ChartPostPaint += OnGradientChartPostPaint;
160
161        var panel = new Panel() {
162          Dock = DockStyle.Fill,
163          Margin = Padding.Empty,
164          BackColor = Color.White
165        };
166
167        panel.Controls.Add(densityChart);
168        panel.Controls.Add(gradientChart);
169        groupingPanels.Add(variableName, panel);
170      }
171      foreach (var variableName in factorVariables) {
172        var gradientChart = CreateFactorGradientChart(variableName, sharedFixedVariables);
173        gradientCharts.Add(variableName, gradientChart);
174
175        var densityChart = new DensityChart() {
176          Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right,
177          Margin = Padding.Empty,
178          Height = 12,
179          Visible = false,
180          Top = (int)(gradientChart.Height * 0.1),
181        };
182        densityCharts.Add(variableName, densityChart);
183        gradientChart.ZoomChanged += (o, e) => {
184          var gradient = (FactorGradientChart)o;
185          var density = densityCharts[gradient.FreeVariable];
186          density.Visible = densityComboBox.SelectedIndex != 0 && !gradient.IsZoomed;
187          if (density.Visible)
188            UpdateDensityChart(density, gradient.FreeVariable);
189        };
190        gradientChart.SizeChanged += (o, e) => {
191          var gradient = (FactorGradientChart)o;
192          var density = densityCharts[gradient.FreeVariable];
193          density.Top = (int)(gradient.Height * 0.1);
194        };
195
196        // Initially, the inner plot areas are not initialized for hidden charts (scollpanel, ...)
197        // This event handler listens for the paint event once (where everything is already initialized) to do some manual layouting.
198        gradientChart.ChartPostPaint += OnFactorGradientChartPostPaint;
199
200        var panel = new Panel() {
201          Dock = DockStyle.Fill,
202          Margin = Padding.Empty,
203          BackColor = Color.White
204        };
205
206        panel.Controls.Add(densityChart);
207        panel.Controls.Add(gradientChart);
208        groupingPanels.Add(variableName, panel);
209      }
210      // update variable list
211      variableListView.ItemChecked -= variableListView_ItemChecked;
212      variableListView.Items.Clear();
213      foreach (var variable in allowedInputVariables)
214        variableListView.Items.Add(key: variable, text: variable, imageIndex: 0);
215
216      foreach (var variable in Content.Model.VariablesUsedForPrediction)
217        variableListView.Items[variable].Checked = true;
218      variableListView.ItemChecked += variableListView_ItemChecked;
219
220      RecalculateAndRelayoutCharts();
221    }
222
223    private void OnGradientChartPostPaint(object o, EventArgs e) {
224      var gradient = (GradientChart)o;
225      var density = densityCharts[gradient.FreeVariable];
226
227      density.Width = gradient.Width;
228
229      var gcPlotPosition = gradient.InnerPlotPosition;
230      density.Left = (int)(gcPlotPosition.X / 100.0 * gradient.Width);
231      density.Width = (int)(gcPlotPosition.Width / 100.0 * gradient.Width);
232      gradient.UpdateTitlePosition();
233
234      // removed after succesful layouting due to performance reasons
235      if (gcPlotPosition.Width != 0)
236        gradient.ChartPostPaint -= OnGradientChartPostPaint;
237    }
238
239    private void OnFactorGradientChartPostPaint(object o, EventArgs e) {
240      var gradient = (FactorGradientChart)o;
241      var density = densityCharts[gradient.FreeVariable];
242
243      density.Width = gradient.Width;
244
245      var gcPlotPosition = gradient.InnerPlotPosition;
246      density.Left = (int)(gcPlotPosition.X / 100.0 * gradient.Width);
247      density.Width = (int)(gcPlotPosition.Width / 100.0 * gradient.Width);
248      gradient.UpdateTitlePosition();
249
250      // removed after succesful layouting due to performance reasons
251      if (gcPlotPosition.Width != 0)
252        gradient.ChartPostPaint -= OnFactorGradientChartPostPaint;
253    }
254
255    private async void RecalculateAndRelayoutCharts() {
256      foreach (var variable in VisibleVariables) {
257        var gradientChart = gradientCharts[variable];
258        await gradientChart.RecalculateAsync(false, false);
259      }
260      gradientChartTableLayout.SuspendLayout();
261      SetupYAxis();
262      ReOrderControls();
263      SetStyles();
264      gradientChartTableLayout.ResumeLayout();
265      gradientChartTableLayout.Refresh();
266      foreach (var variable in VisibleVariables) {
267        DensityChart densityChart;
268        if (densityCharts.TryGetValue(variable, out densityChart)) {
269          UpdateDensityChart(densityChart, variable);
270        }
271      }
272    }
273    private GradientChart CreateGradientChart(string variableName, ModifiableDataset sharedFixedVariables) {
274      var gradientChart = new GradientChart {
275        Dock = DockStyle.Fill,
276        Margin = Padding.Empty,
277        ShowLegend = false,
278        ShowCursor = true,
279        ShowConfigButton = false,
280        YAxisTicks = 5,
281      };
282      gradientChart.VariableValueChanged += async (o, e) => {
283        var recalculations = VisibleGradientCharts
284          .Except(new[] { (IGradientChart)o })
285          .Select(async chart => {
286            await chart.RecalculateAsync(updateOnFinish: false, resetYAxis: false);
287          }).ToList();
288        await Task.WhenAll(recalculations);
289
290        if (recalculations.All(t => t.IsCompleted))
291          SetupYAxis();
292      };
293      gradientChart.Configure(new[] { Content }, sharedFixedVariables, variableName, Points);
294      gradientChart.SolutionAdded += gradientChart_SolutionAdded;
295      gradientChart.SolutionRemoved += gradientChart_SolutionRemoved;
296      return gradientChart;
297    }
298    private FactorGradientChart CreateFactorGradientChart(string variableName, ModifiableDataset sharedFixedVariables) {
299      var gradientChart = new FactorGradientChart {
300        Dock = DockStyle.Fill,
301        Margin = Padding.Empty,
302        ShowLegend = false,
303        ShowCursor = true,
304        YAxisTicks = 5,
305      };
306      gradientChart.VariableValueChanged += async (o, e) => {
307        var recalculations = VisibleGradientCharts
308          .Except(new[] { (FactorGradientChart)o })
309          .Select(async chart => {
310            await chart.RecalculateAsync(updateOnFinish: false, resetYAxis: false);
311          }).ToList();
312        await Task.WhenAll(recalculations);
313
314        if (recalculations.All(t => t.IsCompleted))
315          SetupYAxis();
316      };
317      var variableValues = Content.ProblemData.Dataset.GetStringValues(variableName).Distinct().OrderBy(n => n).ToList();
318      gradientChart.Configure(new[] { Content }, sharedFixedVariables, variableName, variableValues);
319      gradientChart.SolutionAdded += gradientChart_SolutionAdded;
320      gradientChart.SolutionRemoved += gradientChart_SolutionRemoved;
321      return gradientChart;
322    }
323    private void SetupYAxis() {
324      double axisMin, axisMax;
325      if (automaticYAxisCheckBox.Checked) {
326        double min = double.MaxValue, max = double.MinValue;
327        foreach (var chart in VisibleGradientCharts) {
328          if (chart.YMin < min) min = chart.YMin;
329          if (chart.YMax > max) max = chart.YMax;
330        }
331
332        double axisInterval;
333        ChartUtil.CalculateAxisInterval(min, max, 5, out axisMin, out axisMax, out axisInterval);
334      } else {
335        axisMin = limitView.Content.Lower;
336        axisMax = limitView.Content.Upper;
337      }
338
339      foreach (var chart in VisibleGradientCharts) {
340        chart.FixedYAxisMin = axisMin;
341        chart.FixedYAxisMax = axisMax;
342      }
343    }
344
345    // reorder chart controls so that they always appear in the same order as in the list view
346    // the table layout containing the controls should be suspended before calling this method
347    private void ReOrderControls() {
348      var tl = gradientChartTableLayout;
349      tl.Controls.Clear();
350      int row = 0, column = 0;
351      foreach (var v in VisibleVariables) {
352        var chartsPanel = groupingPanels[v];
353        tl.Controls.Add(chartsPanel, column, row);
354
355        var chart = gradientCharts[v];
356        chart.YAxisTitle = column == 0 ? Content.ProblemData.TargetVariable : string.Empty;
357        column++;
358
359        if (column == MaxColumns) {
360          row++;
361          column = 0;
362        }
363      }
364    }
365
366    private void SetStyles() {
367      var tl = gradientChartTableLayout;
368      tl.RowStyles.Clear();
369      tl.ColumnStyles.Clear();
370      int numVariables = VisibleVariables.Count();
371      if (numVariables == 0)
372        return;
373
374      // set column styles
375      tl.ColumnCount = Math.Min(numVariables, MaxColumns);
376      for (int c = 0; c < tl.ColumnCount; c++)
377        tl.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100.0f / tl.ColumnCount));
378
379      // set row styles
380      tl.RowCount = (int)Math.Ceiling((double)numVariables / tl.ColumnCount);
381      var columnWidth = tl.Width / tl.ColumnCount; // assume all columns have the same width
382      var rowHeight = (int)(0.8 * columnWidth);
383      for (int r = 0; r < tl.RowCount; r++)
384        tl.RowStyles.Add(new RowStyle(SizeType.Absolute, rowHeight));
385    }
386
387    private async void gradientChart_SolutionAdded(object sender, EventArgs<IRegressionSolution> e) {
388      var solution = e.Value;
389      foreach (var chart in gradientCharts.Values) {
390        if (sender == chart) continue;
391        await chart.AddSolutionAsync(solution);
392      }
393    }
394
395    private async void gradientChart_SolutionRemoved(object sender, EventArgs<IRegressionSolution> e) {
396      var solution = e.Value;
397      foreach (var chart in gradientCharts.Values) {
398        if (sender == chart) continue;
399        await chart.RemoveSolutionAsync(solution);
400      }
401    }
402
403    private async void variableListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
404      var item = e.Item;
405      var variable = item.Text;
406      var gradientChart = gradientCharts[variable];
407      var chartsPanel = groupingPanels[variable];
408      var tl = gradientChartTableLayout;
409
410      tl.SuspendLayout();
411      if (item.Checked) {
412        tl.Controls.Add(chartsPanel);
413        await gradientChart.RecalculateAsync(false, false);
414      } else {
415        tl.Controls.Remove(chartsPanel);
416      }
417
418      if (tl.Controls.Count > 0) {
419        SetupYAxis();
420        ReOrderControls();
421        SetStyles();
422      }
423      tl.ResumeLayout();
424      tl.Refresh();
425      densityComboBox_SelectedIndexChanged(this, EventArgs.Empty);
426    }
427
428    private void automaticYAxisCheckBox_CheckedChanged(object sender, EventArgs e) {
429      limitView.ReadOnly = automaticYAxisCheckBox.Checked;
430      SetupYAxis();
431      gradientChartTableLayout.Refresh();
432      densityComboBox_SelectedIndexChanged(this, EventArgs.Empty); // necessary to realign the density plots
433    }
434
435    private void limit_ValueChanged(object sender, EventArgs e) {
436      if (automaticYAxisCheckBox.Checked)
437        return;
438      SetupYAxis();
439      gradientChartTableLayout.Refresh();
440      densityComboBox_SelectedIndexChanged(this, EventArgs.Empty); // necessary to realign the density plots
441    }
442
443    private void densityComboBox_SelectedIndexChanged(object sender, EventArgs e) {
444      if (Content == null)
445        return;
446
447      int si = densityComboBox.SelectedIndex;
448      if (si == 0) {
449        foreach (var densityChart in densityCharts.Values)
450          densityChart.Visible = false;
451      } else {
452        var indices = GetDensityIndices(si).ToList();
453
454        foreach (var entry in densityCharts) {
455          var variableName = entry.Key;
456          var densityChart = entry.Value;
457          if (!VisibleVariables.Contains(variableName) || gradientCharts[variableName].IsZoomed)
458            continue;
459
460          UpdateDensityChart(densityChart, variableName, indices);
461        }
462      }
463    }
464    private IEnumerable<int> GetDensityIndices(int selectedIndex) {
465      var problemData = Content.ProblemData;
466      return
467        selectedIndex == 1 ? problemData.TrainingIndices :
468        selectedIndex == 2 ? problemData.TestIndices :
469        problemData.AllIndices;
470    }
471    private void UpdateDensityChart(DensityChart densityChart, string variable, IList<int> indices = null) {
472      if (densityComboBox.SelectedIndex == 0)
473        return;
474      if (indices == null) {
475        indices = GetDensityIndices(densityComboBox.SelectedIndex).ToList();
476      }
477      if (Content.ProblemData.Dataset.VariableHasType<double>(variable)) {
478        var data = Content.ProblemData.Dataset.GetDoubleValues(variable, indices).ToList();
479        var gradientChart = gradientCharts[variable] as GradientChart;
480        if (gradientChart != null) {
481          var min = gradientChart.FixedXAxisMin;
482          var max = gradientChart.FixedXAxisMax;
483          var buckets = gradientChart.DrawingSteps;
484          if (min.HasValue && max.HasValue) {
485            densityChart.UpdateChart(data, min.Value, max.Value, buckets);
486            densityChart.Width = gradientChart.Width;
487
488            var gcPlotPosition = gradientChart.InnerPlotPosition;
489            densityChart.Left = (int)(gcPlotPosition.X / 100.0 * gradientChart.Width);
490            densityChart.Width = (int)(gcPlotPosition.Width / 100.0 * gradientChart.Width);
491
492            densityChart.Visible = true;
493          }
494          gradientChart.UpdateTitlePosition();
495        }
496      } else if (Content.ProblemData.Dataset.VariableHasType<string>(variable)) {
497        var data = Content.ProblemData.Dataset.GetStringValues(variable).ToList();
498        var gradientChart = gradientCharts[variable] as FactorGradientChart;
499        if (gradientChart != null) {
500          densityChart.UpdateChart(data);
501          densityChart.Width = gradientChart.Width;
502
503          var gcPlotPosition = gradientChart.InnerPlotPosition;
504          densityChart.Left = (int)(gcPlotPosition.X / 100.0 * gradientChart.Width);
505          densityChart.Width = (int)(gcPlotPosition.Width / 100.0 * gradientChart.Width);
506
507          densityChart.Visible = true;
508
509          gradientChart.UpdateTitlePosition();
510        }
511      }
512    }
513
514    private void columnsNumericUpDown_ValueChanged(object sender, EventArgs e) {
515      MaxColumns = (int)columnsNumericUpDown.Value;
516      int columns = Math.Min(VisibleVariables.Count(), MaxColumns);
517      if (columns > 0) {
518        var tl = gradientChartTableLayout;
519        MaxColumns = columns;
520        tl.SuspendLayout();
521        ReOrderControls();
522        SetStyles();
523        tl.ResumeLayout();
524        tl.Refresh();
525        densityComboBox_SelectedIndexChanged(this, EventArgs.Empty);
526      }
527    }
528
529    private async void solution_ModelChanged(object sender, EventArgs e) {
530      foreach (var variable in VisibleVariables) {
531        var gradientChart = gradientCharts[variable];
532        var densityChart = densityCharts[variable];
533        // recalculate and refresh
534        await gradientChart.RecalculateAsync(false, false);
535        gradientChart.Refresh();
536        UpdateDensityChart(densityChart, variable);
537      }
538    }
539  }
540}
Note: See TracBrowser for help on using the repository browser.