Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers.Views/3.3/StatisticalTestingView.cs @ 9320

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

#1886 some more improvements for the statistical testing view

File size: 9.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Analysis.AlgorithmBehavior.Analyzers;
28using HeuristicLab.Core.Views;
29using HeuristicLab.Data;
30using HeuristicLab.MainForm;
31using HeuristicLab.Optimization;
32
33namespace HeuristicLab.Analysis.AlgorithmBehavior.Views {
34  [View("Statistical Testing View")]
35  [Content(typeof(RunCollection), false)]
36  public sealed partial class StatisticalTestingView : ItemView {
37    private double[][] data;
38
39    public StatisticalTestingView() {
40      InitializeComponent();
41    }
42
43    public new RunCollection Content {
44      get { return (RunCollection)base.Content; }
45      set { base.Content = value; }
46    }
47
48    public override bool ReadOnly {
49      get { return true; }
50      set { /*not needed because results are always readonly */}
51    }
52
53    protected override void OnContentChanged() {
54      base.OnContentChanged();
55
56      if (Content != null) {
57        UpdateResultComboBox();
58        UpdateGroupsComboBox();
59        RebuildDataTable();
60      }
61    }
62
63    #region events
64    protected override void RegisterContentEvents() {
65      base.RegisterContentEvents();
66    }
67
68    protected override void DeregisterContentEvents() {
69      base.DeregisterContentEvents();
70    }
71    #endregion
72
73    private void UpdateGroupsComboBox() {
74      groupComboBox.Items.Clear();
75
76      var parameters = (from run in Content
77                        where run.Visible
78                        from param in run.Parameters
79                        select param.Key).Distinct().ToArray();
80
81      foreach (var p in parameters) {
82        var variations = (from run in Content
83                          where run.Visible && run.Parameters.ContainsKey(p) &&
84                          (run.Parameters[p] is IntValue || run.Parameters[p] is DoubleValue ||
85                          run.Parameters[p] is StringValue || run.Parameters[p] is BoolValue)
86                          select ((dynamic)run.Parameters[p]).Value).Distinct();
87
88        if (variations.Count() > 1) {
89          groupComboBox.Items.Add(p);
90        }
91      }
92
93      if (groupComboBox.Items.Count > 0) {
94        //try to select something different than "Seed" or "Algorithm Name" as this makes no sense
95        //and takes a long time to group
96        List<int> possibleIndizes = new List<int>();
97        for (int i = 0; i < groupComboBox.Items.Count; i++) {
98          if (groupComboBox.Items[i].ToString() != "Seed"
99            && groupComboBox.Items[i].ToString() != "Algorithm Name") {
100            possibleIndizes.Add(i);
101          }
102        }
103
104        if (possibleIndizes.Count > 0) {
105          groupComboBox.SelectedItem = groupComboBox.Items[possibleIndizes.First()];
106        } else {
107          groupComboBox.SelectedItem = groupComboBox.Items[0];
108        }
109      }
110    }
111
112    private string[] GetColumnNames(IEnumerable<IRun> runs) {
113      string parameterName = (string)groupComboBox.SelectedItem;
114      var r = runs.Where(x => x.Parameters.ContainsKey(parameterName));
115      return r.Select(x => ((dynamic)x.Parameters[parameterName]).Value).Distinct().Select(x => (string)x.ToString()).ToArray();
116    }
117
118    private void UpdateResultComboBox() {
119      resultComboBox.Items.Clear();
120      var results = (from run in Content
121                     where run.Visible
122                     from result in run.Results
123                     where result.Value is IntValue || result.Value is DoubleValue
124                     select result.Key).Distinct().ToArray();
125
126      resultComboBox.Items.AddRange(results);
127      if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
128    }
129
130    private void RebuildDataTable() {
131      string parameterName = (string)groupComboBox.SelectedItem;
132      if (parameterName != null) {
133        string resultName = (string)resultComboBox.SelectedItem;
134
135        var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
136        var columnNames = GetColumnNames(runs);
137        var groups = GetGroups(columnNames, runs);
138        data = new double[columnNames.Count()][];
139
140        DoubleMatrix dt = new DoubleMatrix(groups.Select(x => x.Count()).Max(), columnNames.Count());
141        dt.ColumnNames = columnNames;
142
143        int i = 0;
144        int j = 0;
145        foreach (string columnName in columnNames) {
146          j = 0;
147          data[i] = new double[groups[i].Count()];
148          foreach (IRun run in groups[i]) {
149            dt[j, i] = (double)((dynamic)run.Results[resultName]).Value;
150            data[i][j] = dt[j, i];
151            j++;
152          }
153          i++;
154        }
155
156        stringConvertibleMatrixView.Content = dt;
157      }
158    }
159
160    private List<IEnumerable<IRun>> GetGroups(string[] columnNames, IEnumerable<IRun> runs) {
161      List<IEnumerable<IRun>> runCols = new List<IEnumerable<IRun>>();
162      string parameterName = (string)groupComboBox.SelectedItem;
163
164      foreach (string cn in columnNames) {
165        var tmpRuns = runs.Where(x => ((string)((dynamic)x.Parameters[parameterName]).Value.ToString()) == cn);
166        runCols.Add(tmpRuns);
167      }
168
169      return runCols;
170    }
171
172    private void testButton_Click(object sender, EventArgs e) {
173      double pval = KruskalWallis.Test(data);
174      pValTextBox.Text = pval.ToString();
175    }
176
177    private void normalDistButton_Click(object sender, EventArgs e) {
178      double val;
179      List<double> res = new List<double>();
180
181      for (int i = 0; i < data.Length; i++) {
182        alglib.jarqueberatest(data[i], data[i].Length, out val);
183        res.Add(val);
184      }
185
186      for (int i = 0; i < res.Count(); i++) {
187        if (res[i] < 0.1) {
188          stringConvertibleMatrixView.DataGridView.Columns[i].DefaultCellStyle.ForeColor = Color.Red;
189        } else {
190          stringConvertibleMatrixView.DataGridView.Columns[i].DefaultCellStyle.ForeColor = Color.Green;
191        }
192      }
193    }
194
195    private void resultComboBox_SelectedValueChanged(object sender, EventArgs e) {
196      RebuildDataTable();
197      pValTextBox.Text = string.Empty;
198    }
199
200    private void groupComboBox_SelectedValueChanged(object sender, EventArgs e) {
201      RebuildDataTable();
202      pValTextBox.Text = string.Empty;
203    }
204
205    private void normalityDetails_Click(object sender, EventArgs e) {
206      DoubleMatrix pValsMatrix = new DoubleMatrix(1, stringConvertibleMatrixView.Content.Columns);
207      pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
208      pValsMatrix.RowNames = new string[] { "p-Values" };
209
210      double val;
211      for (int i = 0; i < data.Length; i++) {
212        alglib.jarqueberatest(data[i], data[i].Length, out val);
213        pValsMatrix[0, i] = val;
214      }
215
216      MainFormManager.MainForm.ShowContent(pValsMatrix);
217    }
218
219    private void pairwiseTestButton_Click(object sender, EventArgs e) {
220      var selectedCells = stringConvertibleMatrixView.DataGridView.SelectedCells;
221      if (selectedCells.Count < 1) {
222        MessageBox.Show("Please selected one cell/column for pairwise comparision. ", "HeuristicLab", MessageBoxButtons.OK, MessageBoxIcon.Error);
223        return;
224      }
225
226      int colIndex = selectedCells[0].ColumnIndex;
227      foreach (DataGridViewCell selC in selectedCells) {
228        if (colIndex != selC.ColumnIndex) {
229          MessageBox.Show("Please selected only one column for pairwise comparision. ", "HeuristicLab", MessageBoxButtons.OK, MessageBoxIcon.Error);
230          return;
231        }
232      }
233
234      DoubleMatrix pValsMatrix = new DoubleMatrix(1, stringConvertibleMatrixView.Content.Columns);
235      pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
236      pValsMatrix.RowNames = new string[] { "p-Values" };
237
238      double bothtails;
239      double lefttail;
240      double righttail;
241      for (int i = 0; i < data.Length; i++) {
242        alglib.mannwhitneyutest(data[colIndex], data[colIndex].Length, data[i], data[i].Length, out bothtails, out lefttail, out righttail);
243        pValsMatrix[0, i] = bothtails;
244      }
245
246      MainFormManager.MainForm.ShowContent(pValsMatrix);
247    }
248
249    private void infoLabel_DoubleClick(object sender, EventArgs e) {
250      using (TextDialog dialog = new TextDialog("Description of statistical tests", toolTip1.GetToolTip(this.infoLabel), true)) {
251        dialog.ShowDialog(this);
252      }
253    }
254  }
255}
Note: See TracBrowser for help on using the repository browser.