Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 started working on a statistical testing run collection view

File size: 5.8 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.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Analysis.AlgorithmBehavior.Analyzers;
27using HeuristicLab.Core.Views;
28using HeuristicLab.Data;
29using HeuristicLab.MainForm;
30using HeuristicLab.Optimization;
31
32namespace HeuristicLab.Analysis.AlgorithmBehavior.Views {
33  [View("Statistical Testing View")]
34  [Content(typeof(RunCollection), false)]
35  public sealed partial class StatisticalTestingView : ItemView {
36    private double[][] data;
37
38    public StatisticalTestingView() {
39      InitializeComponent();
40    }
41
42    public new RunCollection Content {
43      get { return (RunCollection)base.Content; }
44      set { base.Content = value; }
45    }
46
47    public override bool ReadOnly {
48      get { return true; }
49      set { /*not needed because results are always readonly */}
50    }
51
52    protected override void OnContentChanged() {
53      base.OnContentChanged();
54
55      if (Content != null) {
56        UpdateResultComboBox();
57        UpdateGroupsComboBox();
58        RebuildDataTable();
59      }
60    }
61
62    #region events
63    protected override void RegisterContentEvents() {
64      base.RegisterContentEvents();
65    }
66
67    protected override void DeregisterContentEvents() {
68      base.DeregisterContentEvents();
69    }
70    #endregion
71
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
159    private void normalDistButton_Click(object sender, EventArgs e) {
160
161    }
162
163    private void resultComboBox_SelectedValueChanged(object sender, EventArgs e) {
164      RebuildDataTable();
165    }
166
167    private void groupComboBox_SelectedValueChanged(object sender, EventArgs e) {
168      RebuildDataTable();
169    }
170  }
171}
Note: See TracBrowser for help on using the repository browser.