Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionBoxPlotView.cs @ 15623

Last change on this file since 15623 was 15623, checked in by fholzing, 6 years ago

#2880: Added additional null/empty-check (otherwise an error would occur if all runs are removed)

File size: 20.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.ComponentModel;
25using System.Linq;
26using System.Windows.Forms;
27using System.Windows.Forms.DataVisualization.Charting;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.MainForm;
32using HeuristicLab.MainForm.WindowsForms;
33
34namespace HeuristicLab.Optimization.Views {
35  [View("Box Plot")]
36  [Content(typeof(RunCollection), false)]
37  public partial class RunCollectionBoxPlotView : AsynchronousContentView {
38    private enum AxisDimension { Color = 0 }
39    private const string BoxPlotSeriesName = "BoxPlotSeries";
40    private const string BoxPlotChartAreaName = "BoxPlotChartArea";
41
42    private bool suppressUpdates = false;
43    private string xAxisValue;
44    private string yAxisValue;
45    private Dictionary<int, Dictionary<object, double>> categoricalMapping;
46    private SortedDictionary<double, Series> seriesCache;
47
48    public RunCollectionBoxPlotView() {
49      InitializeComponent();
50      chart.ContextMenuStrip.Items.Insert(0, openBubbleChartViewToolStripMenuItem);
51
52      categoricalMapping = new Dictionary<int, Dictionary<object, double>>();
53      seriesCache = new SortedDictionary<double, Series>();
54      chart.ChartAreas[0].Visible = false;
55      chart.Series.Clear();
56      chart.ChartAreas.Add(BoxPlotChartAreaName);
57      chart.CustomizeAllChartAreas();
58      chart.ChartAreas[BoxPlotChartAreaName].Axes.ToList().ForEach(x => { x.ScaleView.Zoomable = true; x.ScaleView.MinSize = 0; });
59      chart.ChartAreas[BoxPlotChartAreaName].CursorX.Interval = 0.5;
60      chart.ChartAreas[BoxPlotChartAreaName].CursorY.Interval = 1e-5;
61    }
62
63    public new RunCollection Content {
64      get { return (RunCollection)base.Content; }
65      set { base.Content = value; }
66    }
67    public IStringConvertibleMatrix Matrix {
68      get { return this.Content; }
69    }
70
71    #region RunCollection and Run events
72    protected override void RegisterContentEvents() {
73      base.RegisterContentEvents();
74      Content.Reset += new EventHandler(Content_Reset);
75      Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
76      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
77      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
78      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
79      Content.UpdateOfRunsInProgressChanged += new EventHandler(Content_UpdateOfRunsInProgressChanged);
80      Content.OptimizerNameChanged += new EventHandler(Content_AlgorithmNameChanged);
81      RegisterRunEvents(Content);
82    }
83    protected override void DeregisterContentEvents() {
84      base.DeregisterContentEvents();
85      Content.Reset -= new EventHandler(Content_Reset);
86      Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
87      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
88      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
89      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
90      Content.UpdateOfRunsInProgressChanged -= new EventHandler(Content_UpdateOfRunsInProgressChanged);
91      Content.OptimizerNameChanged -= new EventHandler(Content_AlgorithmNameChanged);
92      DeregisterRunEvents(Content);
93    }
94
95    protected virtual void RegisterRunEvents(IEnumerable<IRun> runs) {
96      foreach (IRun run in runs)
97        run.PropertyChanged += run_PropertyChanged;
98    }
99    protected virtual void DeregisterRunEvents(IEnumerable<IRun> runs) {
100      foreach (IRun run in runs)
101        run.PropertyChanged -= run_PropertyChanged;
102    }
103
104    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
105      DeregisterRunEvents(e.OldItems);
106      RegisterRunEvents(e.Items);
107    }
108    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
109      DeregisterRunEvents(e.Items);
110    }
111    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
112      RegisterRunEvents(e.Items);
113    }
114    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
115      if (InvokeRequired)
116        Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
117      else {
118        suppressUpdates = Content.UpdateOfRunsInProgress;
119        if (!suppressUpdates) UpdateDataPoints();
120      }
121    }
122
123    private void Content_Reset(object sender, EventArgs e) {
124      if (InvokeRequired)
125        Invoke(new EventHandler(Content_Reset), sender, e);
126      else if (!suppressUpdates) {
127        UpdateDataPoints();
128        UpdateAxisLabels();
129      }
130    }
131    private void Content_ColumnNamesChanged(object sender, EventArgs e) {
132      if (InvokeRequired)
133        Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
134      else {
135        UpdateComboBoxes();
136      }
137    }
138    private void run_PropertyChanged(object sender, PropertyChangedEventArgs e) {
139      if (InvokeRequired)
140        this.Invoke((Action<object, PropertyChangedEventArgs>)run_PropertyChanged, sender, e);
141      else if (!suppressUpdates) {
142        if (e.PropertyName == "Visible")
143          UpdateDataPoints();
144      }
145    }
146
147    private void Content_AlgorithmNameChanged(object sender, EventArgs e) {
148      if (InvokeRequired)
149        Invoke(new EventHandler(Content_AlgorithmNameChanged), sender, e);
150      else UpdateCaption();
151    }
152    #endregion
153
154    #region update comboboxes, datapoints, runs
155    protected override void OnContentChanged() {
156      base.OnContentChanged();
157      this.categoricalMapping.Clear();
158      UpdateComboBoxes();
159      UpdateDataPoints();
160      UpdateCaption();
161    }
162
163    private void UpdateCaption() {
164      Caption = Content != null ? Content.OptimizerName + " Box Plot" : ViewAttribute.GetViewName(GetType());
165    }
166
167    private void UpdateComboBoxes() {
168      string selectedXAxis = (string)this.xAxisComboBox.SelectedItem;
169      string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
170      this.xAxisComboBox.Items.Clear();
171      this.yAxisComboBox.Items.Clear();
172      if (Content != null) {
173        string[] additionalAxisDimension = Enum.GetNames(typeof(AxisDimension));
174        this.xAxisComboBox.Items.AddRange(additionalAxisDimension);
175        this.xAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
176        this.yAxisComboBox.Items.AddRange(additionalAxisDimension);
177        this.yAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
178
179        bool changed = false;
180        if (selectedXAxis != null && xAxisComboBox.Items.Contains(selectedXAxis)) {
181          xAxisComboBox.SelectedItem = selectedXAxis;
182          changed = true;
183        }
184        if (selectedYAxis != null && yAxisComboBox.Items.Contains(selectedYAxis)) {
185          yAxisComboBox.SelectedItem = selectedYAxis;
186          changed = true;
187        }
188        if (changed)
189          UpdateDataPoints();
190      }
191    }
192
193    private void UpdateDataPoints() {
194      this.categoricalMapping.Clear();
195      this.chart.Series.Clear();
196      this.seriesCache.Clear();
197      if (Content != null) {
198        foreach (IRun run in this.Content.Where(r => r.Visible))
199          this.AddDataPoint(run);
200        foreach (Series s in this.seriesCache.Values)
201          this.chart.Series.Add(s);
202
203        UpdateStatistics();
204        if (seriesCache.Count > 0) {
205          Series boxPlotSeries = CreateBoxPlotSeries();
206          this.chart.Series.Add(boxPlotSeries);
207        }
208
209        UpdateAxisLabels();
210      }
211      UpdateNoRunsVisibleLabel();
212    }
213
214    private void UpdateStatistics() {
215      DoubleMatrix matrix = new DoubleMatrix(10, seriesCache.Count);
216      matrix.SortableView = false;
217      List<string> columnNames = new List<string>();
218      foreach (Series series in seriesCache.Values) {
219        DataPoint datapoint = series.Points.FirstOrDefault();
220        if (datapoint != null) {
221          IRun run = (IRun)datapoint.Tag;
222          string selectedAxis = xAxisValue;
223          IItem value = null;
224
225          if (Enum.IsDefined(typeof(AxisDimension), selectedAxis)) {
226            AxisDimension axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), selectedAxis);
227            switch (axisDimension) {
228              case AxisDimension.Color:
229                value = new StringValue(run.Color.ToString());
230                break;
231            }
232          } else value = Content.GetValue(run, selectedAxis);
233          string columnName = string.Empty;
234          if (value is DoubleValue || value is IntValue)
235            columnName = selectedAxis + ": ";
236          columnName += value.ToString();
237          columnNames.Add(columnName);
238        }
239      }
240      matrix.ColumnNames = columnNames;
241      matrix.RowNames = new string[] { "Count", "Minimum", "Maximum", "Median", "Average", "Standard Deviation", "Variance", "25th Percentile", "75th Percentile", "Interquartile Range" };
242
243      for (int i = 0; i < seriesCache.Count; i++) {
244        Series series = seriesCache.ElementAt(i).Value;
245        double[] seriesValues = series.Points.Select(p => p.YValues[0]).OrderBy(d => d).ToArray();
246        matrix[0, i] = seriesValues.Length;
247        matrix[1, i] = seriesValues.Min();
248        matrix[2, i] = seriesValues.Max();
249        matrix[3, i] = seriesValues.Median();
250        matrix[4, i] = seriesValues.Average();
251        matrix[5, i] = seriesValues.StandardDeviation();
252        matrix[6, i] = seriesValues.Variance();
253        matrix[7, i] = seriesValues.Quantile(0.25);
254        matrix[8, i] = seriesValues.Quantile(0.75);
255        matrix[9, i] = matrix[8, i] - matrix[7, i];
256      }
257      statisticsMatrixView.Content = matrix;
258    }
259
260    private Series CreateBoxPlotSeries() {
261      Series boxPlotSeries = new Series(BoxPlotSeriesName);
262      string seriesNames = string.Concat(seriesCache.Keys.Select(x => x.ToString() + ";").ToArray());
263      seriesNames = seriesNames.Remove(seriesNames.Length - 1); //delete last ; from string
264
265      boxPlotSeries.ChartArea = BoxPlotChartAreaName;
266      boxPlotSeries.ChartType = SeriesChartType.BoxPlot;
267      boxPlotSeries["BoxPlotSeries"] = seriesNames;
268      boxPlotSeries["BoxPlotShowUnusualValues"] = "true";
269      boxPlotSeries["PointWidth"] = "0.4";
270      boxPlotSeries.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.VerticalCenter;
271      boxPlotSeries.BackSecondaryColor = System.Drawing.Color.FromArgb(130, 224, 64, 10);
272      boxPlotSeries.BorderColor = System.Drawing.Color.FromArgb(64, 64, 64);
273      boxPlotSeries.Color = System.Drawing.Color.FromArgb(224, 64, 10);
274
275      return boxPlotSeries;
276    }
277
278    private void AddDataPoint(IRun run) {
279      double? xValue;
280      double? yValue;
281
282      this.xAxisValue = (string)xAxisComboBox.SelectedItem;
283      this.yAxisValue = (string)yAxisComboBox.SelectedItem;
284
285      xValue = GetValue(run, this.xAxisValue);
286      yValue = GetValue(run, this.yAxisValue);
287
288      if (xValue.HasValue && yValue.HasValue) {
289        if (!this.seriesCache.ContainsKey(xValue.Value))
290          seriesCache[xValue.Value] = new Series(xValue.Value.ToString());
291
292        Series series = seriesCache[xValue.Value];
293        DataPoint point = new DataPoint(xValue.Value, yValue.Value);
294        point.Tag = run;
295        series.Points.Add(point);
296      }
297    }
298    #endregion
299
300    #region get values from run
301    private double? GetValue(IRun run, string columnName) {
302      if (run == null || string.IsNullOrEmpty(columnName))
303        return null;
304
305      if (Enum.IsDefined(typeof(AxisDimension), columnName)) {
306        AxisDimension axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), columnName);
307        return GetValue(run, axisDimension);
308      } else {
309        int columnIndex = Matrix.ColumnNames.ToList().IndexOf(columnName);
310        IItem value = Content.GetValue(run, columnIndex);
311        if (value == null)
312          return null;
313
314        DoubleValue doubleValue = value as DoubleValue;
315        IntValue intValue = value as IntValue;
316        TimeSpanValue timeSpanValue = value as TimeSpanValue;
317        double? ret = null;
318        if (doubleValue != null) {
319          if (!double.IsNaN(doubleValue.Value) && !double.IsInfinity(doubleValue.Value))
320            ret = doubleValue.Value;
321        } else if (intValue != null)
322          ret = intValue.Value;
323        else if (timeSpanValue != null) {
324          ret = timeSpanValue.Value.TotalSeconds;
325        } else
326          ret = GetCategoricalValue(columnIndex, value.ToString());
327
328        return ret;
329      }
330    }
331    private double? GetCategoricalValue(int dimension, string value) {
332      if (!this.categoricalMapping.ContainsKey(dimension)) {
333        this.categoricalMapping[dimension] = new Dictionary<object, double>();
334        var orderedCategories = Content.Where(r => r.Visible && Content.GetValue(r, dimension) != null).Select(r => Content.GetValue(r, dimension).ToString())
335                                       .Distinct().OrderBy(x => x, new NaturalStringComparer());
336        int count = 1;
337        foreach (var category in orderedCategories) {
338          this.categoricalMapping[dimension].Add(category, count);
339          count++;
340        }
341      }
342      if (!this.categoricalMapping[dimension].ContainsKey(value)) return null;
343      return this.categoricalMapping[dimension][value];
344    }
345    private double? GetValue(IRun run, AxisDimension axisDimension) {
346      double? value = double.NaN;
347      switch (axisDimension) {
348        case AxisDimension.Color: {
349            const int colorDimension = -1;
350            if (!categoricalMapping.ContainsKey(colorDimension)) {
351              categoricalMapping[colorDimension] = Content.Where(r => r.Visible)
352                  .Select(r => r.Color.Name)
353                  .Distinct()
354                  .OrderBy(c => c, new NaturalStringComparer())
355                  .Select((c, i) => new { Color = c, Index = i })
356                  .ToDictionary(a => (object)a.Color, a => (double)a.Index);
357
358            }
359            value = GetCategoricalValue(colorDimension, run.Color.Name);
360            break;
361          }
362        default: {
363            throw new ArgumentException("No handling strategy for " + axisDimension.ToString() + " is defined.");
364          }
365      }
366      return value;
367    }
368    #endregion
369
370    #region GUI events
371    private void UpdateNoRunsVisibleLabel() {
372      if (this.chart.Series.Count > 0) {
373        noRunsLabel.Visible = false;
374        showStatisticsCheckBox.Enabled = true;
375        splitContainer.Panel2Collapsed = !showStatisticsCheckBox.Checked;
376      } else {
377        noRunsLabel.Visible = true;
378        showStatisticsCheckBox.Enabled = false;
379        splitContainer.Panel2Collapsed = true;
380      }
381    }
382
383    private void AxisComboBox_SelectionChangeCommitted(object sender, EventArgs e) {
384      UpdateDataPoints();
385    }
386    private void UpdateAxisLabels() {
387      Axis xAxis = this.chart.ChartAreas[BoxPlotChartAreaName].AxisX;
388      Axis yAxis = this.chart.ChartAreas[BoxPlotChartAreaName].AxisY;
389      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
390
391      SetCustomAxisLabels(xAxis, xAxisComboBox.SelectedIndex - axisDimensionCount);
392      SetCustomAxisLabels(yAxis, yAxisComboBox.SelectedIndex - axisDimensionCount);
393
394      xAxis.Title = (string)xAxisComboBox.SelectedItem;
395      yAxis.Title = (string)yAxisComboBox.SelectedItem;
396    }
397
398    private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
399      this.UpdateAxisLabels();
400    }
401
402    private void SetCustomAxisLabels(Axis axis, int dimension) {
403      axis.CustomLabels.Clear();
404      if (categoricalMapping.ContainsKey(dimension)) {
405        int position = 1;
406        foreach (var pair in categoricalMapping[dimension].Where(x => seriesCache.ContainsKey(x.Value))) {
407          string labelText = pair.Key.ToString();
408          CustomLabel label = new CustomLabel();
409          label.ToolTip = labelText;
410          if (labelText.Length > 25)
411            labelText = labelText.Substring(0, 25) + " ... ";
412          label.Text = labelText;
413          label.GridTicks = GridTickTypes.TickMark;
414          label.FromPosition = position - 0.5;
415          label.ToPosition = position + 0.5;
416          axis.CustomLabels.Add(label);
417          position++;
418        }
419      } else if (dimension > 0 && Content != null && Content.Count > 0 && Content.GetValue(0, dimension) is TimeSpanValue) {
420        this.chart.ChartAreas[0].RecalculateAxesScale();
421        Axis correspondingAxis = this.chart.ChartAreas[0].Axes.Where(x => x.Name == axis.Name).SingleOrDefault();
422        if (correspondingAxis == null)
423          correspondingAxis = axis;
424        for (double i = correspondingAxis.Minimum; i <= correspondingAxis.Maximum; i += correspondingAxis.LabelStyle.Interval) {
425          TimeSpan time = TimeSpan.FromSeconds(i);
426          string x = string.Format("{0:00}:{1:00}:{2:00}", (int)time.Hours, time.Minutes, time.Seconds);
427          axis.CustomLabels.Add(i - correspondingAxis.LabelStyle.Interval / 2, i + correspondingAxis.LabelStyle.Interval / 2, x);
428        }
429      } else if (chart.ChartAreas[BoxPlotChartAreaName].AxisX == axis) {
430        double position = 1.0;
431        foreach (Series series in chart.Series) {
432          if (series.Name != BoxPlotSeriesName) {
433            string labelText = series.Points[0].XValue.ToString();
434            CustomLabel label = new CustomLabel();
435            label.FromPosition = position - 0.5;
436            label.ToPosition = position + 0.5;
437            label.GridTicks = GridTickTypes.TickMark;
438            label.Text = labelText;
439            axis.CustomLabels.Add(label);
440            position++;
441          }
442        }
443      }
444    }
445
446    private void openBubbleChartViewToolStripMenuItem_Click(object sender, EventArgs e) {
447      RunCollectionBubbleChartView bubbleChartView = new RunCollectionBubbleChartView();
448      bubbleChartView.Content = this.Content;
449      bubbleChartView.xAxisComboBox.SelectedItem = xAxisComboBox.SelectedItem;
450      bubbleChartView.yAxisComboBox.SelectedItem = yAxisComboBox.SelectedItem;
451      bubbleChartView.Show();
452    }
453
454    private void chart_MouseMove(object sender, MouseEventArgs e) {
455      string newTooltipText = string.Empty;
456      string oldTooltipText;
457      HitTestResult h = this.chart.HitTest(e.X, e.Y);
458      if (h.ChartElementType == ChartElementType.AxisLabels) {
459        newTooltipText = ((CustomLabel)h.Object).ToolTip;
460      }
461
462      oldTooltipText = this.tooltip.GetToolTip(chart);
463      if (newTooltipText != oldTooltipText)
464        this.tooltip.SetToolTip(chart, newTooltipText);
465    }
466    #endregion
467
468    private void showStatisticsCheckBox_CheckedChanged(object sender, EventArgs e) {
469      splitContainer.Panel2Collapsed = !showStatisticsCheckBox.Checked;
470    }
471
472    public bool StatisticsVisible {
473      get { return splitContainer.Panel2Collapsed; }
474      set { splitContainer.Panel2Collapsed = value; }
475    }
476
477    public void SetXAxis(string axisName) {
478      xAxisComboBox.SelectedItem = axisName;
479    }
480
481    public void SetYAxis(string axisName) {
482      yAxisComboBox.SelectedItem = axisName;
483    }
484  }
485}
Note: See TracBrowser for help on using the repository browser.