Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2701_MemPRAlgorithm/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionTargetResponseGradientView.cs @ 16752

Last change on this file since 16752 was 14562, checked in by abeham, 8 years ago

#2701: Updated branch to trunk

File size: 17.7 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.Globalization;
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, GradientChart> gradientCharts;
38    private readonly Dictionary<string, DensityChart> densityCharts;
39    private readonly Dictionary<string, Panel> groupingPanels;
40    private ModifiableDataset sharedFixedVariables;
41
42    private const int Points = 200;
43    private int MaxColumns = 4;
44
45    private IEnumerable<string> VisibleVariables {
46      get {
47        foreach (ListViewItem item in variableListView.CheckedItems)
48          yield return item.Text;
49      }
50    }
51    private IEnumerable<GradientChart> VisibleGradientCharts {
52      get { return VisibleVariables.Select(v => gradientCharts[v]); }
53    }
54    private IEnumerable<DensityChart> VisibleDensityCharts {
55      get { return VisibleVariables.Select(v => densityCharts[v]); }
56    }
57    private IEnumerable<Panel> VisibleChartsPanels {
58      get { return VisibleVariables.Select(v => groupingPanels[v]); }
59    }
60
61    public RegressionSolutionTargetResponseGradientView() {
62      InitializeComponent();
63      gradientCharts = new Dictionary<string, GradientChart>();
64      densityCharts = new Dictionary<string, DensityChart>();
65      groupingPanels = new Dictionary<string, Panel>();
66
67      limitView.Content = new DoubleLimit(0, 1);
68      limitView.Content.ValueChanged += limit_ValueChanged;
69
70      densityComboBox.SelectedIndex = 1; // select Training
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 of problemData input variables and model input variables
115      // necessary workaround to have the variables in the occuring order
116      var inputvariables =
117        new HashSet<string>(Content.ProblemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
118      var allowedInputVariables =
119        Content.ProblemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).ToList();
120
121
122      var variableValues = allowedInputVariables.Select(x => new List<double> { problemData.Dataset.GetDoubleValues(x, problemData.TrainingIndices).Median() });
123      if (sharedFixedVariables != null)
124        sharedFixedVariables.ItemChanged -= SharedFixedVariables_ItemChanged;
125      sharedFixedVariables = new ModifiableDataset(allowedInputVariables, variableValues);
126      // ItemChanged eventhandler is registered later, after creating the gradient charts
127
128      // create controls
129      gradientCharts.Clear();
130      densityCharts.Clear();
131      groupingPanels.Clear();
132      foreach (var variableName in allowedInputVariables) {
133        var gradientChart = CreateGradientChart(variableName, sharedFixedVariables);
134        gradientCharts.Add(variableName, gradientChart);
135
136        var densityChart = new DensityChart() {
137          Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right,
138          Margin = Padding.Empty,
139          Height = 12,
140          Visible = false,
141          Top = (int)(gradientChart.Height * 0.1),
142        };
143        densityCharts.Add(variableName, densityChart);
144
145        gradientChart.ZoomChanged += (o, e) => {
146          var gradient = (GradientChart)o;
147          var density = densityCharts[gradient.FreeVariable];
148          density.Visible = densityComboBox.SelectedIndex != 0 && !gradient.IsZoomed;
149          if (density.Visible)
150            UpdateDensityChart(density, gradient.FreeVariable);
151        };
152        gradientChart.SizeChanged += (o, e) => {
153          var gradient = (GradientChart)o;
154          var density = densityCharts[gradient.FreeVariable];
155          density.Top = (int)(gradient.Height * 0.1);
156        };
157
158        // Initially, the inner plot areas are not initialized for hidden charts (scollpanel, ...)
159        // This event handler listens for the paint event once (where everything is already initialized) to do some manual layouting.
160        gradientChart.ChartPostPaint += OnGradientChartOnChartPostPaint;
161
162        var panel = new Panel() {
163          Dock = DockStyle.Fill,
164          Margin = Padding.Empty,
165          BackColor = Color.White
166        };
167
168        panel.Controls.Add(densityChart);
169        panel.Controls.Add(gradientChart);
170        groupingPanels.Add(variableName, panel);
171      }
172
173      // update variable list
174      variableListView.ItemChecked -= variableListView_ItemChecked;
175      variableListView.Items.Clear();
176      foreach (var variable in allowedInputVariables)
177        variableListView.Items.Add(key: variable, text: variable, imageIndex: 0);
178
179      foreach (var variable in Content.Model.VariablesUsedForPrediction)
180        variableListView.Items[variable].Checked = true;
181      variableListView.ItemChecked += variableListView_ItemChecked;
182
183      sharedFixedVariables.ItemChanged += SharedFixedVariables_ItemChanged;
184
185      RecalculateAndRelayoutCharts();
186    }
187
188    private void SharedFixedVariables_ItemChanged(object sender, EventArgs<int, int> e) {
189      double yValue = Content.Model.GetEstimatedValues(sharedFixedVariables, new[] { 0 }).Single();
190      string title = Content.ProblemData.TargetVariable + ": " + yValue.ToString("G5", CultureInfo.CurrentCulture);
191      foreach (var chart in gradientCharts.Values) {
192        if (!string.IsNullOrEmpty(chart.YAxisTitle)) { // only show title for first column in grid
193          chart.YAxisTitle = title;
194        }
195      }
196    }
197
198    private void OnGradientChartOnChartPostPaint(object o, EventArgs e) {
199      var gradient = (GradientChart)o;
200      var density = densityCharts[gradient.FreeVariable];
201
202      density.Width = gradient.Width;
203
204      var gcPlotPosition = gradient.InnerPlotPosition;
205      density.Left = (int)(gcPlotPosition.X / 100.0 * gradient.Width);
206      density.Width = (int)(gcPlotPosition.Width / 100.0 * gradient.Width);
207      gradient.UpdateTitlePosition();
208
209      // removed after succesful layouting due to performance reasons
210      if (gcPlotPosition.Width != 0)
211        gradient.ChartPostPaint -= OnGradientChartOnChartPostPaint;
212    }
213
214    private async void RecalculateAndRelayoutCharts() {
215      foreach (var variable in VisibleVariables) {
216        var gradientChart = gradientCharts[variable];
217        await gradientChart.RecalculateAsync();
218      }
219      gradientChartTableLayout.SuspendLayout();
220      SetupYAxis();
221      ReOrderControls();
222      SetStyles();
223      gradientChartTableLayout.ResumeLayout();
224      gradientChartTableLayout.Refresh();
225      foreach (var variable in VisibleVariables) {
226        var densityChart = densityCharts[variable];
227        UpdateDensityChart(densityChart, variable);
228      }
229    }
230    private GradientChart CreateGradientChart(string variableName, ModifiableDataset sharedFixedVariables) {
231      var gradientChart = new GradientChart {
232        Dock = DockStyle.Fill,
233        Margin = Padding.Empty,
234        ShowLegend = false,
235        ShowCursor = true,
236        ShowConfigButton = false,
237        YAxisTicks = 5,
238      };
239      gradientChart.VariableValueChanged += async (o, e) => {
240        var recalculations = VisibleGradientCharts.Except(new[] { (GradientChart)o }).Select(async chart => {
241          await chart.RecalculateAsync(updateOnFinish: false, resetYAxis: false);
242        }).ToList();
243        await Task.WhenAll(recalculations);
244
245        if (recalculations.All(t => t.IsCompleted))
246          SetupYAxis();
247      };
248      gradientChart.Configure(new[] { Content }, sharedFixedVariables, variableName, Points);
249      gradientChart.SolutionAdded += gradientChart_SolutionAdded;
250      gradientChart.SolutionRemoved += gradientChart_SolutionRemoved;
251      return gradientChart;
252    }
253
254    private void SetupYAxis() {
255      double axisMin, axisMax;
256      if (automaticYAxisCheckBox.Checked) {
257        double min = double.MaxValue, max = double.MinValue;
258        foreach (var chart in VisibleGradientCharts) {
259          if (chart.YMin < min) min = chart.YMin;
260          if (chart.YMax > max) max = chart.YMax;
261        }
262
263        double axisInterval;
264        ChartUtil.CalculateAxisInterval(min, max, 5, out axisMin, out axisMax, out axisInterval);
265      } else {
266        axisMin = limitView.Content.Lower;
267        axisMax = limitView.Content.Upper;
268      }
269
270      foreach (var chart in VisibleGradientCharts) {
271        chart.FixedYAxisMin = axisMin;
272        chart.FixedYAxisMax = axisMax;
273      }
274    }
275
276    // reorder chart controls so that they always appear in the same order as in the list view
277    // the table layout containing the controls should be suspended before calling this method
278    private void ReOrderControls() {
279      var tl = gradientChartTableLayout;
280      tl.Controls.Clear();
281      int row = 0, column = 0;
282      double yValue = Content.Model.GetEstimatedValues(sharedFixedVariables, new[] { 0 }).Single();
283      string title = Content.ProblemData.TargetVariable + ": " + yValue.ToString("G5", CultureInfo.CurrentCulture);
284
285      foreach (var v in VisibleVariables) {
286        var chartsPanel = groupingPanels[v];
287        tl.Controls.Add(chartsPanel, column, row);
288
289        var chart = gradientCharts[v];
290        chart.YAxisTitle = column == 0 ? title : string.Empty;
291        column++;
292
293        if (column == MaxColumns) {
294          row++;
295          column = 0;
296        }
297      }
298    }
299
300    private void SetStyles() {
301      var tl = gradientChartTableLayout;
302      tl.RowStyles.Clear();
303      tl.ColumnStyles.Clear();
304      int numVariables = VisibleVariables.Count();
305      if (numVariables == 0)
306        return;
307
308      // set column styles
309      tl.ColumnCount = Math.Min(numVariables, MaxColumns);
310      for (int c = 0; c < tl.ColumnCount; c++)
311        tl.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100.0f / tl.ColumnCount));
312
313      // set row styles
314      tl.RowCount = (int)Math.Ceiling((double)numVariables / tl.ColumnCount);
315      var columnWidth = tl.Width / tl.ColumnCount; // assume all columns have the same width
316      var rowHeight = (int)(0.8 * columnWidth);
317      for (int r = 0; r < tl.RowCount; r++)
318        tl.RowStyles.Add(new RowStyle(SizeType.Absolute, rowHeight));
319    }
320
321    private async void gradientChart_SolutionAdded(object sender, EventArgs<IRegressionSolution> e) {
322      var solution = e.Value;
323      foreach (var chart in gradientCharts.Values) {
324        if (sender == chart) continue;
325        await chart.AddSolutionAsync(solution);
326      }
327    }
328
329    private async void gradientChart_SolutionRemoved(object sender, EventArgs<IRegressionSolution> e) {
330      var solution = e.Value;
331      foreach (var chart in gradientCharts.Values) {
332        if (sender == chart) continue;
333        await chart.RemoveSolutionAsync(solution);
334      }
335    }
336
337    private async void variableListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
338      var item = e.Item;
339      var variable = item.Text;
340      var gradientChart = gradientCharts[variable];
341      var chartsPanel = groupingPanels[variable];
342      var tl = gradientChartTableLayout;
343
344      tl.SuspendLayout();
345      if (item.Checked) {
346        tl.Controls.Add(chartsPanel);
347        await gradientChart.RecalculateAsync();
348      } else {
349        tl.Controls.Remove(chartsPanel);
350      }
351
352      if (tl.Controls.Count > 0) {
353        SetupYAxis();
354        ReOrderControls();
355        SetStyles();
356      }
357      tl.ResumeLayout();
358      tl.Refresh();
359      densityComboBox_SelectedIndexChanged(this, EventArgs.Empty);
360    }
361
362    private void automaticYAxisCheckBox_CheckedChanged(object sender, EventArgs e) {
363      limitView.ReadOnly = automaticYAxisCheckBox.Checked;
364      SetupYAxis();
365      gradientChartTableLayout.Refresh();
366      densityComboBox_SelectedIndexChanged(this, EventArgs.Empty); // necessary to realign the density plots
367    }
368
369    private void limit_ValueChanged(object sender, EventArgs e) {
370      if (automaticYAxisCheckBox.Checked)
371        return;
372      SetupYAxis();
373      gradientChartTableLayout.Refresh();
374      densityComboBox_SelectedIndexChanged(this, EventArgs.Empty); // necessary to realign the density plots
375    }
376
377    private void densityComboBox_SelectedIndexChanged(object sender, EventArgs e) {
378      if (Content == null)
379        return;
380
381      int si = densityComboBox.SelectedIndex;
382      if (si == 0) {
383        foreach (var densityChart in densityCharts.Values)
384          densityChart.Visible = false;
385      } else {
386        var indices = GetDensityIndices(si).ToList();
387
388        foreach (var entry in densityCharts) {
389          var variableName = entry.Key;
390          var densityChart = entry.Value;
391          if (!VisibleVariables.Contains(variableName) || gradientCharts[variableName].IsZoomed)
392            continue;
393
394          UpdateDensityChart(densityChart, variableName, indices);
395        }
396      }
397    }
398    private IEnumerable<int> GetDensityIndices(int selectedIndex) {
399      var problemData = Content.ProblemData;
400      return
401        selectedIndex == 1 ? problemData.TrainingIndices :
402        selectedIndex == 2 ? problemData.TestIndices :
403        problemData.AllIndices;
404    }
405    private void UpdateDensityChart(DensityChart densityChart, string variable, IList<int> indices = null) {
406      if (densityComboBox.SelectedIndex == 0)
407        return;
408      if (indices == null) {
409        indices = GetDensityIndices(densityComboBox.SelectedIndex).ToList();
410      }
411      var data = Content.ProblemData.Dataset.GetDoubleValues(variable, indices).ToList();
412      var gradientChart = gradientCharts[variable];
413      var min = gradientChart.FixedXAxisMin;
414      var max = gradientChart.FixedXAxisMax;
415      var buckets = gradientChart.DrawingSteps;
416      if (min.HasValue && max.HasValue) {
417        densityChart.UpdateChart(data, min.Value, max.Value, buckets);
418        densityChart.Width = gradientChart.Width;
419
420        var gcPlotPosition = gradientChart.InnerPlotPosition;
421        densityChart.Left = (int)(gcPlotPosition.X / 100.0 * gradientChart.Width);
422        densityChart.Width = (int)(gcPlotPosition.Width / 100.0 * gradientChart.Width);
423
424        densityChart.Visible = true;
425      }
426
427      gradientChart.UpdateTitlePosition();
428    }
429
430    private void columnsNumericUpDown_ValueChanged(object sender, EventArgs e) {
431      MaxColumns = (int)columnsNumericUpDown.Value;
432      int columns = Math.Min(VisibleVariables.Count(), MaxColumns);
433      if (columns > 0) {
434        var tl = gradientChartTableLayout;
435        MaxColumns = columns;
436        tl.SuspendLayout();
437        ReOrderControls();
438        SetStyles();
439        tl.ResumeLayout();
440        tl.Refresh();
441        densityComboBox_SelectedIndexChanged(this, EventArgs.Empty);
442      }
443    }
444
445    private async void solution_ModelChanged(object sender, EventArgs e) {
446      foreach (var variable in VisibleVariables) {
447        var gradientChart = gradientCharts[variable];
448        var densityChart = densityCharts[variable];
449        // recalculate and refresh
450        await gradientChart.RecalculateAsync();
451        gradientChart.Refresh();
452        UpdateDensityChart(densityChart, variable);
453      }
454    }
455  }
456}
Note: See TracBrowser for help on using the repository browser.