Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11705 was 11705, checked in by ascheibe, 9 years ago

#2031

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