Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis.Statistics.Views/3.3/CorrelationView.cs @ 12137

Last change on this file since 12137 was 12137, checked in by gkronber, 9 years ago

#2352: only show correlations to 3 digits accuracy.

File size: 8.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 HeuristicLab.Core.Views;
26using HeuristicLab.Data;
27using HeuristicLab.MainForm;
28using HeuristicLab.Optimization;
29
30namespace HeuristicLab.Analysis.Statistics.Views {
31  [View("Correlations")]
32  [Content(typeof(RunCollection), false)]
33  public sealed partial class CorrelationView : ItemView {
34    private const string PearsonName = "Pearson product-moment correlation coefficient";
35    private const string SpearmanName = "Spearman's rank correlation coefficient";
36
37    private enum ResultParameterType {
38      Result,
39      Parameter
40    }
41
42    private bool suppressUpdates = false;
43
44    public new RunCollection Content {
45      get { return (RunCollection)base.Content; }
46      set { base.Content = value; }
47    }
48
49    public override bool ReadOnly {
50      get { return true; }
51      set { /*not needed because results are always readonly */}
52    }
53
54    public CorrelationView() {
55      InitializeComponent();
56      stringConvertibleMatrixView.Minimum = -1.0;
57      stringConvertibleMatrixView.Maximum = 1.0;
58
59      methodComboBox.Items.Add(PearsonName);
60      methodComboBox.Items.Add(SpearmanName);
61      methodComboBox.SelectedIndex = 0;
62    }
63
64    protected override void OnContentChanged() {
65      base.OnContentChanged();
66
67      if (Content != null) {
68        RebuildCorrelationTable();
69      }
70      UpdateCaption();
71    }
72
73    private void UpdateCaption() {
74      Caption = Content != null ? Content.OptimizerName + " Correlations" : ViewAttribute.GetViewName(GetType());
75    }
76
77    #region events
78    protected override void RegisterContentEvents() {
79      base.RegisterContentEvents();
80      Content.ColumnsChanged += Content_ColumnsChanged;
81      Content.RowsChanged += Content_RowsChanged;
82      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
83      Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
84    }
85
86    protected override void DeregisterContentEvents() {
87      base.DeregisterContentEvents();
88      Content.ColumnsChanged -= Content_ColumnsChanged;
89      Content.RowsChanged -= Content_RowsChanged;
90      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
91      Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
92    }
93
94    void Content_RowsChanged(object sender, EventArgs e) {
95      UpdateUI();
96    }
97
98    void Content_ColumnsChanged(object sender, EventArgs e) {
99      if (!suppressUpdates) {
100        UpdateUI();
101      }
102    }
103
104    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
105      UpdateUI();
106    }
107
108    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
109      suppressUpdates = Content.UpdateOfRunsInProgress;
110      UpdateUI();
111    }
112    #endregion
113
114    private void UpdateUI() {
115      if (!suppressUpdates) {
116        RebuildCorrelationTable();
117      }
118    }
119
120    private List<string> GetResultRowNames() {
121      var results = (from run in Content
122                     where run.Visible
123                     from result in run.Results
124                     where result.Value is DoubleValue || result.Value is IntValue
125                     select result.Key).Distinct().OrderBy(x => x).ToList();
126
127      return results;
128    }
129
130    private List<string> GetParameterRowNames() {
131      var parameters = (from run in Content
132                        where run.Visible
133                        from parameter in run.Parameters
134                        where parameter.Value is DoubleValue || parameter.Value is IntValue
135                        select parameter.Key).Distinct().OrderBy(x => x).ToList();
136
137      return parameters;
138    }
139
140    private Dictionary<string, ResultParameterType> GetRowNames() {
141      Dictionary<string, ResultParameterType> ret = new Dictionary<string, ResultParameterType>();
142
143      var results = GetResultRowNames();
144      var parameters = GetParameterRowNames();
145
146      foreach (var r in results) {
147        ret.Add(r, ResultParameterType.Result);
148      }
149      foreach (var p in parameters) {
150        if (!ret.ContainsKey(p)) {
151          ret.Add(p, ResultParameterType.Parameter);
152        }
153      }
154
155      return ret;
156    }
157
158    private List<double> GetDoublesFromResults(List<IRun> runs, string key) {
159      List<double> res = new List<double>();
160
161      foreach (var r in runs) {
162        if (r.Results[key] is DoubleValue) {
163          res.Add(((DoubleValue)r.Results[key]).Value);
164        } else {
165          res.Add(((IntValue)r.Results[key]).Value);
166        }
167      }
168      return res;
169    }
170
171    private List<double> GetDoublesFromParameters(List<IRun> runs, string key) {
172      List<double> res = new List<double>();
173
174      foreach (var r in runs) {
175        if (r.Parameters[key] is DoubleValue) {
176          res.Add(((DoubleValue)r.Parameters[key]).Value);
177        } else {
178          res.Add(((IntValue)r.Parameters[key]).Value);
179        }
180      }
181      return res;
182    }
183
184    private List<double> GetValuesFromResultsParameters(IEnumerable<IRun> runs, string name, ResultParameterType type) {
185      if (type == ResultParameterType.Parameter) {
186        return GetDoublesFromParameters(runs.Where(x => x.Parameters.ContainsKey(name)).ToList(), name);
187      } else if (type == ResultParameterType.Result) {
188        return GetDoublesFromResults(runs.Where(x => x.Results.ContainsKey(name)).ToList(), name);
189      } else {
190        return null;
191      }
192    }
193
194    private void RebuildCorrelationTable() {
195      Dictionary<string, ResultParameterType> resultsParameters = GetRowNames();
196      string methodName = (string)methodComboBox.SelectedItem;
197      var columnNames = resultsParameters.Keys.ToArray();
198
199      var runs = Content.Where(x => x.Visible);
200
201      DoubleMatrix dt = new DoubleMatrix(resultsParameters.Count(), columnNames.Count());
202      dt.RowNames = columnNames;
203      dt.ColumnNames = columnNames;
204
205      int i = 0;
206      foreach (var res in resultsParameters) {
207        var rowValues =
208          GetValuesFromResultsParameters(runs, res.Key, res.Value)
209            .Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x));
210
211        int j = 0;
212        foreach (var cres in resultsParameters) {
213          var columnValues = GetValuesFromResultsParameters(runs, cres.Key, cres.Value)
214                .Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x));
215
216          if (!rowValues.Any() || !columnValues.Any() || rowValues.Count() != columnValues.Count()) {
217            dt[i, j] = double.NaN;
218          } else if (i == j) {
219            dt[i, j] = 1.0;
220          } else {
221            if (methodName == PearsonName) {
222              dt[i, j] = Math.Round(alglib.pearsoncorr2(rowValues.ToArray(), columnValues.ToArray()), 3); // for correlations it is usually sufficient to show only 3 digits
223            } else {
224              dt[i, j] = Math.Round(alglib.spearmancorr2(rowValues.ToArray(), columnValues.ToArray()), 3); // for correlations it is usually sufficient to show only 3 digits
225            }
226          }
227          j++;
228        }
229        i++;
230      }
231
232      dt.SortableView = true;
233      stringConvertibleMatrixView.Content = dt;
234    }
235
236    private void methodComboBox_SelectedIndexChanged(object sender, EventArgs e) {
237      if (Content != null) {
238        RebuildCorrelationTable();
239      }
240    }
241  }
242}
Note: See TracBrowser for help on using the repository browser.