Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2031 added confidence intervals to sample size influence view

File size: 21.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;
32using HeuristicLab.Optimization;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Analysis.Statistics {
36  [View("Sample Size Influence")]
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 Influence" : 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[r]);
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(11, 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", "Lower Confidence", "Upper Confidence" };
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        matrix[9, i] = seriesValues.ConfidenceIntervals(0.95).Item1;
314        matrix[10, i] = seriesValues.ConfidenceIntervals(0.95).Item2;
315      }
316      statisticsMatrixView.Content = matrix;
317    }
318
319    private Series CreateBoxPlotSeries() {
320      Series boxPlotSeries = new Series(BoxPlotSeriesName);
321      string seriesNames = string.Concat(seriesCache.Keys.Select(x => x.ToString() + ";").ToArray());
322      seriesNames = seriesNames.Remove(seriesNames.Length - 1); //delete last ; from string
323
324      boxPlotSeries.ChartArea = BoxPlotChartAreaName;
325      boxPlotSeries.ChartType = SeriesChartType.BoxPlot;
326      boxPlotSeries["BoxPlotSeries"] = seriesNames;
327      boxPlotSeries["BoxPlotShowUnusualValues"] = "true";
328      boxPlotSeries["PointWidth"] = "0.4";
329      boxPlotSeries.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.VerticalCenter;
330      boxPlotSeries.BackSecondaryColor = System.Drawing.Color.FromArgb(130, 224, 64, 10);
331      boxPlotSeries.BorderColor = System.Drawing.Color.FromArgb(64, 64, 64);
332      boxPlotSeries.Color = System.Drawing.Color.FromArgb(224, 64, 10);
333
334      return boxPlotSeries;
335    }
336
337    private void AddDataPoint(IRun run, int idx) {
338      double xValue;
339      double? yValue;
340
341
342      this.xAxisValue = xAxisComboBox.Text;
343      if (!yAxisComboBox.DroppedDown)
344        this.yAxisValue = (string)yAxisComboBox.SelectedItem;
345
346      xValue = idx;
347      yValue = GetValue(run, this.yAxisValue);
348
349      if (yValue.HasValue) {
350        if (!this.seriesCache.ContainsKey(xValue))
351          seriesCache[xValue] = new Series(xValue.ToString());
352
353        Series series = seriesCache[xValue];
354        DataPoint point = new DataPoint(xValue, yValue.Value);
355        point.Tag = run;
356        series.Points.Add(point);
357      }
358    }
359    #endregion
360
361    #region get values from run
362    private double? GetValue(IRun run, string columnName) {
363      if (run == null || string.IsNullOrEmpty(columnName))
364        return null;
365
366      if (Enum.IsDefined(typeof(AxisDimension), columnName)) {
367        AxisDimension axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), columnName);
368        return GetValue(run, axisDimension);
369      } else {
370        int columnIndex = Matrix.ColumnNames.ToList().IndexOf(columnName);
371        IItem value = Content.GetValue(run, columnIndex);
372        if (value == null)
373          return null;
374
375        DoubleValue doubleValue = value as DoubleValue;
376        IntValue intValue = value as IntValue;
377        TimeSpanValue timeSpanValue = value as TimeSpanValue;
378        double? ret = null;
379        if (doubleValue != null) {
380          if (!double.IsNaN(doubleValue.Value) && !double.IsInfinity(doubleValue.Value))
381            ret = doubleValue.Value;
382        } else if (intValue != null)
383          ret = intValue.Value;
384        else if (timeSpanValue != null) {
385          ret = timeSpanValue.Value.TotalSeconds;
386        } else
387          ret = GetCategoricalValue(columnIndex, value.ToString());
388
389        return ret;
390      }
391    }
392    private double GetCategoricalValue(int dimension, string value) {
393      if (!this.categoricalMapping.ContainsKey(dimension)) {
394        this.categoricalMapping[dimension] = new Dictionary<object, double>();
395        var orderedCategories = Content.Where(r => r.Visible && Content.GetValue(r, dimension) != null).Select(r => Content.GetValue(r, dimension).ToString())
396
397                                       .Distinct().OrderBy(x => x, new NaturalStringComparer());
398        int count = 1;
399        foreach (var category in orderedCategories) {
400          this.categoricalMapping[dimension].Add(category, count);
401          count++;
402        }
403      }
404      return this.categoricalMapping[dimension][value];
405    }
406    private double GetValue(IRun run, AxisDimension axisDimension) {
407      double value = double.NaN;
408      switch (axisDimension) {
409        case AxisDimension.Color: {
410            value = GetCategoricalValue(-1, run.Color.ToString());
411            break;
412          }
413        default: {
414            throw new ArgumentException("No handling strategy for " + axisDimension.ToString() + " is defined.");
415          }
416      }
417      return value;
418    }
419    #endregion
420
421    #region GUI events
422    private void UpdateNoRunsVisibleLabel() {
423      if (this.chart.Series.Count > 0) {
424        noRunsLabel.Visible = false;
425        showStatisticsCheckBox.Enabled = true;
426        splitContainer.Panel2Collapsed = !showStatisticsCheckBox.Checked;
427      } else {
428        noRunsLabel.Visible = true;
429        showStatisticsCheckBox.Enabled = false;
430        splitContainer.Panel2Collapsed = true;
431      }
432    }
433
434    private void AxisComboBox_SelectedIndexChanged(object sender, EventArgs e) {
435      UpdateSampleSizes();
436      UpdateDataPoints();
437    }
438    private void UpdateAxisLabels() {
439      Axis xAxis = this.chart.ChartAreas[BoxPlotChartAreaName].AxisX;
440      Axis yAxis = this.chart.ChartAreas[BoxPlotChartAreaName].AxisY;
441      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
442
443      SetCustomAxisLabels(xAxis, -1);
444      SetCustomAxisLabels(yAxis, yAxisComboBox.SelectedIndex - axisDimensionCount);
445
446      xAxis.Title = "Group Size";
447      if (yAxisComboBox.SelectedItem != null)
448        yAxis.Title = yAxisComboBox.SelectedItem.ToString();
449    }
450
451    private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
452      this.UpdateAxisLabels();
453    }
454
455    private void SetCustomAxisLabels(Axis axis, int dimension) {
456      axis.CustomLabels.Clear();
457      if (categoricalMapping.ContainsKey(dimension)) {
458        int position = 1;
459        foreach (var pair in categoricalMapping[dimension].Where(x => seriesCache.ContainsKey(x.Value))) {
460          string labelText = pair.Key.ToString();
461          CustomLabel label = new CustomLabel();
462          label.ToolTip = labelText;
463          if (labelText.Length > 25)
464            labelText = labelText.Substring(0, 25) + " ... ";
465          label.Text = labelText;
466          label.GridTicks = GridTickTypes.TickMark;
467          label.FromPosition = position - 0.5;
468          label.ToPosition = position + 0.5;
469          axis.CustomLabels.Add(label);
470          position++;
471        }
472      } else if (dimension > 0 && Content.GetValue(0, dimension) is TimeSpanValue) {
473        this.chart.ChartAreas[0].RecalculateAxesScale();
474        Axis correspondingAxis = this.chart.ChartAreas[0].Axes.Where(x => x.Name == axis.Name).SingleOrDefault();
475        if (correspondingAxis == null)
476          correspondingAxis = axis;
477        for (double i = correspondingAxis.Minimum; i <= correspondingAxis.Maximum; i += correspondingAxis.LabelStyle.Interval) {
478          TimeSpan time = TimeSpan.FromSeconds(i);
479          string x = string.Format("{0:00}:{1:00}:{2:00}", (int)time.Hours, time.Minutes, time.Seconds);
480          axis.CustomLabels.Add(i - correspondingAxis.LabelStyle.Interval / 2, i + correspondingAxis.LabelStyle.Interval / 2, x);
481        }
482      } else if (chart.ChartAreas[BoxPlotChartAreaName].AxisX == axis) {
483        double position = 1.0;
484        foreach (Series series in chart.Series) {
485          if (series.Name != BoxPlotSeriesName) {
486            string labelText = series.Points[0].XValue.ToString();
487            CustomLabel label = new CustomLabel();
488            label.FromPosition = position - 0.5;
489            label.ToPosition = position + 0.5;
490            label.GridTicks = GridTickTypes.TickMark;
491            label.Text = labelText;
492            axis.CustomLabels.Add(label);
493            position++;
494          }
495        }
496      }
497    }
498
499    private void chart_MouseMove(object sender, MouseEventArgs e) {
500      string newTooltipText = string.Empty;
501      string oldTooltipText;
502      HitTestResult h = this.chart.HitTest(e.X, e.Y);
503      if (h.ChartElementType == ChartElementType.AxisLabels) {
504        newTooltipText = ((CustomLabel)h.Object).ToolTip;
505      }
506
507      oldTooltipText = this.tooltip.GetToolTip(chart);
508      if (newTooltipText != oldTooltipText)
509        this.tooltip.SetToolTip(chart, newTooltipText);
510    }
511    #endregion
512
513    private void showStatisticsCheckBox_CheckedChanged(object sender, EventArgs e) {
514      splitContainer.Panel2Collapsed = !showStatisticsCheckBox.Checked;
515    }
516
517    private void defineSampleSizeButton_Click(object sender, EventArgs e) {
518      int min = 0, max = 0, step = 1;
519      var groupSizes = ParseGroupSizesFromText(xAxisComboBox.Text);
520      if (groupSizes.Count() > 0) {
521        min = groupSizes.Min();
522        max = groupSizes.Max();
523      }
524
525      using (var dialog = new DefineArithmeticProgressionDialog(true, min, max, step)) {
526        if (dialog.ShowDialog(this) == DialogResult.OK) {
527          var values = dialog.Values;
528          string newVals = "";
529          foreach (int v in values) {
530            newVals += v + "; ";
531          }
532          xAxisComboBox.Text = newVals;
533        }
534      }
535    }
536
537    private void xAxisComboBox_TextChanged(object sender, EventArgs e) {
538      var result = ParseGroupSizesFromText(xAxisComboBox.Text, false);
539
540      if (seriesCache.Count() == result.Count()) {
541        bool changed = false;
542        int i = 0;
543        foreach (var gs in seriesCache.Keys) {
544          if (((int)gs) != result[i]) {
545            changed = true;
546            break;
547          }
548          i++;
549        }
550
551        if (changed) {
552          UpdateDataPoints();
553        }
554      } else {
555        UpdateDataPoints();
556      }
557    }
558  }
559}
Note: See TracBrowser for help on using the repository browser.