Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Analysis.Statistics.Views/3.3/CorrelationView.cs @ 12520

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

#2352: merged r12137, r12151:12152 from trunk to stable

File size: 8.3 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      stringConvertibleMatrixView.FormatPattern = "0.000";
59
60      methodComboBox.Items.Add(PearsonName);
61      methodComboBox.Items.Add(SpearmanName);
62      methodComboBox.SelectedIndex = 0;
63    }
64
65    protected override void OnContentChanged() {
66      base.OnContentChanged();
67
68      if (Content != null) {
69        RebuildCorrelationTable();
70      }
71      UpdateCaption();
72    }
73
74    private void UpdateCaption() {
75      Caption = Content != null ? Content.OptimizerName + " Correlations" : ViewAttribute.GetViewName(GetType());
76    }
77
78    #region events
79    protected override void RegisterContentEvents() {
80      base.RegisterContentEvents();
81      Content.ColumnsChanged += Content_ColumnsChanged;
82      Content.RowsChanged += Content_RowsChanged;
83      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
84      Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
85    }
86
87    protected override void DeregisterContentEvents() {
88      base.DeregisterContentEvents();
89      Content.ColumnsChanged -= Content_ColumnsChanged;
90      Content.RowsChanged -= Content_RowsChanged;
91      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
92      Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
93    }
94
95    void Content_RowsChanged(object sender, EventArgs e) {
96      UpdateUI();
97    }
98
99    void Content_ColumnsChanged(object sender, EventArgs e) {
100      UpdateUI();
101    }
102
103    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
104      UpdateUI();
105    }
106
107    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
108      suppressUpdates = Content.UpdateOfRunsInProgress;
109      UpdateUI();
110    }
111    #endregion
112
113    private void UpdateUI() {
114      if (!suppressUpdates) {
115        RebuildCorrelationTable();
116      }
117    }
118
119    private List<string> GetResultRowNames() {
120      var results = (from run in Content
121                     where run.Visible
122                     from result in run.Results
123                     where result.Value is DoubleValue || result.Value is IntValue
124                     select result.Key).Distinct().OrderBy(x => x).ToList();
125
126      return results;
127    }
128
129    private List<string> GetParameterRowNames() {
130      var parameters = (from run in Content
131                        where run.Visible
132                        from parameter in run.Parameters
133                        where parameter.Value is DoubleValue || parameter.Value is IntValue
134                        select parameter.Key).Distinct().OrderBy(x => x).ToList();
135
136      return parameters;
137    }
138
139    private Dictionary<string, ResultParameterType> GetRowNames() {
140      Dictionary<string, ResultParameterType> ret = new Dictionary<string, ResultParameterType>();
141
142      var results = GetResultRowNames();
143      var parameters = GetParameterRowNames();
144
145      foreach (var r in results) {
146        ret.Add(r, ResultParameterType.Result);
147      }
148      foreach (var p in parameters) {
149        if (!ret.ContainsKey(p)) {
150          ret.Add(p, ResultParameterType.Parameter);
151        }
152      }
153
154      return ret;
155    }
156
157    private List<double> GetDoublesFromResults(List<IRun> runs, string key) {
158      List<double> res = new List<double>();
159
160      foreach (var r in runs) {
161        if (r.Results[key] is DoubleValue) {
162          res.Add(((DoubleValue)r.Results[key]).Value);
163        } else {
164          res.Add(((IntValue)r.Results[key]).Value);
165        }
166      }
167      return res;
168    }
169
170    private List<double> GetDoublesFromParameters(List<IRun> runs, string key) {
171      List<double> res = new List<double>();
172
173      foreach (var r in runs) {
174        if (r.Parameters[key] is DoubleValue) {
175          res.Add(((DoubleValue)r.Parameters[key]).Value);
176        } else {
177          res.Add(((IntValue)r.Parameters[key]).Value);
178        }
179      }
180      return res;
181    }
182
183    private List<double> GetValuesFromResultsParameters(IEnumerable<IRun> runs, string name, ResultParameterType type) {
184      if (type == ResultParameterType.Parameter) {
185        return GetDoublesFromParameters(runs.Where(x => x.Parameters.ContainsKey(name)).ToList(), name);
186      } else if (type == ResultParameterType.Result) {
187        return GetDoublesFromResults(runs.Where(x => x.Results.ContainsKey(name)).ToList(), name);
188      } else {
189        return null;
190      }
191    }
192
193    private void RebuildCorrelationTable() {
194      Dictionary<string, ResultParameterType> resultsParameters = GetRowNames();
195      string methodName = (string)methodComboBox.SelectedItem;
196      var columnNames = resultsParameters.Keys.ToArray();
197
198      var runs = Content.Where(x => x.Visible);
199
200      DoubleMatrix dt = new DoubleMatrix(resultsParameters.Count(), columnNames.Count());
201      dt.RowNames = columnNames;
202      dt.ColumnNames = columnNames;
203
204      int i = 0;
205      foreach (var res in resultsParameters) {
206        var rowValues =
207          GetValuesFromResultsParameters(runs, res.Key, res.Value)
208            .Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x));
209
210        int j = 0;
211        foreach (var cres in resultsParameters) {
212          var columnValues = GetValuesFromResultsParameters(runs, cres.Key, cres.Value)
213                .Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x));
214
215          if (!rowValues.Any() || !columnValues.Any() || rowValues.Count() != columnValues.Count()) {
216            dt[i, j] = double.NaN;
217          } else if (i == j) {
218            dt[i, j] = 1.0;
219          } else {
220            if (methodName == PearsonName) {
221              dt[i, j] = alglib.pearsoncorr2(rowValues.ToArray(), columnValues.ToArray());
222            } else {
223              dt[i, j] = alglib.spearmancorr2(rowValues.ToArray(), columnValues.ToArray());
224            }
225          }
226          j++;
227        }
228        i++;
229      }
230
231      dt.SortableView = true;
232      stringConvertibleMatrixView.Content = dt;
233    }
234
235    private void methodComboBox_SelectedIndexChanged(object sender, EventArgs e) {
236      if (Content != null) {
237        RebuildCorrelationTable();
238      }
239    }
240  }
241}
Note: See TracBrowser for help on using the repository browser.