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,1198 @@
1
+ import math
2
+ import warnings
3
+ from logging import getLogger
4
+ from typing import overload
5
+
6
+ import linear_operator
7
+ import numpy as np
8
+ import torch
9
+ import torch.nn.functional as F
10
+ from linear_operator.utils.cholesky import psd_safe_cholesky
11
+ from scipy.fftpack import next_fast_len
12
+ from scipy.spatial.distance import squareform
13
+ from sklearn.utils.extmath import svd_flip
14
+ from torch import Tensor
15
+ from torch.fft import irfft, rfft
16
+ from tqdm.auto import trange
17
+
18
+ HAVE_CUPY = False
19
+ try:
20
+ import cupy as cp # type: ignore
21
+
22
+ HAVE_CUPY = True
23
+ except ImportError:
24
+ cp = None
25
+ HAVE_CUPY = False
26
+
27
+ logger = getLogger(__name__)
28
+ log2pi = torch.log(torch.tensor(2 * np.pi))
29
+ _1 = torch.tensor(1.0)
30
+ _0 = torch.tensor(0.0)
31
+
32
+
33
+ def spawn_torch_rg(
34
+ seed: int | np.random.Generator | torch.Generator = 0,
35
+ device: str | torch.device | None = "cpu",
36
+ ):
37
+ if device is None:
38
+ device = "cpu"
39
+ device = torch.device(device)
40
+ if isinstance(seed, torch.Generator) and seed.device == device:
41
+ return seed
42
+ elif isinstance(seed, torch.Generator):
43
+ generator = torch.Generator(device=device)
44
+ generator.manual_seed(seed.seed())
45
+ return generator
46
+ nprg = np.random.default_rng(seed)
47
+ seeder = nprg.spawn(1)[0]
48
+ seed = int.from_bytes(seeder.bytes(8))
49
+ generator = torch.Generator(device=device)
50
+ generator.manual_seed(seed)
51
+ return generator
52
+
53
+
54
+ def ll_via_inv_quad(cov, y, inv_quad_only=False):
55
+ inv_quad, logdet = linear_operator.inv_quad_logdet(
56
+ cov, y.T, logdet=True, reduce_inv_quad=False
57
+ )
58
+ assert inv_quad is not None
59
+ if inv_quad_only:
60
+ return inv_quad
61
+ ll = inv_quad.add_(logdet + log2pi * y.shape[1]).mul_(-0.5)
62
+ return ll
63
+
64
+
65
+ def fast_nanmedian(x, axis=-1):
66
+ is_tensor = torch.is_tensor(x)
67
+ x = torch.nanmedian(torch.as_tensor(x), dim=axis).values
68
+ if is_tensor:
69
+ return x
70
+ else:
71
+ return x.numpy()
72
+
73
+
74
+ def nanmean(x, axis=-1):
75
+ is_tensor = torch.is_tensor(x)
76
+ x = torch.nanmean(torch.as_tensor(x), dim=axis)
77
+ if is_tensor:
78
+ return x
79
+ else:
80
+ return x.numpy()
81
+
82
+
83
+ def sign(x):
84
+ """torch.sign, but nonzero."""
85
+ s = torch.sign(x)
86
+ s.add_(0.1)
87
+ torch.sign(s, out=s)
88
+ return s
89
+
90
+
91
+ @overload
92
+ def ptp(waveforms: Tensor, dim: int = 1, keepdims: bool = False) -> Tensor: ...
93
+
94
+
95
+ @overload
96
+ def ptp(waveforms: np.ndarray, dim: int = 1, keepdims: bool = False) -> np.ndarray: ...
97
+
98
+
99
+ def ptp(waveforms, dim=1, keepdims=False):
100
+ is_tensor = torch.is_tensor(waveforms)
101
+ waveforms = torch.asarray(waveforms)
102
+ if waveforms.shape[dim] > 1:
103
+ amin, amax = torch.aminmax(waveforms, dim=dim)
104
+ v = amax.sub_(amin)
105
+ else:
106
+ v = waveforms.abs().amax(dim=dim)
107
+ if keepdims:
108
+ v = v.unsqueeze(dim)
109
+ if is_tensor:
110
+ return v
111
+ return v.numpy()
112
+
113
+
114
+ @torch.jit.script
115
+ def mean_elbo_dim1(Q: Tensor, log_liks: Tensor) -> Tensor:
116
+ logQ = Q.log().nan_to_num_(nan=None, neginf=0.0)
117
+ logP = log_liks.nan_to_num(nan=None, neginf=0.0)
118
+ oelbo = logP.sub_(logQ).mul_(Q).sum(dim=1)
119
+ oelbo = oelbo.mean()
120
+ return oelbo
121
+
122
+
123
+ @torch.jit.script
124
+ def elbo(Q: Tensor, log_liks: Tensor, reduce_mean: bool = True, dim: int = 1) -> Tensor:
125
+ logQ = Q.log().nan_to_num_(neginf=0.0)
126
+ logP = log_liks.nan_to_num(neginf=0.0)
127
+ oelbo = logP.sub_(logQ).mul_(Q).sum(dim=dim)
128
+ if reduce_mean:
129
+ oelbo = oelbo.mean()
130
+ return oelbo
131
+
132
+
133
+ @torch.jit.script
134
+ def entropy(Q: Tensor, reduce_mean: bool = True, dim: int = 1) -> Tensor:
135
+ logQ = Q.log().nan_to_num_(neginf=0.0)
136
+ H = logQ.mul_(Q).sum(dim=dim)
137
+ if reduce_mean:
138
+ H = H.mean()
139
+ return H.neg_()
140
+
141
+
142
+ @torch.jit.script
143
+ def ecl(
144
+ resps: Tensor, log_liks: Tensor, cl_alpha: float = 1.0, reduce_mean: bool = True
145
+ ) -> Tensor:
146
+ h = entropy(resps, dim=1, reduce_mean=reduce_mean)
147
+ log_lik = log_liks.logsumexp(dim=1)
148
+ if reduce_mean:
149
+ log_lik = log_lik.mean()
150
+ crit = log_lik - cl_alpha * h
151
+ return crit
152
+
153
+
154
+ def taper(waveforms, t_start=10, t_end=20, dim=1):
155
+ nt = waveforms.shape[dim]
156
+ t0 = torch.linspace(-torch.pi, 0.0, steps=t_start)
157
+ t1 = torch.zeros(nt - t_start - t_end)
158
+ t2 = torch.linspace(0, -torch.pi, steps=t_end)
159
+ domain = torch.concatenate((t0, t1, t2))
160
+ window = torch.cos(domain).add(1.0).div(2.0)
161
+ for j in range(dim):
162
+ window = window.unsqueeze(0)
163
+ for j in range(dim + 1, waveforms.ndim):
164
+ window = window.unsqueeze(-1)
165
+ return waveforms * window.to(waveforms)
166
+
167
+
168
+ def minmax(x: np.ndarray) -> np.ndarray:
169
+ x = x - np.min(x)
170
+ return x / np.max(x)
171
+
172
+
173
+ def svd_lowrank_helper(
174
+ x: Tensor,
175
+ rank: int,
176
+ *,
177
+ n_oversamples: int = 10,
178
+ fit_dtype=torch.double,
179
+ niter=21,
180
+ M=None,
181
+ with_loadings: bool = False,
182
+ ) -> tuple[Tensor | None, Tensor, Tensor, Tensor]:
183
+ assert x.ndim == 2
184
+ q = min(rank + n_oversamples, *x.shape)
185
+ orig_dtype = x.dtype
186
+ x = x.to(dtype=fit_dtype)
187
+ if M is not None:
188
+ M = M.to(dtype=fit_dtype)
189
+ U, S, V = torch.svd_lowrank(x, q=q, M=M, niter=niter)
190
+ U = U[..., :rank]
191
+ S = S[..., :rank]
192
+ V = V[..., :rank]
193
+ Vt = V.T
194
+
195
+ # fix sign ambiguity for better reproducibility in unit tests
196
+ U, Vt = svd_flip(U.numpy(force=True), Vt.numpy(force=True))
197
+ U = torch.asarray(U, dtype=orig_dtype, device=S.device).contiguous()
198
+ Vt = torch.asarray(Vt, dtype=orig_dtype, device=S.device).contiguous()
199
+
200
+ if with_loadings:
201
+ loadings = U * S[..., None, :]
202
+ else:
203
+ loadings = None
204
+ components = Vt
205
+ explained_variance = S.square() / (x.shape[0] - 1)
206
+ whitener = torch.sqrt(explained_variance)
207
+ return loadings, components, explained_variance, whitener
208
+
209
+
210
+ def ravel_multi_index(multi_index, dims):
211
+ """torch implementation of np.ravel_multi_index
212
+
213
+ Only implements order="C"
214
+
215
+ Arguments
216
+ ---------
217
+ multi_index : tuple of LongTensor
218
+ dims : tuple of ints
219
+ Shape of tensor to be indexed
220
+
221
+ Returns
222
+ -------
223
+ raveled_indices : LongTensor
224
+ Indices into the flattened tensor of shape `dims`
225
+ """
226
+ if len(dims) == 1:
227
+ if isinstance(multi_index, tuple):
228
+ assert len(multi_index) == 1
229
+ multi_index = multi_index[0]
230
+ assert multi_index.ndim == 1
231
+ return multi_index
232
+
233
+ assert len(multi_index) == len(dims)
234
+
235
+ multi_index = torch.broadcast_tensors(*multi_index)
236
+ strides = torch.tensor([1, *reversed(dims[1:])]).cumprod(0).flip(0)
237
+ out = torch.zeros_like(multi_index[0])
238
+ for j, s in enumerate(strides):
239
+ out += s * multi_index[j]
240
+ return out.view(-1)
241
+
242
+ # collect multi indices
243
+ # multi_index = torch.broadcast_tensors(*multi_index)
244
+ # multi_index = torch.stack(multi_index)
245
+ # # stride along each axis
246
+ # strides = multi_index.new_tensor([1, *reversed(dims[1:])]).cumprod(0).flip(0)
247
+ # # apply strides (along first axis) and reshape
248
+ # strides = strides.view(-1, *([1] * (multi_index.ndim - 1)))
249
+ # raveled_indices = (strides * multi_index).sum(0)
250
+ # return raveled_indices.view(-1)
251
+
252
+
253
+ def torch_add_at_(dest, ix, src, sign=1):
254
+ """Pytorch version of np.{add,subtract}.at
255
+
256
+ Adds src into dest in place at indices (in dest) specified
257
+ by tuple of index arrays ix. So, indices in ix should be
258
+ locations in dest, but the arrays constituting ix should
259
+ have shapes which broadcast to src.shape.
260
+
261
+ Will add multiple times into the same indices. Check out
262
+ docs for scatter_add_ and np.ufunc.at for more details.
263
+ """
264
+ flat_ix = ravel_multi_index(ix, dest.shape)
265
+ if isinstance(src, (float, int)):
266
+ src = torch.tensor(src * sign, dtype=dest.dtype, device=dest.device)
267
+ src = src.broadcast_to(flat_ix.numel())
268
+ else:
269
+ src = src.reshape(-1)
270
+ if sign == -1:
271
+ src = src._neg_view()
272
+ elif sign != 1:
273
+ src = sign * src
274
+ dest.view(-1).scatter_add_(0, flat_ix.to(dest.device), src)
275
+
276
+
277
+ def cupy_add_at_(dest, ix, src, sign=1):
278
+ assert cp is not None
279
+ if torch.is_tensor(dest):
280
+ assert dest.device.type == "cuda"
281
+ dest = cp.asarray(dest)
282
+ if isinstance(ix, tuple):
283
+ ix = tuple(cp.asarray(ii) for ii in ix)
284
+ else:
285
+ ix = cp.asarray(ix)
286
+ if not isinstance(src, (float, int)):
287
+ src = cp.asarray(src)
288
+ if sign == 1:
289
+ cp.add.at(dest, ix, src)
290
+ elif sign == -1:
291
+ cp.subtract.at(dest, ix, src)
292
+ else:
293
+ raise NotImplementedError(f"Need to implement {sign=} in cupy_add_at_.")
294
+
295
+
296
+ add_at_ = torch_add_at_
297
+
298
+
299
+ def try_cupy_add_at_(dest, ix, src, sign=1):
300
+ if not HAVE_CUPY or dest.device.type != "cuda":
301
+ if dest.device.type == "cuda":
302
+ warnings.warn("No cupy.")
303
+ return torch_add_at_(dest, ix, src, sign)
304
+ else:
305
+ return cupy_add_at_(dest, ix, src, sign)
306
+
307
+
308
+ def grab_spikes(
309
+ traces,
310
+ trough_times,
311
+ max_channels,
312
+ channel_index,
313
+ trough_offset=42,
314
+ spike_length_samples=121,
315
+ buffer=0,
316
+ already_padded=True,
317
+ pad_value=torch.nan,
318
+ ):
319
+ """Grab spikes from a tensor of traces"""
320
+ assert trough_times.ndim == 1
321
+ assert max_channels.shape == trough_times.shape
322
+
323
+ if not already_padded:
324
+ traces = F.pad(traces, (0, 1), value=pad_value)
325
+
326
+ spike_sample_offsets = torch.arange(
327
+ buffer - trough_offset,
328
+ buffer - trough_offset + spike_length_samples,
329
+ device=trough_times.device,
330
+ )
331
+ time_ix = trough_times[:, None] + spike_sample_offsets[None, :]
332
+ chan_ix = channel_index[max_channels]
333
+ return traces[time_ix[:, :, None], chan_ix[:, None, :]]
334
+
335
+
336
+ def grab_spikes_full(
337
+ traces,
338
+ trough_times,
339
+ trough_offset=42,
340
+ spike_length_samples=121,
341
+ buffer=0,
342
+ ):
343
+ """Grab spikes from a tensor of traces"""
344
+ assert trough_times.ndim == 1
345
+ spike_sample_offsets = torch.arange(
346
+ buffer - trough_offset,
347
+ buffer - trough_offset + spike_length_samples,
348
+ device=trough_times.device,
349
+ )
350
+ time_ix = trough_times[:, None] + spike_sample_offsets[None, :]
351
+ chan_ix = torch.arange(traces.shape[1], device=traces.device)
352
+ return traces[time_ix[:, :, None], chan_ix[None, None, :]]
353
+
354
+
355
+ def add_spikes_(
356
+ traces,
357
+ trough_times,
358
+ max_channels,
359
+ channel_index,
360
+ waveforms,
361
+ trough_offset=42,
362
+ buffer=0,
363
+ sign=1,
364
+ in_place=True,
365
+ already_padded=True,
366
+ pad_value=torch.nan,
367
+ ):
368
+ """Add or subtract spikes into a tensor of traces
369
+
370
+ Adds or subtracts (sign=-1) spike waveforms into an array of
371
+ traces at times according to trough_times, trough_offset, and buffer,
372
+ and at channels according to channel_index and max_channels.
373
+
374
+ Uses add_at_ above to add overlapping regions multiple times.
375
+ Regular old Tensor.add_() does not do this!
376
+ """
377
+ n_spikes, spike_length_samples, spike_n_chans = waveforms.shape
378
+ T, C_ = traces.shape
379
+ # traces may be padded with an extra chan, so C is the real n_chans
380
+ C = C_ - int(already_padded)
381
+ assert channel_index.shape == (C, spike_n_chans)
382
+ assert trough_times.ndim == 1
383
+ assert max_channels.shape == trough_times.shape == (n_spikes,)
384
+
385
+ if not already_padded:
386
+ traces = F.pad(traces, (0, 1), value=pad_value)
387
+ elif not in_place:
388
+ traces = traces.clone()
389
+
390
+ spike_sample_offsets = torch.arange(
391
+ buffer - trough_offset,
392
+ buffer - trough_offset + spike_length_samples,
393
+ device=trough_times.device,
394
+ )
395
+ time_ix = trough_times[:, None, None] + spike_sample_offsets[None, :, None]
396
+ chan_ix = channel_index[max_channels][:, None, :]
397
+ add_at_(
398
+ traces,
399
+ (time_ix, chan_ix),
400
+ waveforms,
401
+ sign=sign,
402
+ )
403
+ return traces
404
+
405
+
406
+ def subtract_spikes_(
407
+ traces,
408
+ trough_times,
409
+ max_channels,
410
+ channel_index,
411
+ waveforms,
412
+ trough_offset=42,
413
+ buffer=0,
414
+ in_place=True,
415
+ already_padded=True,
416
+ pad_value=torch.nan,
417
+ ):
418
+ return add_spikes_(
419
+ traces,
420
+ trough_times,
421
+ max_channels,
422
+ channel_index,
423
+ waveforms,
424
+ trough_offset=trough_offset,
425
+ buffer=buffer,
426
+ in_place=in_place,
427
+ already_padded=already_padded,
428
+ pad_value=pad_value,
429
+ sign=-1,
430
+ )
431
+
432
+
433
+ def reduce_at_(dest, ix, src, reduce, include_self=True):
434
+ """Pytorch version of np.ufunc.at for a couple of ones torch has
435
+
436
+ Similar to add_at_ but reducing in place with things other than addition
437
+ """
438
+ dest.view(-1).scatter_reduce_(
439
+ 0,
440
+ ravel_multi_index(ix, dest.shape),
441
+ src.reshape(-1),
442
+ reduce=reduce,
443
+ include_self=include_self,
444
+ )
445
+
446
+
447
+ @torch.jit.script
448
+ def argrelmax(
449
+ *,
450
+ x: Tensor,
451
+ radius: int,
452
+ threshold: float,
453
+ arange: Tensor,
454
+ ):
455
+ x1, inds = F.max_pool1d_with_indices(
456
+ x[None, None], kernel_size=(2 * radius + 1,), padding=(radius,), stride=(1,)
457
+ )
458
+ x1 = x1[0, 0]
459
+ inds = inds[0, 0]
460
+ # exclude non-maxima and exact duplicates
461
+ mask = torch.logical_or(x < x1, inds != arange)
462
+ x1.masked_fill_(mask, 0.0)
463
+ F.threshold(x1, threshold, 0.0, inplace=True)
464
+ # exclude edge
465
+ x1[0].zero_()
466
+ x1[-1].zero_()
467
+ ix = torch.nonzero(x1)[:, 0]
468
+ return ix
469
+
470
+
471
+ _cdtypes = {torch.float32: torch.complex64, torch.float64: torch.complex128}
472
+
473
+
474
+ def convolve_lowrank(
475
+ traces,
476
+ spatial_singular,
477
+ temporal_components,
478
+ padding=0,
479
+ out=None,
480
+ ):
481
+ """Depthwise convolution of traces with templates"""
482
+ n_templates, spike_length_samples, rank = temporal_components.shape
483
+ out_len = traces.shape[1] + 2 * padding - spike_length_samples + 1
484
+ if out is None:
485
+ out = torch.empty(
486
+ (n_templates, out_len), dtype=traces.dtype, device=traces.device
487
+ )
488
+ else:
489
+ assert out.shape == (n_templates, out_len)
490
+
491
+ for q in range(rank):
492
+ # units x time
493
+ rec_spatial = spatial_singular[:, q, :] @ traces
494
+
495
+ # convolve with temporal components -- units x time
496
+ temporal = temporal_components[:, :, q]
497
+
498
+ # conv1d with groups! only convolve each unit with its own temporal filter
499
+ conv = F.conv1d(
500
+ rec_spatial[None], temporal[:, None, :], groups=n_templates, padding=padding
501
+ )[0]
502
+
503
+ # o-a turns out not to be helpful, sadly
504
+ # conv = depthwise_oaconv1d(
505
+ # rec_spatial, temporal, padding=padding, f2=temporalf[:, :, q]
506
+ # )
507
+
508
+ if q:
509
+ out += conv
510
+ else:
511
+ out.copy_(conv)
512
+
513
+ # back to units x time (remove extra dim used for conv1d)
514
+ return out
515
+
516
+
517
+ def nancov(
518
+ x,
519
+ weights=None,
520
+ correction=1,
521
+ nan_free=False,
522
+ return_nobs=False,
523
+ force_posdef=False,
524
+ eps=0,
525
+ ):
526
+ """Pairwise covariance estimate
527
+
528
+ Covariances are estimated from masked observations in each feature.
529
+ The result may not be positive definite, but we'll eigh it for you if
530
+ you want.
531
+ """
532
+ if not nan_free:
533
+ mask = x.isfinite().to(x)
534
+ x = x.nan_to_num()
535
+
536
+ if weights is not None:
537
+ xtx = (x.T * weights) @ x
538
+ if nan_free:
539
+ nobs = weights.sum(0)
540
+ else:
541
+ nobs = (mask.T * weights) @ mask # type: ignore
542
+ else:
543
+ xtx = x.T @ x
544
+ if nan_free:
545
+ nobs = np.array(len(x), dtype=x.dtype)
546
+ else:
547
+ nobs = mask.T @ mask # type: ignore
548
+ denom = nobs - correction
549
+ denom[denom <= 0] = 1
550
+ cov = xtx / denom
551
+
552
+ if force_posdef:
553
+ try:
554
+ if eps:
555
+ np.fill_diagonal(cov, np.diagonal(cov) + eps)
556
+ vals, vecs = torch.linalg.eigh(cov)
557
+ good = vals > 0
558
+ cov = (vecs[:, good] * vals[good]) @ vecs[:, good].T
559
+ except Exception as e:
560
+ if not cov.isfinite().all():
561
+ raise e
562
+ else:
563
+ warnings.warn(
564
+ f"Error in nancov's eigh, shown below, was ignored because the covariance remained finite. {e}"
565
+ )
566
+
567
+ if return_nobs:
568
+ return cov, nobs
569
+ return cov
570
+
571
+
572
+ def cosine_distance(means, means_b=None, true_distance=True):
573
+ means = means.reshape(means.shape[0], -1)
574
+ sym = means_b is None
575
+ if sym:
576
+ means_b = means
577
+ else:
578
+ means_b = means_b.reshape(means_b.shape[0], -1)
579
+ dot = means @ means_b.T
580
+ norm = means.square().sum(1).sqrt_()
581
+ norm[norm == 0] = 1
582
+ if sym:
583
+ norm_b = norm
584
+ else:
585
+ norm_b = means_b.square().sum(1).sqrt_()
586
+ norm_b[norm_b == 0] = 1
587
+ dot /= norm[:, None]
588
+ dot /= norm_b[None, :]
589
+ dist = torch.subtract(_1, dot, out=dot)
590
+ if sym:
591
+ dist.diagonal().fill_(0.0)
592
+ if true_distance:
593
+ dist.mul_(2.0).sqrt_()
594
+ return dist
595
+
596
+
597
+ def weighted_normeuc_distance(means, weights, batch_size=512, min_iou=0.75):
598
+ assert means.ndim == 3
599
+ assert weights.ndim == 2
600
+ k, p = means.shape[:2]
601
+ assert weights.shape == (k, means.shape[2])
602
+
603
+ ii, jj = torch.triu_indices(k, k, offset=1)
604
+ npair = ii.shape[0]
605
+
606
+ pdist = means.new_full((npair,), torch.inf)
607
+ piou = means.new_zeros(npair)
608
+
609
+ weights = weights / weights.amax(dim=1, keepdims=True)
610
+
611
+ for i0 in trange(0, npair, batch_size):
612
+ i1 = min(npair, i0 + batch_size)
613
+
614
+ iii = ii[i0:i1]
615
+ jjj = jj[i0:i1]
616
+
617
+ wi = weights[iii]
618
+ wj = weights[jjj]
619
+ wmin = torch.minimum(wi, wj)
620
+ wminsum = wmin.sum(1)
621
+ piou[i0:i1] = wmin.sum(1) / torch.maximum(wi, wj).sum(1)
622
+ (valid,) = (piou[i0:i1] >= min_iou).nonzero(as_tuple=True)
623
+
624
+ xi = means[iii[valid]]
625
+ xj = means[jjj[valid]]
626
+
627
+ # w = wi[valid] * wj[valid]
628
+ w = wmin[valid] / (p * wminsum[valid, None])
629
+ dist = (xi - xj).square_().mul_(w[:, None]).sum(dim=(1, 2))
630
+ nmi = (xi.square_().mul_(w[:, None])).sum(dim=(1, 2)).sqrt_()
631
+ nmj = (xj.square_().mul_(w[:, None])).sum(dim=(1, 2)).sqrt_()
632
+
633
+ dist = dist.div_(nmi).div_(nmj)
634
+ pdist[i0 + valid] = dist
635
+ pdist = pdist.numpy(force=True)
636
+ return squareform(pdist)
637
+
638
+
639
+ def weighted_normsup_distance(means, weights, batch_size=512, min_iou=0.75):
640
+ assert means.ndim == 3
641
+ assert weights.ndim == 2
642
+ k, p = means.shape[:2]
643
+ assert weights.shape == (k, means.shape[2])
644
+
645
+ ii, jj = torch.triu_indices(k, k, offset=1)
646
+ npair = ii.shape[0]
647
+
648
+ pdist = means.new_full((npair,), torch.inf)
649
+ piou = means.new_zeros(npair)
650
+
651
+ weights = weights / weights.amax(dim=1, keepdims=True)
652
+
653
+ for i0 in trange(0, npair, batch_size):
654
+ i1 = min(npair, i0 + batch_size)
655
+
656
+ iii = ii[i0:i1]
657
+ jjj = jj[i0:i1]
658
+
659
+ wi = weights[iii]
660
+ wj = weights[jjj]
661
+ wmin = torch.minimum(wi, wj)
662
+ wminsum = wmin.sum(1)
663
+ piou[i0:i1] = wmin.sum(1) / torch.maximum(wi, wj).sum(1)
664
+ (valid,) = (piou[i0:i1] >= min_iou).nonzero(as_tuple=True)
665
+
666
+ xi = means[iii[valid]]
667
+ xj = means[jjj[valid]]
668
+
669
+ # w = wi[valid] * wj[valid]
670
+ w = wmin[valid] / (p * wminsum[valid, None])
671
+ dist = (xi - xj).abs_().mul_(w[:, None]).amax(dim=(1, 2))
672
+
673
+
674
+ nmi = (xi.abs_().mul_(w[:, None])).amax(dim=(1, 2))
675
+ nmj = (xj.abs_().mul_(w[:, None])).amax(dim=(1, 2))
676
+
677
+ dist = dist.div_(torch.minimum(nmi, nmj))
678
+ pdist[i0 + valid] = dist
679
+ pdist = pdist.numpy(force=True)
680
+ return squareform(pdist)
681
+
682
+
683
+ def maxz_distance(means, stderrs, weights, batch_size=512, min_iou=0.75):
684
+ assert means.ndim == 3
685
+ assert weights.ndim == 2
686
+ k, p = means.shape[:2]
687
+ assert weights.shape == (k, means.shape[2])
688
+
689
+ ii, jj = torch.triu_indices(k, k, offset=1)
690
+ npair = ii.shape[0]
691
+
692
+ pdist = means.new_full((npair,), torch.inf)
693
+ piou = means.new_zeros(npair)
694
+
695
+ weights = weights / weights.amax(dim=1, keepdims=True)
696
+
697
+ for i0 in trange(0, npair, batch_size):
698
+ i1 = min(npair, i0 + batch_size)
699
+
700
+ iii = ii[i0:i1]
701
+ jjj = jj[i0:i1]
702
+
703
+ wi = weights[iii]
704
+ wj = weights[jjj]
705
+ wmin = torch.minimum(wi, wj)
706
+ piou[i0:i1] = wmin.sum(1) / torch.maximum(wi, wj).sum(1)
707
+ (valid,) = (piou[i0:i1] >= min_iou).nonzero(as_tuple=True)
708
+
709
+ xi = means[iii[valid]]
710
+ xj = means[jjj[valid]]
711
+ sei = stderrs[iii[valid]]
712
+ sej = stderrs[jjj[valid]]
713
+
714
+ # se = sei + sej # deliberately multiplying by 2 to get a chi thing.
715
+ se = 2.0 * torch.minimum(sei, sej)
716
+ z_chan = (xi - xj).div_(se).square_().mean(dim=1).sqrt_()
717
+ max_z = z_chan.amax(dim=1)
718
+ pdist[i0 + valid] = max_z
719
+ pdist = pdist.numpy(force=True)
720
+ return squareform(pdist)
721
+
722
+
723
+ def normeuc_distance(means):
724
+ """|a-b|/sqrt(|a||b|)"""
725
+ means = means.reshape(means.shape[0], -1)
726
+ norms_sqrt = means.square().sum(dim=1).sqrt_().sqrt_()
727
+ dist = torch.cdist(means, means)
728
+ dist.div_(norms_sqrt[:, None])
729
+ dist.div_(norms_sqrt[None, :])
730
+ dist.diagonal().zero_()
731
+ return dist
732
+
733
+
734
+ def woodbury_kl_divergence(C, mu, W=None, mus=None, Ws=None, out=None, batch_size=8):
735
+ """KL divergence with the lemmas, up to affine constant with respect to mu and W
736
+
737
+ Here's the logic between the lines below. Variable names follow this notation.
738
+
739
+ If W is None, then
740
+ 2 DKL_0(other || self) = (mu_self - mu_other)' C^-1 (mu_self - mu_other)
741
+ where d is the dimension.
742
+
743
+ To do n triangular matmuls rather than n^2 full ones, compute this via:
744
+ mu' = C^{-1/2} mu
745
+ (mu_self - mu_other)' C^-1 (mu_self - mu_other) = |mu'_self - mu'_other|^2
746
+
747
+ Otherwise,
748
+ 2 DKL(other || self) = tr(S_self^-1 S_other)
749
+ + log(|S_self| / |S_other|)
750
+ + (mu_self - mu_other)' S_self^-1 (mu_self - mu_other)
751
+ - d
752
+
753
+ For the log dets,
754
+ log|S| = log|C + WW'| = log|C| + log|I_M + W'C^-1W|
755
+ Forget about log|C|, even though it's precomputed. For the other part, to save
756
+ some matmuls, let
757
+ U = C^{-1/2}W
758
+ cap = I_M + W'C^{-1}W = I_m + U'U
759
+ Note that log|C| will cancel.
760
+
761
+ For the trace part, we have
762
+ S_self^-1 S_other = (C + Ws Ws')^-1 (C + Wo Wo')
763
+ = [C^-1 - C^-1 Ws caps^-1 Ws' C^-1] (C + Wo Wo')
764
+ = C^-1 C
765
+ + C^-1 Wo Wo'
766
+ - C^-1 Ws caps^-1 Ws'
767
+ - C^-1 Ws caps^-1 Ws' C^-1 Wo Wo'
768
+
769
+ tr(C^-1 C) = d, so that cancels the constant.
770
+
771
+ To save matmuls, introduce
772
+ Vs = Us caps^{-1/2}
773
+
774
+ Then by cyclic property, save more operations by rewriting
775
+ (A+) tr(C^-1 Wo Wo') = tr(Uo' Uo)
776
+ and
777
+ (B-) tr(C^-1 Ws caps^-1 Ws') = tr(Us caps^-1 Us')
778
+ = tr(Vs Vs')
779
+ and
780
+ (C-) tr(C^-1 Ws caps^-1 Ws' C^-1 Wo Wo')
781
+ = tr(Wo' C^{-1/2} C^{-1/2} Ws caps^{-1/2} caps^{-1/2} Ws' C^{-1/2} C^{-1/2} Wo)
782
+ = tr(Uo' Vs Vs' Uo)
783
+
784
+ The Mahalanobis term remains. Woodbury says
785
+ (mus - muo)' (C + Ws Ws')^-1 (mus - muo)
786
+ = (mus - muo)' [C^-1 - C^-1 Ws cap^-1 Ws' C^-1] (mus - muo)
787
+ = |mu's - mu'o|^2
788
+ - (mu's - mu'o)' C^{-1/2} Ws cap^-1 Ws' C^{-1/2} (mu's - mu'o)
789
+ = |mu's - mu'o|^2 - |caps^{-1/2} Ws' C^{-1/2} (mu's - mu'o)|^2
790
+ = |mu's - mu'o|^2 - |Vs' (mu's - mu'o)|^2
791
+
792
+ Returns
793
+ -------
794
+ out : Tensor
795
+ out[i, j] = const(wrt mu,W) * (D(component i || component j) + const(wrt mu,W))
796
+ i = other, j = self
797
+ """
798
+ n, d = mu.shape
799
+ assert C.shape == (d, d)
800
+ if out is None:
801
+ out = mu.new_empty((n, n))
802
+
803
+ Cchol = C.cholesky() # .to_dense()
804
+ # Some weird issues with solve_triangular using the batched input...
805
+ # it seems to allocate something big?
806
+ # Ccholinv = torch.linalg.solve_triangular(
807
+ # Cchol, torch.eye(len(Cchol), out=torch.empty_like(Cchol)), upper=False
808
+ # )
809
+ Ccholinv = Cchol.inverse().to_dense()
810
+ # mu_ = torch.linalg.solve_triangular(Cchol, mu.unsqueeze(2), upper=False)
811
+ mu_ = mu @ Ccholinv.T
812
+
813
+ if W is None:
814
+ # else, better to do this later
815
+ for bs in range(0, n, batch_size):
816
+ osl = slice(bs, min(bs + batch_size, n))
817
+ out[osl] = (mu_[None] - mu_[osl, None]).square_().sum(dim=2)
818
+ out *= 0.5
819
+ return out
820
+
821
+ M = W.shape[2]
822
+ assert W.shape == (n, d, M)
823
+ # U = torch.linalg.solve_triangular(Cchol, W, upper=False)
824
+ U = torch.einsum("de,ndm->nem", Ccholinv, W)
825
+
826
+ # first part of trace
827
+ UTU = U.mT.bmm(U)
828
+ # term A: it's an other-vec
829
+ trA = UTU.diagonal(dim1=-2, dim2=-1).sum(dim=1)
830
+ # assert torch.isclose(trA, U.square().sum(dim=(1, 2))).all()
831
+
832
+ # make capacitance matrix and get its Cholesky
833
+ cap = UTU
834
+ del UTU
835
+ cap.diagonal(dim1=-2, dim2=-1).add_(1.0)
836
+ capchol = psd_safe_cholesky(cap)
837
+ # V is (n, d, M)
838
+ V = torch.linalg.solve_triangular(capchol.mT, U, upper=True, left=False)
839
+
840
+ # log dets via Cholesky
841
+ logdet = capchol.diagonal(dim1=-2, dim2=-1).log().sum(dim=1).mul_(2.0)
842
+ # assert torch.isclose(logdet, cap.logdet()).all()
843
+
844
+ # trace term C: it's a matrix of Frob inner products
845
+ tmp = None
846
+ for bs in range(0, n, batch_size):
847
+ osl = slice(bs, min(bs + batch_size, n))
848
+ if tmp is not None:
849
+ tmp = tmp[: osl.stop - bs]
850
+ tmp = torch.matmul(V.mT[None], U[osl, None], out=tmp)
851
+ out[osl] = -tmp.square_().sum(dim=(2, 3))
852
+
853
+ # mahalanobis term
854
+ dmu = None
855
+ for bs in range(0, n, batch_size):
856
+ osl = slice(bs, min(bs + batch_size, n))
857
+ if dmu is not None:
858
+ dmu = dmu[: osl.stop - bs]
859
+ dmu = torch.sub(mu_[None], mu_[osl, None], out=dmu)
860
+ corr = torch.einsum("osd,sdm->osm", dmu, V)
861
+ corr = corr.square_().sum(dim=2)
862
+ out[osl] -= corr
863
+ mah = dmu.square_().sum(dim=2)
864
+ out[osl] += mah
865
+
866
+ # trace term B: it's a self-vec
867
+ trB = V.square_().sum(dim=(1, 2))
868
+
869
+ # put back together
870
+ # remember: self dim is dim 1, other dim is dim 0
871
+ out += logdet[None, :]
872
+ out -= logdet[:, None]
873
+ out += trA[:, None]
874
+ out -= trB[None, :]
875
+
876
+ out *= 0.5
877
+
878
+ return out
879
+
880
+
881
+ def real_resample(x, num, dim=0):
882
+ """torch version of a special case of scipy.signal.resample
883
+
884
+ Resamples x to have num elements on dim=dim. This is a direct
885
+ copy of the scipy code in the case where there is no window
886
+ and the data is not complex.
887
+ """
888
+ Nx = x.shape[dim]
889
+ # f = rfft(x, dim=dim)
890
+ cdtype = _cdtypes[x.dtype]
891
+
892
+ # pad output spectrum
893
+ newshape = list(x.shape)
894
+ newshape[dim] = num // 2 + 1
895
+ g = torch.zeros(newshape, dtype=cdtype, device=x.device)
896
+ N = min(num, Nx)
897
+ nyq = N // 2 + 1
898
+ sl = [slice(None)] * x.ndim
899
+ sl[dim] = slice(0, nyq)
900
+ rfft(x, dim=dim, out=g[tuple(sl)])
901
+ # g[tuple(sl)] = f[tuple(sl)]
902
+
903
+ # split/join nyquist components if present
904
+ if N % 2 == 0:
905
+ sl[dim] = slice(N // 2, N // 2 + 1)
906
+ if num < Nx: # downsampling
907
+ g[tuple(sl)] *= 2.0
908
+ elif num > Nx: # upsampling
909
+ g[tuple(sl)] *= 0.5
910
+
911
+ # inverse transform
912
+ y = irfft(g, num, dim=dim)
913
+ y *= float(num) / float(Nx)
914
+
915
+ return y
916
+
917
+
918
+ def _calc_oa_lens(s1, s2, block_size=None):
919
+ """Modified from scipy"""
920
+ import math
921
+
922
+ from scipy.special import lambertw
923
+
924
+ fallback = (s1 + s2 - 1, None, s1, s2)
925
+ if s1 == s2 or s1 == 1 or s2 == 1:
926
+ return fallback
927
+ if s2 > s1:
928
+ s1, s2 = s2, s1
929
+ swapped = True
930
+ else:
931
+ swapped = False
932
+
933
+ if s2 >= s1 / 2 and block_size is None:
934
+ return fallback
935
+ overlap = s2 - 1
936
+ opt_size = -overlap * lambertw(-1 / (2 * math.e * overlap), k=-1).real
937
+ if block_size is None:
938
+ block_size = next_fast_len(math.ceil(opt_size))
939
+
940
+ # Use conventional FFT convolve if there is only going to be one block.
941
+ if block_size >= s1:
942
+ return fallback
943
+
944
+ if not swapped:
945
+ in1_step = block_size - s2 + 1
946
+ in2_step = s2
947
+ else:
948
+ in1_step = s2
949
+ in2_step = block_size - s2 + 1
950
+
951
+ return block_size, overlap, in1_step, in2_step
952
+
953
+
954
+ def steps_and_pad(s1, in1_step, s2, in2_step, block_size, overlap):
955
+ shape_final = s1 + s2 - 1
956
+ # figure out n steps and padding
957
+ if s1 > in1_step:
958
+ nstep1 = math.ceil((s1 + 1) / in1_step)
959
+ if (block_size - overlap) * nstep1 < shape_final:
960
+ nstep1 += 1
961
+
962
+ pad1 = nstep1 * in1_step - s1
963
+ else:
964
+ nstep1 = 1
965
+ pad1 = 0
966
+
967
+ if s2 > in2_step:
968
+ nstep2 = math.ceil((s2 + 1) / in2_step)
969
+ if (block_size - overlap) * nstep2 < shape_final:
970
+ nstep2 += 1
971
+
972
+ pad2 = nstep2 * in2_step - s2
973
+ else:
974
+ nstep2 = 1
975
+ pad2 = 0
976
+ return nstep1, pad1, nstep2, pad2
977
+
978
+
979
+ def depthwise_oaconv1d(input, weight, f2=None, padding=0):
980
+ """Depthwise correlation (F.conv1d with groups=in_chans) with overlap-add"""
981
+ # conv on last axis
982
+ # assert input.ndim == weight.ndim == 2
983
+ n1 = input.shape[0]
984
+ n2 = weight.shape[0]
985
+ assert n1 == n2
986
+ s1 = input.shape[1]
987
+ s2 = weight.shape[1]
988
+ assert s1 >= s2
989
+
990
+ # shape_full = s1 + s2 - 1
991
+ block_size, overlap, in1_step, in2_step = _calc_oa_lens(s1, s2)
992
+
993
+ # overlap=None is a signal that no useful blocks for OA can
994
+ # be found, do a vanilla FFT correlation
995
+ if overlap is None:
996
+ f1 = torch.fft.rfft(input, n=s1)
997
+ f2 = torch.fft.rfft(torch.flip(weight, (-1,)), n=s1)
998
+ f1.mul_(
999
+ f2[
1000
+ :,
1001
+ None:,
1002
+ ]
1003
+ )
1004
+ res = torch.fft.irfft(f1, n=s1)
1005
+ valid_len = s1 - s2 + 1
1006
+ valid_start = s2 - 1
1007
+ assert valid_start >= padding
1008
+ res = res[:, valid_start - padding : valid_start + valid_len + padding]
1009
+ return res
1010
+
1011
+ nstep1, pad1, nstep2, pad2 = steps_and_pad(
1012
+ s1, in1_step, s2, in2_step, block_size, overlap
1013
+ )
1014
+
1015
+ if pad1 > 0:
1016
+ input = F.pad(input, (0, pad1))
1017
+ input = input.reshape(n1, nstep1, in1_step)
1018
+
1019
+ # freq domain correlation
1020
+ f1 = torch.fft.rfft(input, n=block_size)
1021
+ if f2 is None:
1022
+ # flip direction of templates to perform cross-correlation
1023
+ f2 = torch.fft.rfft(torch.flip(weight, (-1,)), n=block_size)
1024
+
1025
+ f1.mul_(f2[:, None, :])
1026
+ res = torch.fft.irfft(f1, n=block_size)
1027
+
1028
+ # overlap add part with torch
1029
+ fold_input = res.reshape(n1, nstep1, block_size).permute(0, 2, 1)
1030
+ fold_out_len = nstep1 * in1_step + overlap
1031
+ fold_res = F.fold(
1032
+ fold_input,
1033
+ output_size=(1, fold_out_len),
1034
+ kernel_size=(1, block_size),
1035
+ stride=(1, in1_step),
1036
+ )
1037
+ assert fold_res.shape == (n1, 1, 1, fold_out_len)
1038
+
1039
+ oa = fold_res.reshape(n1, fold_out_len)
1040
+ # this is the full convolution
1041
+ # oa = oa[:, : shape_final]
1042
+ # extract correct padding
1043
+ valid_len = s1 - s2 + 1
1044
+ valid_start = s2 - 1
1045
+ assert valid_start >= padding
1046
+ oa = oa[:, valid_start - padding : valid_start + valid_len + padding]
1047
+
1048
+ return oa
1049
+
1050
+
1051
+ def single_inv_oaconv1d(input, f2, s2, block_size, padding=0, norm="backward"):
1052
+ """Depthwise correlation (F.conv1d with groups=in_chans) with overlap-add"""
1053
+ # conv on last axis
1054
+ # assert input.ndim == weight.ndim == 2
1055
+ n1, s1 = input.shape
1056
+ assert s1 >= s2
1057
+ valid_len = s1 - s2 + 1
1058
+ valid_start = s2 - 1
1059
+
1060
+ # shape_full = s1 + s2 - 1
1061
+ block_size, overlap, in1_step, in2_step = _calc_oa_lens(
1062
+ s1, s2, block_size=block_size
1063
+ )
1064
+ assert overlap is not None
1065
+ # case is hard to support...
1066
+
1067
+ nstep1, pad1, nstep2, pad2 = steps_and_pad(
1068
+ s1, in1_step, s2, in2_step, block_size, overlap
1069
+ )
1070
+
1071
+ if pad1 > 0:
1072
+ input = F.pad(input, (0, pad1))
1073
+ input = input.reshape(n1, nstep1, in1_step)
1074
+
1075
+ # freq domain correlation
1076
+ f1 = torch.fft.rfft(input, n=block_size, norm=norm)
1077
+ if f1.shape[2] > f2.shape[0]:
1078
+ f2 = F.pad(f2, (0, f1.shape[2] - f2.shape[0]))
1079
+
1080
+ f1.mul_(f2)
1081
+ res = torch.fft.irfft(f1, n=block_size, norm=norm)
1082
+
1083
+ # overlap add part with torch
1084
+ fold_input = res.reshape(n1, nstep1, block_size).permute(0, 2, 1)
1085
+ fold_out_len = nstep1 * in1_step + overlap
1086
+ fold_res = F.fold(
1087
+ fold_input,
1088
+ output_size=(1, fold_out_len),
1089
+ kernel_size=(1, block_size),
1090
+ stride=(1, in1_step),
1091
+ )
1092
+ assert fold_res.shape == (n1, 1, 1, fold_out_len)
1093
+
1094
+ oa = fold_res.reshape(n1, fold_out_len)
1095
+ # this is the full convolution
1096
+ # oa = oa[:, : shape_final]
1097
+ # extract correct padding
1098
+ assert valid_start >= padding
1099
+ oa = oa[:, valid_start - padding : valid_start + valid_len + padding]
1100
+
1101
+ return oa
1102
+
1103
+
1104
+ def isin_sorted(x, y):
1105
+ """Like torch.isin(x, y), but faster by assuming both sorted."""
1106
+ if not y.numel():
1107
+ return torch.zeros(x.shape, dtype=torch.bool, device=x.device)
1108
+ ix = torch.searchsorted(y, x, side="right") - 1
1109
+ return x == y[ix]
1110
+
1111
+
1112
+ def average_by_label(x, labels, channels, n_channels, weights=None):
1113
+ """weights should sum to 1 already in each group."""
1114
+ n = x.shape[0]
1115
+ assert x.ndim == 3
1116
+ assert labels.shape == (n,)
1117
+ if weights is None:
1118
+ weights = x.new_ones(n)
1119
+ unique_labels = labels.unique()
1120
+ k = unique_labels.amax() + 1
1121
+ assert labels.shape == weights.shape
1122
+
1123
+ out = x.new_zeros((k + 1, x.shape[1], n_channels + 1))
1124
+ counts = x.new_zeros((k + 1, n_channels + 1))
1125
+ f_arange = torch.arange(x.shape[1]).to(x.device)
1126
+ for u in unique_labels:
1127
+ (in_u,) = (labels == u).nonzero(as_tuple=True)
1128
+ wu = weights[in_u]
1129
+ cu = channels[in_u]
1130
+ wu = wu[:, None].broadcast_to(cu.shape).contiguous()
1131
+ counts[u].scatter_add_(dim=0, index=cu.view(-1), src=wu.view(-1))
1132
+ wu.div_(counts[u][cu])
1133
+
1134
+ wxu = x[in_u].mul_(wu[:, None])
1135
+ torch_add_at_(
1136
+ out[u, None],
1137
+ (
1138
+ torch.zeros_like(in_u)[:, None, None],
1139
+ f_arange[None, :, None],
1140
+ cu[:, None],
1141
+ ),
1142
+ wxu,
1143
+ )
1144
+
1145
+ return out[:k, :, :n_channels], counts[:k, :n_channels]
1146
+
1147
+
1148
+ # -- channel reindexing
1149
+
1150
+
1151
+ def get_relative_index(source_channel_index, target_channel_index):
1152
+ """Pre-compute a channel reindexing helper structure.
1153
+
1154
+ Inputs have shapes:
1155
+ source_channel_index.shape == (n_chans, n_source_chans)
1156
+ target_channel_index.shape == (n_chans, n_target_chans)
1157
+
1158
+ This returns an array (relative_index) of shape (n_chans, n_target_chans)
1159
+ which knows how to translate between the source and target indices:
1160
+
1161
+ relative_index[c, j] = index of target_channel_index[c, j] in source_channel_index[c]
1162
+ if present, else n_source_chans (i.e., an invalid index)
1163
+ (or, n_source chans if target_channel_index[c, j] is n_chans)
1164
+
1165
+ See below:
1166
+ reindex(max_channels, source_waveforms, relative_index)
1167
+ """
1168
+ n_chans, n_source_chans = source_channel_index.shape
1169
+ n_chans_, n_target_chans = target_channel_index.shape
1170
+ assert n_chans == n_chans_
1171
+ relative_index = torch.full_like(target_channel_index, n_source_chans)
1172
+ for c in range(n_chans):
1173
+ row = source_channel_index[c]
1174
+ for j in range(n_target_chans):
1175
+ targ = target_channel_index[c, j]
1176
+ if targ == n_chans:
1177
+ continue
1178
+ mask = row == targ
1179
+ if not mask.any():
1180
+ continue
1181
+ (ixs,) = mask.nonzero(as_tuple=True)
1182
+ assert ixs.numel() == 1
1183
+ relative_index[c, j] = ixs[0]
1184
+ return relative_index
1185
+
1186
+
1187
+ def reindex(
1188
+ max_channels,
1189
+ source_waveforms,
1190
+ relative_index,
1191
+ already_padded=False,
1192
+ pad_value=torch.nan,
1193
+ ):
1194
+ """"""
1195
+ rel_ix = relative_index[max_channels].unsqueeze(1)
1196
+ if not already_padded:
1197
+ source_waveforms = F.pad(source_waveforms, (0, 1), value=pad_value)
1198
+ return torch.take_along_dim(source_waveforms, rel_ix, dim=2)