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,1012 @@
|
|
|
1
|
+
"""Library for flavors of kernel interpolation and data interp utilities"""
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
import torch
|
|
7
|
+
import torch.nn.functional as F
|
|
8
|
+
|
|
9
|
+
from ..clustering.gmm.stable_features import SpikeNeighborhoods
|
|
10
|
+
from .data_util import yield_masked_chunks
|
|
11
|
+
from .internal_config import (
|
|
12
|
+
InterpKernel,
|
|
13
|
+
InterpMethod,
|
|
14
|
+
InterpolationParams,
|
|
15
|
+
default_interpolation_params,
|
|
16
|
+
)
|
|
17
|
+
from .torch_util import BModule
|
|
18
|
+
from .waveform_util import make_channel_index
|
|
19
|
+
|
|
20
|
+
if TYPE_CHECKING:
|
|
21
|
+
from .noise_util import EmbeddedNoise
|
|
22
|
+
else:
|
|
23
|
+
EmbeddedNoise = None # type: ignore
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def interpolate_by_chunk(
|
|
27
|
+
mask,
|
|
28
|
+
dataset,
|
|
29
|
+
geom,
|
|
30
|
+
channel_index,
|
|
31
|
+
channels,
|
|
32
|
+
shifts,
|
|
33
|
+
registered_geom,
|
|
34
|
+
target_channels,
|
|
35
|
+
trim_to_rank: int | None = None,
|
|
36
|
+
params: InterpolationParams = default_interpolation_params,
|
|
37
|
+
device=None,
|
|
38
|
+
store_on_device=False,
|
|
39
|
+
show_progress=True,
|
|
40
|
+
shift_dim=1,
|
|
41
|
+
):
|
|
42
|
+
"""Interpolate data living in an HDF5 file
|
|
43
|
+
|
|
44
|
+
If dataset is a h5py.Dataset and mask is a boolean array indicating
|
|
45
|
+
positions of data to load, this iterates over the HDF5 chunks to
|
|
46
|
+
quickly scan through the data, applying interpolation to all the
|
|
47
|
+
features.
|
|
48
|
+
|
|
49
|
+
Arguments
|
|
50
|
+
---------
|
|
51
|
+
mask : boolean np.ndarray
|
|
52
|
+
Load and interpolate these entries. Shape should be
|
|
53
|
+
(n_spikes_full,), and let's say it has n_spikes nonzero entries.
|
|
54
|
+
dataset : h5py.Dataset
|
|
55
|
+
Chunked dataset, shape (n_spikes_full, feature_dim, n_source_channels)
|
|
56
|
+
Can only be chunked on the first axis
|
|
57
|
+
geom : array or tensor
|
|
58
|
+
channel_index : int array or tensor
|
|
59
|
+
channels : int array or tensor
|
|
60
|
+
Shape (n_spikes,)
|
|
61
|
+
shifts : array or tensor
|
|
62
|
+
Shape (n_spikes,) or (n_spikes, n_source_channels)
|
|
63
|
+
registered_geom : array or tensor
|
|
64
|
+
target_channels : int array or tensor
|
|
65
|
+
(n_spikes, n_target_channels)
|
|
66
|
+
sigma : float
|
|
67
|
+
Kernel bandwidth
|
|
68
|
+
interpolation_method : str
|
|
69
|
+
device : torch device
|
|
70
|
+
store_on_device : bool
|
|
71
|
+
Allocate the output tensor on gpu?
|
|
72
|
+
show_progress : bool
|
|
73
|
+
|
|
74
|
+
Returns
|
|
75
|
+
-------
|
|
76
|
+
out : torch.Tensor
|
|
77
|
+
(n_spikes, feature_dim, n_target_chans)
|
|
78
|
+
"""
|
|
79
|
+
# check shapes
|
|
80
|
+
assert geom.shape[1] == 2, "Haven't implemented 3d."
|
|
81
|
+
if torch.is_tensor(mask):
|
|
82
|
+
mask = mask.numpy(force=True)
|
|
83
|
+
(n_spikes_full,) = mask.shape
|
|
84
|
+
assert mask.dtype.kind == "b"
|
|
85
|
+
n_spikes = mask.sum()
|
|
86
|
+
assert channels.shape == (n_spikes,)
|
|
87
|
+
n_source_chans = channel_index.shape[1]
|
|
88
|
+
assert n_source_chans == dataset.shape[2]
|
|
89
|
+
assert n_spikes_full == dataset.shape[0]
|
|
90
|
+
n_target_chans = target_channels.shape[1]
|
|
91
|
+
assert target_channels.shape == (n_spikes, n_target_chans)
|
|
92
|
+
assert shifts.shape == (n_spikes,)
|
|
93
|
+
assert len(geom) == len(channel_index)
|
|
94
|
+
feature_dim = dataset.shape[1]
|
|
95
|
+
|
|
96
|
+
params = params.normalize()
|
|
97
|
+
|
|
98
|
+
if device is None:
|
|
99
|
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
100
|
+
device = torch.device(device)
|
|
101
|
+
dtype = torch.from_numpy(np.empty((), dtype=dataset.dtype)).dtype
|
|
102
|
+
|
|
103
|
+
# allocate output
|
|
104
|
+
storage_device = device if store_on_device else "cpu"
|
|
105
|
+
out_shape = n_spikes, feature_dim, n_target_chans
|
|
106
|
+
out = torch.empty(out_shape, dtype=dtype, device=storage_device)
|
|
107
|
+
|
|
108
|
+
# build data needed for interpolation
|
|
109
|
+
source_geom = pad_geom(geom, dtype=dtype, device=device)
|
|
110
|
+
target_geom = pad_geom(registered_geom, dtype=dtype, device=device)
|
|
111
|
+
# here, shifts = reg_depths - depths = -usual displacement
|
|
112
|
+
# (reg_depths = depths - displacement)
|
|
113
|
+
shifts = torch.as_tensor(shifts, dtype=dtype)
|
|
114
|
+
target_channels = torch.as_tensor(target_channels)
|
|
115
|
+
channel_index = torch.as_tensor(channel_index, device=device)
|
|
116
|
+
channels = torch.as_tensor(channels)
|
|
117
|
+
|
|
118
|
+
# make interpolator
|
|
119
|
+
erp = StableFeaturesInterpolator(
|
|
120
|
+
source_geom=source_geom,
|
|
121
|
+
target_geom=target_geom,
|
|
122
|
+
channel_index=channel_index,
|
|
123
|
+
params=params,
|
|
124
|
+
shift_dim=shift_dim,
|
|
125
|
+
)
|
|
126
|
+
for sli, chunk_features in yield_masked_chunks(
|
|
127
|
+
mask, dataset, show_progress=show_progress, desc_prefix="Interpolating"
|
|
128
|
+
):
|
|
129
|
+
# interpolate, store
|
|
130
|
+
if trim_to_rank is not None:
|
|
131
|
+
chunk_features = chunk_features[:, :trim_to_rank]
|
|
132
|
+
chunk_features = torch.asarray(chunk_features, device=device, dtype=dtype)
|
|
133
|
+
out[sli] = erp.interp(
|
|
134
|
+
features=chunk_features,
|
|
135
|
+
source_main_channels=channels[sli].to(device),
|
|
136
|
+
target_channels=target_channels[sli],
|
|
137
|
+
source_shifts=shifts[sli].to(device),
|
|
138
|
+
allow_destroy=True,
|
|
139
|
+
).to(out)
|
|
140
|
+
|
|
141
|
+
return out
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def interp_precompute(
|
|
145
|
+
source_geom=None,
|
|
146
|
+
channel_index=None,
|
|
147
|
+
source_pos=None,
|
|
148
|
+
params: InterpolationParams = default_interpolation_params,
|
|
149
|
+
source_geom_is_padded=True,
|
|
150
|
+
):
|
|
151
|
+
params = params.normalize()
|
|
152
|
+
if params.method in ("nearest", "kernel", "normalized", "zero"):
|
|
153
|
+
return None
|
|
154
|
+
assert params.method in ("kriging", "krigingnormalized")
|
|
155
|
+
|
|
156
|
+
if source_pos is None:
|
|
157
|
+
assert source_geom is not None
|
|
158
|
+
source_geom = torch.asarray(source_geom)
|
|
159
|
+
n_source_chans = len(source_geom) - source_geom_is_padded
|
|
160
|
+
if channel_index is None:
|
|
161
|
+
channel_index = torch.arange(n_source_chans)[None]
|
|
162
|
+
channel_index = channel_index.to(source_geom.device)
|
|
163
|
+
source_pos = source_geom[channel_index]
|
|
164
|
+
valid = channel_index < n_source_chans
|
|
165
|
+
else:
|
|
166
|
+
assert source_geom is None
|
|
167
|
+
assert channel_index is None
|
|
168
|
+
source_pos = torch.asarray(source_pos)
|
|
169
|
+
valid = source_pos[..., 0].isfinite()
|
|
170
|
+
assert source_pos.ndim == 3
|
|
171
|
+
valid = valid.to(source_pos.device)
|
|
172
|
+
|
|
173
|
+
ns = len(source_pos)
|
|
174
|
+
neighb_size = source_pos.shape[1]
|
|
175
|
+
dim = source_pos.shape[2]
|
|
176
|
+
if params.kriging_poly_degree < 0:
|
|
177
|
+
design_vars = 0
|
|
178
|
+
elif params.kriging_poly_degree == 0:
|
|
179
|
+
design_vars = 1
|
|
180
|
+
elif params.kriging_poly_degree == 1:
|
|
181
|
+
design_vars = 1 + dim
|
|
182
|
+
elif params.kriging_poly_degree == 2:
|
|
183
|
+
design_vars = 1 + dim + (dim * (dim + 1)) // 2
|
|
184
|
+
else:
|
|
185
|
+
assert False
|
|
186
|
+
|
|
187
|
+
solvers = source_pos.new_zeros(
|
|
188
|
+
(ns, neighb_size + design_vars, neighb_size + design_vars)
|
|
189
|
+
)
|
|
190
|
+
design_inds = torch.arange(neighb_size, neighb_size + design_vars)
|
|
191
|
+
design_inds = design_inds.to(source_pos.device)
|
|
192
|
+
design_zeros = source_pos.new_zeros((design_vars, design_vars))
|
|
193
|
+
|
|
194
|
+
source_kernels = get_kernel(
|
|
195
|
+
source_pos,
|
|
196
|
+
kernel_name=params.kernel.removesuffix("normalized"),
|
|
197
|
+
sigma=params.sigma,
|
|
198
|
+
rq_alpha=params.rq_alpha,
|
|
199
|
+
normalized=params.method.endswith("normalized"),
|
|
200
|
+
smoothing_lambda=params.smoothing_lambda,
|
|
201
|
+
)
|
|
202
|
+
for j in range(ns):
|
|
203
|
+
(present,) = valid[j].nonzero(as_tuple=True)
|
|
204
|
+
kernel = source_kernels[j][present][:, present]
|
|
205
|
+
|
|
206
|
+
if design_vars:
|
|
207
|
+
pos = source_pos[j][present] / params.sigma
|
|
208
|
+
const = pos.new_ones((pos.shape[0], 1))
|
|
209
|
+
ix = torch.concatenate((present, design_inds), dim=0)
|
|
210
|
+
if params.kriging_poly_degree == 0:
|
|
211
|
+
design = const
|
|
212
|
+
elif params.kriging_poly_degree == 1:
|
|
213
|
+
design = torch.cat([pos, const], dim=1)
|
|
214
|
+
elif params.kriging_poly_degree == 2:
|
|
215
|
+
if dim == 1:
|
|
216
|
+
design = torch.cat([pos, pos.square(), const], dim=1)
|
|
217
|
+
elif dim == 2:
|
|
218
|
+
p01 = (pos[:, 0] * pos[:, 1]).unsqueeze(1)
|
|
219
|
+
design = torch.cat([pos, pos.square(), p01, const], dim=1)
|
|
220
|
+
elif dim == 3:
|
|
221
|
+
p01 = (pos[:, 0] * pos[:, 1]).unsqueeze(1)
|
|
222
|
+
p02 = (pos[:, 0] * pos[:, 2]).unsqueeze(1)
|
|
223
|
+
p12 = (pos[:, 1] * pos[:, 2]).unsqueeze(1)
|
|
224
|
+
design = torch.cat([pos, pos.square(), p01, p02, p12, const], dim=1)
|
|
225
|
+
else:
|
|
226
|
+
assert False
|
|
227
|
+
else:
|
|
228
|
+
assert False
|
|
229
|
+
assert design.shape[1] == design_vars
|
|
230
|
+
|
|
231
|
+
top = torch.cat([kernel, design], dim=1)
|
|
232
|
+
bot = torch.cat([design.T, design_zeros], dim=1)
|
|
233
|
+
to_solve = torch.cat([top, bot], dim=0)
|
|
234
|
+
else:
|
|
235
|
+
to_solve = kernel
|
|
236
|
+
ix = present
|
|
237
|
+
|
|
238
|
+
# double+pinv is helpful here. numerics can get weird with large domains.
|
|
239
|
+
solver = torch.linalg.pinv(to_solve.double(), hermitian=True)
|
|
240
|
+
solvers[j, ix[:, None], ix[None, :]] = solver.to(solvers.dtype)
|
|
241
|
+
|
|
242
|
+
return solvers
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def full_probe_precompute(
|
|
246
|
+
source_geom: torch.Tensor, channel_index: torch.Tensor, params: InterpolationParams
|
|
247
|
+
):
|
|
248
|
+
"""When kriging on the full probe, numerical problems arise due to solving huge systems
|
|
249
|
+
|
|
250
|
+
This breaks up the problem into neighborhoods, so that each output depends only on
|
|
251
|
+
a neighborhood of inputs. Same trick that numpy doees in RBFInterpolator if you ask
|
|
252
|
+
it nicely.
|
|
253
|
+
|
|
254
|
+
Usually, precomputed data shape is (nneighbs, nc_neighb + design, nc_neighb + design).
|
|
255
|
+
This one makes a shape (1, nc, nc + design, nc + design), and it hits a special case
|
|
256
|
+
in kriging_solve triggered by the shape. Sorry!
|
|
257
|
+
|
|
258
|
+
TODO can this be simplified?
|
|
259
|
+
"""
|
|
260
|
+
precomputed_data = interp_precompute(
|
|
261
|
+
source_geom=pad_geom(source_geom),
|
|
262
|
+
channel_index=channel_index,
|
|
263
|
+
params=params,
|
|
264
|
+
source_geom_is_padded=True,
|
|
265
|
+
)
|
|
266
|
+
if precomputed_data is None:
|
|
267
|
+
return None
|
|
268
|
+
|
|
269
|
+
nc, nc_pc, nc_pc_ = precomputed_data.shape
|
|
270
|
+
assert nc_pc == nc_pc_
|
|
271
|
+
assert nc_pc >= channel_index.shape[1]
|
|
272
|
+
extra_dim = nc_pc - channel_index.shape[1]
|
|
273
|
+
assert extra_dim >= 0
|
|
274
|
+
# embed into full probe...
|
|
275
|
+
pc_full = precomputed_data.new_zeros((nc, nc + extra_dim, nc + extra_dim))
|
|
276
|
+
for j in range(nc):
|
|
277
|
+
chans = channel_index[j]
|
|
278
|
+
(valid,) = (chans < nc).nonzero(as_tuple=True)
|
|
279
|
+
cvalid = chans[valid]
|
|
280
|
+
pc_full[j, cvalid[:, None], cvalid[None, :]] = precomputed_data[
|
|
281
|
+
j, valid[:, None], valid[None, :]
|
|
282
|
+
]
|
|
283
|
+
if extra_dim > 0:
|
|
284
|
+
pc_full[j, -extra_dim:, -extra_dim:] = precomputed_data[
|
|
285
|
+
j, -extra_dim:, -extra_dim:
|
|
286
|
+
]
|
|
287
|
+
pc_full[j, -extra_dim:, cvalid[None, :]] = precomputed_data[
|
|
288
|
+
j, -extra_dim:, valid[None, :]
|
|
289
|
+
]
|
|
290
|
+
pc_full[j, cvalid[:, None], -extra_dim:] = precomputed_data[
|
|
291
|
+
j, valid[:, None], -extra_dim:
|
|
292
|
+
]
|
|
293
|
+
precomputed_data = pc_full[None]
|
|
294
|
+
return precomputed_data
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def kernel_interpolate(
|
|
298
|
+
features,
|
|
299
|
+
source_pos,
|
|
300
|
+
target_pos,
|
|
301
|
+
params: InterpolationParams = default_interpolation_params,
|
|
302
|
+
precomputed_data=None,
|
|
303
|
+
solver_map=None,
|
|
304
|
+
allow_destroy=False,
|
|
305
|
+
out=None,
|
|
306
|
+
):
|
|
307
|
+
"""Kernel interpolation of multi-channel features or waveforms
|
|
308
|
+
|
|
309
|
+
Arguments
|
|
310
|
+
---------
|
|
311
|
+
features : torch.Tensor
|
|
312
|
+
n_spikes, feature_dim, n_source_channels
|
|
313
|
+
These can be masked, indicated by nans here and in the same
|
|
314
|
+
places of source_pos
|
|
315
|
+
source_pos : torch.Tensor
|
|
316
|
+
n_spikes, n_source_channels, spatial_dim
|
|
317
|
+
target_pos : torch.Tensor
|
|
318
|
+
n_spikes, n_target_channels, spatial_dim
|
|
319
|
+
These can also be masked, indicate with nans and you will
|
|
320
|
+
get nans in those positions
|
|
321
|
+
sigma : float
|
|
322
|
+
Spatial bandwidth of RBF kernels
|
|
323
|
+
allow_destroy : bool
|
|
324
|
+
We need to overwrite nans in the features with 0s. If you
|
|
325
|
+
allow me, I'll do that in-place.
|
|
326
|
+
out : torch.Tensor
|
|
327
|
+
Storage for target
|
|
328
|
+
|
|
329
|
+
Returns
|
|
330
|
+
-------
|
|
331
|
+
features : torch.Tensor
|
|
332
|
+
n_spikes, feature_dim, n_target_channels
|
|
333
|
+
"""
|
|
334
|
+
features = torch.asarray(features)
|
|
335
|
+
source_pos = torch.asarray(source_pos)
|
|
336
|
+
target_pos = torch.asarray(target_pos)
|
|
337
|
+
|
|
338
|
+
params = params.normalize()
|
|
339
|
+
extrap_diff = params.extrap_diff()
|
|
340
|
+
|
|
341
|
+
if extrap_diff:
|
|
342
|
+
if params.actual_extrap_method == "kriging":
|
|
343
|
+
# haven't supported different precomputed kriging data
|
|
344
|
+
assert params.kernel == params.actual_extrap_kernel
|
|
345
|
+
features_out = _kernel_interpolate(
|
|
346
|
+
features=features,
|
|
347
|
+
source_pos=source_pos,
|
|
348
|
+
target_pos=target_pos,
|
|
349
|
+
method=params.actual_extrap_method,
|
|
350
|
+
kernel_name=params.actual_extrap_kernel,
|
|
351
|
+
sigma=params.sigma,
|
|
352
|
+
rq_alpha=params.rq_alpha,
|
|
353
|
+
kriging_poly_degree=params.kriging_poly_degree,
|
|
354
|
+
smoothing_lambda=params.smoothing_lambda,
|
|
355
|
+
precomputed_data=precomputed_data,
|
|
356
|
+
solver_map=solver_map,
|
|
357
|
+
)
|
|
358
|
+
else:
|
|
359
|
+
features_out = None
|
|
360
|
+
|
|
361
|
+
if precomputed_data is None:
|
|
362
|
+
# if at all possible, don't hit this branch. it's just here for vis.
|
|
363
|
+
precomputed_data = interp_precompute(source_pos=source_pos, params=params)
|
|
364
|
+
|
|
365
|
+
features = _kernel_interpolate(
|
|
366
|
+
features=features,
|
|
367
|
+
source_pos=source_pos,
|
|
368
|
+
target_pos=target_pos,
|
|
369
|
+
method=params.method,
|
|
370
|
+
kernel_name=params.kernel,
|
|
371
|
+
sigma=params.sigma,
|
|
372
|
+
rq_alpha=params.rq_alpha,
|
|
373
|
+
kriging_poly_degree=params.kriging_poly_degree,
|
|
374
|
+
smoothing_lambda=params.smoothing_lambda,
|
|
375
|
+
precomputed_data=precomputed_data,
|
|
376
|
+
solver_map=solver_map,
|
|
377
|
+
allow_destroy=allow_destroy,
|
|
378
|
+
out=out,
|
|
379
|
+
)
|
|
380
|
+
|
|
381
|
+
if extrap_diff:
|
|
382
|
+
# control over extrapolation with another method...
|
|
383
|
+
assert features_out is not None
|
|
384
|
+
targ_extrap = extrap_mask(source_pos, target_pos)[:, None]
|
|
385
|
+
features = torch.where(targ_extrap, features_out, features, out=features)
|
|
386
|
+
|
|
387
|
+
return features
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
def _kernel_interpolate(
|
|
391
|
+
*,
|
|
392
|
+
features: torch.Tensor,
|
|
393
|
+
source_pos: torch.Tensor,
|
|
394
|
+
target_pos: torch.Tensor,
|
|
395
|
+
method: InterpMethod,
|
|
396
|
+
kernel_name: InterpKernel,
|
|
397
|
+
sigma: float,
|
|
398
|
+
rq_alpha: float,
|
|
399
|
+
kriging_poly_degree: int,
|
|
400
|
+
smoothing_lambda: float,
|
|
401
|
+
precomputed_data,
|
|
402
|
+
solver_map=None,
|
|
403
|
+
allow_destroy=False,
|
|
404
|
+
out=None,
|
|
405
|
+
):
|
|
406
|
+
kernel = get_kernel(
|
|
407
|
+
source_pos=source_pos,
|
|
408
|
+
target_pos=target_pos,
|
|
409
|
+
kernel_name=kernel_name.removesuffix("normalized"),
|
|
410
|
+
sigma=sigma,
|
|
411
|
+
rq_alpha=rq_alpha,
|
|
412
|
+
normalized=method.endswith("normalized"),
|
|
413
|
+
smoothing_lambda=smoothing_lambda,
|
|
414
|
+
)
|
|
415
|
+
|
|
416
|
+
features = torch.nan_to_num(features, out=features if allow_destroy else None)
|
|
417
|
+
if method == "kriging":
|
|
418
|
+
assert precomputed_data is not None
|
|
419
|
+
precomputed_data = precomputed_data.to(features)
|
|
420
|
+
features = kriging_solve(
|
|
421
|
+
target_pos,
|
|
422
|
+
kernel,
|
|
423
|
+
features,
|
|
424
|
+
solvers=precomputed_data,
|
|
425
|
+
solver_map=solver_map,
|
|
426
|
+
sigma=sigma,
|
|
427
|
+
poly_degree=kriging_poly_degree,
|
|
428
|
+
)
|
|
429
|
+
else:
|
|
430
|
+
features = torch.bmm(features, kernel, out=out)
|
|
431
|
+
|
|
432
|
+
# nan-ify nonexistent chans
|
|
433
|
+
needs_nan = torch.isnan(target_pos).all(2).unsqueeze(1)
|
|
434
|
+
needs_nan = needs_nan.broadcast_to(features.shape)
|
|
435
|
+
features.masked_fill_(needs_nan, torch.nan)
|
|
436
|
+
|
|
437
|
+
return features
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
def get_kernel(
|
|
441
|
+
source_pos,
|
|
442
|
+
target_pos=None,
|
|
443
|
+
kernel_name="rbf",
|
|
444
|
+
sigma=20.0,
|
|
445
|
+
rq_alpha=1.0,
|
|
446
|
+
normalized=False,
|
|
447
|
+
smoothing_lambda=0.0,
|
|
448
|
+
):
|
|
449
|
+
assert source_pos.ndim == 3
|
|
450
|
+
assert source_pos.shape[2] in (1, 2, 3)
|
|
451
|
+
|
|
452
|
+
if kernel_name == "zero":
|
|
453
|
+
tc = source_pos.shape[1] if target_pos is None else target_pos.shape[1]
|
|
454
|
+
return source_pos.new_zeros(*source_pos.shape[:2], tc)
|
|
455
|
+
elif kernel_name == "nearest":
|
|
456
|
+
kernel = nearest_kernel(source_pos, target_pos)
|
|
457
|
+
elif kernel_name == "idw":
|
|
458
|
+
kernel = idw_kernel(source_pos, target_pos)
|
|
459
|
+
elif kernel_name == "rbf":
|
|
460
|
+
kernel = log_rbf(source_pos=source_pos, target_pos=target_pos, sigma=sigma)
|
|
461
|
+
if normalized:
|
|
462
|
+
kernel = F.softmax(kernel, dim=1)
|
|
463
|
+
else:
|
|
464
|
+
kernel = kernel.exp_()
|
|
465
|
+
elif kernel_name == "multiquadric":
|
|
466
|
+
kernel = multiquadric_kernel(
|
|
467
|
+
source_pos=source_pos, target_pos=target_pos, sigma=sigma
|
|
468
|
+
)
|
|
469
|
+
elif kernel_name == "rq":
|
|
470
|
+
kernel = rq_kernel(
|
|
471
|
+
source_pos=source_pos, target_pos=target_pos, sigma=sigma, alpha=rq_alpha
|
|
472
|
+
)
|
|
473
|
+
elif kernel_name == "thinplate":
|
|
474
|
+
kernel = thin_plate_greens(
|
|
475
|
+
source_pos=source_pos, target_pos=target_pos, sigma=sigma
|
|
476
|
+
)
|
|
477
|
+
else:
|
|
478
|
+
assert False
|
|
479
|
+
|
|
480
|
+
kernel = kernel.nan_to_num_()
|
|
481
|
+
if normalized and kernel_name not in ("rbf", "nearest"):
|
|
482
|
+
kernel = kernel.div_(kernel.sum(dim=1, keepdim=True))
|
|
483
|
+
|
|
484
|
+
if smoothing_lambda:
|
|
485
|
+
kernel.diagonal(dim1=-2, dim2=-1).add_(smoothing_lambda)
|
|
486
|
+
|
|
487
|
+
return kernel
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
def kriging_solve(target_pos, kernels, features, solvers, solver_map=None, sigma=1.0, poly_degree=-1):
|
|
491
|
+
n, rank = features.shape[:2]
|
|
492
|
+
n_, n_targ, dim = target_pos.shape
|
|
493
|
+
assert n == n_
|
|
494
|
+
|
|
495
|
+
if poly_degree == -1:
|
|
496
|
+
y = features
|
|
497
|
+
pass
|
|
498
|
+
elif poly_degree == 0:
|
|
499
|
+
zero = features.new_zeros((n, rank, 1))
|
|
500
|
+
y = torch.concatenate([features, zero], dim=2)
|
|
501
|
+
const = features.new_ones((n, 1, n_targ))
|
|
502
|
+
kernels = torch.concatenate([kernels, const], dim=1)
|
|
503
|
+
elif poly_degree == 1:
|
|
504
|
+
zero = features.new_zeros((n, rank, 1 + dim))
|
|
505
|
+
y = torch.concatenate([features, zero], dim=2)
|
|
506
|
+
const = features.new_ones((n, 1, n_targ))
|
|
507
|
+
xy = (target_pos / sigma).nan_to_num_().mT
|
|
508
|
+
kernels = torch.concatenate([kernels, xy, const], dim=1)
|
|
509
|
+
elif poly_degree == 2:
|
|
510
|
+
ddim = 1 + dim + (dim * (dim + 1)) // 2
|
|
511
|
+
zero = features.new_zeros((n, rank, ddim))
|
|
512
|
+
y = torch.concatenate([features, zero], dim=2)
|
|
513
|
+
const = features.new_ones((n, 1, n_targ))
|
|
514
|
+
xy = (target_pos / sigma).nan_to_num_().mT
|
|
515
|
+
if dim == 1:
|
|
516
|
+
xysq = (xy.square(),)
|
|
517
|
+
elif dim == 2:
|
|
518
|
+
xysq = (xy.square(), xy[:, 0:1] * xy[:, 1:2])
|
|
519
|
+
elif dim == 3:
|
|
520
|
+
xysq = (
|
|
521
|
+
xy.square(),
|
|
522
|
+
xy[:, 0:1] * xy[:, 1:2],
|
|
523
|
+
xy[:, 0:1] * xy[:, 2:3],
|
|
524
|
+
xy[:, 1:2] * xy[:, 2:3],
|
|
525
|
+
)
|
|
526
|
+
else:
|
|
527
|
+
assert False
|
|
528
|
+
kernels = torch.concatenate([kernels, xy, *xysq, const], dim=1)
|
|
529
|
+
else:
|
|
530
|
+
assert False
|
|
531
|
+
|
|
532
|
+
if solvers.ndim == 3:
|
|
533
|
+
return y.bmm(solvers).bmm(kernels)
|
|
534
|
+
|
|
535
|
+
assert solvers.ndim == 4
|
|
536
|
+
assert solvers.shape[0] == 1
|
|
537
|
+
# per-channel case (each output chan has its own local neighb)
|
|
538
|
+
# this is meant to mimic the "neighbors" option to scipy's RBFInterpolator,
|
|
539
|
+
# but the implementation here is obscure. the rest of the logic is only shown
|
|
540
|
+
# once, and that's in full_probe_precompute.
|
|
541
|
+
# assumes that output channels and input channels are the same, and that all
|
|
542
|
+
# inputs share the same neighborhood-solver per channel.
|
|
543
|
+
solvers = solvers[0]
|
|
544
|
+
c = kernels.shape[2]
|
|
545
|
+
if solver_map is None:
|
|
546
|
+
assert c == solvers.shape[0]
|
|
547
|
+
c_ = kernels.shape[1]
|
|
548
|
+
assert c_ >= c
|
|
549
|
+
assert c_ == solvers.shape[1] == solvers.shape[2]
|
|
550
|
+
out = y.new_zeros((*y.shape[:2], c))
|
|
551
|
+
for cc in range(c):
|
|
552
|
+
# solver_map brings channels of y (targ chans) to source geom
|
|
553
|
+
if solver_map is None:
|
|
554
|
+
sc = cc
|
|
555
|
+
else:
|
|
556
|
+
sc = solver_map[cc]
|
|
557
|
+
# just reducing memory use here relative to the einsum below
|
|
558
|
+
out[:, :, cc] = torch.einsum("ntp,pq,nq->nt", y, solvers[sc], kernels[:, :, cc])
|
|
559
|
+
# return torch.einsum("ntp,cpq,nqc->ntc", y, solvers, kernels)
|
|
560
|
+
return out
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
def bake_interpolation_1d(
|
|
564
|
+
xx: torch.Tensor, xx_: torch.Tensor, params: InterpolationParams
|
|
565
|
+
) -> tuple[torch.Tensor, int]:
|
|
566
|
+
params = params.normalize()
|
|
567
|
+
|
|
568
|
+
k = get_kernel(
|
|
569
|
+
source_pos=xx[None, :, None],
|
|
570
|
+
target_pos=xx_[None, :, None],
|
|
571
|
+
kernel_name=params.kernel,
|
|
572
|
+
normalized=params.method.endswith("normalized"),
|
|
573
|
+
sigma=params.sigma,
|
|
574
|
+
rq_alpha=params.rq_alpha,
|
|
575
|
+
smoothing_lambda=params.smoothing_lambda,
|
|
576
|
+
)
|
|
577
|
+
nsrc = xx.shape[0]
|
|
578
|
+
assert k.shape == (1, nsrc, xx_.shape[0])
|
|
579
|
+
k = k[0]
|
|
580
|
+
# add design matrix terms on source dim, ie first dim...
|
|
581
|
+
need_design = params.method.startswith("kriging")
|
|
582
|
+
if need_design and params.kriging_poly_degree == 0:
|
|
583
|
+
k = torch.concatenate([k, torch.ones_like(xx_[None])], dim=0)
|
|
584
|
+
elif need_design and params.kriging_poly_degree == 1:
|
|
585
|
+
k = torch.concatenate([k, xx_[None], torch.ones_like(xx_[None])], dim=0)
|
|
586
|
+
elif need_design:
|
|
587
|
+
assert False
|
|
588
|
+
|
|
589
|
+
pdata = interp_precompute(source_pos=xx[None, :, None], params=params)
|
|
590
|
+
if pdata is not None:
|
|
591
|
+
assert pdata.shape == (1, k.shape[0], k.shape[0])
|
|
592
|
+
k = pdata[0] @ k
|
|
593
|
+
zpad = int(need_design) * (1 + params.kriging_poly_degree)
|
|
594
|
+
return k, zpad
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
def pad_geom(geom, dtype=torch.float, device=None):
|
|
598
|
+
geom = torch.as_tensor(geom, dtype=dtype, device=device)
|
|
599
|
+
geom = F.pad(geom, (0, 0, 0, 1), value=torch.nan)
|
|
600
|
+
return geom
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
def idw_kernel(source_pos, target_pos=None):
|
|
604
|
+
d = get_rsq(source_pos, target_pos, nan=None)
|
|
605
|
+
kernel = d.sqrt_().reciprocal_()
|
|
606
|
+
kernel = kernel.nan_to_num_()
|
|
607
|
+
kernel /= kernel.sum(dim=0)
|
|
608
|
+
return kernel
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
def nearest_kernel(source_pos, target_pos=None):
|
|
612
|
+
d = get_rsq(source_pos, target_pos, nan=torch.inf)
|
|
613
|
+
kernel = torch.zeros_like(d)
|
|
614
|
+
kernel.scatter_(1, d.argmin(dim=1, keepdim=True), 1)
|
|
615
|
+
return kernel
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
def log_rbf(source_pos, target_pos=None, sigma=None):
|
|
619
|
+
"""Log of RBF kernel
|
|
620
|
+
|
|
621
|
+
This handles missing values in source_pos or target_pos, indicated by
|
|
622
|
+
nans, by replacing them with -inf so that they exp to 0.
|
|
623
|
+
|
|
624
|
+
Arguments
|
|
625
|
+
---------
|
|
626
|
+
source_pos : torch.tensor
|
|
627
|
+
n source locations
|
|
628
|
+
target_pos : torch.tensor
|
|
629
|
+
m target locations
|
|
630
|
+
sigma : float
|
|
631
|
+
|
|
632
|
+
Returns
|
|
633
|
+
-------
|
|
634
|
+
kernel : torch.tensor
|
|
635
|
+
n by m
|
|
636
|
+
"""
|
|
637
|
+
kernel = get_rsq(source_pos, target_pos, sigma, nan=None).div_(-2.0)
|
|
638
|
+
kernel = kernel.nan_to_num_(nan=-torch.inf)
|
|
639
|
+
return kernel
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
def rq_kernel(source_pos, target_pos=None, sigma=None, alpha=1.0):
|
|
643
|
+
kernel = get_rsq(source_pos, target_pos, sigma)
|
|
644
|
+
kernel = kernel.add_(1.0).pow_(-alpha)
|
|
645
|
+
return kernel
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
def multiquadric_kernel(source_pos, target_pos=None, sigma=None):
|
|
649
|
+
kernel = get_rsq(source_pos, target_pos, sigma)
|
|
650
|
+
kernel = kernel.add_(1.0).pow_(-0.5).neg_()
|
|
651
|
+
return kernel
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
def thin_plate_greens(source_pos, target_pos=None, sigma=1.0):
|
|
655
|
+
rsq = get_rsq(source_pos, target_pos, sigma)
|
|
656
|
+
r = rsq.sqrt()
|
|
657
|
+
is_small = r < 1
|
|
658
|
+
small = r.mul(r.pow(r).log_())
|
|
659
|
+
big = rsq.mul_(r.log_())
|
|
660
|
+
kernel = torch.where(is_small, small, big)
|
|
661
|
+
return kernel
|
|
662
|
+
|
|
663
|
+
|
|
664
|
+
def get_rsq(
|
|
665
|
+
source_pos,
|
|
666
|
+
target_pos=None,
|
|
667
|
+
sigma: float | None = 1.0,
|
|
668
|
+
batch_size=16,
|
|
669
|
+
nan: float | None = 0.0,
|
|
670
|
+
):
|
|
671
|
+
have_sigma = sigma is not None
|
|
672
|
+
if have_sigma and isinstance(sigma, (float, int)):
|
|
673
|
+
have_sigma = sigma != 1.0
|
|
674
|
+
if have_sigma:
|
|
675
|
+
source_pos = source_pos / sigma
|
|
676
|
+
if target_pos is None:
|
|
677
|
+
target_pos = source_pos
|
|
678
|
+
elif have_sigma:
|
|
679
|
+
target_pos = target_pos / sigma
|
|
680
|
+
assert source_pos.ndim == target_pos.ndim == 3
|
|
681
|
+
assert source_pos.shape[0] == target_pos.shape[0]
|
|
682
|
+
assert source_pos.shape[2] == target_pos.shape[2]
|
|
683
|
+
|
|
684
|
+
n, nsrc = source_pos.shape[:2]
|
|
685
|
+
rsq = source_pos.new_zeros((n, nsrc, target_pos.shape[1]))
|
|
686
|
+
for bs in range(0, nsrc, batch_size):
|
|
687
|
+
sl = slice(bs, min(bs + batch_size, nsrc))
|
|
688
|
+
tmp = source_pos[:, sl, None] - target_pos[:, None, :]
|
|
689
|
+
tmp = tmp.square_().sum(dim=3)
|
|
690
|
+
if nan is not None:
|
|
691
|
+
tmp = tmp.nan_to_num_(nan=nan)
|
|
692
|
+
rsq[:, sl] = tmp
|
|
693
|
+
|
|
694
|
+
return rsq
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
def extrap_mask(source_pos, target_pos, eps=1e-3):
|
|
698
|
+
"""Only works for vertical shift."""
|
|
699
|
+
source_x_uniq = source_pos[..., 0].unique()
|
|
700
|
+
source_x_uniq = source_x_uniq[source_x_uniq.isfinite()]
|
|
701
|
+
|
|
702
|
+
targ_x, targ_y = target_pos[..., 0], target_pos[..., 1]
|
|
703
|
+
|
|
704
|
+
# algorithm: start with all finite. on each iter, remove some from the set.
|
|
705
|
+
targ_extrap = targ_y.isfinite()
|
|
706
|
+
|
|
707
|
+
for x in source_x_uniq:
|
|
708
|
+
source_in_col = torch.isclose(source_pos[..., 0], x)
|
|
709
|
+
targ_not_in_col = torch.isclose(targ_x, x).logical_not_()
|
|
710
|
+
|
|
711
|
+
source_col_y = torch.where(source_in_col, source_pos[..., 1], torch.nan)
|
|
712
|
+
source_low = source_col_y.nan_to_num(nan=torch.inf).amin(dim=1).sub_(eps)
|
|
713
|
+
source_high = source_col_y.nan_to_num(nan=-torch.inf).amax(dim=1).add_(eps)
|
|
714
|
+
|
|
715
|
+
targ_col_y = targ_y.masked_fill(targ_not_in_col, torch.nan)
|
|
716
|
+
# this is true for nans, but thats okay.
|
|
717
|
+
targ_outside = targ_col_y != targ_col_y.clamp(
|
|
718
|
+
min=source_low[:, None], max=source_high[:, None]
|
|
719
|
+
)
|
|
720
|
+
targ_extrap.logical_and_(targ_outside)
|
|
721
|
+
|
|
722
|
+
return targ_extrap
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
class StableFeaturesInterpolator(BModule):
|
|
726
|
+
def __init__(
|
|
727
|
+
self,
|
|
728
|
+
*,
|
|
729
|
+
source_geom: torch.Tensor,
|
|
730
|
+
target_geom: torch.Tensor,
|
|
731
|
+
channel_index: torch.Tensor,
|
|
732
|
+
params: InterpolationParams,
|
|
733
|
+
shift_dim: int = 1,
|
|
734
|
+
dtype=torch.float,
|
|
735
|
+
):
|
|
736
|
+
super().__init__()
|
|
737
|
+
self.params = params.normalize()
|
|
738
|
+
self.shift_dim = shift_dim
|
|
739
|
+
assert source_geom.shape[0] == 1 + channel_index.shape[0]
|
|
740
|
+
assert source_geom.ndim == target_geom.ndim == 2
|
|
741
|
+
assert source_geom.shape[1] == target_geom.shape[1] == 2
|
|
742
|
+
self.register_buffer("source_geom", source_geom.to(dtype=dtype))
|
|
743
|
+
self.register_buffer("target_geom", target_geom.to(dtype=dtype))
|
|
744
|
+
self.register_buffer("channel_index", channel_index)
|
|
745
|
+
neighb_data = interp_precompute(
|
|
746
|
+
source_geom=self.b.source_geom,
|
|
747
|
+
channel_index=channel_index,
|
|
748
|
+
params=self.params,
|
|
749
|
+
)
|
|
750
|
+
self.has_neighb_data = neighb_data is not None
|
|
751
|
+
self.register_buffer_or_none("neighb_data", neighb_data)
|
|
752
|
+
|
|
753
|
+
def interp(
|
|
754
|
+
self,
|
|
755
|
+
features: torch.Tensor,
|
|
756
|
+
source_main_channels: torch.Tensor,
|
|
757
|
+
target_channels: torch.Tensor,
|
|
758
|
+
source_shifts: torch.Tensor,
|
|
759
|
+
allow_destroy: bool = False,
|
|
760
|
+
) -> torch.Tensor:
|
|
761
|
+
assert target_channels.ndim == 2
|
|
762
|
+
assert target_channels.shape[0] == source_main_channels.shape[0]
|
|
763
|
+
|
|
764
|
+
# allows per-channel shifts with 2d input
|
|
765
|
+
if source_shifts.ndim == 1:
|
|
766
|
+
source_shifts = source_shifts.unsqueeze(1)
|
|
767
|
+
if self.shift_dim == 0:
|
|
768
|
+
source_shifts = torch.stack(
|
|
769
|
+
[source_shifts, torch.zeros_like(source_shifts)], dim=-1
|
|
770
|
+
)
|
|
771
|
+
elif self.shift_dim == 1:
|
|
772
|
+
source_shifts = torch.stack(
|
|
773
|
+
[torch.zeros_like(source_shifts), source_shifts], dim=-1
|
|
774
|
+
)
|
|
775
|
+
else:
|
|
776
|
+
assert False
|
|
777
|
+
|
|
778
|
+
# used to shift the source, but for kriging it's better to shift targets
|
|
779
|
+
# so that we can cache source kernel choleskys (i.e., my neighb_data)
|
|
780
|
+
source_channels = self.b.channel_index[source_main_channels]
|
|
781
|
+
source_pos = self.b.source_geom[source_channels] # + source_shifts
|
|
782
|
+
|
|
783
|
+
# where are they going?
|
|
784
|
+
target_pos = self.b.target_geom[target_channels] - source_shifts
|
|
785
|
+
|
|
786
|
+
if self.has_neighb_data:
|
|
787
|
+
pcomp = self.b.neighb_data[source_main_channels]
|
|
788
|
+
else:
|
|
789
|
+
pcomp = None
|
|
790
|
+
|
|
791
|
+
return kernel_interpolate(
|
|
792
|
+
features=features,
|
|
793
|
+
source_pos=source_pos,
|
|
794
|
+
target_pos=target_pos,
|
|
795
|
+
params=self.params,
|
|
796
|
+
precomputed_data=pcomp,
|
|
797
|
+
allow_destroy=allow_destroy,
|
|
798
|
+
)
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
class NeighborhoodFiller(BModule):
|
|
802
|
+
batch_size: int
|
|
803
|
+
|
|
804
|
+
def interp_to_chans(
|
|
805
|
+
self,
|
|
806
|
+
waveforms: torch.Tensor,
|
|
807
|
+
neighborhood_ids: torch.Tensor,
|
|
808
|
+
target_channels: torch.Tensor | slice,
|
|
809
|
+
out: torch.Tensor | None = None,
|
|
810
|
+
) -> torch.Tensor:
|
|
811
|
+
if waveforms.shape[0] <= self.batch_size and out is None:
|
|
812
|
+
return self._interp_to_chans(waveforms, neighborhood_ids, target_channels)
|
|
813
|
+
|
|
814
|
+
if torch.is_tensor(target_channels):
|
|
815
|
+
assert target_channels.ndim == 1
|
|
816
|
+
ntarg = target_channels.shape[0]
|
|
817
|
+
else:
|
|
818
|
+
ntarg = len(range(*target_channels.indices(len(self.b.prgeom))))
|
|
819
|
+
out_shp = (*waveforms.shape[:2], ntarg)
|
|
820
|
+
if out is None:
|
|
821
|
+
out = waveforms.new_zeros(out_shp)
|
|
822
|
+
else:
|
|
823
|
+
assert out.shape == out_shp
|
|
824
|
+
n = out.shape[0]
|
|
825
|
+
for i0 in range(0, n, self.batch_size):
|
|
826
|
+
i1 = min(n, i0 + self.batch_size)
|
|
827
|
+
out[i0:i1] = self._interp_to_chans(
|
|
828
|
+
waveforms=waveforms[i0:i1],
|
|
829
|
+
neighborhood_ids=neighborhood_ids[i0:i1],
|
|
830
|
+
target_channels=target_channels,
|
|
831
|
+
)
|
|
832
|
+
return out
|
|
833
|
+
|
|
834
|
+
def _interp_to_chans(
|
|
835
|
+
self,
|
|
836
|
+
waveforms: torch.Tensor,
|
|
837
|
+
neighborhood_ids: torch.Tensor,
|
|
838
|
+
target_channels: torch.Tensor | slice,
|
|
839
|
+
) -> torch.Tensor:
|
|
840
|
+
raise NotImplementedError
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
class NeighborhoodInterpolator(NeighborhoodFiller):
|
|
844
|
+
def __init__(
|
|
845
|
+
self,
|
|
846
|
+
prgeom: torch.Tensor,
|
|
847
|
+
neighborhoods: SpikeNeighborhoods,
|
|
848
|
+
params: InterpolationParams = default_interpolation_params,
|
|
849
|
+
batch_size: int = 1024,
|
|
850
|
+
):
|
|
851
|
+
super().__init__()
|
|
852
|
+
assert len(prgeom) == neighborhoods.n_channels + 1
|
|
853
|
+
self.params: InterpolationParams = params.normalize()
|
|
854
|
+
self.batch_size = batch_size
|
|
855
|
+
self.register_buffer("prgeom", prgeom.clone())
|
|
856
|
+
self.b.prgeom[-1].fill_(torch.nan)
|
|
857
|
+
neighb_data = interp_precompute(
|
|
858
|
+
source_geom=self.prgeom,
|
|
859
|
+
channel_index=neighborhoods.neighborhoods,
|
|
860
|
+
source_geom_is_padded=True,
|
|
861
|
+
params=self.params,
|
|
862
|
+
)
|
|
863
|
+
self.register_buffer_or_none("neighb_data", neighb_data)
|
|
864
|
+
self.register_buffer("neighb_pos", self.b.prgeom[neighborhoods.b.neighborhoods])
|
|
865
|
+
|
|
866
|
+
def _interp_to_chans(
|
|
867
|
+
self,
|
|
868
|
+
waveforms: torch.Tensor,
|
|
869
|
+
neighborhood_ids: torch.Tensor,
|
|
870
|
+
target_channels: torch.Tensor | slice,
|
|
871
|
+
):
|
|
872
|
+
if target_channels is None:
|
|
873
|
+
targ_pos = self.b.prgeom
|
|
874
|
+
else:
|
|
875
|
+
targ_pos = self.b.prgeom[target_channels]
|
|
876
|
+
targ_pos = targ_pos[None].broadcast_to((len(waveforms), *targ_pos.shape))
|
|
877
|
+
source_pos = self.b.neighb_pos[neighborhood_ids]
|
|
878
|
+
neighb_data = self.b.neighb_data
|
|
879
|
+
if neighb_data is not None:
|
|
880
|
+
neighb_data = neighb_data[neighborhood_ids]
|
|
881
|
+
return kernel_interpolate(
|
|
882
|
+
features=waveforms,
|
|
883
|
+
source_pos=source_pos,
|
|
884
|
+
target_pos=targ_pos,
|
|
885
|
+
precomputed_data=neighb_data,
|
|
886
|
+
params=self.params,
|
|
887
|
+
)
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
class NeighborhoodImputer(NeighborhoodFiller):
|
|
891
|
+
def __init__(
|
|
892
|
+
self,
|
|
893
|
+
noise: EmbeddedNoise,
|
|
894
|
+
neighborhoods: SpikeNeighborhoods,
|
|
895
|
+
batch_size: int = 1024,
|
|
896
|
+
):
|
|
897
|
+
super().__init__()
|
|
898
|
+
prec = all_neighb_precisions(noise, neighborhoods)
|
|
899
|
+
self.register_buffer("prec", prec)
|
|
900
|
+
chans_arange = torch.arange(noise.n_channels, device=prec.device)
|
|
901
|
+
self.register_buffer("chans_arange", chans_arange)
|
|
902
|
+
self.neighborhoods = neighborhoods
|
|
903
|
+
self.noise = noise
|
|
904
|
+
self.batch_size = batch_size
|
|
905
|
+
|
|
906
|
+
def _interp_to_chans(
|
|
907
|
+
self,
|
|
908
|
+
waveforms: torch.Tensor,
|
|
909
|
+
neighborhood_ids: torch.Tensor,
|
|
910
|
+
target_channels: torch.Tensor | slice,
|
|
911
|
+
) -> torch.Tensor:
|
|
912
|
+
if not torch.is_tensor(target_channels):
|
|
913
|
+
target_channels = self.b.chans_arange[target_channels]
|
|
914
|
+
source_channels = self.neighborhoods.b.neighborhoods[neighborhood_ids]
|
|
915
|
+
|
|
916
|
+
waveforms = waveforms.view(waveforms.shape[0], 1, -1)
|
|
917
|
+
waveforms = waveforms.bmm(self.b.prec[neighborhood_ids])
|
|
918
|
+
waveforms = waveforms.view(waveforms.shape[0], self.noise.rank, -1)
|
|
919
|
+
|
|
920
|
+
return self.noise.cov_batch_mul(waveforms, source_channels, target_channels)
|
|
921
|
+
|
|
922
|
+
|
|
923
|
+
class FullProbeInterpolator(BModule):
|
|
924
|
+
"""Interpolate from the registered geom to appropriate drifting geom channels."""
|
|
925
|
+
|
|
926
|
+
def __init__(
|
|
927
|
+
self,
|
|
928
|
+
*,
|
|
929
|
+
geom: torch.Tensor,
|
|
930
|
+
rgeom: torch.Tensor,
|
|
931
|
+
neighborhood_radius: float,
|
|
932
|
+
motion_est,
|
|
933
|
+
params: InterpolationParams,
|
|
934
|
+
):
|
|
935
|
+
super().__init__()
|
|
936
|
+
|
|
937
|
+
params = params.normalize()
|
|
938
|
+
rchannel_index = make_channel_index(
|
|
939
|
+
rgeom, radius=neighborhood_radius, to_torch=True
|
|
940
|
+
)
|
|
941
|
+
self.motion_est = motion_est
|
|
942
|
+
rchannel_index = rchannel_index.to(device=rgeom.device)
|
|
943
|
+
self.register_buffer_or_none(
|
|
944
|
+
"data", full_probe_precompute(rgeom, rchannel_index, params)
|
|
945
|
+
)
|
|
946
|
+
self.g_depths = geom[:, 1].numpy(force=True)
|
|
947
|
+
self.register_buffer("geom", geom)
|
|
948
|
+
self.register_buffer("rgeom", rgeom)
|
|
949
|
+
self.c_src: int = rgeom.shape[0]
|
|
950
|
+
self.c_targ: int = geom.shape[0]
|
|
951
|
+
self.dim: int = rgeom.shape[1]
|
|
952
|
+
|
|
953
|
+
def interp_at_time(self, t_s: float, waveforms: torch.Tensor) -> torch.Tensor:
|
|
954
|
+
assert waveforms.shape[2] == self.c_src
|
|
955
|
+
|
|
956
|
+
# move geom to its position at time t_s
|
|
957
|
+
shift = torch.zeros_like(self.b.geom)
|
|
958
|
+
if self.motion_est is not None:
|
|
959
|
+
disp = self.motion_est.disp_at_s(
|
|
960
|
+
t_s=np.array([t_s]), depth_um=self.g_depths, grid=True
|
|
961
|
+
)
|
|
962
|
+
assert disp.shape[1] == 1
|
|
963
|
+
shift[:, 1].copy_(torch.tensor(disp[:, 0]))
|
|
964
|
+
|
|
965
|
+
# which rgeom channel does each shifted geom channel land on?
|
|
966
|
+
sgeom = self.b.geom - shift
|
|
967
|
+
dist = torch.cdist(sgeom, self.b.rgeom)
|
|
968
|
+
# closest rgeom channel to each shifted channel determines which
|
|
969
|
+
# precomputed solver is selected
|
|
970
|
+
solver_map = dist.argmin(1)
|
|
971
|
+
|
|
972
|
+
# interpolate from static geom to shifted geom
|
|
973
|
+
n = waveforms.shape[0]
|
|
974
|
+
return kernel_interpolate(
|
|
975
|
+
features=waveforms,
|
|
976
|
+
source_pos=self.b.rgeom[None].broadcast_to(n, self.c_src, self.dim),
|
|
977
|
+
target_pos=(self.b.geom - shift).broadcast_to(n, self.c_targ, self.dim),
|
|
978
|
+
precomputed_data=self.b.data,
|
|
979
|
+
solver_map=solver_map,
|
|
980
|
+
)
|
|
981
|
+
|
|
982
|
+
|
|
983
|
+
def all_neighb_precisions(noise: EmbeddedNoise, neighborhoods: SpikeNeighborhoods):
|
|
984
|
+
channel_index = neighborhoods.b.neighborhoods
|
|
985
|
+
nneighb = channel_index.shape[0]
|
|
986
|
+
nc_obs = channel_index.shape[1]
|
|
987
|
+
rank = noise.rank
|
|
988
|
+
nc = noise.n_channels
|
|
989
|
+
dev = channel_index.device
|
|
990
|
+
|
|
991
|
+
Cooinv = torch.zeros((nneighb, rank, nc_obs, rank, nc_obs), device=dev)
|
|
992
|
+
|
|
993
|
+
for j, joix in enumerate(channel_index):
|
|
994
|
+
(joixvix,) = (joix < nc).nonzero(as_tuple=True)
|
|
995
|
+
joixv = joix[joixvix]
|
|
996
|
+
ncoi = joixv.numel()
|
|
997
|
+
|
|
998
|
+
jCoo = noise.marginal_covariance(
|
|
999
|
+
channels=joixv, cache_prefix=neighborhoods.name, cache_key=j
|
|
1000
|
+
)
|
|
1001
|
+
|
|
1002
|
+
jL = jCoo.cholesky(upper=False) # C = LL'
|
|
1003
|
+
jLinv = jL.inverse().to_dense()
|
|
1004
|
+
jCooinv = jLinv.T @ jLinv # Cinv = Linv' Linv
|
|
1005
|
+
|
|
1006
|
+
# fancy inds to front! love that.
|
|
1007
|
+
jCooinv = jCooinv.view(rank, ncoi, rank, ncoi)
|
|
1008
|
+
Cooinv[j, :, joixvix[:, None], :, joixvix[None]] = jCooinv.permute(1, 3, 0, 2)
|
|
1009
|
+
|
|
1010
|
+
obsdim = rank * nc_obs
|
|
1011
|
+
Cooinv = Cooinv.view(nneighb, obsdim, obsdim)
|
|
1012
|
+
return Cooinv
|