Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SimulationCore/HeuristicLab.Optimization.Views/3.3/RunCollectionBoxPlotView.cs @ 6703

Last change on this file since 6703 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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