DARTsort 0.3.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. dartsort/__init__.py +68 -0
  2. dartsort/cli.py +105 -0
  3. dartsort/clustering/__init__.py +3 -0
  4. dartsort/clustering/cluster_util.py +392 -0
  5. dartsort/clustering/clustering.py +765 -0
  6. dartsort/clustering/clustering_features.py +202 -0
  7. dartsort/clustering/density.py +1299 -0
  8. dartsort/clustering/forward_backward.py +219 -0
  9. dartsort/clustering/gmm/mixture.py +5790 -0
  10. dartsort/clustering/gmm/stable_features.py +993 -0
  11. dartsort/clustering/kmeans.py +448 -0
  12. dartsort/clustering/merge.py +501 -0
  13. dartsort/clustering/modes.py +214 -0
  14. dartsort/clustering/refine_util.py +191 -0
  15. dartsort/clustering/relocate.py +109 -0
  16. dartsort/clustering/split.py +1285 -0
  17. dartsort/config.py +288 -0
  18. dartsort/detect/__init__.py +2 -0
  19. dartsort/detect/detect.py +449 -0
  20. dartsort/detect/detect_filters.py +37 -0
  21. dartsort/evaluate/__init__.py +1 -0
  22. dartsort/evaluate/analysis.py +594 -0
  23. dartsort/evaluate/comparison.py +534 -0
  24. dartsort/evaluate/hybrid_util.py +599 -0
  25. dartsort/evaluate/noise_recording_tools.py +184 -0
  26. dartsort/evaluate/sim_template_tools.py +842 -0
  27. dartsort/evaluate/simkit.py +840 -0
  28. dartsort/evaluate/simlib.py +270 -0
  29. dartsort/localize/__init__.py +0 -0
  30. dartsort/localize/localize_torch.py +291 -0
  31. dartsort/localize/localize_util.py +313 -0
  32. dartsort/main.py +657 -0
  33. dartsort/peel/__init__.py +6 -0
  34. dartsort/peel/grab.py +208 -0
  35. dartsort/peel/matching.py +628 -0
  36. dartsort/peel/matching_util/__init__.py +3 -0
  37. dartsort/peel/matching_util/compressed_upsampled.py +718 -0
  38. dartsort/peel/matching_util/drifty.py +791 -0
  39. dartsort/peel/matching_util/matching_base.py +616 -0
  40. dartsort/peel/matching_util/pairwise.py +359 -0
  41. dartsort/peel/matching_util/pairwise_util.py +1317 -0
  42. dartsort/peel/peel_base.py +1000 -0
  43. dartsort/peel/running_template.py +1498 -0
  44. dartsort/peel/subtract.py +1123 -0
  45. dartsort/peel/threshold.py +471 -0
  46. dartsort/peel/universal.py +191 -0
  47. dartsort/pretrained/default_temporal_kernel.npy +0 -0
  48. dartsort/pretrained/detect_np1.pt +0 -0
  49. dartsort/pretrained/detect_np2.pt +0 -0
  50. dartsort/pretrained/glasbey1024.npz +0 -0
  51. dartsort/pretrained/offset_trained_denoiser.pt +0 -0
  52. dartsort/pretrained/offset_trained_denoiser_added_temp.pt +0 -0
  53. dartsort/pretrained/offset_trained_denoiser_added_temp_boost.pt +0 -0
  54. dartsort/pretrained/original_single_chan_denoiser.pt +0 -0
  55. dartsort/pretrained/single_chan_denoiser.pt +0 -0
  56. dartsort/templates/__init__.py +11 -0
  57. dartsort/templates/get_templates.py +771 -0
  58. dartsort/templates/postprocess_util.py +555 -0
  59. dartsort/templates/realignment.py +466 -0
  60. dartsort/templates/superres_util.py +204 -0
  61. dartsort/templates/template_util.py +518 -0
  62. dartsort/templates/templates.py +528 -0
  63. dartsort/transform/__init__.py +2 -0
  64. dartsort/transform/_multichan_denoiser_kit.py +650 -0
  65. dartsort/transform/all_transformers.py +58 -0
  66. dartsort/transform/amortized_localization.py +503 -0
  67. dartsort/transform/amplitudes.py +197 -0
  68. dartsort/transform/decollider.py +635 -0
  69. dartsort/transform/enforce_decrease.py +158 -0
  70. dartsort/transform/localize.py +86 -0
  71. dartsort/transform/matching_denoiser.py +44 -0
  72. dartsort/transform/pipeline.py +391 -0
  73. dartsort/transform/single_channel_denoiser.py +153 -0
  74. dartsort/transform/single_channel_templates.py +98 -0
  75. dartsort/transform/supervised_denoiser.py +486 -0
  76. dartsort/transform/temporal_pca.py +413 -0
  77. dartsort/transform/transform_base.py +255 -0
  78. dartsort/util/__init__.py +0 -0
  79. dartsort/util/cli_util.py +179 -0
  80. dartsort/util/data_util.py +1128 -0
  81. dartsort/util/drift_util.py +1054 -0
  82. dartsort/util/internal_config.py +1074 -0
  83. dartsort/util/interpolation_util.py +1012 -0
  84. dartsort/util/job_util.py +19 -0
  85. dartsort/util/logging_util.py +91 -0
  86. dartsort/util/main_util.py +235 -0
  87. dartsort/util/more_operators.py +269 -0
  88. dartsort/util/multiprocessing_util.py +211 -0
  89. dartsort/util/nn_util.py +461 -0
  90. dartsort/util/noise_util.py +1461 -0
  91. dartsort/util/peel_util.py +223 -0
  92. dartsort/util/py_util.py +213 -0
  93. dartsort/util/registration_util.py +128 -0
  94. dartsort/util/residual_util.py +30 -0
  95. dartsort/util/sparse_util.py +81 -0
  96. dartsort/util/spikeio.py +227 -0
  97. dartsort/util/spiketorch.py +1198 -0
  98. dartsort/util/testing_util/__init__.py +0 -0
  99. dartsort/util/testing_util/matching_debug_util.py +517 -0
  100. dartsort/util/testing_util/mixture_testing_util.py +291 -0
  101. dartsort/util/torch_optimization_util.py +156 -0
  102. dartsort/util/torch_util.py +75 -0
  103. dartsort/util/universal_util.py +269 -0
  104. dartsort/util/waveform_util.py +809 -0
  105. dartsort/vis/__init__.py +19 -0
  106. dartsort/vis/analysis_plots.py +392 -0
  107. dartsort/vis/colors.py +36 -0
  108. dartsort/vis/gt.py +392 -0
  109. dartsort/vis/layout.py +149 -0
  110. dartsort/vis/metrics_util.py +23 -0
  111. dartsort/vis/mixture.py +1489 -0
  112. dartsort/vis/over_time.py +514 -0
  113. dartsort/vis/recanim.py +429 -0
  114. dartsort/vis/scatterplots.py +779 -0
  115. dartsort/vis/sorting.py +163 -0
  116. dartsort/vis/unit.py +917 -0
  117. dartsort/vis/unit_comparison.py +1017 -0
  118. dartsort/vis/versus.py +300 -0
  119. dartsort/vis/vismain.py +537 -0
  120. dartsort/vis/waveforms.py +357 -0
  121. dartsort-0.3.5.dist-info/METADATA +93 -0
  122. dartsort-0.3.5.dist-info/RECORD +125 -0
  123. dartsort-0.3.5.dist-info/WHEEL +5 -0
  124. dartsort-0.3.5.dist-info/entry_points.txt +2 -0
  125. dartsort-0.3.5.dist-info/top_level.txt +1 -0
