Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2031 try to filter NaN values in correlation view and some other minor improvements

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