Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionPartialDependencePlotView.cs

Last change on this file was 18208, checked in by gkronber, 2 years ago

#3134: bugfixes and improvements

File size: 27.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Globalization;
27using System.Linq;
28using System.Threading.Tasks;
29using System.Windows.Forms;
30using HeuristicLab.Common;
31using HeuristicLab.MainForm;
32using HeuristicLab.Visualization.ChartControlsExtensions;
33
34namespace HeuristicLab.Problems.DataAnalysis.Views {
35  [View("Partial Dependence Plots")]
36  [Content(typeof(IRegressionSolution))]
37  public partial class RegressionSolutionPartialDependencePlotView : DataAnalysisSolutionEvaluationView {
38    private readonly Dictionary<string, IPartialDependencePlot> partialDependencePlots;
39    private readonly Dictionary<string, DensityChart> densityCharts;
40    private readonly Dictionary<string, Panel> groupingPanels;
41    private ModifiableDataset sharedFixedVariables;
42
43    private const int Points = 200;
44    private int MaxColumns = 4;
45
46    private IEnumerable<string> VisibleVariables {
47      get {
48        foreach (ListViewItem item in variableListView.CheckedItems)
49          yield return item.Text;
50      }
51    }
52    private IEnumerable<IPartialDependencePlot> VisiblePartialDependencePlots {
53      get { return VisibleVariables.Select(v => partialDependencePlots[v]); }
54    }
55    private IEnumerable<DensityChart> VisibleDensityCharts {
56      get { return VisibleVariables.Select(v => densityCharts[v]); }
57    }
58    private IEnumerable<Panel> VisibleChartsPanels {
59      get { return VisibleVariables.Select(v => groupingPanels[v]); }
60    }
61
62    public RegressionSolutionPartialDependencePlotView() {
63      InitializeComponent();
64      partialDependencePlots = new Dictionary<string, IPartialDependencePlot>();
65      densityCharts = new Dictionary<string, DensityChart>();
66      groupingPanels = new Dictionary<string, Panel>();
67
68      limitView.Content = new DoubleLimit(0, 1);
69      limitView.Content.ValueChanged += limit_ValueChanged;
70
71      densityComboBox.SelectedIndex = 1; // select Training
72
73      // Avoid additional horizontal scrollbar
74      var vertScrollWidth = SystemInformation.VerticalScrollBarWidth;
75      scrollPanel.Padding = new Padding(0, 0, vertScrollWidth, 0);
76      scrollPanel.AutoScroll = true;
77    }
78
79    public new IRegressionSolution Content {
80      get { return (IRegressionSolution)base.Content; }
81      set { base.Content = value; }
82    }
83
84    protected override void RegisterContentEvents() {
85      base.RegisterContentEvents();
86      Content.ModelChanged += solution_ModelChanged;
87    }
88
89    protected override void DeregisterContentEvents() {
90      Content.ModelChanged -= solution_ModelChanged;
91      base.DeregisterContentEvents();
92    }
93
94    protected override void OnContentChanged() {
95      base.OnContentChanged();
96      if (Content == null) return;
97      var problemData = Content.ProblemData;
98
99      if (sharedFixedVariables != null) {
100        sharedFixedVariables.ItemChanged -= SharedFixedVariables_ItemChanged;
101        sharedFixedVariables.Reset -= SharedFixedVariables_Reset;
102      }
103
104      // Init Y-axis range
105      double min = double.MaxValue, max = double.MinValue;
106      var trainingTarget = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);
107      foreach (var t in trainingTarget) {
108        if (t < min) min = t;
109        if (t > max) max = t;
110      }
111      double range = max - min;
112      const double scale = 1.0 / 3.0;
113      double axisMin, axisMax, axisInterval;
114      ChartUtil.CalculateAxisInterval(min - scale * range, max + scale * range, 5, out axisMin, out axisMax, out axisInterval);
115      automaticYAxisCheckBox.Checked = false;
116      limitView.ReadOnly = false;
117      limitView.Content.Lower = axisMin;
118      limitView.Content.Upper = axisMax;
119
120      // create dataset of problemData input variables and model input variables
121      // necessary workaround to have the variables in the occurring order
122      var inputvariables =
123        new HashSet<string>(Content.ProblemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
124      var allowedInputVariables =
125        Content.ProblemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).ToList();
126
127      var doubleVariables = allowedInputVariables.Where(problemData.Dataset.VariableHasType<double>);
128      var doubleVariableValues = (IEnumerable<IList>)doubleVariables.Select(x => new List<double> {
129        problemData.Dataset.GetDoubleValue(x, 0)
130      });
131
132      var factorVariables = allowedInputVariables.Where(problemData.Dataset.VariableHasType<string>);
133      var factorVariableValues = (IEnumerable<IList>)factorVariables.Select(x => new List<string> {
134        problemData.Dataset.GetStringValue(x, 0)
135      });
136
137      sharedFixedVariables = new ModifiableDataset(doubleVariables.Concat(factorVariables), doubleVariableValues.Concat(factorVariableValues));
138      variableValuesModeComboBox.SelectedItem = "Median"; // triggers UpdateVariableValue and changes shardFixedVariables
139
140      // create controls
141      partialDependencePlots.Clear();
142      densityCharts.Clear();
143      groupingPanels.Clear();
144      foreach (var variableName in doubleVariables) {
145        var plot = CreatePartialDependencePlot(variableName, sharedFixedVariables);
146        partialDependencePlots.Add(variableName, plot);
147
148        var densityChart = new DensityChart() {
149          Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right,
150          Margin = Padding.Empty,
151          Height = 12,
152          Visible = false,
153          Top = (int)(plot.Height * 0.1),
154        };
155        densityCharts.Add(variableName, densityChart);
156
157        plot.ZoomChanged += (o, e) => {
158          var pdp = (PartialDependencePlot)o;
159          var density = densityCharts[pdp.FreeVariable];
160          density.Visible = densityComboBox.SelectedIndex != 0 && !pdp.IsZoomed;
161          if (density.Visible)
162            UpdateDensityChart(density, pdp.FreeVariable);
163        };
164        plot.SizeChanged += (o, e) => {
165          var pdp = (PartialDependencePlot)o;
166          var density = densityCharts[pdp.FreeVariable];
167          density.Top = (int)(pdp.Height * 0.1);
168        };
169
170        // Initially, the inner plot areas are not initialized for hidden charts (scrollpanel, ...)
171        // This event handler listens for the paint event once (where everything is already initialized) to do some manual layouting.
172        plot.ChartPostPaint += OnPartialDependencePlotPostPaint;
173
174        var panel = new Panel() {
175          Dock = DockStyle.Fill,
176          Margin = Padding.Empty,
177          BackColor = Color.White
178        };
179
180        panel.Controls.Add(densityChart);
181        panel.Controls.Add(plot);
182        groupingPanels.Add(variableName, panel);
183      }
184      foreach (var variableName in factorVariables) {
185        var plot = CreateFactorPartialDependencePlot(variableName, sharedFixedVariables);
186        partialDependencePlots.Add(variableName, plot);
187
188        var densityChart = new DensityChart() {
189          Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right,
190          Margin = Padding.Empty,
191          Height = 12,
192          Visible = false,
193          Top = (int)(plot.Height * 0.1),
194        };
195        densityCharts.Add(variableName, densityChart);
196        plot.ZoomChanged += (o, e) => {
197          var pdp = (FactorPartialDependencePlot)o;
198          var density = densityCharts[pdp.FreeVariable];
199          density.Visible = densityComboBox.SelectedIndex != 0 && !pdp.IsZoomed;
200          if (density.Visible)
201            UpdateDensityChart(density, pdp.FreeVariable);
202        };
203        plot.SizeChanged += (o, e) => {
204          var pdp = (FactorPartialDependencePlot)o;
205          var density = densityCharts[pdp.FreeVariable];
206          density.Top = (int)(pdp.Height * 0.1);
207        };
208
209        // Initially, the inner plot areas are not initialized for hidden charts (scrollpanel, ...)
210        // This event handler listens for the paint event once (where everything is already initialized) to do some manual layouting.
211        plot.ChartPostPaint += OnFactorPartialDependencePlotPostPaint;
212
213        var panel = new Panel() {
214          Dock = DockStyle.Fill,
215          Margin = Padding.Empty,
216          BackColor = Color.White
217        };
218
219        panel.Controls.Add(densityChart);
220        panel.Controls.Add(plot);
221        groupingPanels.Add(variableName, panel);
222      }
223      // update variable list
224      variableListView.ItemChecked -= variableListView_ItemChecked;
225      variableListView.Items.Clear();
226      foreach (var variable in allowedInputVariables)
227        variableListView.Items.Add(key: variable, text: variable, imageIndex: 0);
228
229      foreach (var variable in Content.Model.VariablesUsedForPrediction)
230        variableListView.Items[variable].Checked = true;
231      variableListView.ItemChecked += variableListView_ItemChecked;
232
233      sharedFixedVariables.ItemChanged += SharedFixedVariables_ItemChanged;
234      sharedFixedVariables.Reset += SharedFixedVariables_Reset;
235
236      rowNrNumericUpDown.Maximum = Content.ProblemData.Dataset.Rows - 1;
237
238      RecalculateAndRelayoutCharts();
239    }
240
241    public async Task AddSolutionAsync(IRegressionSolution solution) {
242      foreach (var chart in partialDependencePlots.Values) {
243        await chart.AddSolutionAsync(solution);
244      }
245    }
246
247    private void SharedFixedVariables_ItemChanged(object sender, EventArgs<int, int> e) {
248      SharedFixedVariablesChanged();
249    }
250    private void SharedFixedVariables_Reset(object sender, EventArgs e) {
251      SharedFixedVariablesChanged();
252    }
253    private void SharedFixedVariablesChanged() {
254      if (!setVariableValues) // set mode to "nothing" if change was not initiated from a "mode change"
255        variableValuesModeComboBox.SelectedIndex = -1;
256
257      double yValue = Content.Model.GetEstimatedValues(sharedFixedVariables, new[] { 0 }).Single();
258      string title = Content.ProblemData.TargetVariable + ": " + yValue.ToString("G5", CultureInfo.CurrentCulture);
259      foreach (var chart in partialDependencePlots.Values) {
260        if (!string.IsNullOrEmpty(chart.YAxisTitle)) { // only show title for first column in grid
261          chart.YAxisTitle = title;
262        }
263      }
264    }
265
266    private void OnPartialDependencePlotPostPaint(object o, EventArgs e) {
267      var plot = (PartialDependencePlot)o;
268      var density = densityCharts[plot.FreeVariable];
269
270      density.Width = plot.Width;
271
272      var gcPlotPosition = plot.InnerPlotPosition;
273      density.Left = (int)(gcPlotPosition.X / 100.0 * plot.Width);
274      density.Width = (int)(gcPlotPosition.Width / 100.0 * plot.Width);
275      plot.UpdateTitlePosition();
276
277      // removed after succesful layouting due to performance reasons
278      if (gcPlotPosition.Width != 0)
279        plot.ChartPostPaint -= OnPartialDependencePlotPostPaint;
280    }
281
282    private void OnFactorPartialDependencePlotPostPaint(object o, EventArgs e) {
283      var plot = (FactorPartialDependencePlot)o;
284      var density = densityCharts[plot.FreeVariable];
285
286      density.Width = plot.Width;
287
288      var gcPlotPosition = plot.InnerPlotPosition;
289      density.Left = (int)(gcPlotPosition.X / 100.0 * plot.Width);
290      density.Width = (int)(gcPlotPosition.Width / 100.0 * plot.Width);
291      plot.UpdateTitlePosition();
292
293      // removed after succesful layouting due to performance reasons
294      if (gcPlotPosition.Width != 0)
295        plot.ChartPostPaint -= OnFactorPartialDependencePlotPostPaint;
296    }
297
298    private async void RecalculateAndRelayoutCharts() {
299      foreach (var variable in VisibleVariables) {
300        var plot = partialDependencePlots[variable];
301        await plot.RecalculateAsync(false, false);
302      }
303      partialDependencePlotTableLayout.SuspendLayout();
304      SetupYAxis();
305      ReOrderControls();
306      SetStyles();
307      partialDependencePlotTableLayout.ResumeLayout();
308      partialDependencePlotTableLayout.Refresh();
309      foreach (var variable in VisibleVariables) {
310        DensityChart densityChart;
311        if (densityCharts.TryGetValue(variable, out densityChart)) {
312          UpdateDensityChart(densityChart, variable);
313        }
314      }
315    }
316    private PartialDependencePlot CreatePartialDependencePlot(string variableName, ModifiableDataset sharedFixedVariables) {
317      var plot = new PartialDependencePlot {
318        Dock = DockStyle.Fill,
319        Margin = Padding.Empty,
320        ShowLegend = false,
321        ShowCursor = true,
322        ShowConfigButton = false,
323        YAxisTicks = 5,
324      };
325      plot.VariableValueChanged += async (o, e) => {
326        var recalculations = VisiblePartialDependencePlots
327          .Except(new[] { (IPartialDependencePlot)o })
328          .Select(async chart => {
329            await chart.RecalculateAsync(updateOnFinish: false, resetYAxis: false);
330          }).ToList();
331        await Task.WhenAll(recalculations);
332
333        if (recalculations.All(t => t.IsCompleted))
334          SetupYAxis();
335      };
336      plot.Configure(new[] { Content }, sharedFixedVariables, variableName, Points);
337      plot.SolutionAdded += partialDependencePlot_SolutionAdded;
338      plot.SolutionRemoved += partialDependencePlot_SolutionRemoved;
339      return plot;
340    }
341    private FactorPartialDependencePlot CreateFactorPartialDependencePlot(string variableName, ModifiableDataset sharedFixedVariables) {
342      var plot = new FactorPartialDependencePlot {
343        Dock = DockStyle.Fill,
344        Margin = Padding.Empty,
345        ShowLegend = false,
346        ShowCursor = true,
347        YAxisTicks = 5,
348      };
349      plot.VariableValueChanged += async (o, e) => {
350        var recalculations = VisiblePartialDependencePlots
351          .Except(new[] { (FactorPartialDependencePlot)o })
352          .Select(async chart => {
353            await chart.RecalculateAsync(updateOnFinish: false, resetYAxis: false);
354          }).ToList();
355        await Task.WhenAll(recalculations);
356
357        if (recalculations.All(t => t.IsCompleted))
358          SetupYAxis();
359      };
360      var variableValues = Content.ProblemData.Dataset.GetStringValues(variableName).Distinct().OrderBy(n => n).ToList();
361      plot.Configure(new[] { Content }, sharedFixedVariables, variableName, variableValues);
362      plot.SolutionAdded += partialDependencePlot_SolutionAdded;
363      plot.SolutionRemoved += partialDependencePlot_SolutionRemoved;
364      return plot;
365    }
366    private void SetupYAxis() {
367      double axisMin, axisMax;
368      if (automaticYAxisCheckBox.Checked) {
369        double min = double.MaxValue, max = double.MinValue;
370        foreach (var chart in VisiblePartialDependencePlots) {
371          if (chart.YMin < min) min = chart.YMin;
372          if (chart.YMax > max) max = chart.YMax;
373        }
374
375        double axisInterval;
376        ChartUtil.CalculateAxisInterval(min, max, 5, out axisMin, out axisMax, out axisInterval);
377      } else {
378        axisMin = limitView.Content.Lower;
379        axisMax = limitView.Content.Upper;
380      }
381
382      foreach (var chart in VisiblePartialDependencePlots) {
383        chart.FixedYAxisMin = axisMin;
384        chart.FixedYAxisMax = axisMax;
385      }
386    }
387
388    // reorder chart controls so that they always appear in the same order as in the list view
389    // the table layout containing the controls should be suspended before calling this method
390    private void ReOrderControls() {
391      var tl = partialDependencePlotTableLayout;
392      tl.Controls.Clear();
393      int row = 0, column = 0;
394      double yValue = Content.Model.GetEstimatedValues(sharedFixedVariables, new[] { 0 }).Single();
395      string title = Content.ProblemData.TargetVariable + ": " + yValue.ToString("G5", CultureInfo.CurrentCulture);
396
397      foreach (var v in VisibleVariables) {
398        var chartsPanel = groupingPanels[v];
399        tl.Controls.Add(chartsPanel, column, row);
400
401        var chart = partialDependencePlots[v];
402        chart.YAxisTitle = column == 0 ? title : string.Empty;
403        column++;
404
405        if (column == MaxColumns) {
406          row++;
407          column = 0;
408        }
409      }
410    }
411
412    private void SetStyles() {
413      var tl = partialDependencePlotTableLayout;
414      tl.RowStyles.Clear();
415      tl.ColumnStyles.Clear();
416      int numVariables = VisibleVariables.Count();
417      if (numVariables == 0)
418        return;
419
420      // set column styles
421      tl.ColumnCount = Math.Min(numVariables, MaxColumns);
422      for (int c = 0; c < tl.ColumnCount; c++)
423        tl.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100.0f / tl.ColumnCount));
424
425      // set row styles
426      tl.RowCount = (int)Math.Ceiling((double)numVariables / tl.ColumnCount);
427      var columnWidth = tl.Width / tl.ColumnCount; // assume all columns have the same width
428      var rowHeight = (int)(0.8 * columnWidth);
429      for (int r = 0; r < tl.RowCount; r++)
430        tl.RowStyles.Add(new RowStyle(SizeType.Absolute, rowHeight));
431    }
432
433    private async void partialDependencePlot_SolutionAdded(object sender, EventArgs<IRegressionSolution> e) {
434      var solution = e.Value;
435      foreach (var chart in partialDependencePlots.Values) {
436        if (sender == chart) continue;
437        await chart.AddSolutionAsync(solution);
438      }
439    }
440
441    private async void partialDependencePlot_SolutionRemoved(object sender, EventArgs<IRegressionSolution> e) {
442      var solution = e.Value;
443      foreach (var chart in partialDependencePlots.Values) {
444        if (sender == chart) continue;
445        await chart.RemoveSolutionAsync(solution);
446      }
447    }
448
449    private async void variableListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
450      var item = e.Item;
451      var variable = item.Text;
452      var plot = partialDependencePlots[variable];
453      var chartsPanel = groupingPanels[variable];
454      var tl = partialDependencePlotTableLayout;
455
456      tl.SuspendLayout();
457      if (item.Checked) {
458        tl.Controls.Add(chartsPanel);
459        await plot.RecalculateAsync(false, false);
460      } else {
461        tl.Controls.Remove(chartsPanel);
462      }
463
464      if (tl.Controls.Count > 0) {
465        SetupYAxis();
466        ReOrderControls();
467        SetStyles();
468      }
469      tl.ResumeLayout();
470      tl.Refresh();
471      densityComboBox_SelectedIndexChanged(this, EventArgs.Empty);
472    }
473
474    private void automaticYAxisCheckBox_CheckedChanged(object sender, EventArgs e) {
475      limitView.ReadOnly = automaticYAxisCheckBox.Checked;
476      SetupYAxis();
477      partialDependencePlotTableLayout.Refresh();
478      densityComboBox_SelectedIndexChanged(this, EventArgs.Empty); // necessary to realign the density plots
479    }
480
481    private void limit_ValueChanged(object sender, EventArgs e) {
482      if (automaticYAxisCheckBox.Checked)
483        return;
484      SetupYAxis();
485      partialDependencePlotTableLayout.Refresh();
486      densityComboBox_SelectedIndexChanged(this, EventArgs.Empty); // necessary to realign the density plots
487    }
488
489    private void densityComboBox_SelectedIndexChanged(object sender, EventArgs e) {
490      if (Content == null)
491        return;
492
493      int si = densityComboBox.SelectedIndex;
494      if (si == 0) {
495        foreach (var densityChart in densityCharts.Values)
496          densityChart.Visible = false;
497      } else {
498        var indices = GetDensityIndices(si).ToList();
499
500        foreach (var entry in densityCharts) {
501          var variableName = entry.Key;
502          var densityChart = entry.Value;
503          if (!VisibleVariables.Contains(variableName) || partialDependencePlots[variableName].IsZoomed)
504            continue;
505
506          UpdateDensityChart(densityChart, variableName, indices);
507        }
508      }
509    }
510    private IEnumerable<int> GetDensityIndices(int selectedIndex) {
511      var problemData = Content.ProblemData;
512      return
513        selectedIndex == 1 ? problemData.TrainingIndices :
514        selectedIndex == 2 ? problemData.TestIndices :
515        problemData.AllIndices;
516    }
517    private void UpdateDensityChart(DensityChart densityChart, string variable, IList<int> indices = null) {
518      if (densityComboBox.SelectedIndex == 0)
519        return;
520      if (indices == null) {
521        indices = GetDensityIndices(densityComboBox.SelectedIndex).ToList();
522      }
523      if (Content.ProblemData.Dataset.VariableHasType<double>(variable)) {
524        var data = Content.ProblemData.Dataset.GetDoubleValues(variable, indices).ToList();
525        var plot = partialDependencePlots[variable] as PartialDependencePlot;
526        if (plot != null) {
527          var min = plot.FixedXAxisMin;
528          var max = plot.FixedXAxisMax;
529          var buckets = plot.DrawingSteps;
530          if (min.HasValue && max.HasValue) {
531            densityChart.UpdateChart(data, min.Value, max.Value, buckets);
532            densityChart.Width = plot.Width;
533
534            var gcPlotPosition = plot.InnerPlotPosition;
535            densityChart.Left = (int)(gcPlotPosition.X / 100.0 * plot.Width);
536            densityChart.Width = (int)(gcPlotPosition.Width / 100.0 * plot.Width);
537
538            densityChart.Visible = true;
539          }
540          plot.UpdateTitlePosition();
541        }
542      } else if (Content.ProblemData.Dataset.VariableHasType<string>(variable)) {
543        var data = Content.ProblemData.Dataset.GetStringValues(variable).ToList();
544        var plot = partialDependencePlots[variable] as FactorPartialDependencePlot;
545        if (plot != null) {
546          densityChart.UpdateChart(data);
547          densityChart.Width = plot.Width;
548
549          var gcPlotPosition = plot.InnerPlotPosition;
550          densityChart.Left = (int)(gcPlotPosition.X / 100.0 * plot.Width);
551          densityChart.Width = (int)(gcPlotPosition.Width / 100.0 * plot.Width);
552
553          densityChart.Visible = true;
554
555          plot.UpdateTitlePosition();
556        }
557      }
558    }
559
560    private void columnsNumericUpDown_ValueChanged(object sender, EventArgs e) {
561      MaxColumns = (int)columnsNumericUpDown.Value;
562      int columns = Math.Min(VisibleVariables.Count(), MaxColumns);
563      if (columns > 0) {
564        var tl = partialDependencePlotTableLayout;
565        MaxColumns = columns;
566        tl.SuspendLayout();
567        ReOrderControls();
568        SetStyles();
569        tl.ResumeLayout();
570        tl.Refresh();
571        densityComboBox_SelectedIndexChanged(this, EventArgs.Empty);
572      }
573    }
574
575    private async void solution_ModelChanged(object sender, EventArgs e) {
576      foreach (var variable in VisibleVariables) {
577        var pdp = partialDependencePlots[variable];
578        var densityChart = densityCharts[variable];
579        // recalculate and refresh
580        await pdp.RecalculateAsync(false, false);
581        pdp.Refresh();
582        UpdateDensityChart(densityChart, variable);
583      }
584    }
585
586    // flag that the current change is not triggered by a manual change from within a single plot
587    private bool setVariableValues = false;
588    private void variableValuesComboBox_SelectedValueChanged(object sender, EventArgs e) {
589      if (variableValuesModeComboBox.SelectedIndex == -1)
590        return; // changed to "manual" due to manual change of a variable
591      setVariableValues = true;
592      UpdateVariableValues();
593      setVariableValues = false;
594    }
595    private void rowNrNumericUpDown_ValueChanged(object sender, EventArgs e) {
596      if ((string)variableValuesModeComboBox.SelectedItem != "Row") {
597        variableValuesModeComboBox.SelectedItem = "Row"; // triggers UpdateVariableValues
598      } else {
599        setVariableValues = true;
600        UpdateVariableValues();
601        setVariableValues = false;
602      }
603    }
604    private void UpdateVariableValues() {
605      string mode = (string)variableValuesModeComboBox.SelectedItem;
606
607      var dataset = Content.ProblemData.Dataset;
608      object[] newRow;
609
610      if (mode == "Row") {
611        int rowNumber = (int)rowNrNumericUpDown.Value;
612        newRow = sharedFixedVariables.VariableNames
613          .Select<string, object>(variableName => {
614            if (dataset.DoubleVariables.Contains(variableName)) {
615              return dataset.GetDoubleValue(variableName, rowNumber);
616            } else if (dataset.StringVariables.Contains(variableName)) {
617              return dataset.GetStringValue(variableName, rowNumber);
618            } else {
619              throw new NotSupportedException("Only double and string(factor) columns are currently supported.");
620            }
621          }).ToArray();
622      } else {
623        newRow = sharedFixedVariables.VariableNames
624          .Select<string, object>(variableName => {
625            if (dataset.DoubleVariables.Contains(variableName)) {
626              var values = dataset.GetDoubleValues(variableName);
627              return
628                mode == "Mean" ? values.Average() :
629                mode == "Median" ? values.Median() :
630                mode == "Most Common" ? MostCommon(values) :
631                throw new NotSupportedException();
632            } else if (dataset.StringVariables.Contains(variableName)) {
633              var values = dataset.GetStringValues(variableName);
634              return
635                mode == "Mean" ? MostCommon(values) :
636                mode == "Median" ? MostCommon(values) :
637                mode == "Most Common" ? MostCommon(values) :
638                throw new NotSupportedException();
639            } else {
640              throw new NotSupportedException("Only double and string(factor) columns are currently supported.");
641            }
642          }).ToArray();
643      }
644
645      sharedFixedVariables.ReplaceRow(0, newRow);
646    }
647
648    private static T MostCommon<T>(IEnumerable<T> values) {
649      return values.GroupBy(x => x).OrderByDescending(g => g.Count()).Select(g => g.Key).First();
650    }
651
652    // ToolTips cannot be shown longer than 5000ms, only by using ToolTip.Show manually
653    // See: https://stackoverflow.com/questions/8225807/c-sharp-tooltip-doesnt-display-long-enough
654    private void variableValuesModeComboBox_MouseHover(object sender, EventArgs e) {
655      string tooltipText = @"Sets each variable to a specific value:
656    Row - Selects the value based on a specified row of the dataset.
657    Mean - Sets the value to the arithmetic mean of the variable.
658    Median - Sets the value to the median of the variable.
659    Most Common - Sets the value to the most common value of the variable (first if multiple).
660
661Note: For categorical values, the most common value is used when selecting Mean, Median or Most Common.";
662      toolTip.Show(tooltipText, variableValuesModeComboBox, 30000);
663      toolTip.Active = true;
664    }
665    private void variableValuesModeComboBox_MouseLeave(object sender, EventArgs e) {
666      toolTip.Active = false;
667    }
668  }
669}
Note: See TracBrowser for help on using the repository browser.