dartsort/vis/gt.py ADDED
@@ -0,0 +1,392 @@
1
+ import matplotlib.pyplot as plt
2
+ import numpy as np
3
+ import seaborn as sns
4
+ from matplotlib.colors import FuncNorm
5
+ import warnings
6
+
7
+ from .layout import BasePlot, flow_layout
8
+
9
+
10
+ table_cmap = ["managua", "cividis"]
11
+ for table_cmap in table_cmap:
12
+ if table_cmap in plt.colormaps:
13
+ break
14
+ else:
15
+ assert False
16
+
17
+
18
+ class ComparisonPlot(BasePlot):
19
+ kind = "comparison"
20
+ width = 2
21
+ height = 2
22
+
23
+ def draw(self, panel, comparison):
24
+ raise NotImplementedError
25
+
26
+
27
+ class AgreementMatrix(ComparisonPlot):
28
+ kind = "wide"
29
+ width = 3
30
+ height = 1
31
+
32
+ def draw(self, panel, comparison):
33
+ agreement = comparison.comparison.get_ordered_agreement_scores()
34
+ ax = panel.subplots()
35
+ ax.imshow(agreement, vmin=0, vmax=1)
36
+ # plt.colorbar(im, ax=ax, shrink=0.3)
37
+ ax.set_title("all agreements")
38
+ ax.set_ylabel(f"{comparison.gt_name} unit")
39
+ ax.set_xlabel(f"{comparison.tested_name} unit")
40
+
41
+
42
+ class TrimmedAgreementMatrix(ComparisonPlot):
43
+ kind = "matrix"
44
+ width = 3
45
+ height = 2
46
+
47
+ def __init__(self, trim_kind="auto", ordered=True, cmap=table_cmap):
48
+ self.trim_kind = trim_kind
49
+ self.ordered = ordered
50
+ self.cmap = cmap
51
+
52
+ def draw(self, panel, comparison):
53
+ if self.ordered:
54
+ agreement = comparison.comparison.get_ordered_agreement_scores()
55
+ else:
56
+ agreement = comparison.comparison.agreement_scores
57
+ if self.trim_kind == "auto":
58
+ pass
59
+ else:
60
+ assert self.ordered
61
+ agreement.values[:, : agreement.shape[0]]
62
+ ax = panel.subplots()
63
+ im = ax.imshow(agreement.T, vmin=0, vmax=1, cmap=self.cmap)
64
+ plt.colorbar(im, ax=ax, shrink=0.3)
65
+ ax.set_title("Hung. match agreements")
66
+ ax.set_xlabel(f"{comparison.gt_name} unit" + ("(ord)" * self.ordered))
67
+ ax.set_ylabel(f"{comparison.tested_name} unit" + ("(ord)" * self.ordered))
68
+
69
+
70
+ class TrimmedTemplateDistanceMatrix(ComparisonPlot):
71
+ kind = "matrix"
72
+ width = 3
73
+ height = 2
74
+
75
+ def __init__(self, trim_kind="auto", ordered=True, cmap=plt.cm.magma):
76
+ self.trim_kind = trim_kind
77
+ self.ordered = ordered
78
+ self.cmap = cmap
79
+
80
+ def draw(self, panel, comparison):
81
+ agreement = comparison.comparison.get_ordered_agreement_scores()
82
+ row_order = agreement.index
83
+ col_order = agreement.columns
84
+ dist = comparison.template_distances[row_order, :][:, col_order]
85
+
86
+ ax = panel.subplots()
87
+ log1p_norm = FuncNorm((np.log1p, np.expm1), vmin=0)
88
+ im = ax.imshow(dist.T, norm=log1p_norm, cmap=self.cmap)
89
+ plt.colorbar(im, ax=ax, shrink=0.3)
90
+ ax.set_title("Hung. match temp dists")
91
+ ax.set_xlabel(f"{comparison.gt_name} unit")
92
+ ax.set_ylabel(f"{comparison.tested_name} unit")
93
+
94
+
95
+ class MetricRegPlot(ComparisonPlot):
96
+ kind = "gtmetric"
97
+ width = 2
98
+ height = 2
99
+
100
+ def __init__(
101
+ self,
102
+ x="gt_ptp_amplitude",
103
+ y="accuracy",
104
+ color="b",
105
+ log_x=False,
106
+ logistic=True,
107
+ lowess=False,
108
+ log_y=False,
109
+ quant_cmap="viridis",
110
+ ):
111
+ self.x = x
112
+ self.y = y
113
+ self.color = color
114
+ self.quant_cmap = quant_cmap
115
+ self.log_x = log_x
116
+ self.logistic = logistic
117
+ self.lowess = lowess
118
+ self.log_y = log_y
119
+
120
+ def draw(self, panel, comparison):
121
+ ax = panel.subplots()
122
+ df = comparison.unit_info_dataframe(force_distances=self.x == "min_temp_dist")
123
+
124
+ qcolor = self.color in df.columns
125
+
126
+ with warnings.catch_warnings():
127
+ warnings.simplefilter("ignore", category=RuntimeWarning)
128
+ finite_y = np.isfinite(df[self.y].values)
129
+ finite_x = np.isfinite(df[self.x].values)
130
+ df_show = df[np.logical_and(finite_y, finite_x)]
131
+
132
+ if qcolor:
133
+ ax.scatter(
134
+ df_show[self.x],
135
+ df_show[self.y],
136
+ c=df_show[self.color],
137
+ cmap=plt.get_cmap(self.quant_cmap),
138
+ lw=0,
139
+ )
140
+
141
+ sns.regplot(
142
+ data=df_show,
143
+ x=self.x,
144
+ y=self.y,
145
+ logistic=self.logistic,
146
+ lowess=self.lowess,
147
+ ax=ax,
148
+ scatter=not qcolor,
149
+ line_kws=dict(color="k"),
150
+ **({} if qcolor else dict(color=self.color)),
151
+ )
152
+ if self.log_x and self.log_y:
153
+ ax.loglog()
154
+ elif self.log_x:
155
+ ax.semilogx()
156
+ elif self.log_y:
157
+ ax.semilogy()
158
+ met = df[self.y].mean().item()
159
+ n_inf_y = np.logical_not(finite_y).sum()
160
+ n_inf_x = np.logical_not(finite_x).sum()
161
+ metstr = f"{met:.3f}" if 0 <= met <= 1 else f"{met:6g}"
162
+ title = f"mean {self.y}: {metstr}"
163
+ if n_inf_y or n_inf_x:
164
+ title = f"{title}, yinf: {n_inf_y}, xinf: {n_inf_x}"
165
+ ax.set_title(title, fontsize="small")
166
+ ax.grid(which="both")
167
+
168
+
169
+ class MetricDistribution(ComparisonPlot):
170
+ kind = "wide"
171
+
172
+ def __init__(
173
+ self,
174
+ xs=("recall", "accuracy", "precision"),
175
+ colors=("r", "b", "g"),
176
+ flavor="hist",
177
+ width=3,
178
+ height=2,
179
+ ):
180
+ self.xs = list(xs)
181
+ self.colors = colors
182
+ self.flavor = flavor
183
+ self.width = width
184
+ self.height = height
185
+
186
+ def draw(self, panel, comparison):
187
+ ax = panel.subplots()
188
+ df = comparison.unit_info_dataframe()
189
+ keep = [x in df for x in self.xs]
190
+ xs = [x for i, x in enumerate(self.xs) if keep[i]]
191
+ colors = [c for i, c in enumerate(self.colors) if keep[i]]
192
+ df = df[xs].melt(value_vars=xs, var_name="metric")
193
+ if self.flavor == "hist":
194
+ sns.histplot(
195
+ data=df,
196
+ x="value",
197
+ hue="metric",
198
+ palette=list(colors),
199
+ element="step",
200
+ ax=ax,
201
+ bins=np.linspace(0, 1, 21),
202
+ )
203
+ sns.move_legend(ax, "upper left", frameon=False)
204
+ elif self.flavor == "box":
205
+ sns.boxplot(
206
+ data=df,
207
+ x="metric",
208
+ y="value",
209
+ hue="metric",
210
+ palette=list(colors),
211
+ ax=ax,
212
+ legend=False,
213
+ showmeans=True,
214
+ )
215
+ ax.tick_params(axis="x", rotation=90)
216
+ ax.set_ylim([-0.05, 1.05])
217
+ ax.set(xlabel=None, ylabel=None)
218
+ ax.grid(which="both", axis="y")
219
+
220
+
221
+ class TemplateDistanceMatrix(ComparisonPlot):
222
+ kind = "wide"
223
+ width = 3
224
+ height = 1
225
+
226
+ def __init__(self, cmap=plt.cm.magma):
227
+ self.cmap = cmap
228
+
229
+ def draw(self, panel, comparison):
230
+ agreement = comparison.comparison.get_ordered_agreement_scores()
231
+ row_order = agreement.index
232
+ col_order = np.array(agreement.columns)
233
+ col_order_ix = np.searchsorted(comparison.tested_analysis.sorting.unit_ids, col_order)
234
+ dist = comparison.template_distances[row_order, :][:, col_order_ix]
235
+
236
+ ax = panel.subplots()
237
+ log1p_norm = FuncNorm((np.log1p, np.expm1), vmin=0)
238
+ ax.imshow(dist, cmap=self.cmap, norm=log1p_norm)
239
+ # plt.colorbar(im, ax=ax, shrink=0.3)
240
+ ax.set_title("all temp dists")
241
+ ax.set_ylabel(f"{comparison.gt_name} unit")
242
+ ax.set_xlabel(f"{comparison.tested_name} unit")
243
+
244
+
245
+ class TemplateDistancesHistogram(ComparisonPlot):
246
+ kind = "wide"
247
+ width = 3
248
+ height = 2
249
+
250
+ def __init__(self, axis=0):
251
+ self.axis = axis
252
+
253
+ def draw(self, panel, comparison):
254
+ ax = panel.subplots()
255
+ d = np.nan_to_num(comparison.template_distances, nan=np.inf)
256
+ vm = min(d.min(0).max(), d.min(1).max())
257
+ min_gt_dist_for_tested_units = d.min(axis=self.axis)
258
+ finite = np.isfinite(min_gt_dist_for_tested_units)
259
+ x = min_gt_dist_for_tested_units[finite]
260
+ bins = np.logspace(np.log10(d.min()), np.log10(vm), 96)
261
+ ax.hist(x, bins=bins, color="orange", log=True)
262
+ ax.semilogx()
263
+ ax.grid(which="both")
264
+ ax.set_ylabel("count")
265
+ ninf = np.logical_not(finite).sum()
266
+ if self.axis == 0:
267
+ ax.set_xlabel("dist to GT (min over GT of tested-GT dists)")
268
+ ax.set_title(
269
+ f"tested template distances to GT library ({ninf} infs)",
270
+ fontsize="small",
271
+ )
272
+ elif self.axis == 1:
273
+ ax.set_xlabel("dist to tested (min over tested of tested-GT dists)")
274
+ ax.set_title(
275
+ f"GT template distances to tested library ({ninf} infs)",
276
+ fontsize="small",
277
+ )
278
+ else:
279
+ assert False
280
+
281
+
282
+ box = MetricDistribution(flavor="box", width=2, height=3.5)
283
+ box.kind = "gtmetric"
284
+ full_gt_overview_plots = (
285
+ MetricRegPlot(x="gt_ptp_amplitude", y="accuracy"),
286
+ MetricRegPlot(x="gt_ptp_amplitude", y="recall", color="r"),
287
+ MetricRegPlot(x="gt_ptp_amplitude", y="precision", color="g"),
288
+ MetricRegPlot(x="gt_ptp_amplitude", y="gt_dt_rms", color="palevioletred"),
289
+ MetricRegPlot(x="gt_firing_rate", y="accuracy"),
290
+ MetricRegPlot(x="gt_firing_rate", y="recall", color="r"),
291
+ MetricRegPlot(x="gt_firing_rate", y="precision", color="g"),
292
+ MetricRegPlot(x="min_temp_dist", y="precision", color="g"),
293
+ MetricRegPlot(x="min_temp_dist", y="recall", color="gt_ptp_amplitude", log_x=True),
294
+ MetricRegPlot(
295
+ x="min_temp_dist",
296
+ y="gt_dt_rms",
297
+ color="gt_ptp_amplitude",
298
+ log_x=True,
299
+ logistic=False,
300
+ lowess=True,
301
+ ),
302
+ MetricRegPlot(
303
+ x="min_temp_dist", y="unsorted_recall", color="gt_ptp_amplitude", log_x=True
304
+ ),
305
+ MetricRegPlot(
306
+ x="gt_ptp_amplitude",
307
+ y="min_temp_dist",
308
+ color="orange",
309
+ logistic=False,
310
+ lowess=True,
311
+ log_y=True,
312
+ ),
313
+ MetricRegPlot(
314
+ x="gt_firing_rate",
315
+ y="min_temp_dist",
316
+ color="orange",
317
+ logistic=False,
318
+ lowess=True,
319
+ log_y=True,
320
+ ),
321
+ MetricRegPlot(
322
+ x="gt_dt_rms",
323
+ y="gt_ptp_amplitude",
324
+ color="orange",
325
+ logistic=False,
326
+ lowess=True,
327
+ log_y=True,
328
+ ),
329
+ MetricRegPlot(x="gt_ptp_amplitude", y="unsorted_recall", color="purple"),
330
+ box,
331
+ MetricDistribution(),
332
+ TrimmedAgreementMatrix(),
333
+ TrimmedTemplateDistanceMatrix(),
334
+ TemplateDistancesHistogram(0),
335
+ TemplateDistancesHistogram(1),
336
+ )
337
+
338
+ default_gt_overview_plots = (
339
+ MetricRegPlot(x="gt_ptp_amplitude", y="accuracy"),
340
+ MetricRegPlot(x="gt_ptp_amplitude", y="recall", color="r"),
341
+ MetricRegPlot(x="gt_ptp_amplitude", y="precision", color="g"),
342
+ MetricRegPlot(x="gt_firing_rate", y="accuracy"),
343
+ MetricRegPlot(x="gt_firing_rate", y="recall", color="r"),
344
+ MetricRegPlot(x="gt_firing_rate", y="precision", color="g"),
345
+ MetricRegPlot(x="gt_ptp_amplitude", y="unsorted_recall", color="purple"),
346
+ box,
347
+ MetricDistribution(),
348
+ TrimmedAgreementMatrix(),
349
+ TrimmedAgreementMatrix(ordered=False),
350
+ )
351
+
352
+ # multi comparisons stuff
353
+ # box and whisker between sorters
354
+
355
+
356
+ def make_gt_overview_summary(
357
+ comparison,
358
+ plots=default_gt_overview_plots,
359
+ max_height=10,
360
+ figsize=(18, 14),
361
+ figure=None,
362
+ suptitle=True,
363
+ same_width_flow=True,
364
+ ):
365
+ if not comparison.has_templates:
366
+ plots_ = []
367
+ for plot in plots:
368
+ if isinstance(plot, MetricRegPlot):
369
+ if "temp" in plot.x:
370
+ continue
371
+ if "temp" in plot.y:
372
+ continue
373
+ if isinstance(plot, TemplateDistanceMatrix):
374
+ continue
375
+ if isinstance(plot, TrimmedTemplateDistanceMatrix):
376
+ continue
377
+ plots_.append(plot)
378
+ plots = plots_
379
+ figure = flow_layout(
380
+ plots,
381
+ max_height=max_height,
382
+ figsize=figsize,
383
+ figure=figure,
384
+ comparison=comparison,
385
+ same_width_flow=same_width_flow,
386
+ )
387
+ if suptitle is True:
388
+ figure.suptitle(f"{comparison.gt_name} vs. {comparison.tested_name}")
389
+ elif suptitle:
390
+ figure.suptitle(suptitle)
391
+
392
+ return figure
dartsort/vis/layout.py ADDED
@@ -0,0 +1,149 @@
1
+ from typing import Iterable, cast
2
+
3
+ from collections import namedtuple
4
+
5
+ import matplotlib.pyplot as plt
6
+ import numpy as np
7
+
8
+
9
+ class BasePlot:
10
+ kind: str
11
+ width = 1
12
+ height = 1
13
+
14
+ def draw(self, *args, **kwargs):
15
+ raise NotImplementedError
16
+
17
+ def notify_global_params(self, **params):
18
+ for k, v in params.items():
19
+ if hasattr(self, k):
20
+ setattr(self, k, v)
21
+
22
+
23
+ class BaseMultiPlot:
24
+ def plots(self, *args, **kwargs) -> list[BasePlot]:
25
+ raise NotImplementedError
26
+
27
+ def notify_global_params(self, **params):
28
+ for k, v in params.items():
29
+ if hasattr(self, k):
30
+ setattr(self, k, v)
31
+
32
+
33
+ Card = namedtuple("Card", ["kind", "width", "height", "plots"])
34
+
35
+
36
+ def flow_layout(
37
+ plots: Iterable[BasePlot | BaseMultiPlot],
38
+ same_width_flow=True,
39
+ max_height=4,
40
+ figsize=(8.5, 11),
41
+ figure=None,
42
+ plot_hspace=0.0,
43
+ card_hspace=0.1,
44
+ **plot_kwargs,
45
+ ):
46
+ columns = flow_layout_columns(
47
+ plots, same_width_flow=same_width_flow, max_height=max_height, **plot_kwargs
48
+ )
49
+ max_height = max(sum(card.height for card in col) for col in columns)
50
+
51
+ # -- draw the figure
52
+ width_ratios = [column[0].width for column in columns]
53
+ if figure is None:
54
+ figure = plt.figure(figsize=figsize, layout="constrained")
55
+ subfigures = figure.subfigures(
56
+ nrows=1,
57
+ ncols=len(columns),
58
+ hspace=0.0,
59
+ width_ratios=width_ratios,
60
+ squeeze=False,
61
+ )
62
+ all_panels = subfigures[0].tolist()
63
+ for column, subfig in zip(columns, subfigures[0]):
64
+ n_cards = len(column)
65
+ height_ratios = [card.height for card in column]
66
+ remaining_height = max_height - sum(height_ratios)
67
+ if remaining_height > 0:
68
+ height_ratios.append(remaining_height)
69
+
70
+ cardfigs = subfig.subfigures(
71
+ nrows=n_cards + (remaining_height > 0),
72
+ ncols=1,
73
+ height_ratios=height_ratios,
74
+ hspace=card_hspace,
75
+ )
76
+ cardfigs = np.atleast_1d(cardfigs)
77
+ all_panels.extend(cardfigs)
78
+
79
+ for cardfig, card in zip(cardfigs, column):
80
+ panels = cardfig.subfigures(
81
+ nrows=len(card.plots),
82
+ ncols=1,
83
+ height_ratios=[min(p.height, max_height) for p in card.plots],
84
+ hspace=plot_hspace,
85
+ )
86
+ panels = np.atleast_1d(panels)
87
+ for plot, panel in zip(card.plots, panels):
88
+ plot.draw(panel, **plot_kwargs)
89
+ all_panels.extend(panels)
90
+
91
+ # clean up the panels, or else things get clipped
92
+ for panel in all_panels:
93
+ panel.set_facecolor([0, 0, 0, 0])
94
+ panel.patch.set_facecolor([0, 0, 0, 0])
95
+
96
+ return figure
97
+
98
+
99
+ def flow_layout_columns(plots: Iterable[BasePlot | BaseMultiPlot], max_height=4, same_width_flow=True, **plot_kwargs):
100
+ all_plots: list[BasePlot] = []
101
+ for plot in plots:
102
+ # duck typing this since isinstance() can be weird with autoreload
103
+ # pyright is not duck-friendly.
104
+ if callable(getattr(plot, "plots", None)):
105
+ all_plots.extend(plot.plots(**plot_kwargs)) # type: ignore
106
+ elif callable(getattr(plot, "draw", None)):
107
+ all_plots.append(plot) # type: ignore
108
+ else:
109
+ raise ValueError(f"Not sure what to do with {plot=}")
110
+
111
+ plots_by_kind: dict[str, list[BasePlot]] = {}
112
+ for plot in all_plots:
113
+ if plot.kind not in plots_by_kind:
114
+ plots_by_kind[plot.kind] = []
115
+ plots_by_kind[plot.kind].append(plot)
116
+
117
+ # break plots into groups ("cards") by kind
118
+ cards = []
119
+ for kind, plots in plots_by_kind.items():
120
+ width = max(p.width for p in plots)
121
+ card_plots = []
122
+ for plot in plots:
123
+ card_height = sum(min(p.height, max_height) for p in card_plots)
124
+ plot_height = min(plot.height, max_height)
125
+ if card_height + plot_height <= max_height:
126
+ card_plots.append(plot)
127
+ else:
128
+ cards.append(Card(plots[0].kind, width, card_height, card_plots))
129
+ card_plots = [plot]
130
+ card_height = sum(min(p.height, max_height) for p in card_plots)
131
+ if card_plots:
132
+ cards.append(Card(plots[0].kind, width, card_height, card_plots))
133
+ cards = sorted(cards, key=lambda card: card.width)
134
+
135
+ # flow the cards over columns
136
+ columns = [[]]
137
+ cur_width = cards[0].width
138
+ for card in cards:
139
+ if same_width_flow and card.width != cur_width:
140
+ columns.append([card])
141
+ cur_width = card.width
142
+ continue
143
+
144
+ if sum(c.height for c in columns[-1]) + card.height <= max_height:
145
+ columns[-1].append(card)
146
+ else:
147
+ columns.append([card])
148
+
149
+ return columns
@@ -0,0 +1,23 @@
1
+ import pandas as pd
2
+
3
+ from spikeinterface.comparison.comparisontools import _perf_keys
4
+
5
+
6
+ dartsort_extra_metrics = ["min_temp_dist", "unsorted_recall"]
7
+ # perf keys has stuff in it like accuracy, recall, ...
8
+ metrics = list(set(_perf_keys + dartsort_extra_metrics))
9
+
10
+
11
+ def melt_metrics(
12
+ df, keep_metric_columns=None, metric_columns=metrics, var_name="metric"
13
+ ):
14
+ if keep_metric_columns is not None:
15
+ keep_metric_columns = metric_columns
16
+ value_vars = [c for c in keep_metric_columns if c in df.columns]
17
+ id_vars = [c for c in df.columns if c not in metric_columns]
18
+ return pd.melt(
19
+ df,
20
+ id_vars=id_vars,
21
+ value_vars=value_vars,
22
+ var_name=var_name,
23
+ )