Free cookie consent management tool by TermsFeed Policy Generator

source: branches/StatisticalTesting/HeuristicLab.Analysis.Statistics/3.3/CorrelationView.cs @ 9713

Last change on this file since 9713 was 9713, checked in by ascheibe, 11 years ago

#2031

  • made operations in ChartAnalysisView asynchronous
  • renamed views to go along with the other RunCollection views
File size: 8.4 KB
RevLine 
[9353]1#region License Information
2/* HeuristicLab
[9706]3 * Copyright (C) 2002-2013 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 System.Windows.Forms;
26using HeuristicLab.Core.Views;
27using HeuristicLab.Data;
28using HeuristicLab.MainForm;
29using HeuristicLab.Optimization;
30
31namespace HeuristicLab.Analysis.Statistics {
[9713]32  [View("RunCollection Correlations")]
[9353]33  [Content(typeof(RunCollection), false)]
[9383]34  public sealed partial class CorrelationView : ItemView {
35    private enum ResultParameterType {
36      Result,
37      Parameter
[9353]38    }
39
40    public new RunCollection Content {
41      get { return (RunCollection)base.Content; }
42      set { base.Content = value; }
43    }
44
45    public override bool ReadOnly {
46      get { return true; }
47      set { /*not needed because results are always readonly */}
48    }
49
[9382]50    private Dictionary<string, ResultParameterType> resultsParameters;
51
[9383]52    public CorrelationView() {
53      InitializeComponent();
54      stringConvertibleMatrixView.Minimum = -1.0;
55      stringConvertibleMatrixView.Maximum = 1.0;
56    }
57
[9353]58    protected override void OnContentChanged() {
59      base.OnContentChanged();
60      resultComboBox.Items.Clear();
61
62      if (Content != null) {
[9382]63        resultsParameters = GetRowNames();
[9353]64        UpdateResultComboBox();
65      }
66    }
67
68    #region events
69    protected override void RegisterContentEvents() {
70      base.RegisterContentEvents();
71      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
72      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
73      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
[9382]74      Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
[9353]75    }
76
77    protected override void DeregisterContentEvents() {
78      base.DeregisterContentEvents();
79      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
80      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
81      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
[9382]82      Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
[9353]83    }
84
85    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
[9382]86      RebuildCorrelationTable();
[9353]87    }
88
89    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
[9382]90      RebuildCorrelationTable();
[9353]91    }
92
93    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
[9382]94      RebuildCorrelationTable();
[9353]95    }
96
[9382]97    void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
98      if (!Content.UpdateOfRunsInProgress) {
99        RebuildCorrelationTable();
[9353]100      }
101    }
[9383]102
103    private void calculateCorrelation_Click(object sender, EventArgs e) {
104      RebuildCorrelationTable();
105    }
[9353]106    #endregion
107
[9382]108    private void UpdateResultComboBox() {
109      resultComboBox.Items.Clear();
110
111      resultComboBox.Items.AddRange(resultsParameters.Keys.ToArray());
112      if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
[9353]113    }
114
[9382]115    private List<string> GetResultRowNames() {
[9353]116      var results = (from run in Content
[9382]117                     where run.Visible
[9353]118                     from result in run.Results
[9382]119                     where result.Value is DoubleValue || result.Value is IntValue
120                     select result.Key).Distinct().ToList();
[9353]121
[9382]122      return results;
[9353]123    }
124
[9382]125    private List<string> GetParameterRowNames() {
126      var parameters = (from run in Content
127                        where run.Visible
128                        from parameter in run.Parameters
129                        where parameter.Value is DoubleValue || parameter.Value is IntValue
130                        select parameter.Key).Distinct().ToList();
[9353]131
[9382]132      return parameters;
133    }
[9353]134
[9382]135    private Dictionary<string, ResultParameterType> GetRowNames() {
136      Dictionary<string, ResultParameterType> ret = new Dictionary<string, ResultParameterType>();
137
138      var results = GetResultRowNames();
139      var parameters = GetParameterRowNames();
140
141      foreach (var r in results) {
142        ret.Add(r, ResultParameterType.Result);
143      }
144      foreach (var p in parameters) {
145        if (!ret.ContainsKey(p)) {
146          ret.Add(p, ResultParameterType.Parameter);
[9353]147        }
148      }
149
150      return ret;
151    }
152
[9382]153    private List<double> GetDoublesFromResults(List<IRun> runs, string key) {
154      List<double> res = new List<double>();
155
156      foreach (var r in runs) {
157        if (r.Results[key] is DoubleValue) {
158          res.Add(((DoubleValue)r.Results[key]).Value);
159        } else {
160          res.Add(((IntValue)r.Results[key]).Value);
161        }
162      }
163      return res;
164    }
165
166    private List<double> GetDoublesFromParameters(List<IRun> runs, string key) {
167      List<double> res = new List<double>();
168
169      foreach (var r in runs) {
170        if (r.Parameters[key] is DoubleValue) {
171          res.Add(((DoubleValue)r.Parameters[key]).Value);
172        } else {
173          res.Add(((IntValue)r.Parameters[key]).Value);
174        }
175      }
176      return res;
177    }
178
[9353]179    private void RebuildCorrelationTable() {
180      string resultName = (string)resultComboBox.SelectedItem;
181
182      var columnNames = new string[2];
183      columnNames[0] = "Pearson product-moment correlation coefficient";
184      columnNames[1] = "Spearman's rank correlation coefficient";
185
186      var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
187
[9382]188      DoubleMatrix dt = new DoubleMatrix(resultsParameters.Count(), columnNames.Count());
189      dt.RowNames = resultsParameters.Keys.ToArray();
[9353]190      dt.ColumnNames = columnNames;
191
192      int j = 0;
[9382]193      foreach (var rowName in resultsParameters) {
194        var resultVals = GetDoublesFromResults(runs.Where(x => x.Results.ContainsKey(resultName)).Where(x => x.Results[resultName] is DoubleValue || x.Results[resultName] is IntValue).ToList(), resultName);
[9353]195
[9382]196        List<double> resultRowVals = new List<double>();
197        if (rowName.Value == ResultParameterType.Result) {
198          resultRowVals = GetDoublesFromResults(runs.Where(x => x.Results.ContainsKey(rowName.Key)).Where(x => x.Results[rowName.Key] is DoubleValue || x.Results[rowName.Key] is IntValue).ToList(), rowName.Key);
199        } else {
200          resultRowVals = GetDoublesFromParameters(runs.Where(x => x.Parameters.ContainsKey(rowName.Key)).Where(x => x.Parameters[rowName.Key] is DoubleValue || x.Parameters[rowName.Key] is IntValue).ToList(), rowName.Key);
201        }
202
[9353]203        if (resultVals.Contains(double.NaN)
204          || resultRowVals.Contains(double.NaN)
205          || resultVals.Contains(double.NegativeInfinity)
206          || resultVals.Contains(double.PositiveInfinity)
207          || resultRowVals.Contains(double.NegativeInfinity)
[9398]208          || resultRowVals.Contains(double.PositiveInfinity)
209          || resultRowVals.Count != resultVals.Count) {
[9380]210          dt[j, 0] = double.NaN;
211          dt[j++, 1] = double.NaN;
[9353]212        } else {
[9380]213          dt[j, 0] = alglib.pearsoncorr2(resultVals.ToArray(), resultRowVals.ToArray());
214          dt[j++, 1] = alglib.spearmancorr2(resultVals.ToArray(), resultRowVals.ToArray());
[9353]215        }
216      }
217
[9383]218      dt.SortableView = true;
[9353]219      stringConvertibleMatrixView.Content = dt;
220    }
221  }
222}
Note: See TracBrowser for help on using the repository browser.