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,449 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn.functional as F
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def detect_and_deduplicate(
|
|
6
|
+
traces,
|
|
7
|
+
threshold,
|
|
8
|
+
peak_sign="neg",
|
|
9
|
+
relative_peak_radius=5,
|
|
10
|
+
relative_peak_channel_index=None,
|
|
11
|
+
dedup_temporal_radius=11,
|
|
12
|
+
dedup_channel_index: torch.Tensor | None = None,
|
|
13
|
+
spatial_dedup_batch_size=550,
|
|
14
|
+
exclude_edges=True,
|
|
15
|
+
remove_exact_duplicates=False,
|
|
16
|
+
dedup_index_inds=None,
|
|
17
|
+
return_energies=False,
|
|
18
|
+
detection_mask=None,
|
|
19
|
+
trough_priority=None,
|
|
20
|
+
cumulant_order=None,
|
|
21
|
+
):
|
|
22
|
+
"""Detect and deduplicate peaks
|
|
23
|
+
|
|
24
|
+
torch-based peak detection and deduplication, relying
|
|
25
|
+
on max pooling and scatter operations
|
|
26
|
+
|
|
27
|
+
TODO: reuse bufs and pre-pad.
|
|
28
|
+
|
|
29
|
+
Arguments
|
|
30
|
+
---------
|
|
31
|
+
traces : time by channels tensor
|
|
32
|
+
threshold : float
|
|
33
|
+
dedup_channel_index : channels by n_neighbors tensor
|
|
34
|
+
Channel neighbors index. (See waveform_util for
|
|
35
|
+
more on this format.) If supplied, peaks are kept
|
|
36
|
+
only when they are the largest among their neighbors
|
|
37
|
+
as described by this array
|
|
38
|
+
peak_sign : one of "neg", "pos", "both"
|
|
39
|
+
relative_peak_radius : int
|
|
40
|
+
How many temporal neighbors must you be taller than
|
|
41
|
+
to be considered a peak?
|
|
42
|
+
dedup_temporal_radius : int
|
|
43
|
+
Only the largest peak within this sliding radius
|
|
44
|
+
will be kept
|
|
45
|
+
|
|
46
|
+
Returns
|
|
47
|
+
-------
|
|
48
|
+
times, chans : tensors of shape (n_peaks,)
|
|
49
|
+
peak times in samples relative to start of traces, along
|
|
50
|
+
with corresponding channels
|
|
51
|
+
"""
|
|
52
|
+
if cumulant_order is not None:
|
|
53
|
+
# TODO: combine.
|
|
54
|
+
return detect_and_deduplicate_2d_filters(
|
|
55
|
+
traces,
|
|
56
|
+
cumulant_order=cumulant_order,
|
|
57
|
+
threshold=threshold,
|
|
58
|
+
dedup_channel_index=dedup_channel_index,
|
|
59
|
+
peak_sign=peak_sign,
|
|
60
|
+
relative_peak_radius=relative_peak_radius,
|
|
61
|
+
spatial_dedup_batch_size=spatial_dedup_batch_size,
|
|
62
|
+
exclude_edges=exclude_edges,
|
|
63
|
+
return_energies=return_energies,
|
|
64
|
+
detection_mask=detection_mask,
|
|
65
|
+
trough_priority=trough_priority,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
nsamples, nchans = traces.shape
|
|
69
|
+
sbs = min(nsamples, spatial_dedup_batch_size)
|
|
70
|
+
all_dedup = isinstance(dedup_channel_index, str) and dedup_channel_index == "all"
|
|
71
|
+
if not all_dedup and dedup_channel_index is not None:
|
|
72
|
+
assert dedup_channel_index.shape[0] == nchans
|
|
73
|
+
|
|
74
|
+
# -- handle peak sign. we use max pool below, so make peaks positive
|
|
75
|
+
if peak_sign == "neg":
|
|
76
|
+
energies = traces.neg()
|
|
77
|
+
elif peak_sign == "both":
|
|
78
|
+
energies = traces.abs()
|
|
79
|
+
else:
|
|
80
|
+
assert peak_sign == "pos"
|
|
81
|
+
# no need to copy since max pooling will
|
|
82
|
+
energies = traces
|
|
83
|
+
|
|
84
|
+
# we used to implement with max_pool -> unique, but we can use max_unpool
|
|
85
|
+
# to speed up the second step temporal max pooling
|
|
86
|
+
energies, indices = F.max_pool1d_with_indices(
|
|
87
|
+
energies.T.unsqueeze(0),
|
|
88
|
+
kernel_size=(2 * relative_peak_radius + 1,),
|
|
89
|
+
stride=(1,),
|
|
90
|
+
padding=(relative_peak_radius,),
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
# spatial peak criterion
|
|
94
|
+
if relative_peak_channel_index is not None:
|
|
95
|
+
# we are in 1CT right now
|
|
96
|
+
batch_buffer = energies.new_zeros((nchans + 1, sbs))
|
|
97
|
+
batch_max = energies.new_zeros((nchans, sbs))
|
|
98
|
+
for batch_start in range(0, nsamples, sbs):
|
|
99
|
+
batch_end = min(nsamples, batch_start + sbs)
|
|
100
|
+
batch = energies[:, :, batch_start:batch_end]
|
|
101
|
+
batch_buffer[:nchans, : batch_end - batch_start] = batch
|
|
102
|
+
batch = batch_buffer[:, : batch_end - batch_start]
|
|
103
|
+
torch.amax(
|
|
104
|
+
batch[relative_peak_channel_index],
|
|
105
|
+
dim=1,
|
|
106
|
+
out=batch_max[:, : batch_end - batch_start],
|
|
107
|
+
)
|
|
108
|
+
energies[:, :, batch_start:batch_end].masked_fill_(
|
|
109
|
+
batch_max[:, : batch_end - batch_start]
|
|
110
|
+
> energies[:, :, batch_start:batch_end],
|
|
111
|
+
0.0,
|
|
112
|
+
)
|
|
113
|
+
# unpool will set non-maxima to 0
|
|
114
|
+
energies = F.max_unpool1d(
|
|
115
|
+
energies,
|
|
116
|
+
indices,
|
|
117
|
+
kernel_size=(2 * relative_peak_radius + 1,),
|
|
118
|
+
stride=(1,),
|
|
119
|
+
padding=(relative_peak_radius,),
|
|
120
|
+
output_size=energies.shape,
|
|
121
|
+
)
|
|
122
|
+
# remove peaks smaller than our threshold
|
|
123
|
+
F.threshold(energies, threshold, 0.0, inplace=True)
|
|
124
|
+
if trough_priority and peak_sign == "both":
|
|
125
|
+
tp = torch.where(traces.T < 0, trough_priority, 1.0)
|
|
126
|
+
energies.mul_(tp)
|
|
127
|
+
|
|
128
|
+
# -- temporal deduplication
|
|
129
|
+
if detection_mask is not None:
|
|
130
|
+
energies.mul_(detection_mask.T.to(energies))
|
|
131
|
+
remove = None
|
|
132
|
+
if dedup_temporal_radius and remove_exact_duplicates:
|
|
133
|
+
del indices
|
|
134
|
+
max_energies, indices = F.max_pool1d_with_indices(
|
|
135
|
+
energies,
|
|
136
|
+
kernel_size=(2 * dedup_temporal_radius + 1,),
|
|
137
|
+
stride=(1,),
|
|
138
|
+
padding=(dedup_temporal_radius,),
|
|
139
|
+
)
|
|
140
|
+
self_ix = torch.arange(indices.shape[-1], device=indices.device)
|
|
141
|
+
remove = indices != self_ix
|
|
142
|
+
energies.masked_fill_(remove, 0.0)
|
|
143
|
+
|
|
144
|
+
elif dedup_temporal_radius:
|
|
145
|
+
max_energies = F.max_pool1d(
|
|
146
|
+
energies,
|
|
147
|
+
kernel_size=2 * dedup_temporal_radius + 1,
|
|
148
|
+
stride=1,
|
|
149
|
+
padding=dedup_temporal_radius,
|
|
150
|
+
)
|
|
151
|
+
else:
|
|
152
|
+
max_energies = energies
|
|
153
|
+
# back to TC
|
|
154
|
+
energies = energies[0].T
|
|
155
|
+
max_energies = max_energies[0].T
|
|
156
|
+
|
|
157
|
+
# -- spatial deduplication
|
|
158
|
+
# this is max pooling within the channel index's neighborhood's
|
|
159
|
+
if all_dedup:
|
|
160
|
+
max_energies = max_energies.amax(dim=1, keepdim=True)
|
|
161
|
+
elif dedup_channel_index is not None and remove_exact_duplicates:
|
|
162
|
+
assert remove is not None
|
|
163
|
+
assert dedup_index_inds is not None
|
|
164
|
+
remove = remove[0].T
|
|
165
|
+
|
|
166
|
+
for batch_start in range(0, nsamples, sbs):
|
|
167
|
+
batch_end = min(nsamples, batch_start + sbs)
|
|
168
|
+
|
|
169
|
+
batch = F.pad(max_energies[batch_start:batch_end], (0, 1))
|
|
170
|
+
max_energies[batch_start:batch_end], binds = torch.max(
|
|
171
|
+
batch[:, dedup_channel_index], dim=2
|
|
172
|
+
)
|
|
173
|
+
torch.logical_or(
|
|
174
|
+
remove[batch_start:batch_end],
|
|
175
|
+
binds != dedup_index_inds,
|
|
176
|
+
out=remove[batch_start:batch_end],
|
|
177
|
+
)
|
|
178
|
+
elif dedup_channel_index is not None:
|
|
179
|
+
# pad channel axis with extra chan of 0s
|
|
180
|
+
batch_buffer = max_energies.new_zeros((sbs, nchans + 1))
|
|
181
|
+
for batch_start in range(0, nsamples, sbs):
|
|
182
|
+
batch_end = min(nsamples, batch_start + sbs)
|
|
183
|
+
batch = max_energies[batch_start:batch_end]
|
|
184
|
+
batch_buffer[: batch_end - batch_start, :nchans] = batch
|
|
185
|
+
batch = batch_buffer[: batch_end - batch_start]
|
|
186
|
+
torch.amax(
|
|
187
|
+
batch[:, dedup_channel_index],
|
|
188
|
+
dim=2,
|
|
189
|
+
out=max_energies[batch_start:batch_end],
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
# if temporal/spatial max made you grow, you were not a peak!
|
|
193
|
+
if remove is not None:
|
|
194
|
+
energies.masked_fill_(remove, 0.0)
|
|
195
|
+
if dedup_temporal_radius or (dedup_channel_index is not None):
|
|
196
|
+
remove = torch.gt(max_energies, energies, out=remove)
|
|
197
|
+
max_energies.masked_fill_(remove, 0.0)
|
|
198
|
+
|
|
199
|
+
# this matches the behavior of scipy argrelmax
|
|
200
|
+
if exclude_edges:
|
|
201
|
+
max_energies[[0, -1], :] = 0.0
|
|
202
|
+
|
|
203
|
+
# sparsify and return
|
|
204
|
+
times, chans = torch.nonzero(max_energies, as_tuple=True)
|
|
205
|
+
|
|
206
|
+
if return_energies:
|
|
207
|
+
return times, chans, energies[times, chans]
|
|
208
|
+
|
|
209
|
+
return times, chans
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def singlechan_template_detect_and_deduplicate(
|
|
213
|
+
traces,
|
|
214
|
+
singlechan_templates,
|
|
215
|
+
threshold=40.0,
|
|
216
|
+
trough_offset_samples=42,
|
|
217
|
+
relative_peak_channel_index=None,
|
|
218
|
+
dedup_channel_index=None,
|
|
219
|
+
relative_peak_radius=5,
|
|
220
|
+
dedup_temporal_radius=7,
|
|
221
|
+
spatial_dedup_batch_size=512,
|
|
222
|
+
exclude_edges=True,
|
|
223
|
+
return_energies=False,
|
|
224
|
+
detection_mask=None,
|
|
225
|
+
):
|
|
226
|
+
"""Detect spikes by per-channel matching with normalized templates
|
|
227
|
+
|
|
228
|
+
See peel/universal_util.py to get some templates.
|
|
229
|
+
"""
|
|
230
|
+
# convolve with templates
|
|
231
|
+
conv_traces = traces.T.unsqueeze(1)
|
|
232
|
+
conv_filt = singlechan_templates.unsqueeze(1)
|
|
233
|
+
full = 2 * (singlechan_templates.shape[1] // 2)
|
|
234
|
+
conv = F.conv1d(conv_traces, conv_filt, padding=full)
|
|
235
|
+
|
|
236
|
+
# exactly align the convolution with the original traces
|
|
237
|
+
offset = full - trough_offset_samples
|
|
238
|
+
conv = conv[:, :, offset : offset + len(traces)]
|
|
239
|
+
|
|
240
|
+
# convert to scaled deconvolution objective
|
|
241
|
+
# when templates are normalized, the decrease in residual normsq
|
|
242
|
+
# due to subtracting a scaled template is just the conv squared.
|
|
243
|
+
obj = conv.square_().amax(dim=1).T
|
|
244
|
+
|
|
245
|
+
# get peaks
|
|
246
|
+
times, chans = detect_and_deduplicate( # type: ignore
|
|
247
|
+
obj,
|
|
248
|
+
threshold=threshold,
|
|
249
|
+
relative_peak_channel_index=relative_peak_channel_index,
|
|
250
|
+
dedup_channel_index=dedup_channel_index,
|
|
251
|
+
peak_sign="pos",
|
|
252
|
+
relative_peak_radius=relative_peak_radius,
|
|
253
|
+
dedup_temporal_radius=dedup_temporal_radius,
|
|
254
|
+
spatial_dedup_batch_size=spatial_dedup_batch_size,
|
|
255
|
+
exclude_edges=exclude_edges,
|
|
256
|
+
return_energies=False,
|
|
257
|
+
detection_mask=detection_mask,
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
if return_energies:
|
|
261
|
+
return times, chans, traces[times, chans]
|
|
262
|
+
|
|
263
|
+
return times, chans
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def compute_sliding_2d_cumulant(radiality, order, win_size, chunk_size=256):
|
|
267
|
+
"""
|
|
268
|
+
Compute sliding cumulant statistics (mean, variance, skewness, kurtosis) over spatial 2D windows,
|
|
269
|
+
processing the W-axis in manageable chunks.
|
|
270
|
+
|
|
271
|
+
Args:
|
|
272
|
+
radiality: (C, H, W) tensor
|
|
273
|
+
order: cumulant order (1=mean, 2=variance, 3=skewness, 4=kurtosis)
|
|
274
|
+
win_size: size of spatial window (must be odd for symmetry)
|
|
275
|
+
chunk_size: number of W-axis columns to process per chunk (default: 30,000)
|
|
276
|
+
|
|
277
|
+
Returns:
|
|
278
|
+
Tensor of shape (C, H, W) with the cumulant statistic at each spatial location
|
|
279
|
+
"""
|
|
280
|
+
C, H, W = radiality.shape
|
|
281
|
+
|
|
282
|
+
if win_size % 2 == 0:
|
|
283
|
+
raise ValueError("win_size must be odd for symmetric padding")
|
|
284
|
+
|
|
285
|
+
padding = win_size // 2
|
|
286
|
+
|
|
287
|
+
# Pad spatial dimensions
|
|
288
|
+
padded = F.pad(
|
|
289
|
+
radiality.unsqueeze(1), (padding, padding, padding, padding), mode="reflect"
|
|
290
|
+
) # (C, 1, H+2p, W+2p)
|
|
291
|
+
|
|
292
|
+
results = []
|
|
293
|
+
start = 0
|
|
294
|
+
while start < W:
|
|
295
|
+
end = min(start + chunk_size, W)
|
|
296
|
+
|
|
297
|
+
# Extract current chunk with extra padding
|
|
298
|
+
# Pad adds padding to both sides, so for columns start:end in the original,
|
|
299
|
+
# we need columns start:end + 2*padding in padded space
|
|
300
|
+
padded_start = start
|
|
301
|
+
padded_end = end + 2 * padding
|
|
302
|
+
|
|
303
|
+
chunk = padded[
|
|
304
|
+
:, :, :, padded_start:padded_end
|
|
305
|
+
] # (C, 1, H+2p, chunk_width + 2p)
|
|
306
|
+
|
|
307
|
+
# Unfold to extract sliding windows
|
|
308
|
+
windows = chunk.unfold(2, win_size, 1).unfold(
|
|
309
|
+
3, win_size, 1
|
|
310
|
+
) # (C, 1, H, chunk_width, win_size, win_size)
|
|
311
|
+
windows = windows.contiguous().view(
|
|
312
|
+
C, H, end - start, -1
|
|
313
|
+
) # (C, H, chunk_width, win_size*win_size)
|
|
314
|
+
|
|
315
|
+
# Cumulant calculations
|
|
316
|
+
if order == 1:
|
|
317
|
+
result = windows.mean(dim=-1)
|
|
318
|
+
elif order == 2:
|
|
319
|
+
result = windows.var(dim=-1, unbiased=False)
|
|
320
|
+
elif order == 3:
|
|
321
|
+
mean = windows.mean(dim=-1, keepdim=True)
|
|
322
|
+
std = windows.std(dim=-1, unbiased=False, keepdim=True) + 1e-8
|
|
323
|
+
result = (((windows - mean) / std) ** 3).mean(dim=-1)
|
|
324
|
+
elif order == 4:
|
|
325
|
+
mean = windows.mean(dim=-1, keepdim=True)
|
|
326
|
+
std = windows.std(dim=-1, unbiased=False, keepdim=True) + 1e-8
|
|
327
|
+
result = (((windows - mean) / std) ** 4).mean(dim=-1) - 3
|
|
328
|
+
else:
|
|
329
|
+
raise ValueError(f"Unsupported order: {order}")
|
|
330
|
+
|
|
331
|
+
results.append(result) # (C, H, chunk_width)
|
|
332
|
+
start = end
|
|
333
|
+
|
|
334
|
+
return torch.cat(results, dim=-1) # (C, H, W)
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def detect_and_deduplicate_2d_filters(
|
|
338
|
+
traces,
|
|
339
|
+
cum_traces=None,
|
|
340
|
+
cumulant_order=2,
|
|
341
|
+
cumulant_win_size=11,
|
|
342
|
+
radiality=None,
|
|
343
|
+
threshold=4,
|
|
344
|
+
dedup_channel_index=None,
|
|
345
|
+
peak_sign="neg",
|
|
346
|
+
relative_peak_radius=5,
|
|
347
|
+
spatial_dedup_batch_size=512,
|
|
348
|
+
exclude_edges=True,
|
|
349
|
+
return_energies=False,
|
|
350
|
+
detection_mask=None,
|
|
351
|
+
trough_priority=None,
|
|
352
|
+
):
|
|
353
|
+
if cumulant_order and cum_traces is None:
|
|
354
|
+
inp = radiality if radiality is not None else traces
|
|
355
|
+
cum_traces = compute_sliding_2d_cumulant(
|
|
356
|
+
inp.unsqueeze(0), cumulant_order, cumulant_win_size
|
|
357
|
+
)
|
|
358
|
+
cum_traces = cum_traces.unsqueeze(0)
|
|
359
|
+
else:
|
|
360
|
+
cum_traces = traces.unsqueeze(0).unsqueeze(0)
|
|
361
|
+
|
|
362
|
+
if radiality is not None:
|
|
363
|
+
radiality = radiality.unsqueeze(0).unsqueeze(0)
|
|
364
|
+
|
|
365
|
+
if peak_sign == "neg":
|
|
366
|
+
energies = traces.neg()
|
|
367
|
+
if radiality is not None:
|
|
368
|
+
radiality = radiality.neg()
|
|
369
|
+
elif peak_sign == "both":
|
|
370
|
+
energies = traces.abs()
|
|
371
|
+
if radiality is not None:
|
|
372
|
+
radiality = radiality.abs()
|
|
373
|
+
else:
|
|
374
|
+
assert peak_sign == "pos"
|
|
375
|
+
energies = traces
|
|
376
|
+
|
|
377
|
+
energies = energies.unsqueeze(0).unsqueeze(0)
|
|
378
|
+
|
|
379
|
+
# max_cum_traces = F.max_pool2d(
|
|
380
|
+
# cum_traces,
|
|
381
|
+
# kernel_size=2 * relative_peak_radius + 1,
|
|
382
|
+
# stride=1,
|
|
383
|
+
# padding=relative_peak_radius,
|
|
384
|
+
# )
|
|
385
|
+
#
|
|
386
|
+
# maxima_mask = (cum_traces >= 0.5 * max_cum_traces) # (1, 1, T, C)
|
|
387
|
+
#
|
|
388
|
+
# # Apply maxima mask
|
|
389
|
+
# energies = energies * maxima_mask
|
|
390
|
+
|
|
391
|
+
if radiality is not None:
|
|
392
|
+
max_radiality = F.max_pool2d(
|
|
393
|
+
radiality,
|
|
394
|
+
kernel_size=2 * relative_peak_radius + 1,
|
|
395
|
+
stride=1,
|
|
396
|
+
padding=relative_peak_radius,
|
|
397
|
+
)
|
|
398
|
+
|
|
399
|
+
maxima_mask = radiality == max_radiality # (1, 1, T, C)
|
|
400
|
+
|
|
401
|
+
# Apply maxima mask
|
|
402
|
+
energies = energies * maxima_mask
|
|
403
|
+
else:
|
|
404
|
+
max_cum_traces = F.max_pool2d(
|
|
405
|
+
cum_traces,
|
|
406
|
+
kernel_size=2 * relative_peak_radius + 1,
|
|
407
|
+
stride=1,
|
|
408
|
+
padding=relative_peak_radius,
|
|
409
|
+
)
|
|
410
|
+
|
|
411
|
+
maxima_mask = cum_traces == max_cum_traces # (1, 1, T, C)
|
|
412
|
+
|
|
413
|
+
# Apply maxima mask
|
|
414
|
+
energies = energies * maxima_mask
|
|
415
|
+
|
|
416
|
+
threshold_mask = cum_traces >= threshold
|
|
417
|
+
energies = energies * threshold_mask
|
|
418
|
+
# F.threshold_(energies, threshold, 0.0)
|
|
419
|
+
|
|
420
|
+
if trough_priority and peak_sign == "both":
|
|
421
|
+
tp = torch.where(traces < 0, trough_priority, 1.0)
|
|
422
|
+
energies.mul_(tp)
|
|
423
|
+
|
|
424
|
+
max_energies = F.max_pool2d(
|
|
425
|
+
energies,
|
|
426
|
+
kernel_size=2 * relative_peak_radius + 1,
|
|
427
|
+
stride=1,
|
|
428
|
+
padding=relative_peak_radius,
|
|
429
|
+
)
|
|
430
|
+
|
|
431
|
+
if detection_mask is not None:
|
|
432
|
+
energies.mul_(detection_mask.to(energies))
|
|
433
|
+
else:
|
|
434
|
+
max_energies = energies
|
|
435
|
+
|
|
436
|
+
# Transpose back to (T, C)
|
|
437
|
+
energies = energies[0][0]
|
|
438
|
+
max_energies = max_energies[0][0]
|
|
439
|
+
|
|
440
|
+
max_energies.masked_fill_(max_energies > energies, 0.0)
|
|
441
|
+
|
|
442
|
+
if exclude_edges:
|
|
443
|
+
max_energies[[0, -1], :] = 0.0
|
|
444
|
+
times, chans = torch.nonzero(max_energies, as_tuple=True)
|
|
445
|
+
|
|
446
|
+
if return_energies:
|
|
447
|
+
return times, chans, energies[times, chans]
|
|
448
|
+
|
|
449
|
+
return times, chans
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn.functional as F
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def vexity(snips):
|
|
6
|
+
snips = torch.asarray(snips)
|
|
7
|
+
assert snips.ndim == 2
|
|
8
|
+
radius = snips.shape[1] // 2
|
|
9
|
+
assert snips.shape[1] == 2 * radius + 1
|
|
10
|
+
|
|
11
|
+
# get the autocovariance of the temporal derivative, with sign change
|
|
12
|
+
# across the center. it's positive for convex (or convave) functions.
|
|
13
|
+
ddt = snips.diff(dim=1)
|
|
14
|
+
ddt[:, radius:].neg_()
|
|
15
|
+
del snips
|
|
16
|
+
vex = (ddt[:, :-1] * ddt[:, 1:]).sum(dim=1)
|
|
17
|
+
|
|
18
|
+
return vex
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def convexity_filter(traces, times, channels, threshold=None, radius=3):
|
|
22
|
+
if threshold is None or not radius:
|
|
23
|
+
return slice(None)
|
|
24
|
+
|
|
25
|
+
# throw away spikes whose vexedness can't be checked
|
|
26
|
+
keep = times.clip(radius, traces.shape[0] - radius - 1) == times
|
|
27
|
+
(keep,) = keep.nonzero(as_tuple=True)
|
|
28
|
+
|
|
29
|
+
# get temporal snips along main channels
|
|
30
|
+
tix = times[keep, None] + torch.arange(-radius, radius + 1, device=times.device)
|
|
31
|
+
snips = traces[tix[:, :], channels[keep, None]]
|
|
32
|
+
assert snips.shape == (len(keep), 2 * radius + 1)
|
|
33
|
+
vex = vexity(snips)
|
|
34
|
+
assert vex.shape == keep.shape
|
|
35
|
+
|
|
36
|
+
keep = keep[vex >= threshold]
|
|
37
|
+
return keep
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .hybrid_util import config_grid
|