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