Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11007 was 11007, checked in by mkommend, 10 years ago

#2121: Fixed a bug in the bubble chart and boxplot view regarding filtered runs.

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