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
RevLine 
[9353]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[9353]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
[11705]30namespace HeuristicLab.Analysis.Statistics.Views {
[9911]31  [View("Correlations")]
[9353]32  [Content(typeof(RunCollection), false)]
[9383]33  public sealed partial class CorrelationView : ItemView {
[11644]34    private const string PearsonName = "Pearson product-moment correlation coefficient";
35    private const string SpearmanName = "Spearman's rank correlation coefficient";
36
[9383]37    private enum ResultParameterType {
38      Result,
39      Parameter
[9353]40    }
41
[11644]42    private bool suppressUpdates = false;
43
[9353]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
[9383]54    public CorrelationView() {
55      InitializeComponent();
56      stringConvertibleMatrixView.Minimum = -1.0;
57      stringConvertibleMatrixView.Maximum = 1.0;
[12151]58      stringConvertibleMatrixView.FormatPattern = "0.000";
[11644]59
60      methodComboBox.Items.Add(PearsonName);
61      methodComboBox.Items.Add(SpearmanName);
62      methodComboBox.SelectedIndex = 0;
[9383]63    }
64
[9353]65    protected override void OnContentChanged() {
66      base.OnContentChanged();
67
68      if (Content != null) {
[11644]69        RebuildCorrelationTable();
[9353]70      }
[9911]71      UpdateCaption();
[9353]72    }
73
[9911]74    private void UpdateCaption() {
75      Caption = Content != null ? Content.OptimizerName + " Correlations" : ViewAttribute.GetViewName(GetType());
76    }
77
[9353]78    #region events
79    protected override void RegisterContentEvents() {
80      base.RegisterContentEvents();
[11697]81      Content.ColumnsChanged += Content_ColumnsChanged;
82      Content.RowsChanged += Content_RowsChanged;
[9353]83      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
[9382]84      Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
[9353]85    }
86
87    protected override void DeregisterContentEvents() {
88      base.DeregisterContentEvents();
[11697]89      Content.ColumnsChanged -= Content_ColumnsChanged;
90      Content.RowsChanged -= Content_RowsChanged;
[9353]91      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
[9382]92      Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
[9353]93    }
94
[11697]95    void Content_RowsChanged(object sender, EventArgs e) {
[11378]96      UpdateUI();
[9353]97    }
98
[11697]99    void Content_ColumnsChanged(object sender, EventArgs e) {
[12116]100      if (!suppressUpdates) {
101        UpdateUI();
102      }
[9353]103    }
104
[11697]105    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
[11378]106      UpdateUI();
[9353]107    }
108
[11378]109    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
110      suppressUpdates = Content.UpdateOfRunsInProgress;
111      UpdateUI();
112    }
[9353]113    #endregion
114
[11378]115    private void UpdateUI() {
116      if (!suppressUpdates) {
117        RebuildCorrelationTable();
118      }
119    }
120
[9382]121    private List<string> GetResultRowNames() {
[9353]122      var results = (from run in Content
[9382]123                     where run.Visible
[9353]124                     from result in run.Results
[9382]125                     where result.Value is DoubleValue || result.Value is IntValue
[11644]126                     select result.Key).Distinct().OrderBy(x => x).ToList();
[9353]127
[9382]128      return results;
[9353]129    }
130
[9382]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
[11644]136                        select parameter.Key).Distinct().OrderBy(x => x).ToList();
[9353]137
[9382]138      return parameters;
139    }
[9353]140
[9382]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);
[9353]153        }
154      }
155
156      return ret;
157    }
158
[9382]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
[11644]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
[9353]195    private void RebuildCorrelationTable() {
[11644]196      Dictionary<string, ResultParameterType> resultsParameters = GetRowNames();
197      string methodName = (string)methodComboBox.SelectedItem;
198      var columnNames = resultsParameters.Keys.ToArray();
[9353]199
[11644]200      var runs = Content.Where(x => x.Visible);
[9353]201
[9382]202      DoubleMatrix dt = new DoubleMatrix(resultsParameters.Count(), columnNames.Count());
[11644]203      dt.RowNames = columnNames;
[9353]204      dt.ColumnNames = columnNames;
205
[11644]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));
[9353]211
[11644]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));
[9382]216
[12137]217          if (!rowValues.Any() || !columnValues.Any() || rowValues.Count() != columnValues.Count()) {
[11644]218            dt[i, j] = double.NaN;
[12152]219          } else if (i == j) {
220            dt[i, j] = 1.0;
[11644]221          } else {
222            if (methodName == PearsonName) {
[12151]223              dt[i, j] = alglib.pearsoncorr2(rowValues.ToArray(), columnValues.ToArray());
[11644]224            } else {
[12151]225              dt[i, j] = alglib.spearmancorr2(rowValues.ToArray(), columnValues.ToArray());
[11644]226            }
227          }
228          j++;
[9353]229        }
[11644]230        i++;
[9353]231      }
232
[9383]233      dt.SortableView = true;
[9353]234      stringConvertibleMatrixView.Content = dt;
235    }
[9912]236
[11644]237    private void methodComboBox_SelectedIndexChanged(object sender, EventArgs e) {
238      if (Content != null) {
239        RebuildCorrelationTable();
240      }
[9912]241    }
[9353]242  }
243}
Note: See TracBrowser for help on using the repository browser.