Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2031 implemented review comments for correlations view

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