Free cookie consent management tool by TermsFeed Policy Generator

source: branches/StatisticalTesting/HeuristicLab.Analysis.Statistics/3.3/StatisticalTestingView.cs @ 9749

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

#2031

  • added documentation for chart analysis view
  • some ui improvements
File size: 12.2 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.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Core.Views;
28using HeuristicLab.Data;
29using HeuristicLab.MainForm;
30using HeuristicLab.Optimization;
31using HeuristicLab.Optimization.Views;
32
33namespace HeuristicLab.Analysis.Statistics {
34  [View("RunCollection Statistical Testing")]
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        FillCompComboBox();
60        RebuildDataTable();
61      }
62    }
63
64    #region events
65    protected override void RegisterContentEvents() {
66      base.RegisterContentEvents();
67    }
68
69    protected override void DeregisterContentEvents() {
70      base.DeregisterContentEvents();
71    }
72    #endregion
73
74    private void UpdateGroupsComboBox() {
75      groupComboBox.Items.Clear();
76
77      var parameters = (from run in Content
78                        where run.Visible
79                        from param in run.Parameters
80                        select param.Key).Distinct().ToArray();
81
82      foreach (var p in parameters) {
83        var variations = (from run in Content
84                          where run.Visible && run.Parameters.ContainsKey(p) &&
85                          (run.Parameters[p] is IntValue || run.Parameters[p] is DoubleValue ||
86                          run.Parameters[p] is StringValue || run.Parameters[p] is BoolValue)
87                          select ((dynamic)run.Parameters[p]).Value).Distinct();
88
89        if (variations.Count() > 1) {
90          groupComboBox.Items.Add(p);
91        }
92      }
93
94      if (groupComboBox.Items.Count > 0) {
95        //try to select something different than "Seed" or "Algorithm Name" as this makes no sense
96        //and takes a long time to group
97        List<int> possibleIndizes = new List<int>();
98        for (int i = 0; i < groupComboBox.Items.Count; i++) {
99          if (groupComboBox.Items[i].ToString() != "Seed"
100            && groupComboBox.Items[i].ToString() != "Algorithm Name") {
101            possibleIndizes.Add(i);
102          }
103        }
104
105        if (possibleIndizes.Count > 0) {
106          groupComboBox.SelectedItem = groupComboBox.Items[possibleIndizes.First()];
107        } else {
108          groupComboBox.SelectedItem = groupComboBox.Items[0];
109        }
110      }
111    }
112
113    private string[] GetColumnNames(IEnumerable<IRun> runs) {
114      string parameterName = (string)groupComboBox.SelectedItem;
115      var r = runs.Where(x => x.Parameters.ContainsKey(parameterName));
116      return r.Select(x => ((dynamic)x.Parameters[parameterName]).Value).Distinct().Select(x => (string)x.ToString()).ToArray();
117    }
118
119    private void UpdateResultComboBox() {
120      resultComboBox.Items.Clear();
121      var results = (from run in Content
122                     where run.Visible
123                     from result in run.Results
124                     where result.Value is IntValue || result.Value is DoubleValue
125                     select result.Key).Distinct().ToArray();
126
127      resultComboBox.Items.AddRange(results);
128      if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
129    }
130
131    private void FillCompComboBox() {
132      string parameterName = (string)groupComboBox.SelectedItem;
133      if (parameterName != null) {
134        string resultName = (string)resultComboBox.SelectedItem;
135        if (resultName != null) {
136          var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
137          var columnNames = GetColumnNames(runs).ToList();
138          groupCompComboBox.Items.Clear();
139          columnNames.ForEach(x => groupCompComboBox.Items.Add(x));
140          if (groupCompComboBox.Items.Count > 0) groupCompComboBox.SelectedItem = groupCompComboBox.Items[0];
141        }
142      }
143    }
144
145    private void RebuildDataTable() {
146      string parameterName = (string)groupComboBox.SelectedItem;
147      if (parameterName != null) {
148        string resultName = (string)resultComboBox.SelectedItem;
149
150        var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
151        var columnNames = GetColumnNames(runs);
152        var groups = GetGroups(columnNames, runs);
153        data = new double[columnNames.Count()][];
154
155        DoubleMatrix dt = new DoubleMatrix(groups.Select(x => x.Count()).Max(), columnNames.Count());
156        dt.ColumnNames = columnNames;
157
158        int i = 0;
159        int j = 0;
160        foreach (string columnName in columnNames) {
161          j = 0;
162          data[i] = new double[groups[i].Count()];
163          foreach (IRun run in groups[i]) {
164            dt[j, i] = (double)((dynamic)run.Results[resultName]).Value;
165            data[i][j] = dt[j, i];
166            j++;
167          }
168          i++;
169        }
170
171        stringConvertibleMatrixView.Content = dt;
172      }
173    }
174
175    private List<IEnumerable<IRun>> GetGroups(string[] columnNames, IEnumerable<IRun> runs) {
176      List<IEnumerable<IRun>> runCols = new List<IEnumerable<IRun>>();
177      string parameterName = (string)groupComboBox.SelectedItem;
178
179      foreach (string cn in columnNames) {
180        var tmpRuns = runs.Where(x => ((string)((dynamic)x.Parameters[parameterName]).Value.ToString()) == cn);
181        runCols.Add(tmpRuns);
182      }
183
184      return runCols;
185    }
186
187    private void ResetUI() {
188      normalityLabel.Image = null;
189      groupCompLabel.Image = null;
190      pairwiseLabel.Image = null;
191      pValTextBox.Text = string.Empty;
192      equalDistsTextBox.Text = string.Empty;
193    }
194
195    private void testButton_Click(object sender, EventArgs e) {
196      double pval = KruskalWallis.Test(data);
197      pValTextBox.Text = pval.ToString();
198      if (pval < 0.05) {
199        groupCompLabel.Image = HeuristicLab.Analysis.Statistics.Resources.Default;
200      } else {
201        groupCompLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Warning;
202      }
203    }
204
205    private void normalDistButton_Click(object sender, EventArgs e) {
206      double val;
207      List<double> res = new List<double>();
208
209      for (int i = 0; i < data.Length; i++) {
210        alglib.jarqueberatest(data[i], data[i].Length, out val);
211        res.Add(val);
212      }
213
214      for (int i = 0; i < res.Count(); i++) {
215        if (res[i] < 0.1) {
216          stringConvertibleMatrixView.DataGridView.Columns[i].DefaultCellStyle.ForeColor = Color.Red;
217          normalityLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Warning;
218        } else {
219          stringConvertibleMatrixView.DataGridView.Columns[i].DefaultCellStyle.ForeColor = Color.Green;
220          normalityLabel.Image = HeuristicLab.Analysis.Statistics.Resources.Default;
221        }
222      }
223    }
224
225    private void resultComboBox_SelectedValueChanged(object sender, EventArgs e) {
226      RebuildDataTable();
227      ResetUI();
228    }
229
230    private void groupComboBox_SelectedValueChanged(object sender, EventArgs e) {
231      FillCompComboBox();
232      RebuildDataTable();
233      ResetUI();
234    }
235
236    private void normalityDetails_Click(object sender, EventArgs e) {
237      DoubleMatrix pValsMatrix = new DoubleMatrix(1, stringConvertibleMatrixView.Content.Columns);
238      pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
239      pValsMatrix.RowNames = new string[] { "p-Value" };
240
241      double val;
242      for (int i = 0; i < data.Length; i++) {
243        alglib.jarqueberatest(data[i], data[i].Length, out val);
244        pValsMatrix[0, i] = val;
245      }
246
247      MainFormManager.MainForm.ShowContent(pValsMatrix);
248    }
249
250    private void pairwiseTestButton_Click(object sender, EventArgs e) {
251      string curItem = (string)groupCompComboBox.SelectedItem;
252      int colIndex = 0;
253
254      foreach (string col in stringConvertibleMatrixView.Content.ColumnNames) {
255        if (col == curItem) {
256          break;
257        }
258        colIndex++;
259      }
260
261      DoubleMatrix pValsMatrix = new DoubleMatrix(5, stringConvertibleMatrixView.Content.Columns);
262      pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
263      pValsMatrix.RowNames = new string[] { "p-Value of MannWhitneyU", "p-Value of T-Test", "Necessary Sample Size for T-Test", "Cohen's d", "Hedges' g" };
264
265      double mwuBothtails;
266      double mwuLefttail;
267      double mwuRighttail;
268      double ttestLefttail;
269      for (int i = 0; i < data.Length; i++) {
270        alglib.mannwhitneyutest(data[colIndex], data[colIndex].Length, data[i], data[i].Length, out mwuBothtails, out mwuLefttail, out mwuRighttail);
271        ttestLefttail = TTest.Test(data[colIndex], data[i]);
272        pValsMatrix[0, i] = mwuBothtails;
273        pValsMatrix[1, i] = ttestLefttail;
274        pValsMatrix[2, i] = TTest.GetOptimalSampleSize(data[colIndex], data[i]);
275        pValsMatrix[3, i] = SampleSizeDetermination.CalculateCohensD(data[colIndex], data[i]);
276        pValsMatrix[4, i] = SampleSizeDetermination.CalculateHedgesG(data[colIndex], data[i]);
277      }
278
279      MainFormManager.MainForm.ShowContent(pValsMatrix);
280    }
281
282    private void infoLabel_DoubleClick(object sender, EventArgs e) {
283      using (InfoBox dialog = new InfoBox("Description of Statistical Tests", typeof(StatisticalTestingView).Namespace + ".InfoResources.StatisticalTestsInfo.rtf")) {
284        dialog.ShowDialog(this);
285      }
286    }
287
288    private void openBoxPlotToolStripMenuItem_Click(object sender, EventArgs e) {
289      RunCollectionBoxPlotView boxplotView = new RunCollectionBoxPlotView();
290      boxplotView.Content = Content;
291      // TODO: enable as soon as we move to HeuristicLab.Optimization.Views
292      // boxplotView.xAxisComboBox.SelectedItem = xAxisComboBox.SelectedItem;
293      // boxplotView.yAxisComboBox.SelectedItem = yAxisComboBox.SelectedItem;
294      boxplotView.Show();
295    }
296
297    private void pairwiseCheckDataButton_Click(object sender, EventArgs e) {
298      string curItem = (string)groupCompComboBox.SelectedItem;
299      int colIndex = 0;
300
301      foreach (string col in stringConvertibleMatrixView.Content.ColumnNames) {
302        if (col == curItem) {
303          break;
304        }
305        colIndex++;
306      }
307
308      double mwuBothtails;
309      double mwuLefttail;
310      double mwuRighttail;
311      int cnt = 0;
312
313      for (int i = 0; i < data.Length; i++) {
314        if (i != colIndex) {
315          alglib.mannwhitneyutest(data[colIndex], data[colIndex].Length, data[i], data[i].Length, out mwuBothtails, out mwuLefttail, out mwuRighttail);
316          if (mwuBothtails > 0.05) {
317            cnt++;
318          }
319        }
320      }
321
322      double ratio = ((double)cnt) / data.Length * 100.0;
323      equalDistsTextBox.Text = ratio.ToString() + " %";
324
325      if (cnt == 0) {
326        pairwiseLabel.Image = HeuristicLab.Analysis.Statistics.Resources.Default;
327      } else {
328        pairwiseLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Warning;
329      }
330    }
331  }
332}
Note: See TracBrowser for help on using the repository browser.