Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionBoxPlotView.cs @ 10951

Last change on this file since 10951 was 9929, checked in by ascheibe, 11 years ago

#2099 merged r9910 into stable branch

File size: 18.8 KB
RevLine 
[4094]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4094]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;
[4652]27using HeuristicLab.Common;
[4094]28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.MainForm;
31using HeuristicLab.MainForm.WindowsForms;
32
33namespace HeuristicLab.Optimization.Views {
[9929]34  [View("Box Plot")]
[4094]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
[4883]41    private bool suppressUpdates = false;
[4094]42    private string xAxisValue;
43    private string yAxisValue;
44    private Dictionary<int, Dictionary<object, double>> categoricalMapping;
[4211]45    private SortedDictionary<double, Series> seriesCache;
[4094]46
47    public RunCollectionBoxPlotView() {
48      InitializeComponent();
[4883]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();
[6638]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;
[4094]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);
[4888]76      Content.UpdateOfRunsInProgressChanged += new EventHandler(Content_UpdateOfRunsInProgressChanged);
[8962]77      Content.OptimizerNameChanged += new EventHandler(Content_AlgorithmNameChanged);
[4094]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);
[4888]87      Content.UpdateOfRunsInProgressChanged -= new EventHandler(Content_UpdateOfRunsInProgressChanged);
[8962]88      Content.OptimizerNameChanged -= new EventHandler(Content_AlgorithmNameChanged);
[4094]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    }
[4888]111    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
[4883]112      if (InvokeRequired)
[4888]113        Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
[4883]114      else {
[4888]115        suppressUpdates = Content.UpdateOfRunsInProgress;
[4883]116        if (!suppressUpdates) UpdateDataPoints();
117      }
118    }
[4094]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();
[9235]126        UpdateAxisLabels();
[4094]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);
[4883]139      else if (!suppressUpdates) {
[4094]140        UpdateDataPoints();
141      }
142    }
[8738]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    }
[4094]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();
[8738]157      UpdateCaption();
[4094]158    }
159
[8738]160    private void UpdateCaption() {
[9929]161      Caption = Content != null ? Content.OptimizerName + " Box Plot" : ViewAttribute.GetViewName(GetType());
[8738]162    }
163
[4094]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
[4652]199        UpdateStatistics();
[4094]200        if (seriesCache.Count > 0) {
201          Series boxPlotSeries = CreateBoxPlotSeries();
202          this.chart.Series.Add(boxPlotSeries);
203        }
204
205        UpdateAxisLabels();
206      }
207      UpdateNoRunsVisibleLabel();
208    }
209
[4652]210    private void UpdateStatistics() {
[4768]211      DoubleMatrix matrix = new DoubleMatrix(9, seriesCache.Count);
[4652]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;
[4768]236      matrix.RowNames = new string[] { "Count", "Minimum", "Maximum", "Average", "Median", "Standard Deviation", "Variance", "25th Percentile", "75th Percentile" };
[4652]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();
[4721]241        matrix[0, i] = seriesValues.Length;
[4768]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);
[4652]250      }
251      statisticsMatrixView.Content = matrix;
252    }
253
[4094]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);
[4652]290        point.Tag = run;
[4094]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) {
[9235]328      if (!this.categoricalMapping.ContainsKey(dimension)) {
[4094]329        this.categoricalMapping[dimension] = new Dictionary<object, double>();
[9435]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());
[9235]332        int count = 1;
333        foreach (var category in orderedCategories) {
334          this.categoricalMapping[dimension].Add(category, count);
335          count++;
336        }
[4094]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() {
[4883]357      if (this.chart.Series.Count > 0) {
[4094]358        noRunsLabel.Visible = false;
[4888]359        showStatisticsCheckBox.Enabled = true;
[4883]360        splitContainer.Panel2Collapsed = !showStatisticsCheckBox.Checked;
361      } else {
[4094]362        noRunsLabel.Visible = true;
[4888]363        showStatisticsCheckBox.Enabled = false;
[4883]364        splitContainer.Panel2Collapsed = true;
365      }
[4094]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);
[4652]377      if (xAxisComboBox.SelectedItem != null)
378        xAxis.Title = xAxisComboBox.SelectedItem.ToString();
379      if (yAxisComboBox.SelectedItem != null)
380        yAxis.Title = yAxisComboBox.SelectedItem.ToString();
[4094]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)) {
[9435]390        int position = 1;
391        foreach (var pair in categoricalMapping[dimension].Where(x => seriesCache.ContainsKey(x.Value))) {
[4094]392          string labelText = pair.Key.ToString();
[4211]393          CustomLabel label = new CustomLabel();
394          label.ToolTip = labelText;
[4094]395          if (labelText.Length > 25)
396            labelText = labelText.Substring(0, 25) + " ... ";
[4211]397          label.Text = labelText;
[4094]398          label.GridTicks = GridTickTypes.TickMark;
[9435]399          label.FromPosition = position - 0.5;
400          label.ToPosition = position + 0.5;
[4211]401          axis.CustomLabels.Add(label);
[9435]402          position++;
[4094]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        }
[4211]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        }
[4094]428      }
429    }
[4652]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    }
[4094]443    #endregion
[4652]444
[4721]445    private void showStatisticsCheckBox_CheckedChanged(object sender, EventArgs e) {
446      splitContainer.Panel2Collapsed = !showStatisticsCheckBox.Checked;
447    }
448
[4094]449  }
450}
Note: See TracBrowser for help on using the repository browser.