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
@@ -0,0 +1,779 @@
1
+ import h5py
2
+ import matplotlib.pyplot as plt
3
+ import numpy as np
4
+ from matplotlib import transforms
5
+ from matplotlib.patches import Ellipse
6
+ from scipy.spatial import KDTree
7
+
8
+ from .colors import glasbey1024, gray
9
+ from dartsort.clustering.density import kdtree_inliers
10
+
11
+
12
+ def scatter_spike_features(
13
+ hdf5_filename=None,
14
+ sorting=None,
15
+ motion_est=None,
16
+ registered=False,
17
+ times_s=None,
18
+ depths_um=None,
19
+ x=None,
20
+ amplitudes=None,
21
+ labels=None,
22
+ geom=None,
23
+ figure=None,
24
+ axes=None,
25
+ width_ratios=(1, 1, 3),
26
+ semilog_amplitudes=True,
27
+ show_geom=True,
28
+ geom_scatter_kw=dict(s=5, marker="s", color="k", lw=0),
29
+ amplitude_color_cutoff=15.0,
30
+ amplitude_cmap=plt.cm.viridis,
31
+ max_spikes_plot=500_000,
32
+ probe_margin_um=100,
33
+ t_min=-np.inf,
34
+ t_max=np.inf,
35
+ s=1,
36
+ linewidth=0,
37
+ limits="probe_margin",
38
+ label_axes=True,
39
+ random_seed=0,
40
+ annotate_labels_over_xz=False,
41
+ annotate_labels_over_amp=False,
42
+ amplitudes_dataset_name=("denoised_ptp_amplitudes", "ptp_amplitudes", "amplitudes"),
43
+ localizations_dataset_name=("point_source_localizations", "localizations"),
44
+ extra_features=None,
45
+ show_triaged=True,
46
+ remove_outliers=False,
47
+ label_colors=glasbey1024,
48
+ **scatter_kw,
49
+ ):
50
+ """3-axis scatter plot of spike depths vs. horizontal pos, amplitude, and time
51
+
52
+ Returns
53
+ -------
54
+ figure, axes, (s_x, s_a, s_t)
55
+ Matplotlib figure, axes array, and 3-tuple of scatterplot PathCollections
56
+ """
57
+ if axes is not None:
58
+ assert axes.size == 3
59
+ figure = axes.flat[0].figure
60
+ if figure is None:
61
+ figure = plt.gcf()
62
+ if extra_features is None:
63
+ extra_features = {}
64
+ else:
65
+ if len(width_ratios) != 3 + len(extra_features):
66
+ width_ratios = (
67
+ *width_ratios[:2],
68
+ *([1] * len(extra_features)),
69
+ width_ratios[-1],
70
+ )
71
+ if axes is None:
72
+ axes = figure.subplots(
73
+ ncols=3 + len(extra_features),
74
+ sharey=True,
75
+ gridspec_kw=dict(width_ratios=width_ratios),
76
+ )
77
+
78
+ if sorting is not None:
79
+ if times_s is None:
80
+ times_s = getattr(sorting, "times_seconds", None)
81
+ if x is None:
82
+ x = _try_getattr(sorting, localizations_dataset_name, None)
83
+ if x is not None:
84
+ depths_um = x[:, 2]
85
+ x = x[:, 0]
86
+ if amplitudes is None:
87
+ amplitudes = _try_getattr(sorting, amplitudes_dataset_name, None)
88
+ if hdf5_filename is None:
89
+ hdf5_filename = sorting.parent_h5_path
90
+
91
+ needs_load = any(v is None for v in (times_s, x, depths_um, amplitudes, geom))
92
+ if needs_load and hdf5_filename is not None:
93
+ with h5py.File(hdf5_filename, "r", locking=False) as h5:
94
+ if times_s is None:
95
+ times_s = h5["times_seconds"][:]
96
+ if x is None or depths_um is None:
97
+ localizations = _try_load(h5, localizations_dataset_name)
98
+ if x is None:
99
+ x = localizations[:, 0]
100
+ if depths_um is None:
101
+ depths_um = localizations[:, 2]
102
+ if amplitudes is None:
103
+ amplitudes = _try_load(h5, amplitudes_dataset_name)
104
+ if geom is None:
105
+ geom = h5["geom"][:]
106
+
107
+ to_show = np.flatnonzero(np.clip(times_s, t_min, t_max) == times_s)
108
+ if x is not None:
109
+ to_show = to_show[np.isfinite(x[to_show])]
110
+ if depths_um is not None:
111
+ to_show = to_show[np.isfinite(depths_um[to_show])]
112
+ if geom is not None:
113
+ to_show = to_show[
114
+ (depths_um[to_show] > geom[:, 1].min() - probe_margin_um)
115
+ & (depths_um[to_show] < geom[:, 1].max() + probe_margin_um)
116
+ & (x[to_show] > geom[:, 0].min() - probe_margin_um)
117
+ & (x[to_show] < geom[:, 0].max() + probe_margin_um)
118
+ ]
119
+ if isinstance(limits, (list, tuple, np.ndarray)):
120
+ assert len(limits) == 2
121
+ to_show = to_show[depths_um[to_show] == depths_um[to_show].clip(*limits)]
122
+ if len(to_show) > max_spikes_plot:
123
+ np.random.default_rng(random_seed).shuffle(to_show)
124
+ to_show = to_show[:max_spikes_plot]
125
+ to_show.sort()
126
+ if remove_outliers:
127
+ a = 50 * np.log(amplitudes + 5)
128
+ inliers, kdtree = kdtree_inliers(np.c_[depths_um, x, a][to_show])
129
+ to_show = to_show[inliers]
130
+ _, s_x = scatter_x_vs_depth(
131
+ x=x,
132
+ depths_um=depths_um,
133
+ times_s=times_s,
134
+ amplitudes=amplitudes,
135
+ labels=labels,
136
+ show_geom=show_geom,
137
+ geom_scatter_kw=geom_scatter_kw,
138
+ sorting=sorting,
139
+ motion_est=motion_est,
140
+ registered=registered,
141
+ geom=geom,
142
+ probe_margin_um=probe_margin_um,
143
+ ax=axes.flat[0],
144
+ max_spikes_plot=max_spikes_plot,
145
+ amplitude_color_cutoff=amplitude_color_cutoff,
146
+ amplitude_cmap=amplitude_cmap,
147
+ random_seed=random_seed,
148
+ s=s,
149
+ limits=limits,
150
+ linewidth=linewidth,
151
+ to_show=to_show,
152
+ amplitudes_dataset_name=amplitudes_dataset_name,
153
+ localizations_dataset_name=localizations_dataset_name,
154
+ show_triaged=show_triaged,
155
+ annotate_labels=annotate_labels_over_xz,
156
+ label_colors=label_colors,
157
+ **scatter_kw,
158
+ )
159
+
160
+ _, s_a = scatter_amplitudes_vs_depth(
161
+ depths_um=depths_um,
162
+ amplitudes=amplitudes,
163
+ labels=labels,
164
+ times_s=times_s,
165
+ semilog_amplitudes=semilog_amplitudes,
166
+ sorting=sorting,
167
+ motion_est=motion_est,
168
+ registered=registered,
169
+ geom=geom,
170
+ probe_margin_um=probe_margin_um,
171
+ ax=axes.flat[1],
172
+ max_spikes_plot=max_spikes_plot,
173
+ amplitude_color_cutoff=amplitude_color_cutoff,
174
+ amplitude_cmap=amplitude_cmap,
175
+ random_seed=random_seed,
176
+ s=s,
177
+ limits=limits,
178
+ linewidth=linewidth,
179
+ to_show=to_show,
180
+ annotate_labels=annotate_labels_over_amp,
181
+ amplitudes_dataset_name=amplitudes_dataset_name,
182
+ localizations_dataset_name=localizations_dataset_name,
183
+ show_triaged=show_triaged,
184
+ label_colors=label_colors,
185
+ **scatter_kw,
186
+ )
187
+
188
+ extra_scatters = []
189
+ for j, (featname, feature) in enumerate(extra_features.items()):
190
+ _, scatter = scatter_feature_vs_depth(
191
+ feature,
192
+ depths_um=depths_um,
193
+ amplitudes=amplitudes,
194
+ labels=labels,
195
+ sorting=sorting,
196
+ times_s=times_s,
197
+ motion_est=motion_est,
198
+ registered=registered,
199
+ geom=geom,
200
+ ax=axes.flat[2 + j],
201
+ max_spikes_plot=max_spikes_plot,
202
+ amplitude_color_cutoff=amplitude_color_cutoff,
203
+ amplitude_cmap=amplitude_cmap,
204
+ probe_margin_um=probe_margin_um,
205
+ s=s,
206
+ linewidth=linewidth,
207
+ limits=limits,
208
+ random_seed=random_seed,
209
+ to_show=to_show,
210
+ show_triaged=show_triaged,
211
+ label_colors=label_colors,
212
+ **scatter_kw,
213
+ )
214
+ extra_scatters.append(scatter)
215
+ if label_axes:
216
+ axes.flat[2 + j].set_xlabel(featname)
217
+
218
+ _, s_t = scatter_time_vs_depth(
219
+ times_s=times_s,
220
+ depths_um=depths_um,
221
+ amplitudes=amplitudes,
222
+ labels=labels,
223
+ sorting=sorting,
224
+ motion_est=motion_est,
225
+ registered=registered,
226
+ geom=geom,
227
+ probe_margin_um=probe_margin_um,
228
+ ax=axes.flat[-1],
229
+ max_spikes_plot=max_spikes_plot,
230
+ amplitude_color_cutoff=amplitude_color_cutoff,
231
+ amplitude_cmap=amplitude_cmap,
232
+ random_seed=random_seed,
233
+ s=s,
234
+ limits=limits,
235
+ linewidth=linewidth,
236
+ to_show=to_show,
237
+ amplitudes_dataset_name=amplitudes_dataset_name,
238
+ localizations_dataset_name=localizations_dataset_name,
239
+ show_triaged=show_triaged,
240
+ label_colors=label_colors,
241
+ **scatter_kw,
242
+ )
243
+
244
+ if label_axes:
245
+ axes[0].set_ylabel(("registered " * registered) + "depth (um)")
246
+ axes[0].set_xlabel("x (um)")
247
+ axes[1].set_xlabel("amplitude (su)")
248
+ axes[-1].set_xlabel("time (s)")
249
+
250
+ return figure, axes, (s_x, s_a, *extra_scatters, s_t)
251
+
252
+
253
+ def scatter_time_vs_depth(
254
+ hdf5_filename=None,
255
+ sorting=None,
256
+ motion_est=None,
257
+ registered=False,
258
+ times_s=None,
259
+ depths_um=None,
260
+ amplitudes=None,
261
+ labels=None,
262
+ geom=None,
263
+ probe_margin_um=100,
264
+ ax=None,
265
+ max_spikes_plot=500_000,
266
+ amplitude_color_cutoff=15,
267
+ amplitude_cmap=plt.cm.viridis,
268
+ limits="probe_margin",
269
+ random_seed=0,
270
+ s=1,
271
+ linewidth=0,
272
+ to_show=None,
273
+ amplitudes_dataset_name="denoised_ptp_amplitudes",
274
+ localizations_dataset_name="point_source_localizations",
275
+ show_triaged=True,
276
+ time_range=None,
277
+ label_colors=glasbey1024,
278
+ **scatter_kw,
279
+ ):
280
+ """Scatter plot of spike time vs spike depth (vertical position on probe)
281
+
282
+ In this plot, each dot is a spike. Spike data can be passed directly via
283
+ the times_s, depths_um, and (one of) amplitudes or labels as arrays, or
284
+ alternatively, these can be left unset and they will be loaded from
285
+ hdf5_filename when it is supplied.
286
+
287
+ Returns: axis, scatter
288
+ """
289
+ if sorting is not None:
290
+ if times_s is None:
291
+ times_s = getattr(sorting, "times_seconds", None)
292
+ if depths_um is None:
293
+ depths_um = _try_getattr(sorting, localizations_dataset_name, None)
294
+ if depths_um is not None:
295
+ depths_um = depths_um[:, 2]
296
+ if amplitudes is None:
297
+ amplitudes = _try_getattr(sorting, amplitudes_dataset_name, None)
298
+ if hdf5_filename is None:
299
+ hdf5_filename = sorting.parent_h5_path
300
+
301
+ needs_load = any(v is None for v in (times_s, depths_um, amplitudes, geom))
302
+ if needs_load and hdf5_filename is not None:
303
+ with h5py.File(hdf5_filename, "r", locking=False) as h5:
304
+ if times_s is None:
305
+ times_s = h5["times_seconds"][:]
306
+ if depths_um is None:
307
+ depths_um = _try_load(h5, localizations_dataset_name)[:, 2]
308
+ if amplitudes is None:
309
+ amplitudes = _try_load(h5, amplitudes_dataset_name)
310
+ if geom is None:
311
+ geom = h5["geom"][:]
312
+
313
+ ax, scat = scatter_feature_vs_depth(
314
+ times_s,
315
+ depths_um,
316
+ times_s=times_s,
317
+ amplitudes=amplitudes,
318
+ labels=labels,
319
+ sorting=sorting,
320
+ motion_est=motion_est,
321
+ registered=registered,
322
+ geom=geom,
323
+ ax=ax,
324
+ max_spikes_plot=max_spikes_plot,
325
+ amplitude_color_cutoff=amplitude_color_cutoff,
326
+ amplitude_cmap=amplitude_cmap,
327
+ probe_margin_um=probe_margin_um,
328
+ s=s,
329
+ limits=limits,
330
+ linewidth=linewidth,
331
+ random_seed=random_seed,
332
+ to_show=to_show,
333
+ show_triaged=show_triaged,
334
+ time_range=time_range,
335
+ label_colors=label_colors,
336
+ **scatter_kw,
337
+ )
338
+
339
+ if time_range is not None:
340
+ ax.set_xlim(time_range)
341
+
342
+ return ax, scat
343
+
344
+
345
+ def scatter_x_vs_depth(
346
+ hdf5_filename=None,
347
+ sorting=None,
348
+ motion_est=None,
349
+ registered=False,
350
+ x=None,
351
+ depths_um=None,
352
+ amplitudes=None,
353
+ times_s=None,
354
+ labels=None,
355
+ show_geom=True,
356
+ geom_scatter_kw=dict(s=5, marker="s", color="k", lw=0),
357
+ geom=None,
358
+ probe_margin_um=100,
359
+ ax=None,
360
+ max_spikes_plot=500_000,
361
+ amplitude_color_cutoff=15,
362
+ amplitude_cmap=plt.cm.viridis,
363
+ limits="probe_margin",
364
+ random_seed=0,
365
+ s=1,
366
+ linewidth=0,
367
+ to_show=None,
368
+ amplitudes_dataset_name="denoised_ptp_amplitudes",
369
+ localizations_dataset_name="point_source_localizations",
370
+ show_triaged=True,
371
+ annotate_labels=False,
372
+ label_colors=glasbey1024,
373
+ **scatter_kw,
374
+ ):
375
+ """Scatter plot of spike horizontal pos vs spike depth (vertical position on probe)"""
376
+ if x is None and sorting is not None:
377
+ x = _try_getattr(sorting, localizations_dataset_name)[:, 0]
378
+ if depths_um is None and sorting is not None:
379
+ depths_um = _try_getattr(sorting, localizations_dataset_name)[:, 2]
380
+ if amplitudes is None and sorting is not None:
381
+ amplitudes = _try_getattr(sorting, amplitudes_dataset_name)
382
+
383
+ if to_show is None and geom is not None:
384
+ to_show = np.flatnonzero(
385
+ (x > geom[:, 0].min() - probe_margin_um)
386
+ & (x < geom[:, 0].max() + probe_margin_um)
387
+ )
388
+
389
+ ax, s1 = scatter_feature_vs_depth(
390
+ x,
391
+ depths_um,
392
+ times_s=times_s,
393
+ amplitudes=amplitudes,
394
+ labels=labels,
395
+ sorting=sorting,
396
+ motion_est=motion_est,
397
+ registered=registered,
398
+ geom=geom,
399
+ ax=ax,
400
+ max_spikes_plot=max_spikes_plot,
401
+ amplitude_color_cutoff=amplitude_color_cutoff,
402
+ amplitude_cmap=amplitude_cmap,
403
+ probe_margin_um=probe_margin_um,
404
+ s=s,
405
+ limits=limits,
406
+ linewidth=linewidth,
407
+ random_seed=random_seed,
408
+ to_show=to_show,
409
+ show_triaged=show_triaged,
410
+ annotate_labels=annotate_labels,
411
+ label_colors=label_colors,
412
+ **scatter_kw,
413
+ )
414
+ if show_geom and geom is not None:
415
+ ax.scatter(*geom.T, **geom_scatter_kw)
416
+ if limits == "probe_margin" and geom is not None:
417
+ ax.set_xlim(
418
+ [
419
+ geom[:, 0].min() - probe_margin_um,
420
+ geom[:, 0].max() + probe_margin_um,
421
+ ]
422
+ )
423
+ return ax, s1
424
+
425
+
426
+ def scatter_amplitudes_vs_depth(
427
+ hdf5_filename=None,
428
+ sorting=None,
429
+ motion_est=None,
430
+ registered=False,
431
+ depths_um=None,
432
+ amplitudes=None,
433
+ times_s=None,
434
+ labels=None,
435
+ semilog_amplitudes=True,
436
+ geom=None,
437
+ probe_margin_um=100,
438
+ ax=None,
439
+ max_spikes_plot=500_000,
440
+ amplitude_color_cutoff=15,
441
+ amplitude_cmap=plt.cm.viridis,
442
+ limits="probe_margin",
443
+ random_seed=0,
444
+ s=1,
445
+ linewidth=0,
446
+ to_show=None,
447
+ amplitudes_dataset_name="denoised_ptp_amplitudes",
448
+ localizations_dataset_name="point_source_localizations",
449
+ show_triaged=True,
450
+ label_colors=glasbey1024,
451
+ annotate_labels=False,
452
+ **scatter_kw,
453
+ ):
454
+ """Scatter plot of spike amplitude vs spike depth (vertical position on probe)"""
455
+ if sorting is not None:
456
+ if depths_um is None:
457
+ depths_um = _try_getattr(sorting, localizations_dataset_name, None)
458
+ if depths_um is not None:
459
+ depths_um = x[:, 2]
460
+ if amplitudes is None:
461
+ amplitudes = _try_getattr(sorting, amplitudes_dataset_name, None)
462
+ if hdf5_filename is None:
463
+ hdf5_filename = sorting.parent_h5_path
464
+
465
+ needs_load = any(v is None for v in (depths_um, amplitudes, geom))
466
+ if needs_load and hdf5_filename is not None:
467
+ with h5py.File(hdf5_filename, "r", locking=False) as h5:
468
+ if depths_um is None:
469
+ depths_um = h5[localizations_dataset_name][:, 2]
470
+ if amplitudes is None:
471
+ amplitudes = h5[amplitudes_dataset_name][:]
472
+ if geom is None:
473
+ geom = h5["geom"][:]
474
+
475
+ ax, s = scatter_feature_vs_depth(
476
+ amplitudes,
477
+ depths_um,
478
+ times_s=times_s,
479
+ amplitudes=amplitudes,
480
+ labels=labels,
481
+ sorting=sorting,
482
+ motion_est=motion_est,
483
+ registered=registered,
484
+ geom=geom,
485
+ ax=ax,
486
+ max_spikes_plot=max_spikes_plot,
487
+ amplitude_color_cutoff=amplitude_color_cutoff,
488
+ amplitude_cmap=amplitude_cmap,
489
+ probe_margin_um=probe_margin_um,
490
+ s=s,
491
+ limits=limits,
492
+ linewidth=linewidth,
493
+ random_seed=random_seed,
494
+ to_show=to_show,
495
+ show_triaged=show_triaged,
496
+ label_colors=label_colors,
497
+ annotate_labels=annotate_labels,
498
+ **scatter_kw,
499
+ )
500
+ if semilog_amplitudes:
501
+ ax.semilogx()
502
+ return ax, s
503
+
504
+
505
+ def scatter_feature_vs_depth(
506
+ feature,
507
+ depths_um,
508
+ amplitudes=None,
509
+ sorting=None,
510
+ times_s=None,
511
+ labels=None,
512
+ motion_est=None,
513
+ registered=False,
514
+ geom=None,
515
+ ax=None,
516
+ max_spikes_plot=500_000,
517
+ amplitude_color_cutoff=15,
518
+ amplitude_cmap=plt.cm.viridis,
519
+ probe_margin_um=100,
520
+ s=1,
521
+ linewidth=0,
522
+ limits="probe_margin",
523
+ random_seed=0,
524
+ to_show=None,
525
+ time_range=None,
526
+ rasterized=True,
527
+ show_triaged=True,
528
+ show_ellipses=False,
529
+ scat=None,
530
+ ellip=None,
531
+ max_n_labels=None,
532
+ pad_to_max=False,
533
+ annotate_labels=False,
534
+ label_colors=glasbey1024,
535
+ **scatter_kw,
536
+ ):
537
+ assert feature.shape == depths_um.shape
538
+ if amplitudes is not None:
539
+ assert feature.shape == amplitudes.shape
540
+ assert feature.ndim == 1
541
+
542
+ updating = scat is not None
543
+ if updating:
544
+ max_spikes_plot = scat.get_offsets().shape[0]
545
+ assert ax is not None
546
+
547
+ if ax is None:
548
+ ax = plt.gca()
549
+
550
+ # subset spikes according to margin and sampling
551
+ n_spikes = len(feature)
552
+ if to_show is None:
553
+ to_show = np.arange(n_spikes)
554
+
555
+ to_show = to_show[np.isfinite(depths_um[to_show])]
556
+ to_show = to_show[np.isfinite(feature[to_show])]
557
+ if geom is not None:
558
+ to_show = to_show[
559
+ (depths_um[to_show] > geom[:, 1].min() - probe_margin_um)
560
+ & (depths_um[to_show] < geom[:, 1].max() + probe_margin_um)
561
+ ]
562
+ if time_range is not None:
563
+ assert times_s is not None
564
+ to_show = to_show[np.clip(times_s[to_show], *time_range) == times_s[to_show]]
565
+ rg = np.random.default_rng(random_seed)
566
+ if len(to_show) > max_spikes_plot:
567
+ to_show = rg.choice(to_show, size=max_spikes_plot, replace=False)
568
+ else:
569
+ to_show = rg.choice(to_show, size=to_show.size, replace=False)
570
+
571
+ if registered:
572
+ assert motion_est is not None
573
+ assert times_s is not None
574
+ depths_um = depths_um.copy()
575
+ depths_um[to_show] = motion_est.correct_s(times_s[to_show], depths_um[to_show])
576
+
577
+ # order by amplitude so that high amplitude units show up
578
+ if amplitudes is not None:
579
+ to_show = to_show[np.argsort(amplitudes[to_show])]
580
+
581
+ if sorting is not None and labels is None:
582
+ if sorting.labels is not None and sorting.labels.max() > 0:
583
+ labels = sorting.labels
584
+
585
+ if labels is None:
586
+ c = np.clip(amplitudes[to_show], 0, amplitude_color_cutoff)
587
+ c = amplitude_cmap(c / amplitude_color_cutoff)
588
+ order = slice(None)
589
+ show_ellipses = False
590
+ else:
591
+ order = np.concatenate(
592
+ (
593
+ np.flatnonzero(labels[to_show] < 0),
594
+ np.flatnonzero(labels[to_show] >= 0),
595
+ )
596
+ )
597
+ to_show = to_show[order]
598
+ c = label_colors[labels[to_show] % len(label_colors)]
599
+ if show_triaged:
600
+ triaged = labels[to_show] < 0
601
+ tc = np.clip(amplitudes[to_show[triaged]], 0, amplitude_color_cutoff)
602
+ tc = plt.cm.binary(tc / amplitude_color_cutoff)
603
+ c[triaged, :3] = tc[..., :3]
604
+ else:
605
+ c = c[labels[to_show] >= 0]
606
+ to_show = to_show[labels[to_show] >= 0]
607
+
608
+ feature = feature[to_show]
609
+ depths_um = depths_um[to_show]
610
+ if pad_to_max and len(to_show) < max_spikes_plot:
611
+ n_pad = max_spikes_plot - len(to_show)
612
+ feature = np.pad(feature, (0, n_pad), constant_values=np.nan)
613
+ depths_um = np.pad(depths_um, (0, n_pad), constant_values=np.nan)
614
+ c = np.pad(c, [(0, n_pad), (0, 0)], constant_values=0)
615
+
616
+ if updating:
617
+ scat.set_offsets(np.c_[feature, depths_um])
618
+ scat.set_facecolors(c)
619
+ else:
620
+ scat = ax.scatter(
621
+ feature,
622
+ depths_um,
623
+ c=c,
624
+ s=s,
625
+ linewidth=linewidth,
626
+ rasterized=rasterized,
627
+ **scatter_kw,
628
+ )
629
+
630
+ if show_ellipses:
631
+ ellip = add_ellipses(
632
+ ax,
633
+ labels,
634
+ feature,
635
+ depths_um,
636
+ to_show,
637
+ ellip,
638
+ pad_to_max,
639
+ max_n_labels,
640
+ )
641
+
642
+ if limits == "probe_margin" and geom is not None:
643
+ ax.set_ylim(
644
+ [
645
+ geom[:, 1].min() - probe_margin_um,
646
+ geom[:, 1].max() + probe_margin_um,
647
+ ]
648
+ )
649
+ elif limits is not None and limits != "probe_margin":
650
+ ax.set_ylim(limits)
651
+
652
+ if annotate_labels:
653
+ add_labels(ax, labels[to_show], feature, depths_um, rev=annotate_labels < 0)
654
+
655
+ if show_ellipses:
656
+ return ax, scat, ellip
657
+ return ax, scat
658
+
659
+
660
+ def add_labels(ax, labels, feature, depths_um, rev=False):
661
+ fs = []
662
+ ds = []
663
+ us = np.unique(labels)
664
+ if rev:
665
+ us = reversed(us)
666
+ for u in us:
667
+ if u < 0:
668
+ continue
669
+ inu = np.flatnonzero(labels == u)
670
+ f = feature[inu].mean()
671
+ fs.append(f)
672
+ d = depths_um[inu].mean()
673
+ ds.append(d)
674
+ ax.annotate(
675
+ str(u),
676
+ (f, d),
677
+ annotation_clip=True,
678
+ color=glasbey1024[u % len(glasbey1024)],
679
+ bbox=dict(
680
+ boxstyle="square",
681
+ pad=0.0,
682
+ facecolor="w",
683
+ ),
684
+ )
685
+
686
+
687
+ def add_ellipses(
688
+ ax, labels, feature, depths_um, to_show, ellip, pad_to_max, max_n_labels
689
+ ):
690
+ unit_ids = np.unique(labels[to_show])
691
+ unit_ids = unit_ids[unit_ids >= 0]
692
+ if pad_to_max and max_n_labels:
693
+ n_pad = max_n_labels - unit_ids.size
694
+ unit_ids = np.concatenate((unit_ids, unit_ids.max() + 10 + np.arange(n_pad)))
695
+ if ellip is None:
696
+ ellip = {}
697
+ for uid in unit_ids:
698
+ color = glasbey1024[uid % len(glasbey1024)]
699
+ in_unit = np.flatnonzero(labels[to_show] == uid)
700
+ bad = in_unit.size <= 2
701
+ if not bad:
702
+ # nans
703
+ f = feature[in_unit]
704
+ d = depths_um[in_unit]
705
+ valid = np.flatnonzero(np.isfinite(f + d))
706
+ bad = valid.size <= 2
707
+ if not bad:
708
+ # remove outliers to stabilize [co]variance
709
+ kdt = KDTree(np.c_[f[valid], d[valid]])
710
+ dd, ii = kdt.query(np.c_[f[valid], d[valid]], distance_upper_bound=10.0)
711
+ valid = valid[ii < kdt.n]
712
+ bad = valid.size <= 2
713
+ if not bad:
714
+ fm = f[valid].mean()
715
+ dm = d[valid].mean()
716
+ cov = np.cov(f[valid], d[valid])
717
+ vx, vy = cov[0, 0], cov[1, 1]
718
+ if min(vx, vy) <= 0:
719
+ bad = True
720
+ rho = np.minimum(1.0, cov[0, 1] / np.sqrt(vx * vy))
721
+ rhoss = cov[0, 1]
722
+ if not bad:
723
+ apc2 = (vx + vy) / 2
724
+ amc2sq = ((vx - vy) ** 2) / 4
725
+ lambda1 = apc2 + np.sqrt(amc2sq + rhoss**2)
726
+ lambda2 = apc2 - np.sqrt(amc2sq + rhoss**2)
727
+ if rhoss == 0:
728
+ theta = (np.pi / 2) * (vx < vy)
729
+ else:
730
+ theta = np.arctan2(lambda1 - vx, rhoss)
731
+ theta = 180 * theta / np.pi
732
+ center = fm, dm
733
+ else:
734
+ center = 0, 0
735
+ color = (0, 0, 0, 0)
736
+ lambda1 = lambda2 = 1
737
+ theta = 0
738
+ if uid in ellip:
739
+ ell = ellip[uid]
740
+ # ell.remove()
741
+ ell.set(
742
+ center=center,
743
+ width=2 * np.sqrt(lambda1),
744
+ height=2 * np.sqrt(lambda2),
745
+ angle=theta,
746
+ edgecolor=color,
747
+ )
748
+ else:
749
+ ell = Ellipse(
750
+ center,
751
+ width=2 * np.sqrt(lambda1),
752
+ height=2 * np.sqrt(lambda2),
753
+ angle=theta,
754
+ facecolor=(0, 0, 0, 0),
755
+ edgecolor=color,
756
+ linewidth=1,
757
+ animated=True,
758
+ )
759
+ ellip[uid] = ell
760
+ ax.add_patch(ell)
761
+ return ellip
762
+
763
+
764
+ def _try_load(h5, dataset_name_or_names):
765
+ if isinstance(dataset_name_or_names, str):
766
+ return h5[dataset_name_or_names][:]
767
+ for dset in dataset_name_or_names:
768
+ if dset in h5:
769
+ return h5[dset]
770
+ raise ValueError(f"{dataset_name_or_names} not present in {h5.filename}.")
771
+
772
+
773
+ def _try_getattr(obj, attr_name_or_names, default=None):
774
+ if isinstance(attr_name_or_names, str):
775
+ return getattr(obj, attr_name_or_names, default)
776
+ for name in attr_name_or_names:
777
+ if hasattr(obj, name):
778
+ return getattr(obj, name)
779
+ return default