Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Analysis.Statistics.Views/3.3/CorrelationView.cs @ 15761

Last change on this file since 15761 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 8.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Views;
27using HeuristicLab.Data;
28using HeuristicLab.MainForm;
29using HeuristicLab.Optimization;
30
31namespace HeuristicLab.Analysis.Statistics.Views {
32  [View("Correlations")]
33  [Content(typeof(RunCollection), false)]
34  public sealed partial class CorrelationView : ItemView {
35    private const string PearsonName = "Pearson product-moment correlation coefficient";
36    private const string SpearmanName = "Spearman's rank correlation coefficient";
37
38    private enum ResultParameterType {
39      Result,
40      Parameter
41    }
42
43    private bool suppressUpdates = false;
44
45    public new RunCollection Content {
46      get { return (RunCollection)base.Content; }
47      set { base.Content = value; }
48    }
49
50    public override bool ReadOnly {
51      get { return true; }
52      set { /*not needed because results are always readonly */}
53    }
54
55    public CorrelationView() {
56      InitializeComponent();
57      stringConvertibleMatrixView.Minimum = -1.0;
58      stringConvertibleMatrixView.Maximum = 1.0;
59      stringConvertibleMatrixView.FormatPattern = "0.000";
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.ColumnsChanged += Content_ColumnsChanged;
83      Content.RowsChanged += Content_RowsChanged;
84      Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
85      Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
86    }
87
88    protected override void DeregisterContentEvents() {
89      base.DeregisterContentEvents();
90      Content.ColumnsChanged -= Content_ColumnsChanged;
91      Content.RowsChanged -= Content_RowsChanged;
92      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
93      Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
94    }
95
96    void Content_RowsChanged(object sender, EventArgs e) {
97      if (suppressUpdates) return;
98      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_RowsChanged, sender, e);
99      else {
100        UpdateUI();
101      }
102    }
103
104    void Content_ColumnsChanged(object sender, EventArgs e) {
105      if (suppressUpdates) return;
106      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ColumnsChanged, sender, e);
107      else {
108        UpdateUI();
109      }
110    }
111
112    private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
113      if (suppressUpdates) return;
114      if (InvokeRequired) Invoke((Action<object, CollectionItemsChangedEventArgs<IRun>>)Content_CollectionReset, sender, e);
115      else {
116        UpdateUI();
117      }
118    }
119
120    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
121      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_UpdateOfRunsInProgressChanged, sender, e);
122      else {
123        suppressUpdates = Content.UpdateOfRunsInProgress;
124        UpdateUI();
125      }
126    }
127    #endregion
128
129    private void UpdateUI() {
130      RebuildCorrelationTable();
131    }
132
133    private List<string> GetResultRowNames() {
134      var results = (from run in Content
135                     where run.Visible
136                     from result in run.Results
137                     where result.Value is DoubleValue || result.Value is IntValue
138                     select result.Key).Distinct().OrderBy(x => x).ToList();
139
140      return results;
141    }
142
143    private List<string> GetParameterRowNames() {
144      var parameters = (from run in Content
145                        where run.Visible
146                        from parameter in run.Parameters
147                        where parameter.Value is DoubleValue || parameter.Value is IntValue
148                        select parameter.Key).Distinct().OrderBy(x => x).ToList();
149
150      return parameters;
151    }
152
153    private Dictionary<string, ResultParameterType> GetRowNames() {
154      Dictionary<string, ResultParameterType> ret = new Dictionary<string, ResultParameterType>();
155
156      var results = GetResultRowNames();
157      var parameters = GetParameterRowNames();
158
159      foreach (var r in results) {
160        ret.Add(r, ResultParameterType.Result);
161      }
162      foreach (var p in parameters) {
163        if (!ret.ContainsKey(p)) {
164          ret.Add(p, ResultParameterType.Parameter);
165        }
166      }
167
168      return ret;
169    }
170
171    private List<double> GetDoublesFromResults(List<IRun> runs, string key) {
172      List<double> res = new List<double>();
173
174      foreach (var r in runs) {
175        if (r.Results[key] is DoubleValue) {
176          res.Add(((DoubleValue)r.Results[key]).Value);
177        } else {
178          res.Add(((IntValue)r.Results[key]).Value);
179        }
180      }
181      return res;
182    }
183
184    private List<double> GetDoublesFromParameters(List<IRun> runs, string key) {
185      List<double> res = new List<double>();
186
187      foreach (var r in runs) {
188        if (r.Parameters[key] is DoubleValue) {
189          res.Add(((DoubleValue)r.Parameters[key]).Value);
190        } else {
191          res.Add(((IntValue)r.Parameters[key]).Value);
192        }
193      }
194      return res;
195    }
196
197    private List<double> GetValuesFromResultsParameters(IEnumerable<IRun> runs, string name, ResultParameterType type) {
198      if (type == ResultParameterType.Parameter) {
199        return GetDoublesFromParameters(runs.Where(x => x.Parameters.ContainsKey(name)).ToList(), name);
200      } else if (type == ResultParameterType.Result) {
201        return GetDoublesFromResults(runs.Where(x => x.Results.ContainsKey(name)).ToList(), name);
202      } else {
203        return null;
204      }
205    }
206
207    private void RebuildCorrelationTable() {
208      Dictionary<string, ResultParameterType> resultsParameters = GetRowNames();
209      string methodName = (string)methodComboBox.SelectedItem;
210      var columnNames = resultsParameters.Keys.ToArray();
211
212      var runs = Content.Where(x => x.Visible);
213
214      DoubleMatrix dt = new DoubleMatrix(resultsParameters.Count(), columnNames.Count());
215      dt.RowNames = columnNames;
216      dt.ColumnNames = columnNames;
217
218      int i = 0;
219      foreach (var res in resultsParameters) {
220        var rowValues =
221          GetValuesFromResultsParameters(runs, res.Key, res.Value)
222            .Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x));
223
224        int j = 0;
225        foreach (var cres in resultsParameters) {
226          var columnValues = GetValuesFromResultsParameters(runs, cres.Key, cres.Value)
227                .Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x));
228
229          if (!rowValues.Any() || !columnValues.Any() || rowValues.Count() != columnValues.Count()) {
230            dt[i, j] = double.NaN;
231          } else if (i == j) {
232            dt[i, j] = 1.0;
233          } else {
234            if (methodName == PearsonName) {
235              dt[i, j] = alglib.pearsoncorr2(rowValues.ToArray(), columnValues.ToArray());
236            } else {
237              dt[i, j] = alglib.spearmancorr2(rowValues.ToArray(), columnValues.ToArray());
238            }
239          }
240          j++;
241        }
242        i++;
243      }
244
245      dt.SortableView = true;
246      stringConvertibleMatrixView.Content = dt;
247    }
248
249    private void methodComboBox_SelectedIndexChanged(object sender, EventArgs e) {
250      if (Content != null) {
251        RebuildCorrelationTable();
252      }
253    }
254  }
255}
Note: See TracBrowser for help on using the repository browser.