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,791 @@
|
|
|
1
|
+
"""Template data for matching with on-the-fly drift handling
|
|
2
|
+
|
|
3
|
+
Features
|
|
4
|
+
- Shared or individual template SVD compression
|
|
5
|
+
- Amplitude scaling
|
|
6
|
+
- Temporal upsampling
|
|
7
|
+
- No YASS-style adaptive temporal compression, which adds overhead
|
|
8
|
+
- No mutually-refractory group support yet (TODO if needed)
|
|
9
|
+
- Pick your favorite interpolation strategy for drift handling. If it's not
|
|
10
|
+
thin plate splines, then maybe you haven't heard of 'em?
|
|
11
|
+
|
|
12
|
+
Strategy
|
|
13
|
+
- Pre-compute the convolutions of all pairs of temporal basis elements
|
|
14
|
+
across all pairs of units
|
|
15
|
+
- This is shared if the template basis is shared
|
|
16
|
+
- Read off the pairwise convolutions by multiplication with the spatial basis
|
|
17
|
+
as needed
|
|
18
|
+
- This can be precomputed if there's no drift
|
|
19
|
+
|
|
20
|
+
Coarse-to-fine approach
|
|
21
|
+
- Objective is amplitude-scaled but not temporally upsampled
|
|
22
|
+
- Separate notions of "coarse templates" (not upsampled) and "fine templates"
|
|
23
|
+
(upsampled)
|
|
24
|
+
- For pairwise convolutions, we need the convolution of coarse library with
|
|
25
|
+
the fine library
|
|
26
|
+
- At matching time, we propose peaks coarsely and then refine them
|
|
27
|
+
- TODO how to do the fine step most efficiently? Can probably do the inner
|
|
28
|
+
product more efficiently than I did before...
|
|
29
|
+
|
|
30
|
+
TODO: MatchingPeaks may need to be tweaked or abstracted?
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
from pathlib import Path
|
|
34
|
+
from typing import Literal, Self
|
|
35
|
+
|
|
36
|
+
import numpy as np
|
|
37
|
+
import torch
|
|
38
|
+
import torch.nn.functional as F
|
|
39
|
+
from spikeinterface.core import BaseRecording
|
|
40
|
+
from torch import Tensor
|
|
41
|
+
|
|
42
|
+
from ...templates import TemplateData
|
|
43
|
+
from ...templates.template_util import shared_basis_compress_templates
|
|
44
|
+
from ...util.internal_config import ComputationConfig, MatchingConfig
|
|
45
|
+
from ...util.interpolation_util import (
|
|
46
|
+
FullProbeInterpolator,
|
|
47
|
+
InterpolationParams,
|
|
48
|
+
bake_interpolation_1d,
|
|
49
|
+
default_interpolation_params,
|
|
50
|
+
)
|
|
51
|
+
from ...util.logging_util import get_logger
|
|
52
|
+
from ...util.job_util import ensure_computation_config
|
|
53
|
+
from ...util.py_util import databag
|
|
54
|
+
from ...util.waveform_util import upsample_singlechan_torch
|
|
55
|
+
from .matching_base import (
|
|
56
|
+
ChunkTemplateData,
|
|
57
|
+
MatchingPeaks,
|
|
58
|
+
MatchingTemplates,
|
|
59
|
+
subtract_precomputed_pconv,
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
logger = get_logger(__name__)
|
|
64
|
+
|
|
65
|
+
default_upsampling_params = InterpolationParams(sigma=1.0)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class DriftyMatchingTemplates(MatchingTemplates):
|
|
69
|
+
template_type = "drifty"
|
|
70
|
+
|
|
71
|
+
def __init__(
|
|
72
|
+
self,
|
|
73
|
+
*,
|
|
74
|
+
temporal_comps: Tensor,
|
|
75
|
+
spatial_sing: Tensor,
|
|
76
|
+
motion_est,
|
|
77
|
+
geom: Tensor,
|
|
78
|
+
trough_offset_samples: int,
|
|
79
|
+
unit_ids: Tensor | None = None,
|
|
80
|
+
rgeom: Tensor | None = None,
|
|
81
|
+
whitener: Tensor | None = None,
|
|
82
|
+
up_factor: int = 1,
|
|
83
|
+
up_method: Literal["interpolation", "keys3", "keys4", "direct"] = "keys4",
|
|
84
|
+
interp_up_radius: int = 8,
|
|
85
|
+
up_interp_params=default_upsampling_params,
|
|
86
|
+
drift_interp_params: InterpolationParams = default_interpolation_params,
|
|
87
|
+
interp_neighborhood_radius: float = 150.0,
|
|
88
|
+
refractory_radius_frames: int = 10,
|
|
89
|
+
device: torch.device,
|
|
90
|
+
):
|
|
91
|
+
"""
|
|
92
|
+
Arguments
|
|
93
|
+
---------
|
|
94
|
+
up_method:
|
|
95
|
+
How to pick the upsampling index? If we have a coarse match at time t,
|
|
96
|
+
"""
|
|
97
|
+
super().__init__()
|
|
98
|
+
self.up_factor = up_factor
|
|
99
|
+
self.upsampling = up_factor > 1
|
|
100
|
+
self.up_method = up_method
|
|
101
|
+
self.interpolating = motion_est is not None
|
|
102
|
+
self.whitening = whitener is not None
|
|
103
|
+
|
|
104
|
+
# validation / shape documentation
|
|
105
|
+
assert temporal_comps.ndim == 2
|
|
106
|
+
assert self.interpolating == (motion_est is not None)
|
|
107
|
+
self.n_units, rank, n_channels = spatial_sing.shape
|
|
108
|
+
assert rank == temporal_comps.shape[0]
|
|
109
|
+
self.spike_length_samples = temporal_comps.shape[1]
|
|
110
|
+
assert trough_offset_samples <= self.spike_length_samples
|
|
111
|
+
|
|
112
|
+
if self.interpolating:
|
|
113
|
+
assert rgeom is not None
|
|
114
|
+
logger.dartsortdebug("Drifty matching will interpolate templates.")
|
|
115
|
+
self.erp = FullProbeInterpolator(
|
|
116
|
+
geom=geom,
|
|
117
|
+
rgeom=rgeom,
|
|
118
|
+
neighborhood_radius=interp_neighborhood_radius,
|
|
119
|
+
motion_est=motion_est,
|
|
120
|
+
params=drift_interp_params,
|
|
121
|
+
)
|
|
122
|
+
else:
|
|
123
|
+
logger.dartsortdebug("No interpolation in matching.")
|
|
124
|
+
self.erp = None
|
|
125
|
+
|
|
126
|
+
if self.upsampling and up_method != "direct":
|
|
127
|
+
self.up_data = get_interp_upsampling_data(
|
|
128
|
+
up_factor=up_factor,
|
|
129
|
+
up_method=up_method,
|
|
130
|
+
interp_up_radius=interp_up_radius,
|
|
131
|
+
interp_params=up_interp_params,
|
|
132
|
+
device=device,
|
|
133
|
+
)
|
|
134
|
+
else:
|
|
135
|
+
self.up_data = None
|
|
136
|
+
|
|
137
|
+
up_temporal_comps = upsample_singlechan_torch(
|
|
138
|
+
temporal_comps, temporal_jitter=up_factor
|
|
139
|
+
)
|
|
140
|
+
temporal_pconv = shared_temporal_pconv(temporal_comps, up_temporal_comps)
|
|
141
|
+
|
|
142
|
+
assert temporal_comps.shape == (rank, self.spike_length_samples)
|
|
143
|
+
self.register_buffer("temporal_comps", temporal_comps.contiguous())
|
|
144
|
+
assert up_temporal_comps.shape == (rank, up_factor, self.spike_length_samples)
|
|
145
|
+
self.register_buffer_or_none(
|
|
146
|
+
"up_temporal_comps", up_temporal_comps.contiguous()
|
|
147
|
+
)
|
|
148
|
+
if up_temporal_comps is not None:
|
|
149
|
+
up_major_temporal_comps = up_temporal_comps.permute(1, 0, 2).contiguous()
|
|
150
|
+
else:
|
|
151
|
+
up_major_temporal_comps = None
|
|
152
|
+
self.register_buffer_or_none("up_major_temporal_comps", up_major_temporal_comps)
|
|
153
|
+
self.register_buffer("spatial_sing", spatial_sing)
|
|
154
|
+
self.register_buffer("temporal_pconv", temporal_pconv)
|
|
155
|
+
if unit_ids is None:
|
|
156
|
+
unit_ids = torch.arange(self.n_units, device=device)
|
|
157
|
+
self.register_buffer("unit_ids", unit_ids)
|
|
158
|
+
|
|
159
|
+
if self.whitening:
|
|
160
|
+
assert torch.is_tensor(whitener)
|
|
161
|
+
whitener = whitener.to(spatial_sing.device)
|
|
162
|
+
conv_spatial_sing = torch.einsum(
|
|
163
|
+
"nqc,dc->nqd", spatial_sing, whitener.T @ whitener
|
|
164
|
+
)
|
|
165
|
+
pconv_spatial_sing = torch.einsum("nqc,dc->nqd", spatial_sing, whitener)
|
|
166
|
+
else:
|
|
167
|
+
pconv_spatial_sing = conv_spatial_sing = None
|
|
168
|
+
self.register_buffer_or_none("whitener", whitener)
|
|
169
|
+
self.register_buffer_or_none("pconv_spatial_sing", pconv_spatial_sing)
|
|
170
|
+
self.register_buffer_or_none("conv_spatial_sing", conv_spatial_sing)
|
|
171
|
+
|
|
172
|
+
if self.whitening and not self.interpolating:
|
|
173
|
+
pconv = full_shared_pconv(self.b.temporal_pconv, self.b.pconv_spatial_sing)
|
|
174
|
+
if not self.interpolating:
|
|
175
|
+
# can precompute the full pconv in this case.
|
|
176
|
+
pconv = full_shared_pconv(self.b.temporal_pconv, self.b.spatial_sing)
|
|
177
|
+
else:
|
|
178
|
+
pconv = None
|
|
179
|
+
self.register_buffer_or_none("pconv", pconv)
|
|
180
|
+
|
|
181
|
+
# indexing helpers
|
|
182
|
+
t = self.spike_length_samples
|
|
183
|
+
rr = refractory_radius_frames
|
|
184
|
+
self.register_buffer("refrac_ix", torch.arange(-rr, rr + 1, device=device))
|
|
185
|
+
self.register_buffer("time_ix", torch.arange(t, device=device))
|
|
186
|
+
self.register_buffer("chan_ix", torch.arange(n_channels, device=device))
|
|
187
|
+
self.register_buffer("rank_ix", torch.arange(rank, device=device))
|
|
188
|
+
self.register_buffer("conv_lags", torch.arange(-t + 1, t, device=device))
|
|
189
|
+
offset = torch.asarray(trough_offset_samples, device=device)
|
|
190
|
+
self.register_buffer("trough_offset_samples", offset)
|
|
191
|
+
|
|
192
|
+
@classmethod
|
|
193
|
+
def _from_config(
|
|
194
|
+
cls,
|
|
195
|
+
save_folder: Path,
|
|
196
|
+
recording: BaseRecording,
|
|
197
|
+
template_data: TemplateData,
|
|
198
|
+
matching_cfg: MatchingConfig,
|
|
199
|
+
computation_cfg: ComputationConfig | None = None,
|
|
200
|
+
motion_est=None,
|
|
201
|
+
whitener: Tensor | None = None,
|
|
202
|
+
overwrite: bool = False,
|
|
203
|
+
dtype=torch.float,
|
|
204
|
+
) -> Self:
|
|
205
|
+
del overwrite, save_folder # I don't save anything.
|
|
206
|
+
|
|
207
|
+
computation_cfg = ensure_computation_config(computation_cfg)
|
|
208
|
+
device = computation_cfg.actual_device()
|
|
209
|
+
|
|
210
|
+
unit_ids = torch.asarray(template_data.unit_ids, device=device)
|
|
211
|
+
geom = torch.asarray(recording.get_channel_locations())
|
|
212
|
+
geom = geom.to(device=device, dtype=dtype)
|
|
213
|
+
rgeom = torch.asarray(template_data.registered_geom)
|
|
214
|
+
rgeom = rgeom.to(device=device, dtype=dtype)
|
|
215
|
+
|
|
216
|
+
shared_basis_temps = shared_basis_compress_templates(
|
|
217
|
+
template_data,
|
|
218
|
+
min_channel_amplitude=matching_cfg.template_min_channel_amplitude,
|
|
219
|
+
rank=matching_cfg.template_svd_compression_rank,
|
|
220
|
+
computation_cfg=computation_cfg,
|
|
221
|
+
)
|
|
222
|
+
temporal_comps = torch.asarray(shared_basis_temps.temporal_components)
|
|
223
|
+
spatial_sing = torch.asarray(shared_basis_temps.spatial_singular)
|
|
224
|
+
|
|
225
|
+
return cls(
|
|
226
|
+
temporal_comps=temporal_comps.to(device=device, dtype=dtype),
|
|
227
|
+
spatial_sing=spatial_sing.to(device=device, dtype=dtype),
|
|
228
|
+
motion_est=motion_est,
|
|
229
|
+
geom=geom,
|
|
230
|
+
trough_offset_samples=template_data.trough_offset_samples,
|
|
231
|
+
unit_ids=unit_ids,
|
|
232
|
+
rgeom=rgeom,
|
|
233
|
+
up_factor=matching_cfg.template_temporal_upsampling_factor,
|
|
234
|
+
up_method=matching_cfg.up_method,
|
|
235
|
+
interp_up_radius=matching_cfg.upsampling_radius,
|
|
236
|
+
drift_interp_params=matching_cfg.drift_interp_params,
|
|
237
|
+
interp_neighborhood_radius=matching_cfg.drift_interp_neighborhood_radius,
|
|
238
|
+
refractory_radius_frames=matching_cfg.refractory_radius_frames,
|
|
239
|
+
whitener=whitener,
|
|
240
|
+
device=device,
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
def spatial_at_time(self, t_s: float) -> tuple[Tensor, ...]:
|
|
244
|
+
if self.interpolating:
|
|
245
|
+
assert self.erp is not None
|
|
246
|
+
spatial_sing = self.erp.interp_at_time(
|
|
247
|
+
t_s=t_s, waveforms=self.b.spatial_sing
|
|
248
|
+
)
|
|
249
|
+
else:
|
|
250
|
+
spatial_sing = self.b.spatial_sing
|
|
251
|
+
if self.whitening and self.interpolating:
|
|
252
|
+
assert self.erp is not None
|
|
253
|
+
# TODO not this
|
|
254
|
+
conv_spatial_sing = self.erp.interp_at_time(
|
|
255
|
+
t_s=t_s, waveforms=self.b.conv_spatial_sing
|
|
256
|
+
)
|
|
257
|
+
pconv_spatial_sing = self.erp.interp_at_time(
|
|
258
|
+
t_s=t_s, waveforms=self.b.pconv_spatial_sing
|
|
259
|
+
)
|
|
260
|
+
elif self.whitening:
|
|
261
|
+
conv_spatial_sing = self.b.conv_spatial_sing
|
|
262
|
+
pconv_spatial_sing = self.b.pconv_spatial_sing
|
|
263
|
+
else:
|
|
264
|
+
conv_spatial_sing = pconv_spatial_sing = spatial_sing
|
|
265
|
+
|
|
266
|
+
# normsq for channel selection from original
|
|
267
|
+
# normsq for conv is whitened
|
|
268
|
+
normsq_by_chan = spatial_sing.square().sum(dim=1)
|
|
269
|
+
main_channels = normsq_by_chan.argmax(dim=1)
|
|
270
|
+
if self.whitening:
|
|
271
|
+
normsq = pconv_spatial_sing.square().sum(dim=(1, 2))
|
|
272
|
+
else:
|
|
273
|
+
normsq = normsq_by_chan.sum(dim=1)
|
|
274
|
+
|
|
275
|
+
# padded spatial sing is used for clean wfs only
|
|
276
|
+
padded_spatial_sing = F.pad(spatial_sing, (0, 1))
|
|
277
|
+
|
|
278
|
+
if self.b.pconv is None:
|
|
279
|
+
pconv = full_shared_pconv(self.b.temporal_pconv, pconv_spatial_sing)
|
|
280
|
+
else:
|
|
281
|
+
pconv = self.b.pconv
|
|
282
|
+
|
|
283
|
+
return conv_spatial_sing, normsq, main_channels, padded_spatial_sing, pconv
|
|
284
|
+
|
|
285
|
+
def data_at_time(
|
|
286
|
+
self,
|
|
287
|
+
t_s: float,
|
|
288
|
+
scaling: bool,
|
|
289
|
+
inv_lambda: float,
|
|
290
|
+
scale_min: float,
|
|
291
|
+
scale_max: float,
|
|
292
|
+
) -> ChunkTemplateData:
|
|
293
|
+
spatial_sing, normsq, main_channels, padded_spatial_sing, pconv = (
|
|
294
|
+
self.spatial_at_time(t_s=t_s)
|
|
295
|
+
)
|
|
296
|
+
return DriftyChunkTemplateData(
|
|
297
|
+
spike_length_samples=self.spike_length_samples,
|
|
298
|
+
unit_ids=self.b.unit_ids,
|
|
299
|
+
main_channels=main_channels,
|
|
300
|
+
obj_normsq=normsq,
|
|
301
|
+
obj_n_templates=self.n_units,
|
|
302
|
+
scaling=scaling,
|
|
303
|
+
upsampling=self.upsampling,
|
|
304
|
+
needs_fine_pass=self.upsampling,
|
|
305
|
+
inv_lambda=torch.asarray(inv_lambda).to(normsq, non_blocking=True),
|
|
306
|
+
scale_min=torch.asarray(scale_min).to(normsq, non_blocking=True),
|
|
307
|
+
scale_max=torch.asarray(scale_max).to(normsq, non_blocking=True),
|
|
308
|
+
temporal_comps=self.b.temporal_comps,
|
|
309
|
+
up_major_temporal_comps=self.b.up_major_temporal_comps,
|
|
310
|
+
spatial_sing=spatial_sing,
|
|
311
|
+
pconv=pconv,
|
|
312
|
+
time_ix=self.b.time_ix,
|
|
313
|
+
chan_ix=self.b.chan_ix,
|
|
314
|
+
rank_ix=self.b.rank_ix,
|
|
315
|
+
conv_lags=self.b.conv_lags,
|
|
316
|
+
refrac_ix=self.b.refrac_ix,
|
|
317
|
+
padded_spatial_sing=padded_spatial_sing,
|
|
318
|
+
up_data=self.up_data,
|
|
319
|
+
)
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
@databag
|
|
323
|
+
class DriftyChunkTemplateData(ChunkTemplateData):
|
|
324
|
+
spike_length_samples: int
|
|
325
|
+
# for the full templates
|
|
326
|
+
unit_ids: Tensor
|
|
327
|
+
main_channels: Tensor
|
|
328
|
+
# for obj templates
|
|
329
|
+
obj_normsq: Tensor
|
|
330
|
+
obj_n_templates: int
|
|
331
|
+
upsampling: bool
|
|
332
|
+
scaling: bool
|
|
333
|
+
needs_fine_pass: bool
|
|
334
|
+
inv_lambda: Tensor
|
|
335
|
+
scale_min: Tensor
|
|
336
|
+
scale_max: Tensor
|
|
337
|
+
|
|
338
|
+
temporal_comps: Tensor
|
|
339
|
+
up_major_temporal_comps: Tensor
|
|
340
|
+
spatial_sing: Tensor
|
|
341
|
+
padded_spatial_sing: Tensor
|
|
342
|
+
pconv: Tensor
|
|
343
|
+
|
|
344
|
+
time_ix: Tensor
|
|
345
|
+
chan_ix: Tensor
|
|
346
|
+
rank_ix: Tensor
|
|
347
|
+
refrac_ix: Tensor
|
|
348
|
+
conv_lags: Tensor
|
|
349
|
+
up_data: "UpsamplingData | None"
|
|
350
|
+
|
|
351
|
+
# template grouping is not implemented, so this is always false.
|
|
352
|
+
coarse_objective: bool = False
|
|
353
|
+
|
|
354
|
+
def convolve(self, traces: Tensor, padding: int = 0, out: Tensor | None = None):
|
|
355
|
+
out_len = traces.shape[1] + 2 * padding - self.spike_length_samples + 1
|
|
356
|
+
if out is not None:
|
|
357
|
+
assert out.shape == (self.obj_n_templates, out_len)
|
|
358
|
+
return convolve_lowrank_shared(
|
|
359
|
+
traces=traces,
|
|
360
|
+
spatial_singular=self.spatial_sing,
|
|
361
|
+
temporal_components=self.temporal_comps,
|
|
362
|
+
padding=padding,
|
|
363
|
+
out=out,
|
|
364
|
+
)
|
|
365
|
+
|
|
366
|
+
def subtract(self, traces: Tensor, peaks: "MatchingPeaks", sign: int = -1):
|
|
367
|
+
if not peaks.n_spikes:
|
|
368
|
+
return
|
|
369
|
+
assert peaks.times is not None
|
|
370
|
+
assert peaks.template_inds is not None
|
|
371
|
+
if peaks.up_inds is not None:
|
|
372
|
+
tempc = self.up_major_temporal_comps[peaks.up_inds]
|
|
373
|
+
else:
|
|
374
|
+
tempc = self.temporal_comps
|
|
375
|
+
tempc = tempc[None].broadcast_to(peaks.n_spikes, *tempc.shape)
|
|
376
|
+
if peaks.scalings is None:
|
|
377
|
+
batch_templates = torch.bmm(
|
|
378
|
+
tempc.mT, self.padded_spatial_sing[peaks.template_inds]
|
|
379
|
+
)
|
|
380
|
+
else:
|
|
381
|
+
tempc = tempc * peaks.scalings[:, None, None]
|
|
382
|
+
batch_templates = torch.bmm(
|
|
383
|
+
tempc.mT, self.padded_spatial_sing[peaks.template_inds]
|
|
384
|
+
)
|
|
385
|
+
n, t, c = batch_templates.shape
|
|
386
|
+
time_ix = peaks.times[:, None] + self.time_ix[None, :]
|
|
387
|
+
assert traces.shape[1] in (c, c + 1)
|
|
388
|
+
assert time_ix.shape == (n, t)
|
|
389
|
+
batch_templates = batch_templates.view(n * t, c)
|
|
390
|
+
time_ix = time_ix.view(n * t)[:, None].broadcast_to(batch_templates.shape)
|
|
391
|
+
if sign == -1:
|
|
392
|
+
traces.scatter_add_(dim=0, src=batch_templates._neg_view(), index=time_ix)
|
|
393
|
+
elif sign == 1:
|
|
394
|
+
traces.scatter_add_(dim=0, src=batch_templates, index=time_ix)
|
|
395
|
+
else:
|
|
396
|
+
assert False
|
|
397
|
+
|
|
398
|
+
def subtract_conv(
|
|
399
|
+
self, conv: Tensor, peaks: "MatchingPeaks", padding=0, batch_size=256, sign=-1
|
|
400
|
+
):
|
|
401
|
+
subtract_precomputed_pconv(
|
|
402
|
+
conv=conv,
|
|
403
|
+
pconv=self.pconv,
|
|
404
|
+
peaks=peaks,
|
|
405
|
+
padding=padding,
|
|
406
|
+
conv_lags=self.conv_lags,
|
|
407
|
+
sign=sign,
|
|
408
|
+
batch_size=batch_size,
|
|
409
|
+
)
|
|
410
|
+
|
|
411
|
+
def get_clean_waveforms(
|
|
412
|
+
self,
|
|
413
|
+
peaks: "MatchingPeaks",
|
|
414
|
+
channels: Tensor,
|
|
415
|
+
channel_index: Tensor,
|
|
416
|
+
add_into: Tensor | None = None,
|
|
417
|
+
):
|
|
418
|
+
if not peaks.n_spikes:
|
|
419
|
+
return add_into
|
|
420
|
+
assert peaks.times is not None
|
|
421
|
+
assert peaks.template_inds is not None
|
|
422
|
+
spatial = self.padded_spatial_sing[
|
|
423
|
+
peaks.template_inds[:, None, None],
|
|
424
|
+
self.rank_ix[None, :, None],
|
|
425
|
+
channel_index[channels, None, :],
|
|
426
|
+
]
|
|
427
|
+
if peaks.scalings is not None:
|
|
428
|
+
spatial.mul_(peaks.scalings[:, None, None])
|
|
429
|
+
if peaks.up_inds is not None:
|
|
430
|
+
tempc = self.up_major_temporal_comps[peaks.up_inds]
|
|
431
|
+
else:
|
|
432
|
+
tempc = self.temporal_comps
|
|
433
|
+
tempc = tempc[None].broadcast_to(peaks.n_spikes, *tempc.shape)
|
|
434
|
+
if add_into is None:
|
|
435
|
+
return tempc.mT.bmm(spatial)
|
|
436
|
+
else:
|
|
437
|
+
return add_into.baddbmm_(tempc.mT, spatial)
|
|
438
|
+
|
|
439
|
+
def _enforce_refractory(self, mask, peaks, offset=0, value=-torch.inf):
|
|
440
|
+
if not peaks.n_spikes:
|
|
441
|
+
return
|
|
442
|
+
assert peaks.times is not None
|
|
443
|
+
assert peaks.obj_template_inds is not None
|
|
444
|
+
time_ix = peaks.times[:, None] + (self.refrac_ix[None, :] + offset)
|
|
445
|
+
row_ix = peaks.obj_template_inds[:, None]
|
|
446
|
+
mask[row_ix, time_ix] = value
|
|
447
|
+
|
|
448
|
+
def fine_match(
|
|
449
|
+
self,
|
|
450
|
+
*,
|
|
451
|
+
peaks: "MatchingPeaks",
|
|
452
|
+
residual: Tensor,
|
|
453
|
+
conv: Tensor,
|
|
454
|
+
padding: int = 0,
|
|
455
|
+
) -> "MatchingPeaks":
|
|
456
|
+
if not self.needs_fine_pass:
|
|
457
|
+
return peaks
|
|
458
|
+
if not peaks.n_spikes:
|
|
459
|
+
return peaks
|
|
460
|
+
assert peaks.times is not None
|
|
461
|
+
if self.up_data is not None:
|
|
462
|
+
scalings, up_inds, up_best, objs = _upsampling_fine_match(
|
|
463
|
+
conv=conv,
|
|
464
|
+
template_inds=peaks.template_inds,
|
|
465
|
+
times=peaks.times,
|
|
466
|
+
padding=padding,
|
|
467
|
+
normsq=self.obj_normsq,
|
|
468
|
+
scaling=self.scaling,
|
|
469
|
+
inv_lambda=self.inv_lambda,
|
|
470
|
+
scale_min=self.scale_min,
|
|
471
|
+
scale_max=self.scale_max,
|
|
472
|
+
up_zpad=self.up_data.zpad,
|
|
473
|
+
objective_window=self.up_data.objective_window,
|
|
474
|
+
up_ix=self.up_data.up_ix,
|
|
475
|
+
interpolator=self.up_data.interpolator,
|
|
476
|
+
)
|
|
477
|
+
else:
|
|
478
|
+
del residual
|
|
479
|
+
raise NotImplementedError
|
|
480
|
+
# scalings, up_inds, time_shifts, objs = _direct_fine_match()
|
|
481
|
+
assert self.scaling == (scalings is not None)
|
|
482
|
+
subtract_shift = self.up_data.up_time_shift[up_best]
|
|
483
|
+
extract_shift = self.up_data.up_extract_shift[up_best]
|
|
484
|
+
return MatchingPeaks(
|
|
485
|
+
times=peaks.times + subtract_shift,
|
|
486
|
+
obj_template_inds=peaks.obj_template_inds,
|
|
487
|
+
template_inds=peaks.template_inds,
|
|
488
|
+
up_inds=up_inds,
|
|
489
|
+
scalings=scalings,
|
|
490
|
+
scores=objs,
|
|
491
|
+
time_shifts=extract_shift,
|
|
492
|
+
)
|
|
493
|
+
|
|
494
|
+
def reconstruct_up_templates(self):
|
|
495
|
+
return torch.einsum(
|
|
496
|
+
"nrc,urt->nutc",
|
|
497
|
+
self.padded_spatial_sing[..., :-1].cpu(),
|
|
498
|
+
self.up_major_temporal_comps.cpu(),
|
|
499
|
+
)
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
# -- helpers
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
def get_interp_upsampling_indices(
|
|
506
|
+
*, up_factor: int, up_radius: int, device: torch.device
|
|
507
|
+
):
|
|
508
|
+
"""Time domain and indexing helper for upsampled objective methods (not direct)."""
|
|
509
|
+
assert not (up_factor % 2)
|
|
510
|
+
|
|
511
|
+
# window around the coarse peak in the objective which will be the input to upsampling
|
|
512
|
+
objective_window = torch.arange(-up_radius, up_radius + 1, device=device)
|
|
513
|
+
|
|
514
|
+
# above considered as the domain of a function
|
|
515
|
+
objective_tt = objective_window.to(torch.float)
|
|
516
|
+
|
|
517
|
+
# we want to get interpolated objective at the following domain
|
|
518
|
+
# the idea is that the peak would have been at a different time point if the optimal
|
|
519
|
+
# upsampled value weren't within 0.5 (inclusive) of the peak time
|
|
520
|
+
up_half = up_factor // 2
|
|
521
|
+
up_tt = torch.linspace(-0.5, 0.5, steps=2 * up_half + 1, device=device)
|
|
522
|
+
|
|
523
|
+
# which upsampled template would correspond to a match at each upsampled time?
|
|
524
|
+
uarange = np.arange(up_factor)
|
|
525
|
+
up_ix = np.concatenate([uarange[: up_half + 1][::-1], uarange[-up_half:][::-1]])
|
|
526
|
+
up_ix = torch.asarray(up_ix, device=device)
|
|
527
|
+
|
|
528
|
+
# tricky part: at which time should we then subtract the upsampled template?
|
|
529
|
+
up_time_shift = (up_tt > 0).long()
|
|
530
|
+
|
|
531
|
+
# which is not the same as the featurization window.
|
|
532
|
+
up_extract_shift = torch.logical_and(up_tt > 0, up_ix > up_half).long().neg()
|
|
533
|
+
|
|
534
|
+
return objective_window, objective_tt, up_tt, up_ix, up_time_shift, up_extract_shift
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
@databag
|
|
538
|
+
class UpsamplingData:
|
|
539
|
+
objective_window: Tensor
|
|
540
|
+
objective_tt: Tensor
|
|
541
|
+
up_tt: Tensor
|
|
542
|
+
up_ix: Tensor
|
|
543
|
+
up_time_shift: Tensor
|
|
544
|
+
up_extract_shift: Tensor
|
|
545
|
+
interpolator: Tensor
|
|
546
|
+
zpad: int
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
def get_interp_upsampling_data(
|
|
550
|
+
*,
|
|
551
|
+
up_factor: int,
|
|
552
|
+
up_method: Literal["interpolation", "keys3", "keys4"],
|
|
553
|
+
interp_up_radius: int,
|
|
554
|
+
interp_params: InterpolationParams = default_upsampling_params,
|
|
555
|
+
device: torch.device,
|
|
556
|
+
):
|
|
557
|
+
if up_method == "interpolation":
|
|
558
|
+
up_radius = interp_up_radius
|
|
559
|
+
elif up_method == "keys3":
|
|
560
|
+
# keys3 only ever needs to look at 2 neighbors
|
|
561
|
+
up_radius = 2
|
|
562
|
+
elif up_method == "keys4":
|
|
563
|
+
up_radius = 3
|
|
564
|
+
else:
|
|
565
|
+
assert False
|
|
566
|
+
ixs = get_interp_upsampling_indices(
|
|
567
|
+
up_factor=up_factor, up_radius=up_radius, device=device
|
|
568
|
+
)
|
|
569
|
+
objective_window, objective_tt, up_tt, up_ix, up_time_shift, up_extract_shift = ixs
|
|
570
|
+
if up_method == "interpolation":
|
|
571
|
+
kernel, zpad = bake_interpolation_1d(objective_tt, up_tt, interp_params)
|
|
572
|
+
elif up_method == "keys3":
|
|
573
|
+
kernel = _this_keys_kernel(3, up_tt)
|
|
574
|
+
zpad = 0
|
|
575
|
+
elif up_method == "keys4":
|
|
576
|
+
kernel = _this_keys_kernel(4, up_tt)
|
|
577
|
+
zpad = 0
|
|
578
|
+
else:
|
|
579
|
+
assert False
|
|
580
|
+
return UpsamplingData(
|
|
581
|
+
objective_window=objective_window,
|
|
582
|
+
objective_tt=objective_tt,
|
|
583
|
+
up_tt=up_tt,
|
|
584
|
+
up_ix=up_ix,
|
|
585
|
+
up_time_shift=up_time_shift,
|
|
586
|
+
up_extract_shift=up_extract_shift,
|
|
587
|
+
interpolator=kernel,
|
|
588
|
+
zpad=zpad,
|
|
589
|
+
)
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
# -- computing pairwise convolutions
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
def shared_temporal_pconv(temporal_comps: Tensor, up_temporal_comps: Tensor) -> Tensor:
|
|
596
|
+
rank, t = temporal_comps.shape
|
|
597
|
+
assert t >= rank
|
|
598
|
+
rank_, up, t_ = up_temporal_comps.shape
|
|
599
|
+
assert t == t_
|
|
600
|
+
assert rank == rank_
|
|
601
|
+
|
|
602
|
+
# NIL = rank, 1, t
|
|
603
|
+
inp = temporal_comps[:, None, :]
|
|
604
|
+
# OIL = rank * up, 1, t. rank major, not up major.
|
|
605
|
+
fil = up_temporal_comps.reshape(rank * up, 1, t)
|
|
606
|
+
# NOL = rank, rank * up, 2 * t - 1
|
|
607
|
+
pconv = F.conv1d(input=inp, weight=fil, padding=t - 1)
|
|
608
|
+
assert pconv.shape == (rank, rank * up, 2 * t - 1)
|
|
609
|
+
pconv = pconv.view(rank, rank, up, 2 * t - 1)
|
|
610
|
+
pconv = torch.flip(pconv, dims=(3,))
|
|
611
|
+
|
|
612
|
+
return pconv
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
@torch.jit.script
|
|
616
|
+
def full_shared_pconv(
|
|
617
|
+
temporal_pconv: Tensor, spatial_sing: Tensor, batch_size: int = 64
|
|
618
|
+
) -> Tensor:
|
|
619
|
+
rank, rank_, up, conv_len = temporal_pconv.shape
|
|
620
|
+
n_units, rank__, chans = spatial_sing.shape
|
|
621
|
+
assert rank == rank_ == rank__
|
|
622
|
+
out = spatial_sing.new_empty((n_units, n_units, up, conv_len))
|
|
623
|
+
spatial_sing_flat = spatial_sing.view(n_units * rank, chans)
|
|
624
|
+
temporal_pconv_flat = temporal_pconv.view(rank * rank, up * conv_len)
|
|
625
|
+
for i0 in range(0, n_units, batch_size):
|
|
626
|
+
i1 = min(n_units, i0 + batch_size)
|
|
627
|
+
chunksz = (i1 - i0) * n_units
|
|
628
|
+
spatial_left = spatial_sing[i0:i1]
|
|
629
|
+
spatial_outer = spatial_left.view((i1 - i0) * rank, chans) @ spatial_sing_flat.T
|
|
630
|
+
spatial_outer = spatial_outer.view(i1 - i0, rank, n_units, rank)
|
|
631
|
+
spatial_outer = spatial_outer.permute(0, 2, 1, 3).reshape(chunksz, rank * rank)
|
|
632
|
+
torch.mm(
|
|
633
|
+
spatial_outer,
|
|
634
|
+
temporal_pconv_flat,
|
|
635
|
+
out=out[i0:i1].view(chunksz, up * conv_len),
|
|
636
|
+
)
|
|
637
|
+
return out
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
# -- convolution
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
@torch.jit.script
|
|
644
|
+
def convolve_lowrank_shared(
|
|
645
|
+
traces: Tensor,
|
|
646
|
+
spatial_singular: Tensor,
|
|
647
|
+
temporal_components: Tensor,
|
|
648
|
+
padding: int = 0,
|
|
649
|
+
out: Tensor | None = None,
|
|
650
|
+
):
|
|
651
|
+
for q in range(temporal_components.shape[0]):
|
|
652
|
+
# convolve recording with this rank's basis element
|
|
653
|
+
tconv = F.conv1d(
|
|
654
|
+
traces[:, None, :], temporal_components[q, None, None], padding=padding
|
|
655
|
+
)
|
|
656
|
+
assert tconv.shape[1] == 1
|
|
657
|
+
tconv = tconv[:, 0]
|
|
658
|
+
|
|
659
|
+
# multiply spatially and add into output
|
|
660
|
+
if out is None:
|
|
661
|
+
out = torch.mm(spatial_singular[:, q, :], tconv)
|
|
662
|
+
else:
|
|
663
|
+
out.addmm_(spatial_singular[:, q, :], tconv)
|
|
664
|
+
|
|
665
|
+
return out
|
|
666
|
+
|
|
667
|
+
|
|
668
|
+
# -- fine matching
|
|
669
|
+
|
|
670
|
+
|
|
671
|
+
@torch.jit.script
|
|
672
|
+
def _upsampling_fine_match(
|
|
673
|
+
*,
|
|
674
|
+
conv: Tensor,
|
|
675
|
+
template_inds: Tensor,
|
|
676
|
+
times: Tensor,
|
|
677
|
+
padding: int,
|
|
678
|
+
normsq: Tensor,
|
|
679
|
+
scaling: bool,
|
|
680
|
+
inv_lambda: Tensor,
|
|
681
|
+
scale_min: Tensor,
|
|
682
|
+
scale_max: Tensor,
|
|
683
|
+
up_zpad: int,
|
|
684
|
+
objective_window: Tensor,
|
|
685
|
+
up_ix: Tensor,
|
|
686
|
+
interpolator: Tensor,
|
|
687
|
+
) -> tuple[Tensor | None, Tensor, Tensor, Tensor]:
|
|
688
|
+
# extract conv snippets
|
|
689
|
+
conv_snips = conv[
|
|
690
|
+
template_inds[:, None], times[:, None] + objective_window + padding
|
|
691
|
+
]
|
|
692
|
+
|
|
693
|
+
# upsample conv
|
|
694
|
+
if up_zpad:
|
|
695
|
+
conv_snips = F.pad(conv_snips, (0, up_zpad))
|
|
696
|
+
conv_up = conv_snips @ interpolator
|
|
697
|
+
|
|
698
|
+
# compute objective
|
|
699
|
+
if scaling and inv_lambda == 0.0:
|
|
700
|
+
scalings = conv_up.relu().div_(normsq[template_inds][:, None])
|
|
701
|
+
obj_up = scalings.mul(conv_up)
|
|
702
|
+
elif scaling:
|
|
703
|
+
b = conv_up + inv_lambda
|
|
704
|
+
a = normsq[template_inds] + inv_lambda
|
|
705
|
+
scalings = (b / a[:, None]).clamp_(scale_min, scale_max)
|
|
706
|
+
obj_up = 2.0 * scalings * b - scalings.square() * a[:, None] - inv_lambda
|
|
707
|
+
else:
|
|
708
|
+
scalings = None
|
|
709
|
+
obj_up = 2.0 * conv_up - normsq[template_inds, None]
|
|
710
|
+
|
|
711
|
+
# get best match and figure out what to do
|
|
712
|
+
objs, up_best = obj_up.max(dim=1)
|
|
713
|
+
upsampling_ixs = up_ix[up_best]
|
|
714
|
+
|
|
715
|
+
if scalings is not None:
|
|
716
|
+
scalings = scalings.take_along_dim(dim=1, indices=up_best[:, None])[:, 0]
|
|
717
|
+
|
|
718
|
+
return scalings, upsampling_ixs, up_best, objs
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
# -- Keys' piecewise cubic interpolation impl (order 3 and 4)
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
def _this_keys_kernel(deg: Literal[3, 4], xx_: Tensor) -> Tensor:
|
|
725
|
+
"""Dense Keys' cubic convolution kernel for this setting (specific length, no boundary)."""
|
|
726
|
+
# this function assumes that...
|
|
727
|
+
assert xx_.ndim == 1
|
|
728
|
+
assert xx_.shape[0] % 2
|
|
729
|
+
assert torch.equal(xx_, -xx_.flip((0,)))
|
|
730
|
+
assert (xx_.abs() < 1).all()
|
|
731
|
+
|
|
732
|
+
# t -2 -1 0 1 2
|
|
733
|
+
# x 0 1 2 3 4
|
|
734
|
+
# | | | | |
|
|
735
|
+
# left: x_
|
|
736
|
+
# |s|< | 2 | 1 | 2 | |
|
|
737
|
+
# right: x_
|
|
738
|
+
# |s|< | | 2 | 1 | 2 |
|
|
739
|
+
|
|
740
|
+
# the argument to u is s + j - k, where j is the left endpoint
|
|
741
|
+
if deg == 3:
|
|
742
|
+
jj = torch.tensor([1.0, 1, 2, 2, 2]).to(xx_) + (deg - 3)
|
|
743
|
+
else:
|
|
744
|
+
jj = torch.tensor([1.0, 1, 1, 2, 2, 2, 2]).to(xx_) + (deg - 3)
|
|
745
|
+
kk = torch.arange(2 * (deg - 1) + 1, device=xx_.device, dtype=xx_.dtype)
|
|
746
|
+
|
|
747
|
+
# the output kernel has input on rows, output on columns (multiplies on the right side)
|
|
748
|
+
if deg == 3:
|
|
749
|
+
xjj = torch.tensor([-1.0, -1, 0, 0, 0]).to(xx_)
|
|
750
|
+
else:
|
|
751
|
+
xjj = torch.tensor([-1.0, -1, -1, 0, 0, 0, 0]).to(xx_)
|
|
752
|
+
u_arg = (jj - kk)[:, None] + (xx_ - xjj[:, None])
|
|
753
|
+
if deg == 3:
|
|
754
|
+
kernel = _keys3_u(u_arg)
|
|
755
|
+
elif deg == 4:
|
|
756
|
+
kernel = _keys4_u(u_arg)
|
|
757
|
+
else:
|
|
758
|
+
assert False
|
|
759
|
+
|
|
760
|
+
# interp at |s| = 0 is exact, and I didn't handle that case in _keys_u
|
|
761
|
+
kernel[:, xx_.shape[0] // 2] = 0.0
|
|
762
|
+
kernel[deg - 1, xx_.shape[0] // 2] = 1.0
|
|
763
|
+
|
|
764
|
+
return kernel
|
|
765
|
+
|
|
766
|
+
|
|
767
|
+
def _keys3_u(s: Tensor):
|
|
768
|
+
s_abs = s.abs()
|
|
769
|
+
case0 = s_abs < 1
|
|
770
|
+
case1 = (s_abs < 2).logical_and_(case0.logical_not())
|
|
771
|
+
case0_val = 1.5 * s_abs**3 - 2.5 * s_abs**2 + 1.0
|
|
772
|
+
case1_val = -0.5 * s_abs**3 + 2.5 * s_abs**2 - 4.0 * s_abs + 2.0
|
|
773
|
+
out = torch.zeros_like(s)
|
|
774
|
+
out = torch.where(case0, case0_val, out)
|
|
775
|
+
out = torch.where(case1, case1_val, out)
|
|
776
|
+
return out
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
def _keys4_u(s: Tensor):
|
|
780
|
+
s_abs = s.abs()
|
|
781
|
+
case0 = s_abs < 1
|
|
782
|
+
case1 = (s_abs < 2).logical_and_(case0.logical_not())
|
|
783
|
+
case2 = (s_abs < 3).logical_and_(case0.logical_or(case1).logical_not())
|
|
784
|
+
case0_val = (4 / 3) * s_abs**3 - (7 / 3) * s_abs**2 + 1.0
|
|
785
|
+
case1_val = (-7 / 12) * s_abs**3 + 3 * s_abs**2 - (59 / 12) * s_abs + (15 / 6)
|
|
786
|
+
case2_val = (1 / 12) * s_abs**3 - (2 / 3) * s_abs**2 + (21 / 12) * s_abs - (3 / 2)
|
|
787
|
+
out = torch.zeros_like(s)
|
|
788
|
+
out = torch.where(case0, case0_val, out)
|
|
789
|
+
out = torch.where(case1, case1_val, out)
|
|
790
|
+
out = torch.where(case2, case2_val, out)
|
|
791
|
+
return out
|