Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2031 the ResultCorrelationView can now also handle parameters and IntValues

File size: 8.3 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    private Dictionary<string, ResultParameterType> resultsParameters;
52
53    protected override void OnContentChanged() {
54      base.OnContentChanged();
55      resultComboBox.Items.Clear();
56
57      if (Content != null) {
58        resultsParameters = GetRowNames();
59        UpdateResultComboBox();
60      }
61    }
62
63    #region events
64    protected override void RegisterContentEvents() {
65      base.RegisterContentEvents();
66      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
67      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
68      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
69      Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
70    }
71
72    protected override void DeregisterContentEvents() {
73      base.DeregisterContentEvents();
74      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
75      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
76      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
77      Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
78    }
79
80    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
81      RebuildCorrelationTable();
82    }
83
84    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
85      RebuildCorrelationTable();
86    }
87
88    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
89      RebuildCorrelationTable();
90    }
91
92    void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
93      if (!Content.UpdateOfRunsInProgress) {
94        RebuildCorrelationTable();
95      }
96    }
97    #endregion
98
99    private void UpdateResultComboBox() {
100      resultComboBox.Items.Clear();
101
102      resultComboBox.Items.AddRange(resultsParameters.Keys.ToArray());
103      if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
104    }
105
106    private List<string> GetResultRowNames() {
107      var results = (from run in Content
108                     where run.Visible
109                     from result in run.Results
110                     where result.Value is DoubleValue || result.Value is IntValue
111                     select result.Key).Distinct().ToList();
112
113      return results;
114    }
115
116    private List<string> GetParameterRowNames() {
117      var parameters = (from run in Content
118                        where run.Visible
119                        from parameter in run.Parameters
120                        where parameter.Value is DoubleValue || parameter.Value is IntValue
121                        select parameter.Key).Distinct().ToList();
122
123      return parameters;
124    }
125
126    private Dictionary<string, ResultParameterType> GetRowNames() {
127      Dictionary<string, ResultParameterType> ret = new Dictionary<string, ResultParameterType>();
128
129      var results = GetResultRowNames();
130      var parameters = GetParameterRowNames();
131
132      foreach (var r in results) {
133        ret.Add(r, ResultParameterType.Result);
134      }
135      foreach (var p in parameters) {
136        if (!ret.ContainsKey(p)) {
137          ret.Add(p, ResultParameterType.Parameter);
138        }
139      }
140
141      return ret;
142    }
143
144    private List<double> GetDoublesFromResults(List<IRun> runs, string key) {
145      List<double> res = new List<double>();
146
147      foreach (var r in runs) {
148        if (r.Results[key] is DoubleValue) {
149          res.Add(((DoubleValue)r.Results[key]).Value);
150        } else {
151          res.Add(((IntValue)r.Results[key]).Value);
152        }
153      }
154      return res;
155    }
156
157    private List<double> GetDoublesFromParameters(List<IRun> runs, string key) {
158      List<double> res = new List<double>();
159
160      foreach (var r in runs) {
161        if (r.Parameters[key] is DoubleValue) {
162          res.Add(((DoubleValue)r.Parameters[key]).Value);
163        } else {
164          res.Add(((IntValue)r.Parameters[key]).Value);
165        }
166      }
167      return res;
168    }
169
170    private void RebuildCorrelationTable() {
171      string resultName = (string)resultComboBox.SelectedItem;
172
173      var columnNames = new string[2];
174      columnNames[0] = "Pearson product-moment correlation coefficient";
175      columnNames[1] = "Spearman's rank correlation coefficient";
176
177
178      var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
179
180      DoubleMatrix dt = new DoubleMatrix(resultsParameters.Count(), columnNames.Count());
181      dt.RowNames = resultsParameters.Keys.ToArray();
182      dt.ColumnNames = columnNames;
183
184      int j = 0;
185      foreach (var rowName in resultsParameters) {
186        var resultVals = GetDoublesFromResults(runs.Where(x => x.Results.ContainsKey(resultName)).Where(x => x.Results[resultName] is DoubleValue || x.Results[resultName] is IntValue).ToList(), resultName);
187
188        List<double> resultRowVals = new List<double>();
189        if (rowName.Value == ResultParameterType.Result) {
190          resultRowVals = GetDoublesFromResults(runs.Where(x => x.Results.ContainsKey(rowName.Key)).Where(x => x.Results[rowName.Key] is DoubleValue || x.Results[rowName.Key] is IntValue).ToList(), rowName.Key);
191        } else {
192          resultRowVals = GetDoublesFromParameters(runs.Where(x => x.Parameters.ContainsKey(rowName.Key)).Where(x => x.Parameters[rowName.Key] is DoubleValue || x.Parameters[rowName.Key] is IntValue).ToList(), rowName.Key);
193        }
194
195        if (resultVals.Contains(double.NaN)
196          || resultRowVals.Contains(double.NaN)
197          || resultVals.Contains(double.NegativeInfinity)
198          || resultVals.Contains(double.PositiveInfinity)
199          || resultRowVals.Contains(double.NegativeInfinity)
200          || resultRowVals.Contains(double.PositiveInfinity)) {
201          dt[j, 0] = double.NaN;
202          dt[j++, 1] = double.NaN;
203        } else {
204          dt[j, 0] = alglib.pearsoncorr2(resultVals.ToArray(), resultRowVals.ToArray());
205          dt[j++, 1] = alglib.spearmancorr2(resultVals.ToArray(), resultRowVals.ToArray());
206        }
207      }
208
209      stringConvertibleMatrixView.Content = dt;
210    }
211
212    private void calculateCorrelation_Click(object sender, EventArgs e) {
213      RebuildCorrelationTable();
214    }
215
216    private enum ResultParameterType {
217      Result,
218      Parameter
219    }
220  }
221}
Note: See TracBrowser for help on using the repository browser.