Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1721-RandomForestPersistence/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionBoxPlotView.cs @ 10306

Last change on this file since 10306 was 9910, checked in by ascheibe, 11 years ago

#2099 renamed run collection views

File size: 18.8 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        this.categoricalMapping.Clear();
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_Changed(object sender, EventArgs e) {
137      if (InvokeRequired)
138        this.Invoke(new EventHandler(run_Changed), sender, e);
139      else if (!suppressUpdates) {
140        UpdateDataPoints();
141      }
142    }
143
144    private void Content_AlgorithmNameChanged(object sender, EventArgs e) {
145      if (InvokeRequired)
146        Invoke(new EventHandler(Content_AlgorithmNameChanged), sender, e);
147      else UpdateCaption();
148    }
149    #endregion
150
151    #region update comboboxes, datapoints, runs
152    protected override void OnContentChanged() {
153      base.OnContentChanged();
154      this.categoricalMapping.Clear();
155      UpdateComboBoxes();
156      UpdateDataPoints();
157      UpdateCaption();
158    }
159
160    private void UpdateCaption() {
161      Caption = Content != null ? Content.OptimizerName + " Box Plot" : ViewAttribute.GetViewName(GetType());
162    }
163
164    private void UpdateComboBoxes() {
165      string selectedXAxis = (string)this.xAxisComboBox.SelectedItem;
166      string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
167      this.xAxisComboBox.Items.Clear();
168      this.yAxisComboBox.Items.Clear();
169      if (Content != null) {
170        string[] additionalAxisDimension = Enum.GetNames(typeof(AxisDimension));
171        this.xAxisComboBox.Items.AddRange(additionalAxisDimension);
172        this.xAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
173        this.yAxisComboBox.Items.AddRange(additionalAxisDimension);
174        this.yAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
175
176        bool changed = false;
177        if (selectedXAxis != null && xAxisComboBox.Items.Contains(selectedXAxis)) {
178          xAxisComboBox.SelectedItem = selectedXAxis;
179          changed = true;
180        }
181        if (selectedYAxis != null && yAxisComboBox.Items.Contains(selectedYAxis)) {
182          yAxisComboBox.SelectedItem = selectedYAxis;
183          changed = true;
184        }
185        if (changed)
186          UpdateDataPoints();
187      }
188    }
189
190    private void UpdateDataPoints() {
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", "Average", "Median", "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.Average();
245        matrix[4, i] = seriesValues.Median();
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      return this.categoricalMapping[dimension][value];
339    }
340    private double GetValue(IRun run, AxisDimension axisDimension) {
341      double value = double.NaN;
342      switch (axisDimension) {
343        case AxisDimension.Color: {
344            value = GetCategoricalValue(-1, run.Color.ToString());
345            break;
346          }
347        default: {
348            throw new ArgumentException("No handling strategy for " + axisDimension.ToString() + " is defined.");
349          }
350      }
351      return value;
352    }
353    #endregion
354
355    #region GUI events
356    private void UpdateNoRunsVisibleLabel() {
357      if (this.chart.Series.Count > 0) {
358        noRunsLabel.Visible = false;
359        showStatisticsCheckBox.Enabled = true;
360        splitContainer.Panel2Collapsed = !showStatisticsCheckBox.Checked;
361      } else {
362        noRunsLabel.Visible = true;
363        showStatisticsCheckBox.Enabled = false;
364        splitContainer.Panel2Collapsed = true;
365      }
366    }
367
368    private void AxisComboBox_SelectedIndexChanged(object sender, EventArgs e) {
369      UpdateDataPoints();
370    }
371    private void UpdateAxisLabels() {
372      Axis xAxis = this.chart.ChartAreas[BoxPlotChartAreaName].AxisX;
373      Axis yAxis = this.chart.ChartAreas[BoxPlotChartAreaName].AxisY;
374      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
375      SetCustomAxisLabels(xAxis, xAxisComboBox.SelectedIndex - axisDimensionCount);
376      SetCustomAxisLabels(yAxis, yAxisComboBox.SelectedIndex - axisDimensionCount);
377      if (xAxisComboBox.SelectedItem != null)
378        xAxis.Title = xAxisComboBox.SelectedItem.ToString();
379      if (yAxisComboBox.SelectedItem != null)
380        yAxis.Title = yAxisComboBox.SelectedItem.ToString();
381    }
382
383    private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
384      this.UpdateAxisLabels();
385    }
386
387    private void SetCustomAxisLabels(Axis axis, int dimension) {
388      axis.CustomLabels.Clear();
389      if (categoricalMapping.ContainsKey(dimension)) {
390        int position = 1;
391        foreach (var pair in categoricalMapping[dimension].Where(x => seriesCache.ContainsKey(x.Value))) {
392          string labelText = pair.Key.ToString();
393          CustomLabel label = new CustomLabel();
394          label.ToolTip = labelText;
395          if (labelText.Length > 25)
396            labelText = labelText.Substring(0, 25) + " ... ";
397          label.Text = labelText;
398          label.GridTicks = GridTickTypes.TickMark;
399          label.FromPosition = position - 0.5;
400          label.ToPosition = position + 0.5;
401          axis.CustomLabels.Add(label);
402          position++;
403        }
404      } else if (dimension > 0 && Content.GetValue(0, dimension) is TimeSpanValue) {
405        this.chart.ChartAreas[0].RecalculateAxesScale();
406        Axis correspondingAxis = this.chart.ChartAreas[0].Axes.Where(x => x.Name == axis.Name).SingleOrDefault();
407        if (correspondingAxis == null)
408          correspondingAxis = axis;
409        for (double i = correspondingAxis.Minimum; i <= correspondingAxis.Maximum; i += correspondingAxis.LabelStyle.Interval) {
410          TimeSpan time = TimeSpan.FromSeconds(i);
411          string x = string.Format("{0:00}:{1:00}:{2:00}", (int)time.Hours, time.Minutes, time.Seconds);
412          axis.CustomLabels.Add(i - correspondingAxis.LabelStyle.Interval / 2, i + correspondingAxis.LabelStyle.Interval / 2, x);
413        }
414      } else if (chart.ChartAreas[BoxPlotChartAreaName].AxisX == axis) {
415        double position = 1.0;
416        foreach (Series series in chart.Series) {
417          if (series.Name != BoxPlotSeriesName) {
418            string labelText = series.Points[0].XValue.ToString();
419            CustomLabel label = new CustomLabel();
420            label.FromPosition = position - 0.5;
421            label.ToPosition = position + 0.5;
422            label.GridTicks = GridTickTypes.TickMark;
423            label.Text = labelText;
424            axis.CustomLabels.Add(label);
425            position++;
426          }
427        }
428      }
429    }
430
431    private void chart_MouseMove(object sender, MouseEventArgs e) {
432      string newTooltipText = string.Empty;
433      string oldTooltipText;
434      HitTestResult h = this.chart.HitTest(e.X, e.Y);
435      if (h.ChartElementType == ChartElementType.AxisLabels) {
436        newTooltipText = ((CustomLabel)h.Object).ToolTip;
437      }
438
439      oldTooltipText = this.tooltip.GetToolTip(chart);
440      if (newTooltipText != oldTooltipText)
441        this.tooltip.SetToolTip(chart, newTooltipText);
442    }
443    #endregion
444
445    private void showStatisticsCheckBox_CheckedChanged(object sender, EventArgs e) {
446      splitContainer.Panel2Collapsed = !showStatisticsCheckBox.Checked;
447    }
448
449  }
450}
Note: See TracBrowser for help on using the repository browser.