Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12787 was 12787, checked in by mkommend, 9 years ago

#2436: Fixed exception in BoxplotView when color is selected as category.

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