Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12358 was 12152, checked in by mkommend, 10 years ago

#2352: Readded line in correlationview (handling of diagonal values).

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      if (!suppressUpdates) {
101        UpdateUI();
102      }
103    }
104
105    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
106      UpdateUI();
107    }
108
109    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
110      suppressUpdates = Content.UpdateOfRunsInProgress;
111      UpdateUI();
112    }
113    #endregion
114
115    private void UpdateUI() {
116      if (!suppressUpdates) {
117        RebuildCorrelationTable();
118      }
119    }
120
121    private List<string> GetResultRowNames() {
122      var results = (from run in Content
123                     where run.Visible
124                     from result in run.Results
125                     where result.Value is DoubleValue || result.Value is IntValue
126                     select result.Key).Distinct().OrderBy(x => x).ToList();
127
128      return results;
129    }
130
131    private List<string> GetParameterRowNames() {
132      var parameters = (from run in Content
133                        where run.Visible
134                        from parameter in run.Parameters
135                        where parameter.Value is DoubleValue || parameter.Value is IntValue
136                        select parameter.Key).Distinct().OrderBy(x => x).ToList();
137
138      return parameters;
139    }
140
141    private Dictionary<string, ResultParameterType> GetRowNames() {
142      Dictionary<string, ResultParameterType> ret = new Dictionary<string, ResultParameterType>();
143
144      var results = GetResultRowNames();
145      var parameters = GetParameterRowNames();
146
147      foreach (var r in results) {
148        ret.Add(r, ResultParameterType.Result);
149      }
150      foreach (var p in parameters) {
151        if (!ret.ContainsKey(p)) {
152          ret.Add(p, ResultParameterType.Parameter);
153        }
154      }
155
156      return ret;
157    }
158
159    private List<double> GetDoublesFromResults(List<IRun> runs, string key) {
160      List<double> res = new List<double>();
161
162      foreach (var r in runs) {
163        if (r.Results[key] is DoubleValue) {
164          res.Add(((DoubleValue)r.Results[key]).Value);
165        } else {
166          res.Add(((IntValue)r.Results[key]).Value);
167        }
168      }
169      return res;
170    }
171
172    private List<double> GetDoublesFromParameters(List<IRun> runs, string key) {
173      List<double> res = new List<double>();
174
175      foreach (var r in runs) {
176        if (r.Parameters[key] is DoubleValue) {
177          res.Add(((DoubleValue)r.Parameters[key]).Value);
178        } else {
179          res.Add(((IntValue)r.Parameters[key]).Value);
180        }
181      }
182      return res;
183    }
184
185    private List<double> GetValuesFromResultsParameters(IEnumerable<IRun> runs, string name, ResultParameterType type) {
186      if (type == ResultParameterType.Parameter) {
187        return GetDoublesFromParameters(runs.Where(x => x.Parameters.ContainsKey(name)).ToList(), name);
188      } else if (type == ResultParameterType.Result) {
189        return GetDoublesFromResults(runs.Where(x => x.Results.ContainsKey(name)).ToList(), name);
190      } else {
191        return null;
192      }
193    }
194
195    private void RebuildCorrelationTable() {
196      Dictionary<string, ResultParameterType> resultsParameters = GetRowNames();
197      string methodName = (string)methodComboBox.SelectedItem;
198      var columnNames = resultsParameters.Keys.ToArray();
199
200      var runs = Content.Where(x => x.Visible);
201
202      DoubleMatrix dt = new DoubleMatrix(resultsParameters.Count(), columnNames.Count());
203      dt.RowNames = columnNames;
204      dt.ColumnNames = columnNames;
205
206      int i = 0;
207      foreach (var res in resultsParameters) {
208        var rowValues =
209          GetValuesFromResultsParameters(runs, res.Key, res.Value)
210            .Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x));
211
212        int j = 0;
213        foreach (var cres in resultsParameters) {
214          var columnValues = GetValuesFromResultsParameters(runs, cres.Key, cres.Value)
215                .Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x));
216
217          if (!rowValues.Any() || !columnValues.Any() || rowValues.Count() != columnValues.Count()) {
218            dt[i, j] = double.NaN;
219          } else if (i == j) {
220            dt[i, j] = 1.0;
221          } else {
222            if (methodName == PearsonName) {
223              dt[i, j] = alglib.pearsoncorr2(rowValues.ToArray(), columnValues.ToArray());
224            } else {
225              dt[i, j] = alglib.spearmancorr2(rowValues.ToArray(), columnValues.ToArray());
226            }
227          }
228          j++;
229        }
230        i++;
231      }
232
233      dt.SortableView = true;
234      stringConvertibleMatrixView.Content = dt;
235    }
236
237    private void methodComboBox_SelectedIndexChanged(object sender, EventArgs e) {
238      if (Content != null) {
239        RebuildCorrelationTable();
240      }
241    }
242  }
243}
Note: See TracBrowser for help on using the repository browser.