Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2031

  • renamed statistical run collection views
  • implemented RunCollection events in statistical testing view
  • incorporate content name into view caption
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
108    private void calculateCorrelation_Click(object sender, EventArgs e) {
109      RebuildCorrelationTable();
110    }
111    #endregion
112
113    private void UpdateResultComboBox() {
114      resultComboBox.Items.Clear();
115
116      resultComboBox.Items.AddRange(resultsParameters.Keys.ToArray());
117      if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
118    }
119
120    private List<string> GetResultRowNames() {
121      var results = (from run in Content
122                     where run.Visible
123                     from result in run.Results
124                     where result.Value is DoubleValue || result.Value is IntValue
125                     select result.Key).Distinct().ToList();
126
127      return results;
128    }
129
130    private List<string> GetParameterRowNames() {
131      var parameters = (from run in Content
132                        where run.Visible
133                        from parameter in run.Parameters
134                        where parameter.Value is DoubleValue || parameter.Value is IntValue
135                        select parameter.Key).Distinct().ToList();
136
137      return parameters;
138    }
139
140    private Dictionary<string, ResultParameterType> GetRowNames() {
141      Dictionary<string, ResultParameterType> ret = new Dictionary<string, ResultParameterType>();
142
143      var results = GetResultRowNames();
144      var parameters = GetParameterRowNames();
145
146      foreach (var r in results) {
147        ret.Add(r, ResultParameterType.Result);
148      }
149      foreach (var p in parameters) {
150        if (!ret.ContainsKey(p)) {
151          ret.Add(p, ResultParameterType.Parameter);
152        }
153      }
154
155      return ret;
156    }
157
158    private List<double> GetDoublesFromResults(List<IRun> runs, string key) {
159      List<double> res = new List<double>();
160
161      foreach (var r in runs) {
162        if (r.Results[key] is DoubleValue) {
163          res.Add(((DoubleValue)r.Results[key]).Value);
164        } else {
165          res.Add(((IntValue)r.Results[key]).Value);
166        }
167      }
168      return res;
169    }
170
171    private List<double> GetDoublesFromParameters(List<IRun> runs, string key) {
172      List<double> res = new List<double>();
173
174      foreach (var r in runs) {
175        if (r.Parameters[key] is DoubleValue) {
176          res.Add(((DoubleValue)r.Parameters[key]).Value);
177        } else {
178          res.Add(((IntValue)r.Parameters[key]).Value);
179        }
180      }
181      return res;
182    }
183
184    private void RebuildCorrelationTable() {
185      string resultName = (string)resultComboBox.SelectedItem;
186
187      var columnNames = new string[2];
188      columnNames[0] = "Pearson product-moment correlation coefficient";
189      columnNames[1] = "Spearman's rank correlation coefficient";
190
191      var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
192
193      DoubleMatrix dt = new DoubleMatrix(resultsParameters.Count(), columnNames.Count());
194      dt.RowNames = resultsParameters.Keys.ToArray();
195      dt.ColumnNames = columnNames;
196
197      int j = 0;
198      foreach (var rowName in resultsParameters) {
199        var resultVals = GetDoublesFromResults(runs.Where(x => x.Results.ContainsKey(resultName)).Where(x => x.Results[resultName] is DoubleValue || x.Results[resultName] is IntValue).ToList(), resultName);
200
201        List<double> resultRowVals = new List<double>();
202        if (rowName.Value == ResultParameterType.Result) {
203          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);
204        } else {
205          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);
206        }
207
208        if (resultVals.Contains(double.NaN)
209          || resultRowVals.Contains(double.NaN)
210          || resultVals.Contains(double.NegativeInfinity)
211          || resultVals.Contains(double.PositiveInfinity)
212          || resultRowVals.Contains(double.NegativeInfinity)
213          || resultRowVals.Contains(double.PositiveInfinity)
214          || resultRowVals.Count != resultVals.Count) {
215          dt[j, 0] = double.NaN;
216          dt[j++, 1] = double.NaN;
217        } else {
218          dt[j, 0] = alglib.pearsoncorr2(resultVals.ToArray(), resultRowVals.ToArray());
219          dt[j++, 1] = alglib.spearmancorr2(resultVals.ToArray(), resultRowVals.ToArray());
220        }
221      }
222
223      dt.SortableView = true;
224      stringConvertibleMatrixView.Content = dt;
225    }
226  }
227}
Note: See TracBrowser for help on using the repository browser.