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.
- dartsort/__init__.py +68 -0
- dartsort/cli.py +105 -0
- dartsort/clustering/__init__.py +3 -0
- dartsort/clustering/cluster_util.py +392 -0
- dartsort/clustering/clustering.py +765 -0
- dartsort/clustering/clustering_features.py +202 -0
- dartsort/clustering/density.py +1299 -0
- dartsort/clustering/forward_backward.py +219 -0
- dartsort/clustering/gmm/mixture.py +5790 -0
- dartsort/clustering/gmm/stable_features.py +993 -0
- dartsort/clustering/kmeans.py +448 -0
- dartsort/clustering/merge.py +501 -0
- dartsort/clustering/modes.py +214 -0
- dartsort/clustering/refine_util.py +191 -0
- dartsort/clustering/relocate.py +109 -0
- dartsort/clustering/split.py +1285 -0
- dartsort/config.py +288 -0
- dartsort/detect/__init__.py +2 -0
- dartsort/detect/detect.py +449 -0
- dartsort/detect/detect_filters.py +37 -0
- dartsort/evaluate/__init__.py +1 -0
- dartsort/evaluate/analysis.py +594 -0
- dartsort/evaluate/comparison.py +534 -0
- dartsort/evaluate/hybrid_util.py +599 -0
- dartsort/evaluate/noise_recording_tools.py +184 -0
- dartsort/evaluate/sim_template_tools.py +842 -0
- dartsort/evaluate/simkit.py +840 -0
- dartsort/evaluate/simlib.py +270 -0
- dartsort/localize/__init__.py +0 -0
- dartsort/localize/localize_torch.py +291 -0
- dartsort/localize/localize_util.py +313 -0
- dartsort/main.py +657 -0
- dartsort/peel/__init__.py +6 -0
- dartsort/peel/grab.py +208 -0
- dartsort/peel/matching.py +628 -0
- dartsort/peel/matching_util/__init__.py +3 -0
- dartsort/peel/matching_util/compressed_upsampled.py +718 -0
- dartsort/peel/matching_util/drifty.py +791 -0
- dartsort/peel/matching_util/matching_base.py +616 -0
- dartsort/peel/matching_util/pairwise.py +359 -0
- dartsort/peel/matching_util/pairwise_util.py +1317 -0
- dartsort/peel/peel_base.py +1000 -0
- dartsort/peel/running_template.py +1498 -0
- dartsort/peel/subtract.py +1123 -0
- dartsort/peel/threshold.py +471 -0
- dartsort/peel/universal.py +191 -0
- dartsort/pretrained/default_temporal_kernel.npy +0 -0
- dartsort/pretrained/detect_np1.pt +0 -0
- dartsort/pretrained/detect_np2.pt +0 -0
- dartsort/pretrained/glasbey1024.npz +0 -0
- dartsort/pretrained/offset_trained_denoiser.pt +0 -0
- dartsort/pretrained/offset_trained_denoiser_added_temp.pt +0 -0
- dartsort/pretrained/offset_trained_denoiser_added_temp_boost.pt +0 -0
- dartsort/pretrained/original_single_chan_denoiser.pt +0 -0
- dartsort/pretrained/single_chan_denoiser.pt +0 -0
- dartsort/templates/__init__.py +11 -0
- dartsort/templates/get_templates.py +771 -0
- dartsort/templates/postprocess_util.py +555 -0
- dartsort/templates/realignment.py +466 -0
- dartsort/templates/superres_util.py +204 -0
- dartsort/templates/template_util.py +518 -0
- dartsort/templates/templates.py +528 -0
- dartsort/transform/__init__.py +2 -0
- dartsort/transform/_multichan_denoiser_kit.py +650 -0
- dartsort/transform/all_transformers.py +58 -0
- dartsort/transform/amortized_localization.py +503 -0
- dartsort/transform/amplitudes.py +197 -0
- dartsort/transform/decollider.py +635 -0
- dartsort/transform/enforce_decrease.py +158 -0
- dartsort/transform/localize.py +86 -0
- dartsort/transform/matching_denoiser.py +44 -0
- dartsort/transform/pipeline.py +391 -0
- dartsort/transform/single_channel_denoiser.py +153 -0
- dartsort/transform/single_channel_templates.py +98 -0
- dartsort/transform/supervised_denoiser.py +486 -0
- dartsort/transform/temporal_pca.py +413 -0
- dartsort/transform/transform_base.py +255 -0
- dartsort/util/__init__.py +0 -0
- dartsort/util/cli_util.py +179 -0
- dartsort/util/data_util.py +1128 -0
- dartsort/util/drift_util.py +1054 -0
- dartsort/util/internal_config.py +1074 -0
- dartsort/util/interpolation_util.py +1012 -0
- dartsort/util/job_util.py +19 -0
- dartsort/util/logging_util.py +91 -0
- dartsort/util/main_util.py +235 -0
- dartsort/util/more_operators.py +269 -0
- dartsort/util/multiprocessing_util.py +211 -0
- dartsort/util/nn_util.py +461 -0
- dartsort/util/noise_util.py +1461 -0
- dartsort/util/peel_util.py +223 -0
- dartsort/util/py_util.py +213 -0
- dartsort/util/registration_util.py +128 -0
- dartsort/util/residual_util.py +30 -0
- dartsort/util/sparse_util.py +81 -0
- dartsort/util/spikeio.py +227 -0
- dartsort/util/spiketorch.py +1198 -0
- dartsort/util/testing_util/__init__.py +0 -0
- dartsort/util/testing_util/matching_debug_util.py +517 -0
- dartsort/util/testing_util/mixture_testing_util.py +291 -0
- dartsort/util/torch_optimization_util.py +156 -0
- dartsort/util/torch_util.py +75 -0
- dartsort/util/universal_util.py +269 -0
- dartsort/util/waveform_util.py +809 -0
- dartsort/vis/__init__.py +19 -0
- dartsort/vis/analysis_plots.py +392 -0
- dartsort/vis/colors.py +36 -0
- dartsort/vis/gt.py +392 -0
- dartsort/vis/layout.py +149 -0
- dartsort/vis/metrics_util.py +23 -0
- dartsort/vis/mixture.py +1489 -0
- dartsort/vis/over_time.py +514 -0
- dartsort/vis/recanim.py +429 -0
- dartsort/vis/scatterplots.py +779 -0
- dartsort/vis/sorting.py +163 -0
- dartsort/vis/unit.py +917 -0
- dartsort/vis/unit_comparison.py +1017 -0
- dartsort/vis/versus.py +300 -0
- dartsort/vis/vismain.py +537 -0
- dartsort/vis/waveforms.py +357 -0
- dartsort-0.3.5.dist-info/METADATA +93 -0
- dartsort-0.3.5.dist-info/RECORD +125 -0
- dartsort-0.3.5.dist-info/WHEEL +5 -0
- dartsort-0.3.5.dist-info/entry_points.txt +2 -0
- dartsort-0.3.5.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,514 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
import h5py
|
|
4
|
+
import matplotlib.pyplot as plt
|
|
5
|
+
import numpy as np
|
|
6
|
+
from matplotlib import animation
|
|
7
|
+
|
|
8
|
+
from ..clustering import merge
|
|
9
|
+
from ..templates import templates
|
|
10
|
+
from ..templates.get_templates import fit_tsvd
|
|
11
|
+
from ..util import data_util
|
|
12
|
+
from ..util.internal_config import raw_template_cfg
|
|
13
|
+
from ..evaluate import analysis
|
|
14
|
+
from . import analysis_plots, scatterplots, layout, unit
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# -- over_time_summary stuff
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class OverTimePlot(layout.BasePlot):
|
|
21
|
+
width = 8
|
|
22
|
+
height = 1
|
|
23
|
+
kind = "over_time"
|
|
24
|
+
|
|
25
|
+
def draw(self, panel, chunk_sorting_analyses, unit_id):
|
|
26
|
+
raise NotImplementedError
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class UnitPlotOverTime(OverTimePlot):
|
|
30
|
+
def __init__(self, unit_plot, height=1, sharey=True):
|
|
31
|
+
self.height = height
|
|
32
|
+
self.unit_plot = unit_plot
|
|
33
|
+
self.sharey = sharey and unit_plot.can_sharey
|
|
34
|
+
|
|
35
|
+
def notify_global_params(self, **params):
|
|
36
|
+
super().notify_global_params(**params)
|
|
37
|
+
self.unit_plot.notify_global_params(**params)
|
|
38
|
+
|
|
39
|
+
def draw(self, panel, chunk_sorting_analyses, unit_id):
|
|
40
|
+
axes = panel.subplots(ncols=len(chunk_sorting_analyses), sharey=self.sharey)
|
|
41
|
+
first = True
|
|
42
|
+
for j, (ax, csa) in enumerate(zip(axes, chunk_sorting_analyses)):
|
|
43
|
+
if unit_id not in csa.unit_ids:
|
|
44
|
+
ax.axis("off")
|
|
45
|
+
continue
|
|
46
|
+
self.unit_plot.draw(None, csa, unit_id, axis=ax)
|
|
47
|
+
if self.sharey:
|
|
48
|
+
ax.set_yticks([])
|
|
49
|
+
ax.set_ylabel("")
|
|
50
|
+
if not first:
|
|
51
|
+
lg = ax.get_legend()
|
|
52
|
+
if lg is not None:
|
|
53
|
+
lg.remove()
|
|
54
|
+
first = False
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class SelfDistanceOverTime(OverTimePlot):
|
|
58
|
+
height = 1.5
|
|
59
|
+
|
|
60
|
+
def __init__(self, show_values=True):
|
|
61
|
+
super().__init__()
|
|
62
|
+
self.show_values = show_values
|
|
63
|
+
|
|
64
|
+
def draw(self, panel, chunk_sorting_analyses, unit_id):
|
|
65
|
+
imax, longax = panel.subplots(ncols=2, width_ratios=[1, 2])
|
|
66
|
+
nchunks = len(chunk_sorting_analyses)
|
|
67
|
+
|
|
68
|
+
temps = []
|
|
69
|
+
counts = []
|
|
70
|
+
chunkixs = []
|
|
71
|
+
for j, csa in enumerate(chunk_sorting_analyses):
|
|
72
|
+
ctd = csa.coarse_template_data
|
|
73
|
+
um = ctd.unit_mask(unit_id)
|
|
74
|
+
if not um.sum():
|
|
75
|
+
continue
|
|
76
|
+
um = np.flatnonzero(um)
|
|
77
|
+
assert um.size == 1
|
|
78
|
+
um = um[0]
|
|
79
|
+
temps.append(ctd.templates[um])
|
|
80
|
+
counts.append(ctd.spike_counts[um])
|
|
81
|
+
chunkixs.append(j)
|
|
82
|
+
temps = np.stack(temps, axis=0)
|
|
83
|
+
counts = np.ravel(counts)
|
|
84
|
+
chunkixs = np.array(chunkixs)
|
|
85
|
+
chunks_td = templates.TemplateData(temps, chunkixs, counts)
|
|
86
|
+
chunks, dists, _, _ = merge.calculate_merge_distances(chunks_td, n_jobs=0)
|
|
87
|
+
|
|
88
|
+
im = imax.imshow(
|
|
89
|
+
dists,
|
|
90
|
+
vmin=0,
|
|
91
|
+
vmax=1,
|
|
92
|
+
cmap=plt.cm.RdGy,
|
|
93
|
+
origin="lower",
|
|
94
|
+
interpolation="none",
|
|
95
|
+
)
|
|
96
|
+
imax.set_xticks(range(len(chunkixs)), chunkixs)
|
|
97
|
+
imax.set_yticks(range(len(chunkixs)), chunkixs)
|
|
98
|
+
if self.show_values:
|
|
99
|
+
for (j, i), label in np.ndenumerate(dists):
|
|
100
|
+
imax.text(i, j, f"{label:.2f}", ha="center", va="center")
|
|
101
|
+
plt.colorbar(im, ax=imax, shrink=0.3)
|
|
102
|
+
imax.set_title("inter-chunk template distances")
|
|
103
|
+
|
|
104
|
+
longax.plot(chunkixs[:-1], np.diag(dists, k=1))
|
|
105
|
+
longax.set_ylabel("dist between temps of chunk $i,i+1$")
|
|
106
|
+
longax.set_xlabel("chunk index $i$")
|
|
107
|
+
longax.set_title("neighboring chunk template distances")
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
over_time_plots = (
|
|
111
|
+
UnitPlotOverTime(unit.RawWaveformPlot(), height=2),
|
|
112
|
+
SelfDistanceOverTime(),
|
|
113
|
+
UnitPlotOverTime(unit.PCAScatter()),
|
|
114
|
+
UnitPlotOverTime(unit.XZScatter()),
|
|
115
|
+
UnitPlotOverTime(unit.TimeAmpScatter()),
|
|
116
|
+
UnitPlotOverTime(unit.NearbyCoarseTemplatesPlot(), height=2),
|
|
117
|
+
UnitPlotOverTime(unit.CoarseTemplateDistancePlot(), height=1.5),
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def make_all_over_time_summaries(
|
|
122
|
+
sorting_analysis,
|
|
123
|
+
save_folder,
|
|
124
|
+
plots=over_time_plots,
|
|
125
|
+
chunk_length_s=300.0,
|
|
126
|
+
template_config=raw_template_cfg,
|
|
127
|
+
channel_show_radius_um=50.0,
|
|
128
|
+
amplitude_color_cutoff=15.0,
|
|
129
|
+
pca_radius_um=75.0,
|
|
130
|
+
max_height=None,
|
|
131
|
+
figsize=(16, 8.5),
|
|
132
|
+
hspace=0.05,
|
|
133
|
+
dpi=200,
|
|
134
|
+
image_ext="png",
|
|
135
|
+
n_jobs=0,
|
|
136
|
+
n_jobs_templates=0,
|
|
137
|
+
show_progress=True,
|
|
138
|
+
overwrite=False,
|
|
139
|
+
unit_ids=None,
|
|
140
|
+
analysis_kw=None,
|
|
141
|
+
):
|
|
142
|
+
# get unit ids
|
|
143
|
+
if unit_ids is None:
|
|
144
|
+
unit_ids = sorting_analysis.unit_ids
|
|
145
|
+
|
|
146
|
+
# check if done before expensive stuff
|
|
147
|
+
done = not overwrite and unit.all_summaries_done(unit_ids, save_folder)
|
|
148
|
+
if done:
|
|
149
|
+
return
|
|
150
|
+
|
|
151
|
+
# make chunk sorting analyses
|
|
152
|
+
# chunk sortings
|
|
153
|
+
chunk_time_ranges_s, chunk_sortings = data_util.time_chunk_sortings(
|
|
154
|
+
sorting=sorting_analysis.sorting,
|
|
155
|
+
recording=sorting_analysis.recording,
|
|
156
|
+
chunk_length_samples=chunk_length_s
|
|
157
|
+
* sorting_analysis.recording.sampling_frequency,
|
|
158
|
+
)
|
|
159
|
+
# tsvd
|
|
160
|
+
tsvd = fit_tsvd(
|
|
161
|
+
sorting_analysis.recording,
|
|
162
|
+
sorting_analysis.sorting,
|
|
163
|
+
denoising_fit_radius=pca_radius_um,
|
|
164
|
+
)
|
|
165
|
+
# chunk sorting analyses
|
|
166
|
+
if analysis_kw is None:
|
|
167
|
+
analysis_kw = {}
|
|
168
|
+
chunk_sorting_analyses = [
|
|
169
|
+
analysis.DARTsortAnalysis.from_sorting(
|
|
170
|
+
sorting_analysis.recording,
|
|
171
|
+
chunk_sorting,
|
|
172
|
+
motion_est=sorting_analysis.motion_est,
|
|
173
|
+
template_config=template_config,
|
|
174
|
+
allow_template_reload=False,
|
|
175
|
+
n_jobs_templates=n_jobs_templates,
|
|
176
|
+
denoising_tsvd=tsvd,
|
|
177
|
+
**analysis_kw,
|
|
178
|
+
)
|
|
179
|
+
for chunk_sorting in chunk_sortings
|
|
180
|
+
]
|
|
181
|
+
|
|
182
|
+
if max_height is None:
|
|
183
|
+
max_height = sum(p.height for p in plots)
|
|
184
|
+
unit.make_all_summaries(
|
|
185
|
+
chunk_sorting_analyses,
|
|
186
|
+
save_folder,
|
|
187
|
+
plots=plots,
|
|
188
|
+
channel_show_radius_um=channel_show_radius_um,
|
|
189
|
+
amplitude_color_cutoff=amplitude_color_cutoff,
|
|
190
|
+
pca_radius_um=pca_radius_um,
|
|
191
|
+
max_height=max_height,
|
|
192
|
+
figsize=figsize,
|
|
193
|
+
hspace=hspace,
|
|
194
|
+
dpi=dpi,
|
|
195
|
+
image_ext=image_ext,
|
|
196
|
+
n_jobs=n_jobs,
|
|
197
|
+
show_progress=show_progress,
|
|
198
|
+
overwrite=overwrite,
|
|
199
|
+
unit_ids=unit_ids,
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
# -- animation stuff
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def sorting_scatter_animation(
|
|
207
|
+
sorting_analysis,
|
|
208
|
+
output_path,
|
|
209
|
+
chunk_sortings=None,
|
|
210
|
+
chunk_templates=None,
|
|
211
|
+
chunk_time_ranges_s=None,
|
|
212
|
+
chunk_length_samples=None,
|
|
213
|
+
show_template_main_traces=True,
|
|
214
|
+
scatter_template_features=True,
|
|
215
|
+
suptitle=None,
|
|
216
|
+
depth_feature="z",
|
|
217
|
+
features=("x", "amplitude", "log_peak_to_trough"),
|
|
218
|
+
template_config=raw_template_cfg,
|
|
219
|
+
templates_tsvd=None,
|
|
220
|
+
trough_offset_samples=42,
|
|
221
|
+
spike_length_samples=121,
|
|
222
|
+
n_jobs_templates=0,
|
|
223
|
+
device=None,
|
|
224
|
+
feature_getters=None,
|
|
225
|
+
interval=1500,
|
|
226
|
+
figsize=(11, 8.5),
|
|
227
|
+
dpi=150,
|
|
228
|
+
):
|
|
229
|
+
output_path = Path(output_path)
|
|
230
|
+
assert output_path.parent.exists()
|
|
231
|
+
assert output_path.name.endswith(".mp4")
|
|
232
|
+
|
|
233
|
+
if chunk_templates is not None:
|
|
234
|
+
assert chunk_sortings is not None
|
|
235
|
+
|
|
236
|
+
# first, what sortings are we using in each chunk?
|
|
237
|
+
if chunk_sortings is None:
|
|
238
|
+
chunk_time_ranges_s, chunk_sortings = data_util.time_chunk_sortings(
|
|
239
|
+
sorting=sorting_analysis.sorting,
|
|
240
|
+
recording=sorting_analysis.recording,
|
|
241
|
+
chunk_time_ranges_s=chunk_time_ranges_s,
|
|
242
|
+
chunk_length_samples=chunk_length_samples,
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
# next, if we need them, make templates for each unit in each chunk
|
|
246
|
+
use_chunk_templates = show_template_main_traces or scatter_template_features
|
|
247
|
+
if use_chunk_templates and chunk_templates is None:
|
|
248
|
+
chunk_templates = templates.get_chunked_templates(
|
|
249
|
+
sorting_analysis.recording,
|
|
250
|
+
template_config,
|
|
251
|
+
global_sorting=sorting_analysis.sorting,
|
|
252
|
+
chunk_sortings=chunk_sortings,
|
|
253
|
+
chunk_time_ranges_s=chunk_time_ranges_s,
|
|
254
|
+
motion_est=sorting_analysis.motion_est,
|
|
255
|
+
n_jobs=n_jobs_templates,
|
|
256
|
+
device=device,
|
|
257
|
+
trough_offset_samples=trough_offset_samples,
|
|
258
|
+
spike_length_samples=spike_length_samples,
|
|
259
|
+
tsvd=templates_tsvd,
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
# make the animation
|
|
263
|
+
fig, anim = make_animation(
|
|
264
|
+
sorting_analysis,
|
|
265
|
+
chunk_time_ranges_s,
|
|
266
|
+
chunk_sortings,
|
|
267
|
+
chunk_templates,
|
|
268
|
+
feature_names=features,
|
|
269
|
+
depth_feature=depth_feature,
|
|
270
|
+
show_template_main_traces=show_template_main_traces,
|
|
271
|
+
scatter_template_features=scatter_template_features,
|
|
272
|
+
feature_getters=None,
|
|
273
|
+
suptitle=suptitle,
|
|
274
|
+
interval=interval,
|
|
275
|
+
figsize=figsize,
|
|
276
|
+
)
|
|
277
|
+
# messing with this to try to make the videos work
|
|
278
|
+
# with mac quicktime
|
|
279
|
+
anim.save(
|
|
280
|
+
str(output_path),
|
|
281
|
+
writer="ffmpeg",
|
|
282
|
+
codec="libx264",
|
|
283
|
+
extra_args=[
|
|
284
|
+
"-pix_fmt",
|
|
285
|
+
"yuv420p",
|
|
286
|
+
"-vf",
|
|
287
|
+
"pad=ceil(iw/2)*2:ceil(ih/2)*2",
|
|
288
|
+
# "-c:v", "hevc_videotoolbox",
|
|
289
|
+
# "-level:v", "5.1", "-profile:v", "High",
|
|
290
|
+
],
|
|
291
|
+
dpi=dpi,
|
|
292
|
+
bitrate=-1,
|
|
293
|
+
)
|
|
294
|
+
plt.close(fig)
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
usual_feature_getters = {
|
|
298
|
+
"x": ("point_source_localizations", (slice(None), 0)),
|
|
299
|
+
"z": ("point_source_localizations", (slice(None), 2)),
|
|
300
|
+
"amplitude": ("denoised_ptp_amplitudes", slice(None)),
|
|
301
|
+
"log_peak_to_trough": ("denoised_logpeaktotrough", slice(None)),
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def get_template_features(template_data, feature_names, scatter_template_features):
|
|
306
|
+
if not scatter_template_features:
|
|
307
|
+
return {}
|
|
308
|
+
|
|
309
|
+
features = {}
|
|
310
|
+
if "x" in feature_names or "z" in feature_names:
|
|
311
|
+
xyza = template_data.template_locations()
|
|
312
|
+
features["x"] = xyza["x"]
|
|
313
|
+
features["z"] = xyza["z_abs"]
|
|
314
|
+
|
|
315
|
+
if "amplitude" in feature_names:
|
|
316
|
+
features["amplitude"] = np.ptp(template_data.templates, 1).max(1)
|
|
317
|
+
|
|
318
|
+
if "log_peak_to_trough" in feature_names:
|
|
319
|
+
features["log_peak_to_trough"] = np.log(
|
|
320
|
+
-template_data.templates.max((1, 2)) / template_data.templates.min((1, 2))
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
return features
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
def update_frame(
|
|
327
|
+
axes,
|
|
328
|
+
feature_scatters,
|
|
329
|
+
feature_ellips,
|
|
330
|
+
template_scatters,
|
|
331
|
+
geom_scatter,
|
|
332
|
+
chunk_time_range,
|
|
333
|
+
chunk_sorting,
|
|
334
|
+
all_spike_features,
|
|
335
|
+
geom,
|
|
336
|
+
motion_est,
|
|
337
|
+
depth_feature="z",
|
|
338
|
+
semilog_amplitude=True,
|
|
339
|
+
chunk_template_data=None,
|
|
340
|
+
max_n_templates=None,
|
|
341
|
+
chunk_template_features=None,
|
|
342
|
+
show_template_main_traces=True,
|
|
343
|
+
):
|
|
344
|
+
# main traces
|
|
345
|
+
if show_template_main_traces and chunk_template_data is not None:
|
|
346
|
+
axes[0].clear()
|
|
347
|
+
analysis_plots.scatter_max_channel_waveforms(axes[0], chunk_template_data, lw=1)
|
|
348
|
+
xmin, dx = geom[:, 0].min(), np.ptp(geom[:, 0])
|
|
349
|
+
axes[0].set_xlim([xmin - dx / 2, xmin + 3 * dx / 2])
|
|
350
|
+
axes = axes[1:]
|
|
351
|
+
|
|
352
|
+
spike_mask = np.equal(
|
|
353
|
+
chunk_sorting.times_seconds,
|
|
354
|
+
chunk_sorting.times_seconds.clip(*chunk_time_range),
|
|
355
|
+
)
|
|
356
|
+
non_depth_keys = [k for k in all_spike_features if k != depth_feature]
|
|
357
|
+
for j, k in enumerate(non_depth_keys):
|
|
358
|
+
# determine this chunk's spike mask
|
|
359
|
+
(
|
|
360
|
+
_,
|
|
361
|
+
feature_scatters[k],
|
|
362
|
+
feature_ellips[k],
|
|
363
|
+
) = scatterplots.scatter_feature_vs_depth(
|
|
364
|
+
all_spike_features[k][spike_mask],
|
|
365
|
+
all_spike_features[depth_feature][spike_mask],
|
|
366
|
+
ax=axes[j],
|
|
367
|
+
labels=chunk_sorting.labels[spike_mask],
|
|
368
|
+
pad_to_max=True,
|
|
369
|
+
scat=feature_scatters.get(k, None),
|
|
370
|
+
geom=chunk_template_data.registered_geom,
|
|
371
|
+
show_ellipses=True,
|
|
372
|
+
max_n_labels=max_n_templates,
|
|
373
|
+
ellip=feature_ellips.get(k, None),
|
|
374
|
+
show_triaged=False,
|
|
375
|
+
)
|
|
376
|
+
|
|
377
|
+
if k != "x":
|
|
378
|
+
xmin, xmax = (
|
|
379
|
+
all_spike_features[k].min(),
|
|
380
|
+
all_spike_features[k].max(),
|
|
381
|
+
)
|
|
382
|
+
margin = (xmax - xmin) * 0.025
|
|
383
|
+
else:
|
|
384
|
+
xmin, xmax = geom[:, 0].min(), geom[:, 0].max()
|
|
385
|
+
margin = 100
|
|
386
|
+
disp = motion_est.disp_at_s(
|
|
387
|
+
sum(chunk_time_range) / 2, geom[:, 1], grid=True
|
|
388
|
+
)
|
|
389
|
+
disp = np.c_[np.zeros_like(disp), disp]
|
|
390
|
+
if geom_scatter:
|
|
391
|
+
geom_scatter[0].set_offsets(geom - disp)
|
|
392
|
+
else:
|
|
393
|
+
geom_scatter.append(
|
|
394
|
+
axes[j].scatter(*(geom - disp).T, s=5, marker="s", color="k", lw=0)
|
|
395
|
+
)
|
|
396
|
+
|
|
397
|
+
if chunk_template_features is not None:
|
|
398
|
+
_, template_scatters[k] = scatterplots.scatter_feature_vs_depth(
|
|
399
|
+
chunk_template_features[k],
|
|
400
|
+
chunk_template_features[depth_feature],
|
|
401
|
+
ax=axes[j],
|
|
402
|
+
labels=chunk_template_data.unit_ids,
|
|
403
|
+
pad_to_max=True,
|
|
404
|
+
max_spikes_plot=max_n_templates,
|
|
405
|
+
scat=template_scatters.get(k, None),
|
|
406
|
+
s=15,
|
|
407
|
+
linewidth=1,
|
|
408
|
+
edgecolor="k",
|
|
409
|
+
marker="s",
|
|
410
|
+
geom=chunk_template_data.registered_geom,
|
|
411
|
+
rasterized=False,
|
|
412
|
+
show_triaged=False,
|
|
413
|
+
)
|
|
414
|
+
|
|
415
|
+
if k == "amplitude" and semilog_amplitude:
|
|
416
|
+
margin = 1.05
|
|
417
|
+
axes[j].set_xlim([xmin / margin, xmax * margin])
|
|
418
|
+
axes[j].semilogx()
|
|
419
|
+
else:
|
|
420
|
+
axes[j].set_xlim([xmin - margin, xmax + margin])
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
def make_animation(
|
|
424
|
+
sorting_analysis,
|
|
425
|
+
chunk_time_ranges_s,
|
|
426
|
+
chunk_sortings,
|
|
427
|
+
chunk_templates,
|
|
428
|
+
feature_names,
|
|
429
|
+
depth_feature="z",
|
|
430
|
+
show_template_main_traces=True,
|
|
431
|
+
scatter_template_features=True,
|
|
432
|
+
feature_getters=None,
|
|
433
|
+
suptitle=None,
|
|
434
|
+
interval=200,
|
|
435
|
+
figsize=(11, 8.5),
|
|
436
|
+
):
|
|
437
|
+
if feature_getters is None:
|
|
438
|
+
feature_getters = usual_feature_getters
|
|
439
|
+
|
|
440
|
+
if len(feature_names):
|
|
441
|
+
with h5py.File(sorting_analysis.hdf5_path, "r", locking=False) as h5:
|
|
442
|
+
all_spike_features = {
|
|
443
|
+
name: h5[feature_getters[name][0]][feature_getters[name][1]]
|
|
444
|
+
for name in list(feature_names) + [depth_feature]
|
|
445
|
+
}
|
|
446
|
+
if (
|
|
447
|
+
depth_feature in all_spike_features
|
|
448
|
+
and sorting_analysis.motion_est is not None
|
|
449
|
+
):
|
|
450
|
+
all_spike_features[depth_feature] = (
|
|
451
|
+
sorting_analysis.motion_est.correct_s(
|
|
452
|
+
t_s=h5["times_seconds"][()],
|
|
453
|
+
depth_um=all_spike_features[depth_feature],
|
|
454
|
+
)
|
|
455
|
+
)
|
|
456
|
+
else:
|
|
457
|
+
all_spike_features = {}
|
|
458
|
+
|
|
459
|
+
all_template_features = None
|
|
460
|
+
max_n_templates = max(t.templates.shape[0] for t in chunk_templates)
|
|
461
|
+
if chunk_templates is not None and scatter_template_features:
|
|
462
|
+
all_template_features = [
|
|
463
|
+
get_template_features(
|
|
464
|
+
chunk_td,
|
|
465
|
+
list(feature_names) + [depth_feature],
|
|
466
|
+
scatter_template_features,
|
|
467
|
+
)
|
|
468
|
+
for chunk_td in chunk_templates
|
|
469
|
+
]
|
|
470
|
+
|
|
471
|
+
# create and lay out figure
|
|
472
|
+
ncols = len(feature_names) + show_template_main_traces
|
|
473
|
+
fig, axes = plt.subplots(
|
|
474
|
+
nrows=1, ncols=ncols, figsize=figsize, layout="constrained", sharey=True
|
|
475
|
+
)
|
|
476
|
+
feature_scatters = {}
|
|
477
|
+
feature_ellips = {}
|
|
478
|
+
template_scatters = {}
|
|
479
|
+
geom_scatter = []
|
|
480
|
+
|
|
481
|
+
def closure(frame):
|
|
482
|
+
j = frame
|
|
483
|
+
print(f"animation {frame=}")
|
|
484
|
+
update_frame(
|
|
485
|
+
axes,
|
|
486
|
+
feature_scatters,
|
|
487
|
+
feature_ellips,
|
|
488
|
+
template_scatters,
|
|
489
|
+
geom_scatter,
|
|
490
|
+
chunk_time_ranges_s[j],
|
|
491
|
+
chunk_sortings[j],
|
|
492
|
+
all_spike_features,
|
|
493
|
+
sorting_analysis.recording.get_channel_locations(),
|
|
494
|
+
sorting_analysis.motion_est,
|
|
495
|
+
depth_feature=depth_feature,
|
|
496
|
+
chunk_template_data=chunk_templates[j],
|
|
497
|
+
max_n_templates=max_n_templates,
|
|
498
|
+
chunk_template_features=all_template_features[j],
|
|
499
|
+
show_template_main_traces=show_template_main_traces,
|
|
500
|
+
)
|
|
501
|
+
|
|
502
|
+
# initialize
|
|
503
|
+
closure(0)
|
|
504
|
+
if suptitle is not None:
|
|
505
|
+
fig.suptitle(suptitle)
|
|
506
|
+
for j, k in enumerate(feature_names):
|
|
507
|
+
axes[j + show_template_main_traces].set_xlabel(k)
|
|
508
|
+
|
|
509
|
+
# run
|
|
510
|
+
anim = animation.FuncAnimation(
|
|
511
|
+
fig, closure, interval=interval, frames=len(chunk_time_ranges_s)
|
|
512
|
+
)
|
|
513
|
+
|
|
514
|
+
return fig, anim
|