Free cookie consent management tool by TermsFeed Policy Generator

source: branches/StatisticalTesting/HeuristicLab.Analysis.Statistics/3.3/SampleSizeInfluenceView.cs @ 9742

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

#2031 added a rich text box dialog for displaying formatted help texts

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