Free cookie consent management tool by TermsFeed Policy Generator

source: branches/StatisticalTesting/HeuristicLab.Analysis.Statistics/3.3/ResultCorrelationView.cs @ 9380

Last change on this file since 9380 was 9380, checked in by ascheibe, 10 years ago

#2031 use EnhancedStringConvertibleMatrixView instead of StringConvertibleMatrixView in ResultCorrelationView

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