-
Notifications
You must be signed in to change notification settings - Fork 89
/
_critical_difference.py
534 lines (465 loc) · 16.8 KB
/
_critical_difference.py
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# -*- coding: utf-8 -*-
"""Function to compute and plot critical difference diagrams."""
__author__ = ["SveaMeyer13", "dguijo"]
import math
import operator
import numpy as np
from scipy.stats import distributions, find_repeats, rankdata, wilcoxon
from aeon.benchmarking.utils import get_qalpha
from aeon.utils.validation._dependencies import _check_soft_dependencies
def _check_friedman(n_estimators, n_datasets, ranked_data, alpha):
"""
Check whether Friedman test is significant.
Larger parts of code copied from scipy.
Parameters
----------
n_estimators : int
number of strategies to evaluate
n_datasets : int
number of datasets classified per strategy
ranked_data : np.array (shape: n_estimators * n_datasets)
rank of strategy on dataset
Returns
-------
is_significant : bool
Indicates whether strategies differ significantly in terms of performance
(according to Friedman test).
"""
if n_estimators < 3:
raise ValueError(
"At least 3 sets of measurements must be given for Friedmann test, "
f"got {n_estimators}."
)
# calculate c to correct chisq for ties:
ties = 0
for i in range(n_datasets):
replist, repnum = find_repeats(ranked_data[i])
for t in repnum:
ties += t * (t * t - 1)
c = 1 - ties / (n_estimators * (n_estimators * n_estimators - 1) * n_datasets)
ssbn = np.sum(ranked_data.sum(axis=0) ** 2)
chisq = (
12.0 / (n_estimators * n_datasets * (n_estimators + 1)) * ssbn
- 3 * n_datasets * (n_estimators + 1)
) / c
p = distributions.chi2.sf(chisq, n_estimators - 1)
if p < alpha:
is_significant = True
else:
is_significant = False
return is_significant
def nemenyi_cliques(n_estimators, n_datasets, avranks, alpha):
"""Find cliques using post hoc Nemenyi test."""
# Get critical value, there is an exact way now
qalpha = get_qalpha(alpha)
# calculate critical difference with Nemenyi
cd = qalpha[n_estimators] * np.sqrt(
n_estimators * (n_estimators + 1) / (6 * n_datasets)
)
# compute statistically similar cliques
cliques = np.tile(avranks, (n_estimators, 1)) - np.tile(
np.vstack(avranks.T), (1, n_estimators)
)
cliques[cliques < 0] = np.inf
cliques = cliques < cd
cliques = build_cliques(cliques)
return cliques
def wilcoxon_holm_cliques(results, labels, avranks, alpha):
"""Find cliques using Wilcoxon and post hoc Holm test."""
# get number of strategies:
n_estimators = results.shape[1]
# init array that contains the p-values calculated by the Wilcoxon signed rank test
p_values = []
# loop through the algorithms to compare pairwise
for i in range(n_estimators - 1):
# get the name of classifier one
classifier_1 = labels[i]
# get the performance of classifier one
perf_1 = np.array(results[:, i])
for j in range(i + 1, n_estimators):
# get the name of the second classifier
classifier_2 = labels[j]
# get the performance of classifier two
perf_2 = np.array(results[:, j])
# calculate the p_value
p_value = wilcoxon(perf_1, perf_2, zero_method="wilcox")[1]
# append to the list
p_values.append((classifier_1, classifier_2, p_value, False))
# get the number of hypothesis
n_hypothesis = len(p_values)
# sort the list in ascending manner of p-value
p_values.sort(key=operator.itemgetter(2))
# correct alpha with holm
new_alpha = float(alpha / (n_estimators - 1))
ordered_labels = [i for _, i in sorted(zip(avranks, labels))]
same = np.eye(len(ordered_labels), dtype=bool)
# loop through the hypothesis
for i in range(n_hypothesis):
# test if significant after holm's correction of alpha
if p_values[i][2] <= new_alpha:
p_values[i] = (p_values[i][0], p_values[i][1], p_values[i][2], True)
else:
idx_0 = np.where(np.array(ordered_labels) == p_values[i][0])[0][0]
idx_1 = np.where(np.array(ordered_labels) == p_values[i][1])[0][0]
same[idx_0][idx_1] = True
same[idx_1][idx_0] = True
cliques = build_cliques(same)
return cliques
def build_cliques(same):
"""Build cliques."""
n_estimators = same.shape[1]
for i in range(n_estimators):
if np.sum(same[i, :]) > 1:
true_values_i = np.where(same[i, :] == 1)[0]
first_true_i = true_values_i[0]
last_true_i = true_values_i[-1]
for j in range(i + 1, n_estimators):
if np.sum(same[j, :]) >= 1:
true_values_j = np.where(same[j, :] == 1)[0]
first_true_j = true_values_j[0]
last_true_j = true_values_j[-1]
# if j is contained in i
if first_true_i <= first_true_j and last_true_i >= last_true_j:
if len(true_values_i) >= len(true_values_j):
same[j, :] = 0
else:
same[i, :] = 0
# if i is contained in j
elif first_true_i >= first_true_j and last_true_i <= last_true_j:
if len(true_values_i) >= len(true_values_j):
same[j, :] = 0
else:
same[i, :] = 0
n = np.sum(same, 1)
cliques = same[n > 1, :]
return cliques
def plot_critical_difference(
scores,
labels,
highlight=None,
errors=False,
cliques=None,
clique_method="holm",
alpha=0.05,
width=6,
textspace=1.5,
reverse=True,
):
"""
Draw critical difference diagram.
Step 1 & 2: Calculate average ranks from data
Step 3: Use Friedman test to check whether
the strategy significantly affects the classification performance
Step 4: Compute critical differences using Nemenyi post-hoc test.
(How much should the average rank of two strategies differ to be
statistically significant)
Step 5: Compute statistically similar cliques of strategies
Step 6: Draw the diagram
See Janez Demsar, Statistical Comparisons of Classifiers over
Multiple Data Sets, 7(Jan):1--30, 2006.
Parts of the code are copied and adapted from here:
https://github.com/hfawaz/cd-diagram
Parameters
----------
scores : np.array
scores (either accuracies or errors) of dataset x strategy
labels : list of estimators
list with names of the estimators. Order should be the same as scores
highlight: dict with labels and HTML colours to be used, default = None
dict with labels and HTML colours to be used for highlighting. Order should
be the same as scores
errors : bool, default = False
indicates whether scores are passed as errors (default) or accuracies
cliques : lists of bit vectors, default = None
e.g. [[0,1,1,1,0,0], [0,0,0,0,1,1]]
statistically similiar cliques of estimators
If none, cliques will be computed depending on clique_method
clique_method : string, default = "holm"
clique forming method, to include "nemenyi" and "holm"
alpha : float default = 0.05
Alpha level for statistical tests currently supported: 0.1, 0.05 or 0.01)
width : int, default = 6
width in inches
textspace : int
space on figure sides (in inches) for the method names (default: 1.5)
reverse : bool, default = True
if set to 'True', the lowest rank is on the right
Returns
-------
fig: matplotlib.figure
Figure created.
Example
-------
>>> from aeon.benchmarking import plot_critical_difference
>>> from aeon.benchmarking.results_loaders import get_estimator_results_as_array
>>> methods = ["IT", "WEASEL-Dilation", "HIVECOTE2", "FreshPRINCE"]
>>> results = get_estimator_results_as_array(estimators=methods)
>>> plot = plot_critical_difference(results[0], methods, alpha=0.1)\
# doctest: +SKIP
>>> plot.show() # doctest: +SKIP
>>> plot.savefig("scatterplot.pdf", bbox_inches="tight") # doctest: +SKIP
"""
_check_soft_dependencies("matplotlib")
import matplotlib.pyplot as plt
# Helper Functions
# get number of datasets and strategies:
n_datasets, n_estimators = scores.shape[0], scores.shape[1]
# Step 1: rank data: best algorithm gets rank of 1 second best rank of 2...
# in case of ties average ranks are assigned
if errors:
# low is good -> rank 1
ranked_data = rankdata(scores, axis=1)
else:
# assign opposite ranks
ranked_data = rankdata(-1 * scores, axis=1)
# Step 2: calculate average rank per strategy
avranks = ranked_data.mean(axis=0)
# Sort labels
combined = zip(avranks, labels)
temp_labels = []
x = sorted(combined)
i = 0
for s, n in x:
avranks[i] = s
temp_labels.append(n)
i = i + 1
# sort out colours for labels
if highlight is not None:
colours = [
highlight[label] if label in highlight else "#000000"
for label in temp_labels
]
else:
colours = ["#000000"] * len(temp_labels)
# Step 3 : check whether Friedman test is significant
is_significant = _check_friedman(n_estimators, n_datasets, ranked_data, alpha)
# Step 4: If Friedman test is significant find cliques
if is_significant:
if cliques is None:
if clique_method == "nemenyi":
cliques = nemenyi_cliques(n_estimators, n_datasets, avranks, alpha)
elif clique_method == "holm":
cliques = wilcoxon_holm_cliques(
scores, labels, ranked_data.mean(axis=0), alpha
)
else:
raise ValueError(
"clique methods available are only nemenyi, bonferroni and holm."
)
# If Friedman test is not significant everything has to be one clique
else:
if cliques is None:
cliques = [
[
1,
]
* n_estimators
]
# Step 6 create the diagram:
# check from where to where the axis has to go
lowv = min(1, int(math.floor(min(avranks))))
highv = max(len(avranks), int(math.ceil(max(avranks))))
# set up the figure
width = float(width)
textspace = float(textspace)
cline = 0.6 # space needed above scale
linesblank = 1 # lines between scale and text
scalewidth = width - 2 * textspace
# calculate needed height
minnotsignificant = max(2 * 0.2, linesblank)
height = cline + ((n_estimators + 1) / 2) * 0.2 + minnotsignificant + 0.2
fig = plt.figure(figsize=(width, height))
fig.set_facecolor("white")
ax = fig.add_axes([0, 0, 1, 1]) # reverse y axis
ax.set_axis_off()
hf = 1.0 / height # height factor
wf = 1.0 / width
# Upper left corner is (0,0).
ax.plot([0, 1], [0, 1], c="w")
ax.set_xlim(0.1, 0.9)
ax.set_ylim(1, 0)
def _lloc(lst, n):
"""
List location in list of list structure.
Enable the use of negative locations:
-1 is the last element, -2 second last...
"""
if n < 0:
return len(lst[0]) + n
else:
return n
def _nth(lst, n):
n = _lloc(lst, n)
return [a[n] for a in lst]
def _hfl(lst):
return [a * hf for a in lst]
def _wfl(lst):
return [a * wf for a in lst]
def _line(lst, color="k", **kwargs):
ax.plot(_wfl(_nth(lst, 0)), _hfl(_nth(lst, 1)), color=color, **kwargs)
# draw scale
_line([(textspace, cline), (width - textspace, cline)], linewidth=2)
bigtick = 0.3
smalltick = 0.15
linewidth = 2.0
linewidth_sign = 4.0
def _rankpos(rank):
if not reverse:
a = rank - lowv
else:
a = highv - rank
return textspace + scalewidth / (highv - lowv) * a
# add ticks to scale
tick = None
for a in list(np.arange(lowv, highv, 0.5)) + [highv]:
tick = smalltick
if a == int(a):
tick = bigtick
_line([(_rankpos(a), cline - tick / 2), (_rankpos(a), cline)], linewidth=2)
def _text(x, y, s, *args, **kwargs):
ax.text(wf * x, hf * y, s, *args, **kwargs)
for a in range(lowv, highv + 1):
_text(
_rankpos(a),
cline - tick / 2 - 0.05,
str(a),
ha="center",
va="bottom",
size=16,
)
# sort out lines and text based on whether order is reversed or not
space_between_names = 0.24
for i in range(math.ceil(len(avranks) / 2)):
chei = cline + minnotsignificant + i * space_between_names
if reverse:
_line(
[
(_rankpos(avranks[i]), cline),
(_rankpos(avranks[i]), chei),
(textspace + scalewidth + 0.2, chei),
],
linewidth=linewidth,
color=colours[i],
)
_text( # labels left side.
textspace + scalewidth + 0.3,
chei,
temp_labels[i],
ha="left",
va="center",
size=16,
color=colours[i],
)
_text( # ranks left side.
textspace + scalewidth - 0.3,
chei - 0.075,
format(avranks[i], ".4f"),
ha="left",
va="center",
size=10,
color=colours[i],
)
else:
_line(
[
(_rankpos(avranks[i]), cline),
(_rankpos(avranks[i]), chei),
(textspace - 0.1, chei),
],
linewidth=linewidth,
color=colours[i],
)
_text( # labels left side.
textspace - 0.2,
chei,
temp_labels[i],
ha="right",
va="center",
size=16,
color=colours[i],
)
_text( # ranks left side.
textspace + 0.4,
chei - 0.075,
format(avranks[i], ".4f"),
ha="right",
va="center",
size=10,
color=colours[i],
)
for i in range(math.ceil(len(avranks) / 2), len(avranks)):
chei = cline + minnotsignificant + (len(avranks) - i - 1) * space_between_names
if reverse:
_line(
[
(_rankpos(avranks[i]), cline),
(_rankpos(avranks[i]), chei),
(textspace - 0.1, chei),
],
linewidth=linewidth,
color=colours[i],
)
_text( # labels right side.
textspace - 0.2,
chei,
temp_labels[i],
ha="right",
va="center",
size=16,
color=colours[i],
)
_text( # ranks right side.
textspace + 0.4,
chei - 0.075,
format(avranks[i], ".4f"),
ha="right",
va="center",
size=10,
color=colours[i],
)
else:
_line(
[
(_rankpos(avranks[i]), cline),
(_rankpos(avranks[i]), chei),
(textspace + scalewidth + 0.1, chei),
],
linewidth=linewidth,
color=colours[i],
)
_text( # labels right side.
textspace + scalewidth + 0.2,
chei,
temp_labels[i],
ha="left",
va="center",
size=16,
color=colours[i],
)
_text( # ranks right side.
textspace + scalewidth - 0.4,
chei - 0.075,
format(avranks[i], ".4f"),
ha="left",
va="center",
size=10,
color=colours[i],
)
# draw lines for cliques
start = cline + 0.2
side = -0.02 if reverse else 0.02
height = 0.1
i = 1
for clq in cliques:
positions = np.where(np.array(clq) == 1)[0]
min_idx = np.array(positions).min()
max_idx = np.array(positions).max()
_line(
[
(_rankpos(avranks[min_idx]) - side, start),
(_rankpos(avranks[max_idx]) + side, start),
],
linewidth=linewidth_sign,
)
start += height
return fig