Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 added test for normality and pairwise statistical testing

File size: 8.4 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
74    private void UpdateGroupsComboBox() {
75      groupComboBox.Items.Clear();
76
77      var parameters = (from run in Content
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.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      if (groupComboBox.Items.Count > 0) groupComboBox.SelectedItem = groupComboBox.Items[0];
93    }
94
95    private string[] GetColumnNames(IEnumerable<IRun> runs) {
96      string parameterName = (string)groupComboBox.SelectedItem;
97      var r = runs.Where(x => x.Parameters.ContainsKey(parameterName));
98      return r.Select(x => ((dynamic)x.Parameters[parameterName]).Value).Distinct().Select(x => (string)x.ToString()).ToArray();
99    }
100
101    private void UpdateResultComboBox() {
102      resultComboBox.Items.Clear();
103      var results = (from run in Content
104                     from result in run.Results
105                     where result.Value is IntValue || result.Value is DoubleValue
106                     select result.Key).Distinct().ToArray();
107
108      resultComboBox.Items.AddRange(results);
109      if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
110    }
111
112    private void RebuildDataTable() {
113      string parameterName = (string)groupComboBox.SelectedItem;
114      if (parameterName != null) {
115        string resultName = (string)resultComboBox.SelectedItem;
116
117        var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
118        var columnNames = GetColumnNames(runs);
119        var groups = GetGroups(columnNames, runs);
120        data = new double[columnNames.Count()][];
121
122        DoubleMatrix dt = new DoubleMatrix(groups.Select(x => x.Count()).Max(), columnNames.Count());
123        dt.ColumnNames = columnNames;
124
125        int i = 0;
126        int j = 0;
127        foreach (string columnName in columnNames) {
128          j = 0;
129          data[i] = new double[groups[i].Count()];
130          foreach (IRun run in groups[i]) {
131            dt[j, i] = (double)((dynamic)run.Results[resultName]).Value;
132            data[i][j] = dt[j, i];
133            j++;
134          }
135          i++;
136        }
137
138        stringConvertibleMatrixView.Content = dt;
139      }
140    }
141
142    private List<RunCollection> GetGroups(string[] columnNames, IEnumerable<IRun> runs) {
143      List<RunCollection> runCols = new List<RunCollection>();
144      string parameterName = (string)groupComboBox.SelectedItem;
145
146      foreach (string cn in columnNames) {
147        var tmpRuns = runs.Where(x => ((string)((dynamic)x.Parameters[parameterName]).Value.ToString()) == cn);
148        runCols.Add(new RunCollection(tmpRuns));
149      }
150
151      return runCols;
152    }
153
154    private void testButton_Click(object sender, EventArgs e) {
155      double pval = KruskalWallis.Test(data);
156      pValTextBox.Text = pval.ToString();
157    }
158
159
160    private void normalDistButton_Click(object sender, EventArgs e) {
161
162      double val;
163      List<double> res = new List<double>();
164      for (int i = 0; i < data.Length; i++) {
165        alglib.jarqueberatest(data[i], data[i].Length, out val);
166        res.Add(val);
167      }
168
169      for (int i = 0; i < res.Count(); i++) {
170        if (res[i] < 0.1) {
171          stringConvertibleMatrixView.DataGridView.Columns[i].DefaultCellStyle.ForeColor = Color.Red;
172        } else {
173          stringConvertibleMatrixView.DataGridView.Columns[i].DefaultCellStyle.ForeColor = Color.Green;
174        }
175      }
176    }
177
178    private void resultComboBox_SelectedValueChanged(object sender, EventArgs e) {
179      RebuildDataTable();
180    }
181
182    private void groupComboBox_SelectedValueChanged(object sender, EventArgs e) {
183      RebuildDataTable();
184    }
185
186    private void normalityDetails_Click(object sender, EventArgs e) {
187      DoubleMatrix pValsMatrix = new DoubleMatrix(1, stringConvertibleMatrixView.Content.Columns);
188      pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
189      pValsMatrix.RowNames = new string[] { "p-Values" };
190
191      double val;
192      for (int i = 0; i < data.Length; i++) {
193        alglib.jarqueberatest(data[i], data[i].Length, out val);
194        pValsMatrix[0, i] = val;
195      }
196
197      MainFormManager.MainForm.ShowContent(pValsMatrix);
198    }
199
200    private void pairwiseTestButton_Click(object sender, EventArgs e) {
201      var selectedCells = stringConvertibleMatrixView.DataGridView.SelectedCells;
202      if (selectedCells.Count < 1) {
203        MessageBox.Show("Please selected one cell/column for pairwise comparision. ", "HeuristicLab", MessageBoxButtons.OK, MessageBoxIcon.Error);
204        return;
205      }
206
207      int colIndex = selectedCells[0].ColumnIndex;
208      foreach (DataGridViewCell selC in selectedCells) {
209        if (colIndex != selC.ColumnIndex) {
210          MessageBox.Show("Please selected only one column for pairwise comparision. ", "HeuristicLab", MessageBoxButtons.OK, MessageBoxIcon.Error);
211          return;
212        }
213      }
214
215      DoubleMatrix pValsMatrix = new DoubleMatrix(1, stringConvertibleMatrixView.Content.Columns);
216      pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
217      pValsMatrix.RowNames = new string[] { "p-Values" };
218
219      double bothtails;
220      double lefttail;
221      double righttail;
222      for (int i = 0; i < data.Length; i++) {
223        alglib.mannwhitneyutest(data[colIndex], data[colIndex].Length, data[i], data[i].Length, out bothtails, out lefttail, out righttail);
224        pValsMatrix[0, i] = bothtails;
225      }
226
227      MainFormManager.MainForm.ShowContent(pValsMatrix);
228    }
229
230    private void infoLabel_DoubleClick(object sender, EventArgs e) {
231      using (TextDialog dialog = new TextDialog("Description of statistical tests", toolTip1.GetToolTip(this.infoLabel), true)) {
232        dialog.ShowDialog(this);
233      }
234    }
235  }
236}
Note: See TracBrowser for help on using the repository browser.