Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Analysis.Statistics.Views/3.3/CorrelationView.cs

Last change on this file was 16057, checked in by jkarder, 6 years ago

#2839:

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