Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 improved view and added documentation

File size: 8.5 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                        from param in run.Parameters
78                        select param.Key).Distinct().ToArray();
79
80      foreach (var p in parameters) {
81        var variations = (from run in Content
82                          where run.Parameters.ContainsKey(p) &&
83                          (run.Parameters[p] is IntValue || run.Parameters[p] is DoubleValue ||
84                          run.Parameters[p] is StringValue || run.Parameters[p] is BoolValue)
85                          select ((dynamic)run.Parameters[p]).Value).Distinct();
86
87        if (variations.Count() > 1) {
88          groupComboBox.Items.Add(p);
89        }
90      }
91      if (groupComboBox.Items.Count > 0) groupComboBox.SelectedItem = groupComboBox.Items[0];
92    }
93
94    private string[] GetColumnNames(IEnumerable<IRun> runs) {
95      string parameterName = (string)groupComboBox.SelectedItem;
96      var r = runs.Where(x => x.Parameters.ContainsKey(parameterName));
97      return r.Select(x => ((dynamic)x.Parameters[parameterName]).Value).Distinct().Select(x => (string)x.ToString()).ToArray();
98    }
99
100    private void UpdateResultComboBox() {
101      resultComboBox.Items.Clear();
102      var results = (from run in Content
103                     from result in run.Results
104                     where result.Value is IntValue || result.Value is DoubleValue
105                     select result.Key).Distinct().ToArray();
106
107      resultComboBox.Items.AddRange(results);
108      if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
109    }
110
111    private void RebuildDataTable() {
112      string parameterName = (string)groupComboBox.SelectedItem;
113      if (parameterName != null) {
114        string resultName = (string)resultComboBox.SelectedItem;
115
116        var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
117        var columnNames = GetColumnNames(runs);
118        var groups = GetGroups(columnNames, runs);
119        data = new double[columnNames.Count()][];
120
121        DoubleMatrix dt = new DoubleMatrix(groups.Select(x => x.Count()).Max(), columnNames.Count());
122        dt.ColumnNames = columnNames;
123
124        int i = 0;
125        int j = 0;
126        foreach (string columnName in columnNames) {
127          j = 0;
128          data[i] = new double[groups[i].Count()];
129          foreach (IRun run in groups[i]) {
130            dt[j, i] = (double)((dynamic)run.Results[resultName]).Value;
131            data[i][j] = dt[j, i];
132            j++;
133          }
134          i++;
135        }
136
137        stringConvertibleMatrixView.Content = dt;
138      }
139    }
140
141    private List<RunCollection> GetGroups(string[] columnNames, IEnumerable<IRun> runs) {
142      List<RunCollection> runCols = new List<RunCollection>();
143      string parameterName = (string)groupComboBox.SelectedItem;
144
145      foreach (string cn in columnNames) {
146        var tmpRuns = runs.Where(x => ((string)((dynamic)x.Parameters[parameterName]).Value.ToString()) == cn);
147        runCols.Add(new RunCollection(tmpRuns));
148      }
149
150      return runCols;
151    }
152
153    private void testButton_Click(object sender, EventArgs e) {
154      double pval = KruskalWallis.Test(data);
155      pValTextBox.Text = pval.ToString();
156    }
157
158    private void normalDistButton_Click(object sender, EventArgs e) {
159      double val;
160      List<double> res = new List<double>();
161
162      for (int i = 0; i < data.Length; i++) {
163        alglib.jarqueberatest(data[i], data[i].Length, out val);
164        res.Add(val);
165      }
166
167      for (int i = 0; i < res.Count(); i++) {
168        if (res[i] < 0.1) {
169          stringConvertibleMatrixView.DataGridView.Columns[i].DefaultCellStyle.ForeColor = Color.Red;
170        } else {
171          stringConvertibleMatrixView.DataGridView.Columns[i].DefaultCellStyle.ForeColor = Color.Green;
172        }
173      }
174    }
175
176    private void resultComboBox_SelectedValueChanged(object sender, EventArgs e) {
177      RebuildDataTable();
178      pValTextBox.Text = string.Empty;
179    }
180
181    private void groupComboBox_SelectedValueChanged(object sender, EventArgs e) {
182      RebuildDataTable();
183      pValTextBox.Text = string.Empty;
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.