Free cookie consent management tool by TermsFeed Policy Generator

source: branches/StatisticalTesting/HeuristicLab.Analysis.Statistics/3.3/CorrelationView.cs @ 9573

Last change on this file since 9573 was 9398, checked in by ascheibe, 12 years ago

#2031 fixed a bug in the correlation view

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