forked from s-wagner/HeuristicLab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCorrelationView.cs
255 lines (216 loc) · 8.8 KB
/
CorrelationView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#region License Information
/* HeuristicLab
* Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Collections;
using HeuristicLab.Core.Views;
using HeuristicLab.Data;
using HeuristicLab.MainForm;
using HeuristicLab.Optimization;
namespace HeuristicLab.Analysis.Statistics.Views {
[View("Correlations")]
[Content(typeof(RunCollection), false)]
public sealed partial class CorrelationView : ItemView {
private const string PearsonName = "Pearson product-moment correlation coefficient";
private const string SpearmanName = "Spearman's rank correlation coefficient";
private enum ResultParameterType {
Result,
Parameter
}
private bool suppressUpdates = false;
public new RunCollection Content {
get { return (RunCollection)base.Content; }
set { base.Content = value; }
}
public override bool ReadOnly {
get { return true; }
set { /*not needed because results are always readonly */}
}
public CorrelationView() {
InitializeComponent();
stringConvertibleMatrixView.Minimum = -1.0;
stringConvertibleMatrixView.Maximum = 1.0;
stringConvertibleMatrixView.FormatPattern = "0.000";
methodComboBox.Items.Add(PearsonName);
methodComboBox.Items.Add(SpearmanName);
methodComboBox.SelectedIndex = 0;
}
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content != null) {
RebuildCorrelationTable();
}
UpdateCaption();
}
private void UpdateCaption() {
Caption = Content != null ? Content.OptimizerName + " Correlations" : ViewAttribute.GetViewName(GetType());
}
#region events
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.ColumnsChanged += Content_ColumnsChanged;
Content.RowsChanged += Content_RowsChanged;
Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
}
protected override void DeregisterContentEvents() {
base.DeregisterContentEvents();
Content.ColumnsChanged -= Content_ColumnsChanged;
Content.RowsChanged -= Content_RowsChanged;
Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
}
void Content_RowsChanged(object sender, EventArgs e) {
if (suppressUpdates) return;
if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_RowsChanged, sender, e);
else {
UpdateUI();
}
}
void Content_ColumnsChanged(object sender, EventArgs e) {
if (suppressUpdates) return;
if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ColumnsChanged, sender, e);
else {
UpdateUI();
}
}
private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
if (suppressUpdates) return;
if (InvokeRequired) Invoke((Action<object, CollectionItemsChangedEventArgs<IRun>>)Content_CollectionReset, sender, e);
else {
UpdateUI();
}
}
private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_UpdateOfRunsInProgressChanged, sender, e);
else {
suppressUpdates = Content.UpdateOfRunsInProgress;
UpdateUI();
}
}
#endregion
private void UpdateUI() {
RebuildCorrelationTable();
}
private List<string> GetResultRowNames() {
var results = (from run in Content
where run.Visible
from result in run.Results
where result.Value is DoubleValue || result.Value is IntValue
select result.Key).Distinct().OrderBy(x => x).ToList();
return results;
}
private List<string> GetParameterRowNames() {
var parameters = (from run in Content
where run.Visible
from parameter in run.Parameters
where parameter.Value is DoubleValue || parameter.Value is IntValue
select parameter.Key).Distinct().OrderBy(x => x).ToList();
return parameters;
}
private Dictionary<string, ResultParameterType> GetRowNames() {
Dictionary<string, ResultParameterType> ret = new Dictionary<string, ResultParameterType>();
var results = GetResultRowNames();
var parameters = GetParameterRowNames();
foreach (var r in results) {
ret.Add(r, ResultParameterType.Result);
}
foreach (var p in parameters) {
if (!ret.ContainsKey(p)) {
ret.Add(p, ResultParameterType.Parameter);
}
}
return ret;
}
private List<double> GetDoublesFromResults(List<IRun> runs, string key) {
List<double> res = new List<double>();
foreach (var r in runs) {
if (r.Results[key] is DoubleValue) {
res.Add(((DoubleValue)r.Results[key]).Value);
} else {
res.Add(((IntValue)r.Results[key]).Value);
}
}
return res;
}
private List<double> GetDoublesFromParameters(List<IRun> runs, string key) {
List<double> res = new List<double>();
foreach (var r in runs) {
if (r.Parameters[key] is DoubleValue) {
res.Add(((DoubleValue)r.Parameters[key]).Value);
} else {
res.Add(((IntValue)r.Parameters[key]).Value);
}
}
return res;
}
private List<double> GetValuesFromResultsParameters(IEnumerable<IRun> runs, string name, ResultParameterType type) {
if (type == ResultParameterType.Parameter) {
return GetDoublesFromParameters(runs.Where(x => x.Parameters.ContainsKey(name)).ToList(), name);
} else if (type == ResultParameterType.Result) {
return GetDoublesFromResults(runs.Where(x => x.Results.ContainsKey(name)).ToList(), name);
} else {
return null;
}
}
private void RebuildCorrelationTable() {
Dictionary<string, ResultParameterType> resultsParameters = GetRowNames();
string methodName = (string)methodComboBox.SelectedItem;
var columnNames = resultsParameters.Keys.ToArray();
var runs = Content.Where(x => x.Visible);
DoubleMatrix dt = new DoubleMatrix(resultsParameters.Count(), columnNames.Count());
dt.RowNames = columnNames;
dt.ColumnNames = columnNames;
int i = 0;
foreach (var res in resultsParameters) {
var rowValues =
GetValuesFromResultsParameters(runs, res.Key, res.Value)
.Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x));
int j = 0;
foreach (var cres in resultsParameters) {
var columnValues = GetValuesFromResultsParameters(runs, cres.Key, cres.Value)
.Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x));
if (!rowValues.Any() || !columnValues.Any() || rowValues.Count() != columnValues.Count()) {
dt[i, j] = double.NaN;
} else if (i == j) {
dt[i, j] = 1.0;
} else {
if (methodName == PearsonName) {
dt[i, j] = alglib.pearsoncorr2(rowValues.ToArray(), columnValues.ToArray());
} else {
dt[i, j] = alglib.spearmancorr2(rowValues.ToArray(), columnValues.ToArray());
}
}
j++;
}
i++;
}
dt.SortableView = true;
stringConvertibleMatrixView.Content = dt;
}
private void methodComboBox_SelectedIndexChanged(object sender, EventArgs e) {
if (Content != null) {
RebuildCorrelationTable();
}
}
}
}