-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathbenchmark_plot_tools.py
More file actions
1199 lines (1013 loc) · 40.6 KB
/
benchmark_plot_tools.py
File metadata and controls
1199 lines (1013 loc) · 40.6 KB
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import warnings
import numpy as np
from spikeinterface.widgets import get_some_colors
from .benchmark_tools import sigmoid, fit_sigmoid
def despine(ax_or_axes):
if not isinstance(ax_or_axes, (list, tuple, np.ndarray)):
ax_or_axes = [ax_or_axes]
for ax in np.array(ax_or_axes).flatten():
for loc in (
"top",
"right",
):
ax.spines[loc].set_visible(False)
def clean_axis(ax):
for loc in ("top", "right", "left", "bottom"):
ax.spines[loc].set_visible(False)
ax.set_xticks([])
ax.set_yticks([])
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
def plot_study_legend(study, case_keys=None, levels_to_group_by=None, ax=None, figsize=None):
"""
Make a ax with only legend
"""
import matplotlib.pyplot as plt
if case_keys is None:
case_keys = list(study.cases.keys())
if ax is None:
fig, ax = plt.subplots(figsize=figsize)
else:
fig = ax.get_figure()
if levels_to_group_by is None:
colors = study.get_colors()
for k in case_keys:
ax.plot([], color=colors[k], label=study.cases[k]["label"])
else:
colors = study.get_colors(levels_to_group_by=levels_to_group_by)
case_group_keys, labels = study.get_grouped_keys_mapping(
levels_to_group_by=levels_to_group_by, case_keys=case_keys
)
for k in case_group_keys.keys():
ax.plot([], color=colors[k], label=labels[k])
ax.legend()
clean_axis(ax)
return fig
def aggregate_dataframe_by_levels(df, study, case_keys=None, levels_to_group_by=None):
"""
Aggregate a DataFrame by dropping levels not to keep.
Parameters
----------
df : pd.DataFrame
A DataFrame with a MultiIndex.
study : BenchmarkStudy
A study object.
case_keys : list | None, default: None
A list of case keys to use. If None, then all cases are used.
levels_to_group_by : list | None, default: None
A list of levels to keep. If None, the original dataframe, keys, labels and colros are returned.
map_name : str | None, default: None
The name of the map to use for colors.
Returns
-------
df : pd.DataFrame
The aggregated DataFrame.
new_case_keys : list
The aggregated case keys.
labels : dict
A dictionary of labels.
colors : dict
A dictionary of colors for each new case key.
"""
import pandas as pd
if case_keys is None:
case_keys = list(study.cases.keys())
if levels_to_group_by is not None:
if not isinstance(levels_to_group_by, list):
levels_to_group_by = [levels_to_group_by]
drop_levels = [l for l in study.levels if l not in levels_to_group_by]
df = df.droplevel(drop_levels) # .sort_index()
if len(levels_to_group_by) > 1:
df = df.reorder_levels(levels_to_group_by)
new_case_keys = list(np.unique(df.index))
level_key = tuple(levels_to_group_by) if len(levels_to_group_by) > 1 else levels_to_group_by[0]
if level_key in study.labels_by_levels:
labels = study.labels_by_levels[level_key]
else:
if isinstance(df.index, pd.MultiIndex):
labels = {key: "-".join(key) for key in new_case_keys}
else:
labels = {key: key for key in new_case_keys}
# get colors
colors = study.get_colors(levels_to_group_by=levels_to_group_by)
else:
new_case_keys = case_keys
labels = {key: study.cases[key]["label"] for key in case_keys}
colors = study.get_colors()
return df, new_case_keys, labels, colors
def plot_run_times(
study, case_keys=None, mode="bar", levels_to_group_by=None, xticks_rotation=45.0, figsize=None, ax=None
):
"""
Plot run times for a BenchmarkStudy.
Parameters
----------
study : SorterStudy
A study object.
case_keys : list | None, default: None
A selection of cases to plot, if None, then all.
levels_to_group_by : list | None, default: None
A list of levels to keep. Run times are aggregated by these levels.
show_legend : bool, default True
Show legend or not
ax : matplotlib.axes.Axes | None, default: None
The axes to use for plotting.
Returns
-------
fig : matplotlib.figure.Figure
The resulting figure containing the plots
"""
import matplotlib.pyplot as plt
if case_keys is None:
case_keys = list(study.cases.keys())
run_times = study.get_run_times(case_keys=case_keys)
if ax is None:
fig, ax = plt.subplots(figsize=figsize)
else:
fig = ax.get_figure()
if levels_to_group_by is None:
colors = study.get_colors()
labels = []
for i, key in enumerate(case_keys):
labels.append(study.cases[key]["label"])
rt = run_times.at[key, "run_times"]
ax.bar(i, rt, width=0.8, color=colors[key])
ax.set_xticks(np.arange(len(labels)))
ax.set_xticklabels(labels, rotation=45.0)
ax.set_ylabel("Run times (s)")
else:
colors = study.get_colors(levels_to_group_by=levels_to_group_by)
keys_mapping, labels = study.get_grouped_keys_mapping(
levels_to_group_by=levels_to_group_by, case_keys=case_keys
)
if mode == "bar":
x, height, yerr, color = [], [], [], []
for i, key in enumerate(keys_mapping):
if len(levels_to_group_by) == 1:
rt = run_times.xs((key,), level=levels_to_group_by).values.flatten()
else:
rt = run_times.xs(key, level=levels_to_group_by).values.flatten()
x.append(i + 1)
height.append(np.mean(rt))
if rt.size > 1:
yerr.append(np.std(rt))
else:
yerr.append(0)
color.append(colors[key])
ax.bar(x, height, yerr=yerr, color=color, ecolor="k")
elif mode == "box":
rts = []
for i, key in enumerate(keys_mapping):
if len(levels_to_group_by) == 1:
rt = run_times.xs((key,), level=levels_to_group_by).values.flatten()
else:
rt = run_times.xs(key, level=levels_to_group_by).values.flatten()
rts.append(rt)
lines = ax.boxplot(rts)
for k in lines:
for i, e in enumerate(lines[k]):
# small hack to control the colors
if len(lines[k]) == len(rts):
color = colors[list(keys_mapping.keys())[i]]
elif len(lines[k]) == len(rts) * 2:
color = colors[list(keys_mapping.keys())[i // 2]]
else:
color = "k"
e.set_color(color)
ax.set_ylabel("Run times (s)")
labels_list = [labels[k] for k in keys_mapping]
ax.set_xticks(np.arange(len(labels_list)) + 1)
ax.set_xticklabels(labels_list, rotation=xticks_rotation)
despine(ax)
return fig
def plot_unit_counts(
study,
case_keys=None,
levels_to_group_by=None,
colors=None,
columns=None,
with_rectangle=True,
revert_bad=True,
xticks_rotation=45.0,
figsize=None,
ax=None,
):
"""
Plot unit counts for a study: "num_well_detected", "num_false_positive", "num_redundant", "num_overmerged"
Parameters
----------
study : SorterStudy
A study object.
case_keys : list or None
A selection of cases to plot, if None, then all.
levels_to_group_by : list | None, default: None
A list of levels to keep. Unit counts are aggregated by these levels.
colors : dict | None, default: None
A dictionary of colors to use for each class ("Well Detected", "False Positive", "Redundant", "Overmerged").
columns : None | list
Optionaly select which columns to display
with_rectangle : bool
Add or not a grouping colored rectangle for each case.
revert_bad : bool
Revert or not bad columns ('num_false_positive', 'num_redundant', 'num_overmerged' ...)
figsize : tuple | None, default: None
The size of the figure.
ax : matplotlib.axes.Axes | None, default: None
The axes to use for plotting.
Returns
-------
fig : matplotlib.figure.Figure
The resulting figure containing the plots
"""
import matplotlib.pyplot as plt
from spikeinterface.widgets.utils import get_some_colors
if case_keys is None:
case_keys = list(study.cases.keys())
if ax is None:
fig, ax = plt.subplots(figsize=figsize)
else:
fig = ax.get_figure()
count_units = study.get_count_units(case_keys=case_keys)
keys_mapping, labels = study.get_grouped_keys_mapping(levels_to_group_by=levels_to_group_by, case_keys=case_keys)
if columns is None:
columns = count_units.columns.tolist()
columns.remove("num_gt")
columns.remove("num_sorter")
ncol = len(columns)
width = 1 / (ncol + 2)
if colors is None:
colors = get_some_colors(columns, color_engine="auto", map_name="hot")
colors["num_well_detected"] = "green"
case_colors = study.get_colors(levels_to_group_by=levels_to_group_by)
ymin, ymax = np.inf, -np.inf
for i, key in enumerate(keys_mapping):
for c, col in enumerate(columns):
x = i + 1 + c / (ncol + 1)
if levels_to_group_by is None:
y = count_units.at[key, col]
yerr = None
else:
if len(levels_to_group_by) == 1:
all_y = count_units.xs((key,), level=levels_to_group_by)[col].values
else:
all_y = count_units.xs(key, level=levels_to_group_by)[col].values
y = np.mean(all_y)
if len(all_y) > 1:
yerr = [np.std(all_y)]
else:
yerr = None
if not "well_detected" in col and revert_bad:
y = -y
if i == 0:
label = col.replace("num_", "").replace("_", " ").title()
else:
label = None
ax.bar([x], [y], yerr=yerr, width=width, label=label, color=colors[col])
if yerr is None:
ymin = min(ymin, y)
ymax = max(ymax, y)
else:
ymin = min(ymin, y - yerr[0])
ymax = max(ymax, y + yerr[0])
if with_rectangle:
if revert_bad:
ymin = 0
spacing = width * 0.3
for i, key in enumerate(keys_mapping):
rect = plt.Rectangle(
(i + 1 - width / 2 - spacing, ymin),
1 - spacing,
ymax - ymin,
linewidth=2,
edgecolor=case_colors[key],
facecolor="none",
)
ax.add_patch(rect)
labels_list = [labels[k] for k in keys_mapping]
xticklabels = labels_list
ax.set_xticks(np.arange(len(xticklabels)) + 1.5 - width)
ax.set_xticklabels(xticklabels, rotation=xticks_rotation)
ax.legend()
despine(ax)
return fig
def plot_agreement_matrix(study, ordered=True, case_keys=None, axs=None):
"""
Plot agreement matri ces for cases in a study.
Parameters
----------
study : GroundTruthStudy
A study object.
case_keys : list or None
A selection of cases to plot, if None, then all.
ordered : bool
Order units with best agreement scores.
This enable to see agreement on a diagonal.
axs : matplotlib.axes.Axes | None, default: None
The axs to use for plotting. Should be the same size as len(case_keys).
Returns
-------
fig : matplotlib.figure.Figure
The resulting figure containing the plots
"""
import matplotlib.pyplot as plt
from spikeinterface.widgets import AgreementMatrixWidget
if case_keys is None:
case_keys = list(study.cases.keys())
num_axes = len(case_keys)
if axs is None:
fig, axs = plt.subplots(ncols=num_axes, squeeze=False)
axs = axs[0, :]
else:
assert len(axs) == num_axes, "axs should have the same number of axes as case_keys"
fig = axs[0].get_figure()
for count, key in enumerate(case_keys):
ax = axs[count]
comp = study.get_result(key)["gt_comparison"]
unit_ticks = len(comp.sorting1.unit_ids) <= 16
count_text = len(comp.sorting1.unit_ids) <= 16
AgreementMatrixWidget(
comp, ordered=ordered, count_text=count_text, unit_ticks=unit_ticks, backend="matplotlib", ax=ax
)
label = study.cases[key]["label"]
ax.set_xlabel(label)
if count > 0:
ax.set_ylabel(None)
ax.set_yticks([])
ax.set_xticks([])
return fig
def _plot_performances_vs_metric(
study,
metric_name,
case_keys=None,
figsize=None,
performance_names=("accuracy", "recall", "precision"),
metric_dataset_reference=None,
levels_to_group_by=None,
orientation="vertical",
show_legend=True,
show_scatter=True,
with_sigmoid_fit=False,
show_average_by_bin=True,
scatter_size=4,
scatter_alpha=1.0,
num_bin_average=20,
axs=None,
):
import matplotlib.pyplot as plt
if case_keys is None:
case_keys = list(study.cases.keys())
if orientation == "vertical":
ncols = 1
nrows = len(performance_names)
elif orientation == "horizontal":
ncols = len(performance_names)
nrows = 1
else:
raise ValueError("orientation must be 'vertical' or 'horizontal'")
if axs is None:
fig, axs = plt.subplots(ncols=ncols, nrows=nrows, figsize=figsize, squeeze=False)
if orientation == "vertical":
axs = axs[:, 0]
else:
axs = axs[0, :]
else:
assert len(axs) == len(performance_names), "axs should have the same number of axes as performance_names"
fig = axs[0].get_figure()
for count, performance_name in enumerate(performance_names):
ax = axs[count]
if levels_to_group_by is not None:
case_group_keys, labels = study.get_grouped_keys_mapping(
levels_to_group_by=levels_to_group_by, case_keys=case_keys
)
else:
labels = {k: study.cases[k]["label"] for k in case_keys}
case_group_keys = {k: [k] for k in case_keys}
colors = study.get_colors(levels_to_group_by=levels_to_group_by)
assert all(
[key in colors for key in case_group_keys]
), f"colors must have a color for each case key: {case_group_keys}"
for key, key_list in case_group_keys.items():
color = colors[key]
label = labels[key]
all_xs = []
all_ys = []
for sub_key in key_list:
if metric_dataset_reference is None:
# use the SNR of each dataset
analyzer = study.get_sorting_analyzer(sub_key)
else:
# use the same SNR from a reference dataset
analyzer = study.get_sorting_analyzer(dataset_key=metric_dataset_reference)
quality_metrics = analyzer.get_extension("quality_metrics").get_data()
x = quality_metrics[metric_name].to_numpy(dtype="float64")
y = (
study.get_result(sub_key)["gt_comparison"]
.get_performance()[performance_name]
.to_numpy(dtype="float64")
)
mask = ~np.isnan(x) & ~np.isnan(y)
all_xs.append(x[mask])
all_ys.append(y[mask])
if with_sigmoid_fit:
max_snr = max(np.max(x) for x in all_xs)
xfit = np.linspace(0, max_snr, 100)
all_y_fit = []
for x, y in zip(all_xs, all_ys):
popt = fit_sigmoid(x, y, p0=None)
fit_y = sigmoid(xfit, *popt)
all_y_fit.append(fit_y)
average_y_fit = np.nanmean(np.stack(all_y_fit, axis=1), axis=1)
ax.plot(xfit, average_y_fit, color=color)
if len(all_y_fit) > 1:
std_y_fit = np.nanstd(np.stack(all_y_fit, axis=1), axis=1)
ax.fill_between(xfit, average_y_fit - std_y_fit, average_y_fit + std_y_fit, color=color, alpha=0.5)
if show_average_by_bin:
from scipy.stats import binned_statistic
bins = np.percentile(np.concatenate(all_xs), np.linspace(0, 100, num_bin_average + 1))
bin_centers = bins[:-1] + np.diff(bins) / 2.0
all_average = []
for x, y in zip(all_xs, all_ys):
average, bins, _ = binned_statistic(x, y, statistic="mean", bins=bins)
ax.plot(bin_centers, average, color=color, lw=0.5)
all_average.append(average)
average_y = np.nanmean(np.stack(all_average, axis=1), axis=1)
ax.plot(bin_centers, average_y, color=color)
if len(all_average) > 1:
std_y = np.nanstd(np.stack(all_average, axis=1), axis=1)
ax.fill_between(bin_centers, average_y - std_y, average_y + std_y, color=color, alpha=0.5)
# accumulate x and y and make one final plot
all_xs = np.concatenate(all_xs)
all_ys = np.concatenate(all_ys)
if show_scatter:
ax.scatter(all_xs, all_ys, marker=".", label=label, color=color, s=scatter_size, alpha=scatter_alpha)
ax.set_ylabel(performance_name)
ax.set_ylim(-0.05, 1.05)
if show_legend and (count == len(performance_names) - 1):
ax.legend()
despine(axs)
return fig
def plot_performances_vs_snr(
study,
case_keys=None,
figsize=None,
performance_names=("accuracy", "recall", "precision"),
metric_dataset_reference=None,
levels_to_group_by=None,
orientation="vertical",
show_legend=True,
show_scatter=True,
with_sigmoid_fit=False,
show_average_by_bin=True,
scatter_size=4,
scatter_alpha=1.0,
num_bin_average=20,
axs=None,
):
"""
Plots performance metrics against signal-to-noise ratio (SNR) for different cases in a study.
Parameters
----------
study : object
The study object containing the cases and results.
case_keys : list | None, default: None
List of case keys to include in the plot. If None, all cases in the study are included.
figsize : tuple | None, default: None
Size of the figure.
performance_names : tuple, default: ("accuracy", "recall", "precision")
Names of the performance metrics to plot. Default is ("accuracy", "recall", "precision").
metric_dataset_reference : str | None, default: None
Reference dataset metric key to use. If None, the SNR of each dataset is used.
levels_to_group_by : list | None, default: None
Levels to group by when mapping case keys.
orientation : "vertical" | "horizontal", default: "vertical"
The orientation of the plot.
show_legend : bool, default True
Show legend or not
show_scatter : bool, default True
Show scatter or not
show_sigmoid_fit : bool, default True
Show sigmoid that fit the performances.
show_average_by_bin : bool, default False
Instead of the sigmoid an average by bins can be plotted.
scatter_size : int, default 4
scatter size
scatter_alpha : float, default 1.0
scatter alpha
num_bin_average : int, default 2
Num bin for average
axs : matplotlib.axes.Axes | None, default: None
The axs to use for plotting. Should be the same size as len(performance_names).
Returns
-------
fig : matplotlib.figure.Figure
The resulting figure containing the plots.
"""
return _plot_performances_vs_metric(
study,
"snr",
case_keys=case_keys,
figsize=figsize,
performance_names=performance_names,
metric_dataset_reference=metric_dataset_reference,
levels_to_group_by=levels_to_group_by,
orientation=orientation,
show_legend=show_legend,
show_scatter=show_scatter,
with_sigmoid_fit=with_sigmoid_fit,
show_average_by_bin=show_average_by_bin,
scatter_size=scatter_size,
scatter_alpha=scatter_alpha,
num_bin_average=num_bin_average,
axs=axs,
)
def plot_performances_vs_firing_rate(
study,
case_keys=None,
figsize=None,
performance_names=("accuracy", "recall", "precision"),
metric_dataset_reference=None,
levels_to_group_by=None,
orientation="vertical",
show_legend=True,
show_scatter=True,
with_sigmoid_fit=False,
show_average_by_bin=True,
scatter_size=4,
scatter_alpha=1.0,
num_bin_average=20,
axs=None,
):
"""
Plots performance metrics against firing rate for different cases in a study.
Parameters
----------
study : object
The study object containing the cases and results.
case_keys : list | None, default: None
List of case keys to include in the plot. If None, all cases in the study are included.
figsize : tuple | None, default: None
Size of the figure.
performance_names : tuple, default: ("accuracy", "recall", "precision")
Names of the performance metrics to plot. Default is ("accuracy", "recall", "precision").
metric_dataset_reference : str | None, default: None
Reference dataset metric key to use. If None, the SNR of each dataset is used.
levels_to_group_by : list | None, default: None
Levels to group by when mapping case keys.
orientation : "vertical" | "horizontal", default: "vertical"
The orientation of the plot.
show_legend : bool, default True
Show legend or not
show_scatter : bool, default True
Show scatter or not
show_sigmoid_fit : bool, default True
Show sigmoid that fit the performances.
show_average_by_bin : bool, default False
Instead of the sigmoid an average by bins can be plotted.
scatter_size : int, default 4
scatter size
scatter_alpha : float, default 1.0
scatter alpha
num_bin_average : int, default 2
Num bin for average
axs : matplotlib.axes.Axes | None, default: None
The axs to use for plotting. Should be the same size as len(performance_names).
Returns
-------
fig : matplotlib.figure.Figure
The resulting figure containing the plots.
"""
return _plot_performances_vs_metric(
study,
"firing_rate",
case_keys=case_keys,
figsize=figsize,
performance_names=performance_names,
metric_dataset_reference=metric_dataset_reference,
levels_to_group_by=levels_to_group_by,
orientation=orientation,
show_legend=show_legend,
show_scatter=show_scatter,
with_sigmoid_fit=with_sigmoid_fit,
show_average_by_bin=show_average_by_bin,
scatter_size=scatter_size,
scatter_alpha=scatter_alpha,
num_bin_average=num_bin_average,
axs=axs,
)
def plot_performances_ordered(
study,
case_keys=None,
performance_names=("accuracy", "recall", "precision"),
levels_to_group_by=None,
orientation="vertical",
show_legend=True,
figsize=None,
axs=None,
):
"""
Plot performances ordered by decreasing performance.
Parameters
----------
study : BenchmarkStudy
A study object.
case_keys : list | None, default: None
A selection of cases to plot, if None, then all.
performance_names : list | tuple, default: ("accuracy", "recall", "precision")
A list of performance names to plot.
levels_to_group_by : list | None, default: None
A list of levels to keep. Performances are aggregated by these levels.
orientation : "vertical" | "horizontal", default: "vertical"
The orientation of the plot.
show_legend : bool, default True
Show legend or not
figsize : tuple | None, default: None
The size of the figure.
axs : matplotlib.axes.Axes | None, default: None
The axs to use for plotting. Should be the same size as len(performance_names).
Returns
-------
fig : matplotlib.figure.Figure
The resulting figure containing the plots.
"""
import matplotlib.pyplot as plt
num_axes = len(performance_names)
if case_keys is None:
case_keys = list(study.cases.keys())
perfs = study.get_performance_by_unit(case_keys=case_keys)
perfs, case_keys, labels, colors = aggregate_dataframe_by_levels(perfs, study, case_keys, levels_to_group_by)
assert all([key in colors for key in case_keys]), f"colors must have a color for each case key: {case_keys}"
if axs is None:
if orientation == "vertical":
fig, axs = plt.subplots(nrows=num_axes, figsize=figsize, squeeze=False)
axs = axs[:, 0]
elif orientation == "horizontal":
fig, axs = plt.subplots(ncols=num_axes, figsize=figsize, squeeze=False)
axs = axs[0, :]
else:
raise ValueError("orientation must be 'vertical' or 'horizontal'")
else:
assert len(axs) == num_axes, "axs should have the same number of axes as performance_names"
fig = axs[0].get_figure()
for count, performance_name in enumerate(performance_names):
ax = axs[count]
for key in case_keys:
color = colors[key]
label = labels[key]
val = perfs.xs(key).loc[:, performance_name].values
val = np.sort(val)[::-1]
ax.plot(val, label=label, c=color)
ax.set_title(performance_name)
if show_legend and (count == len(performance_names) - 1):
ax.legend(bbox_to_anchor=(0.05, 0.05), loc="lower left", framealpha=0.8)
despine(axs)
return fig
def plot_performances_swarm(
study,
case_keys=None,
performance_names=("accuracy", "recall", "precision"),
figsize=None,
levels_to_group_by=None,
performance_colors={"accuracy": "g", "recall": "b", "precision": "r"},
ax=None,
):
"""
Parameters
----------
study : BenchmarkStudy
A study object.
case_keys : list | None, default: None
A selection of cases to plot, if None, then all.
performance_names : list | tuple, default: ("accuracy", "recall", "precision")
A list of performance names to plot.
levels_to_group_by : list | None, default: None
A list of levels to keep. Performances are aggregated by these levels.
performance_colors : dict, default: {"accuracy": "g", "recall": "b", "precision": "r"}
A dictionary of colors to use for each performance name.
figsize : tuple | None, default: None
The size of the figure.
ax : matplotlib.axes.Axes | None, default: None
The ax to use for plotting
Returns
-------
fig : matplotlib.figure.Figure
The resulting figure containing the plots
"""
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
if case_keys is None:
case_keys = list(study.cases.keys())
perfs = study.get_performance_by_unit(case_keys=case_keys)
perfs, case_keys, _, _ = aggregate_dataframe_by_levels(perfs, study, case_keys, levels_to_group_by)
assert all(
[key in performance_colors for key in performance_names]
), f"performance_colors must have a color for each performance name: {performance_names}"
if ax is None:
fig, ax = plt.subplots(figsize=figsize)
else:
fig = ax.get_figure()
levels = perfs.index.names
df = pd.melt(
perfs.reset_index(),
id_vars=levels,
var_name="Metric",
value_name="Score",
value_vars=performance_names,
)
df["x"] = df.apply(lambda r: " ".join([str(r[col]) for col in levels]), axis=1)
sns.swarmplot(data=df, x="x", y="Score", hue="Metric", dodge=True, ax=ax, palette=performance_colors)
despine(ax)
return fig
def plot_performances_comparison(
study,
case_keys=None,
figsize=None,
performance_names=("accuracy", "recall", "precision"),
performance_colors={"accuracy": "g", "recall": "b", "precision": "r"},
levels_to_group_by=None,
ylim=(-0.1, 1.1),
axs=None,
):
"""
Plot performances comparison for a study.
Parameters
----------
study : BenchmarkStudy
A study object.
case_keys : list | None, default: None
A selection of cases to plot, if None, then all.
figsize : tuple | None, default: None
The size of the figure.
performance_names : list | tuple, default: ("accuracy", "recall", "precision")
A list of performance names to plot.
performance_colors : dict, default: {"accuracy": "g", "recall": "b", "precision": "r"}
A dictionary of colors to use for each performance name.
levels_to_group_by : list | None, default: None
A list of levels to keep. Performances are aggregated by these levels.
ylim : tuple, default: (-0.1, 1.1)
The y-axis limits.
Returns
-------
fig : matplotlib.figure.Figure
The resulting figure containing the plots.
"""
import matplotlib.pyplot as plt
if case_keys is None:
case_keys = list(study.cases.keys())
case_keys, labels = study.get_grouped_keys_mapping(levels_to_group_by=levels_to_group_by, case_keys=case_keys)
num_methods = len(case_keys)
assert num_methods >= 2, "plot_performances_comparison need at least 2 cases!"
assert all(
[key in performance_colors for key in performance_names]
), f"performance_colors must have a color for each performance name: {performance_names}"
if axs is None:
fig, axs = plt.subplots(ncols=num_methods - 1, nrows=num_methods - 1, figsize=figsize, squeeze=False)
for i, key1 in enumerate(case_keys):
for j, key2 in enumerate(case_keys):
if i < j:
ax = axs[i, j - 1]
label1 = labels[key1]
label2 = labels[key2]
if i == j - 1:
ax.set_xlabel(label2)
ax.set_ylabel(label1)
for sub_key1 in case_keys[key1]:
for sub_key2 in case_keys[key2]:
comp1 = study.get_result(sub_key1)["gt_comparison"]
comp2 = study.get_result(sub_key2)["gt_comparison"]
for performance_name in performance_names:
color = performance_colors[performance_name]
perf1 = comp1.get_performance()[performance_name]
perf2 = comp2.get_performance()[performance_name]
ax.scatter(perf2, perf1, marker=".", label=performance_name, color=color)
ax.plot([0, 1], [0, 1], "k--", alpha=0.5)
ax.set_ylim(ylim)
ax.set_xlim(ylim)
despine(ax)
ax.set_aspect("equal")
if i != j - 1:
ax.set_xlabel("")
ax.set_ylabel("")
ax.set_xticks([])
ax.set_yticks([])
ax.set_xticklabels([])
ax.set_yticklabels([])
else:
if j >= 1 and i < num_methods - 1:
ax = axs[i, j - 1]
ax.axis("off")
ax = axs[num_methods - 2, 0]
patches = []
from matplotlib.patches import Patch
for performance_name in performance_names:
color = performance_colors[performance_name]
patches.append(Patch(color=color, label=performance_name))
ax.legend(handles=patches)
fig = ax.figure
fig.subplots_adjust(hspace=0.1, wspace=0.1)
return fig
def plot_performances_vs_depth_and_snr(
study,
performance_name="accuracy",
case_keys=None,
figsize=None,
levels_to_group_by=None,
map_name="viridis",
axs=None,
):
"""
Plot performances vs depth and snr for a study.
Parameters
----------
study : BenchmarkStudy
A study object.
performance_name : str, default: "accuracy"
The performance metric to plot.
case_keys : list | None, default: None
A selection of cases to plot, if None, then all.
levels_to_group_by : list | None, default: None
A list of levels to keep. Performances are aggregated by these levels.
map_name : str | None, default: "viridis"
The name of the map to use for colors.
figsize : tuple | None, default: None
The size of the figure.
axs : matplotlib.axes.Axes | None, default: None
The axs to use for plotting. Should be the same size as len(case_keys).
Returns
-------
fig : matplotlib.figure.Figure
The resulting figure containing the plots.
"""
import matplotlib.pyplot as plt
if case_keys is None:
case_keys = list(study.cases.keys())
case_keys, labels = study.get_grouped_keys_mapping(levels_to_group_by=levels_to_group_by, case_keys=case_keys)
if axs is None:
fig, axs = plt.subplots(ncols=len(case_keys), figsize=figsize, squeeze=False)
axs = axs[0, :]
else:
assert len(axs) == len(case_keys), "axs should have the same number of axes as case_keys"
fig = axs[0].get_figure()
for count, (key, key_list) in enumerate(case_keys.items()):
all_snrs = []
all_perfs = []
all_depths = []