Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers.Views/3.3/ResultCorrelationView.cs @ 9328

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

#1886

  • fixed a bug in the ResultCorrelationView
  • added sample size determination by estimating means, Cohen's D and Hedges' G
File size: 6.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.Core.Views;
27using HeuristicLab.Data;
28using HeuristicLab.MainForm;
29using HeuristicLab.Optimization;
30
31namespace HeuristicLab.Analysis.AlgorithmBehavior.Views {
32  [View("Result Correlation View")]
33  [Content(typeof(RunCollection), false)]
34  public sealed partial class ResultCorrelationView : ItemView {
35    public ResultCorrelationView() {
36      InitializeComponent();
37    }
38
39    public new RunCollection Content {
40      get { return (RunCollection)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public override bool ReadOnly {
45      get { return true; }
46      set { /*not needed because results are always readonly */}
47    }
48
49    protected override void OnContentChanged() {
50      base.OnContentChanged();
51      resultComboBox.Items.Clear();
52
53      if (Content != null) {
54        UpdateResultComboBox();
55      }
56    }
57
58    #region events
59    protected override void RegisterContentEvents() {
60      base.RegisterContentEvents();
61      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
62      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
63      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
64
65      RegisterRunEvents(Content);
66    }
67
68    private void RegisterRunEvents(IEnumerable<IRun> runs) {
69      foreach (IRun run in runs)
70        run.Changed += new EventHandler(run_Changed);
71    }
72
73    protected override void DeregisterContentEvents() {
74      base.DeregisterContentEvents();
75      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
76      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
77      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
78
79      DeregisterRunEvents(Content);
80    }
81
82    private void DeregisterRunEvents(IEnumerable<IRun> runs) {
83      foreach (IRun run in runs)
84        run.Changed -= new EventHandler(run_Changed);
85    }
86
87    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
88      DeregisterRunEvents(e.OldItems);
89      RegisterRunEvents(e.Items);
90      //RebuildInfluenceDataTable();
91    }
92
93    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
94      DeregisterRunEvents(e.Items);
95      //RebuildInfluenceDataTable();
96    }
97
98    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
99      RegisterRunEvents(e.Items);
100      //RebuildInfluenceDataTable();
101    }
102
103    private void run_Changed(object sender, EventArgs e) {
104      if (InvokeRequired)
105        this.Invoke(new EventHandler(run_Changed), sender, e);
106      else {
107        IRun run = (IRun)sender;
108        UpdateRun(run);
109      }
110    }
111    #endregion
112
113    private void UpdateRun(IRun run) {
114      //TODO: hacky di hack... this is baaaadddd
115      RebuildCorrelationTable();
116    }
117
118    private void UpdateResultComboBox() {
119      resultComboBox.Items.Clear();
120      var results = (from run in Content
121                     from result in run.Results
122                     select result.Key).Distinct().ToArray();
123
124      resultComboBox.Items.AddRange(results);
125      if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
126    }
127
128
129    private List<string> GetRowNames() {
130      string resultName = (string)resultComboBox.SelectedItem;
131      var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
132      List<string> ret = new List<string>();
133
134      foreach (var r in runs) {
135        var ress = r.Results.Where(y => y.Value is DoubleValue).Select(z => z.Key);
136        foreach (var s in ress) {
137          if (!ret.Contains(s))
138            ret.Add(s);
139        }
140      }
141
142      return ret;
143    }
144
145    private void RebuildCorrelationTable() {
146      string resultName = (string)resultComboBox.SelectedItem;
147
148      var columnNames = new string[2];
149      columnNames[0] = "Pearson product-moment correlation coefficient";
150      columnNames[1] = "Spearman's rank correlation coefficient";
151      var rowNames = GetRowNames();
152
153      var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
154
155      StringMatrix dt = new StringMatrix(rowNames.Count(), columnNames.Count());
156      dt.RowNames = rowNames;
157      dt.ColumnNames = columnNames;
158
159      int j = 0;
160      foreach (var rowName in rowNames) {
161        var resultRowVals = runs.Where(x => x.Results.ContainsKey(rowName)).Where(x => x.Results[rowName] is DoubleValue).Select(x => ((DoubleValue)x.Results[rowName]).Value);
162        var resultVals = runs.Where(x => x.Results.ContainsKey(resultName)).Where(x => x.Results[resultName] is DoubleValue).Select(x => ((DoubleValue)x.Results[resultName]).Value);
163
164        if (resultVals.Contains(double.NaN)
165          || resultRowVals.Contains(double.NaN)
166          || resultVals.Contains(double.NegativeInfinity)
167          || resultVals.Contains(double.PositiveInfinity)
168          || resultRowVals.Contains(double.NegativeInfinity)
169          || resultRowVals.Contains(double.PositiveInfinity)) {
170          dt[j, 0] = "X";
171          dt[j++, 1] = "X";
172        } else {
173          dt[j, 0] = alglib.pearsoncorr2(resultVals.ToArray(), resultRowVals.ToArray()).ToString();
174          dt[j++, 1] = alglib.spearmancorr2(resultVals.ToArray(), resultRowVals.ToArray()).ToString();
175        }
176      }
177
178      stringConvertibleMatrixView.Content = dt;
179    }
180
181    private void calculateCorrelation_Click(object sender, EventArgs e) {
182      RebuildCorrelationTable();
183    }
184  }
185}
Note: See TracBrowser for help on using the repository browser.