Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.Analysis.Statistics.Views/3.3/CorrelationView.cs @ 11975

Last change on this file since 11975 was 11705, checked in by ascheibe, 10 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
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.Core.Views;
26using HeuristicLab.Data;
27using HeuristicLab.MainForm;
28using HeuristicLab.Optimization;
29
30namespace HeuristicLab.Analysis.Statistics.Views {
31  [View("Correlations")]
32  [Content(typeof(RunCollection), false)]
33  public sealed partial class CorrelationView : ItemView {
34    private const string PearsonName = "Pearson product-moment correlation coefficient";
35    private const string SpearmanName = "Spearman's rank correlation coefficient";
36
37    private enum ResultParameterType {
38      Result,
39      Parameter
40    }
41
42    private bool suppressUpdates = false;
43
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
54    public CorrelationView() {
55      InitializeComponent();
56      stringConvertibleMatrixView.Minimum = -1.0;
57      stringConvertibleMatrixView.Maximum = 1.0;
58
59      methodComboBox.Items.Add(PearsonName);
60      methodComboBox.Items.Add(SpearmanName);
61      methodComboBox.SelectedIndex = 0;
62    }
63
64    protected override void OnContentChanged() {
65      base.OnContentChanged();
66
67      if (Content != null) {
68        RebuildCorrelationTable();
69      }
70      UpdateCaption();
71    }
72
73    private void UpdateCaption() {
74      Caption = Content != null ? Content.OptimizerName + " Correlations" : ViewAttribute.GetViewName(GetType());
75    }
76
77    #region events
78    protected override void RegisterContentEvents() {
79      base.RegisterContentEvents();
80      Content.ColumnsChanged += Content_ColumnsChanged;
81      Content.RowsChanged += Content_RowsChanged;
82      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
83      Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
84    }
85
86    protected override void DeregisterContentEvents() {
87      base.DeregisterContentEvents();
88      Content.ColumnsChanged -= Content_ColumnsChanged;
89      Content.RowsChanged -= Content_RowsChanged;
90      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
91      Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
92    }
93
94    void Content_RowsChanged(object sender, EventArgs e) {
95      UpdateUI();
96    }
97
98    void Content_ColumnsChanged(object sender, EventArgs e) {
99      UpdateUI();
100    }
101
102    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
103      UpdateUI();
104    }
105
106    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
107      suppressUpdates = Content.UpdateOfRunsInProgress;
108      UpdateUI();
109    }
110    #endregion
111
112    private void UpdateUI() {
113      if (!suppressUpdates) {
114        RebuildCorrelationTable();
115      }
116    }
117
118    private List<string> GetResultRowNames() {
119      var results = (from run in Content
120                     where run.Visible
121                     from result in run.Results
122                     where result.Value is DoubleValue || result.Value is IntValue
123                     select result.Key).Distinct().OrderBy(x => x).ToList();
124
125      return results;
126    }
127
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
133                        select parameter.Key).Distinct().OrderBy(x => x).ToList();
134
135      return parameters;
136    }
137
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);
150        }
151      }
152
153      return ret;
154    }
155
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
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
192    private void RebuildCorrelationTable() {
193      Dictionary<string, ResultParameterType> resultsParameters = GetRowNames();
194      string methodName = (string)methodComboBox.SelectedItem;
195      var columnNames = resultsParameters.Keys.ToArray();
196
197      var runs = Content.Where(x => x.Visible);
198
199      DoubleMatrix dt = new DoubleMatrix(resultsParameters.Count(), columnNames.Count());
200      dt.RowNames = columnNames;
201      dt.ColumnNames = columnNames;
202
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));
208
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));
213
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++;
224        }
225        i++;
226      }
227
228      dt.SortableView = true;
229      stringConvertibleMatrixView.Content = dt;
230    }
231
232    private void methodComboBox_SelectedIndexChanged(object sender, EventArgs e) {
233      if (Content != null) {
234        RebuildCorrelationTable();
235      }
236    }
237  }
238}
Note: See TracBrowser for help on using the repository browser.