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,718 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import Self
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
import torch
|
|
7
|
+
import torch.nn.functional as F
|
|
8
|
+
from scipy.spatial import KDTree
|
|
9
|
+
from scipy.spatial.distance import pdist
|
|
10
|
+
from spikeinterface.core import BaseRecording
|
|
11
|
+
from torch import Tensor
|
|
12
|
+
|
|
13
|
+
from ...templates import (
|
|
14
|
+
CompressedUpsampledTemplates,
|
|
15
|
+
LowRankTemplates,
|
|
16
|
+
TemplateData,
|
|
17
|
+
compressed_upsampled_templates,
|
|
18
|
+
svd_compress_templates,
|
|
19
|
+
templates_at_time,
|
|
20
|
+
)
|
|
21
|
+
from ...util.internal_config import ComputationConfig, MatchingConfig
|
|
22
|
+
from ...util.job_util import ensure_computation_config
|
|
23
|
+
from ...util.logging_util import get_logger, DARTSORTVERBOSE
|
|
24
|
+
from ...util.spiketorch import add_at_, convolve_lowrank, grab_spikes_full
|
|
25
|
+
from .matching_base import (
|
|
26
|
+
ChunkTemplateData,
|
|
27
|
+
MatchingPeaks,
|
|
28
|
+
MatchingTemplates,
|
|
29
|
+
PconvBase,
|
|
30
|
+
)
|
|
31
|
+
from .pairwise import CompressedPairwiseConv
|
|
32
|
+
|
|
33
|
+
logger = get_logger(__name__)
|
|
34
|
+
_extra_checks = logger.isEnabledFor(DARTSORTVERBOSE)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class CompressedUpsampledMatchingTemplates(MatchingTemplates):
|
|
38
|
+
"""YASS-style individually-compressed temporally upsampled templates
|
|
39
|
+
|
|
40
|
+
Prioritizes good reconstruction over fast computation by using individual
|
|
41
|
+
SVD basis for each template. Tries to be smart with temporal upsampling,
|
|
42
|
+
using more or fewer upsamples per component, which ends up requiring a lot
|
|
43
|
+
of bookkeeping logic.
|
|
44
|
+
|
|
45
|
+
Also supports this concept of groups, where some templates are conceptually
|
|
46
|
+
identified as mutually co-exclusive / co-refractory. This is applied when
|
|
47
|
+
the input template_data has duplicates in its unit_ids in from_config, and
|
|
48
|
+
there are cases in the code to try to avoid its cost when not used.
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
template_type = "individual_compressed_upsampled"
|
|
52
|
+
|
|
53
|
+
def __init__(
|
|
54
|
+
self,
|
|
55
|
+
low_rank_templates: LowRankTemplates,
|
|
56
|
+
pconv_db: PconvBase,
|
|
57
|
+
compressed_upsampled_temporal: CompressedUpsampledTemplates,
|
|
58
|
+
trough_offset_samples: int,
|
|
59
|
+
obj_low_rank_templates: LowRankTemplates | None = None,
|
|
60
|
+
geom: np.ndarray | None = None,
|
|
61
|
+
registered_geom: np.ndarray | None = None,
|
|
62
|
+
registered_template_depths_um: np.ndarray | None = None,
|
|
63
|
+
refractory_radius_frames: int = 10,
|
|
64
|
+
motion_est=None,
|
|
65
|
+
dtype=torch.float,
|
|
66
|
+
):
|
|
67
|
+
super().__init__()
|
|
68
|
+
global _extra_checks
|
|
69
|
+
_extra_checks = logger.isEnabledFor(DARTSORTVERBOSE)
|
|
70
|
+
|
|
71
|
+
lrt = low_rank_templates
|
|
72
|
+
del low_rank_templates
|
|
73
|
+
|
|
74
|
+
# in this case there is bookkeeping to manage correspondence
|
|
75
|
+
# between coarse and fine templates
|
|
76
|
+
self.coarse_objective = obj_low_rank_templates is not None
|
|
77
|
+
self.n_templates = lrt.unit_ids.size
|
|
78
|
+
self.spike_length_samples = lrt.temporal_components.shape[1]
|
|
79
|
+
n_cupt = compressed_upsampled_temporal.n_compressed_upsampled_templates
|
|
80
|
+
self.upsampling = n_cupt > self.n_templates
|
|
81
|
+
self.comp_up_max = n_cupt
|
|
82
|
+
self.registered_template_depths_um = registered_template_depths_um
|
|
83
|
+
self.pconv_db = pconv_db
|
|
84
|
+
|
|
85
|
+
# -- store relevant arrays from LRTs and obj LRTs
|
|
86
|
+
self.svd_rank = lrt.singular_values.shape[1]
|
|
87
|
+
uids = torch.asarray(lrt.unit_ids, dtype=torch.int32)
|
|
88
|
+
tc = torch.asarray(lrt.temporal_components, dtype=dtype)
|
|
89
|
+
sv = torch.asarray(lrt.singular_values, dtype=dtype)
|
|
90
|
+
sc = torch.asarray(lrt.spatial_components, dtype=dtype)
|
|
91
|
+
if _extra_checks:
|
|
92
|
+
assert tc.isfinite().all()
|
|
93
|
+
assert sv.isfinite().all()
|
|
94
|
+
assert sc.isfinite().all()
|
|
95
|
+
self.register_buffer("unit_ids", uids)
|
|
96
|
+
self.register_buffer("temporal_comps", tc)
|
|
97
|
+
self.register_buffer("spatial_sing", sv[:, :, None] * sc)
|
|
98
|
+
self.register_buffer("padded_spatial_sing", F.pad(self.b.spatial_sing, (0, 1)))
|
|
99
|
+
if obj_low_rank_templates is None:
|
|
100
|
+
self.obj_lrts = lrt
|
|
101
|
+
self.register_buffer("obj_unit_ids", self.b.unit_ids)
|
|
102
|
+
self.register_buffer("obj_temporal_comps", self.b.temporal_comps)
|
|
103
|
+
self.register_buffer("obj_spatial_sing", self.b.spatial_sing)
|
|
104
|
+
else:
|
|
105
|
+
olrt = obj_low_rank_templates
|
|
106
|
+
self.obj_lrts = obj_low_rank_templates
|
|
107
|
+
uids = torch.asarray(olrt.temporal_components, dtype=torch.int32)
|
|
108
|
+
tc = torch.asarray(olrt.temporal_components, dtype=dtype)
|
|
109
|
+
sv = torch.asarray(olrt.singular_values, dtype=dtype)
|
|
110
|
+
sc = torch.asarray(olrt.spatial_components, dtype=dtype)
|
|
111
|
+
self.register_buffer("obj_spike_counts", uids)
|
|
112
|
+
self.register_buffer("obj_unit_ids", uids)
|
|
113
|
+
self.register_buffer("obj_temporal_comps", tc)
|
|
114
|
+
self.register_buffer("obj_spatial_sing", sv[:, :, None] * sc)
|
|
115
|
+
self.obj_n_templates = len(self.b.obj_unit_ids)
|
|
116
|
+
|
|
117
|
+
# -- geometry, as needed
|
|
118
|
+
self.drifting = motion_est is not None and np.any(motion_est.displacement)
|
|
119
|
+
if self.drifting:
|
|
120
|
+
assert geom is not None
|
|
121
|
+
assert registered_geom is not None
|
|
122
|
+
self.geom = geom
|
|
123
|
+
self.geom_kdtree = KDTree(geom)
|
|
124
|
+
self.rgeom = registered_geom
|
|
125
|
+
self.motion_est = motion_est
|
|
126
|
+
self.match_rad = pdist(geom).min() / 1.5
|
|
127
|
+
self.n_channels_full = len(self.rgeom)
|
|
128
|
+
self.n_channels = len(geom)
|
|
129
|
+
else:
|
|
130
|
+
self.geom_kdtree = self.geom = self.motion_est = self.rgeom = None
|
|
131
|
+
self.n_channels = self.b.spatial_sing.shape[2]
|
|
132
|
+
self.n_channels_full = self.n_channels
|
|
133
|
+
self.match_rad = None
|
|
134
|
+
self.check_shapes()
|
|
135
|
+
|
|
136
|
+
# -- upsampled temporal bases
|
|
137
|
+
cupt = compressed_upsampled_temporal
|
|
138
|
+
cup_map = torch.asarray(cupt.compressed_upsampling_map)
|
|
139
|
+
cup_index = torch.asarray(cupt.compressed_upsampling_index)
|
|
140
|
+
cup_ix_to_up_ix = torch.asarray(cupt.compressed_index_to_upsampling_index)
|
|
141
|
+
cup_temporal = torch.asarray(cupt.compressed_upsampled_templates)
|
|
142
|
+
self.register_buffer("cup_map", cup_map)
|
|
143
|
+
self.register_buffer("cup_index", cup_index)
|
|
144
|
+
self.register_buffer("cup_ix_to_up_ix", cup_ix_to_up_ix)
|
|
145
|
+
self.register_buffer("cup_temporal", cup_temporal)
|
|
146
|
+
|
|
147
|
+
# -- template grouping and coarse objective indexing
|
|
148
|
+
gres = handle_template_groups(
|
|
149
|
+
self.obj_unit_ids, self.unit_ids, self.coarse_objective
|
|
150
|
+
)
|
|
151
|
+
self.have_groups, group_index, fine_to_coarse, coarse_index = gres
|
|
152
|
+
self.register_buffer_or_none("group_index", group_index)
|
|
153
|
+
self.register_buffer_or_none("fine_to_coarse", fine_to_coarse)
|
|
154
|
+
self.register_buffer_or_none("coarse_index", coarse_index)
|
|
155
|
+
|
|
156
|
+
# aux bufs
|
|
157
|
+
rr = refractory_radius_frames
|
|
158
|
+
self.register_buffer("refrac_ix", torch.arange(-rr, rr + 1))
|
|
159
|
+
self.register_buffer("rank_ix", torch.arange(self.svd_rank))
|
|
160
|
+
sls = self.spike_length_samples
|
|
161
|
+
self.register_buffer("time_ix", torch.arange(sls))
|
|
162
|
+
self.register_buffer("chan_ix", torch.arange(self.n_channels))
|
|
163
|
+
self.register_buffer("conv_lags", torch.arange(-sls + 1, sls))
|
|
164
|
+
|
|
165
|
+
@property
|
|
166
|
+
def device(self) -> torch.device:
|
|
167
|
+
return self.b.refrac_ix.device
|
|
168
|
+
|
|
169
|
+
def check_shapes(self):
|
|
170
|
+
assert self.b.temporal_comps.shape == (
|
|
171
|
+
self.n_templates,
|
|
172
|
+
self.spike_length_samples,
|
|
173
|
+
self.svd_rank,
|
|
174
|
+
)
|
|
175
|
+
assert self.b.spatial_sing.shape == (
|
|
176
|
+
self.n_templates,
|
|
177
|
+
self.svd_rank,
|
|
178
|
+
self.n_channels_full,
|
|
179
|
+
)
|
|
180
|
+
assert self.unit_ids.shape == (self.n_templates,)
|
|
181
|
+
|
|
182
|
+
@classmethod
|
|
183
|
+
def _from_config(
|
|
184
|
+
cls,
|
|
185
|
+
save_folder: Path,
|
|
186
|
+
recording: BaseRecording,
|
|
187
|
+
template_data: TemplateData,
|
|
188
|
+
matching_cfg: MatchingConfig,
|
|
189
|
+
computation_cfg: ComputationConfig | None = None,
|
|
190
|
+
motion_est=None,
|
|
191
|
+
whitener: Tensor | None = None,
|
|
192
|
+
overwrite: bool = False,
|
|
193
|
+
dtype: torch.dtype = torch.float,
|
|
194
|
+
) -> Self:
|
|
195
|
+
assert matching_cfg.up_method == "direct"
|
|
196
|
+
assert whitener is None
|
|
197
|
+
computation_cfg = ensure_computation_config(computation_cfg)
|
|
198
|
+
|
|
199
|
+
unit_ids, id_counts = np.unique(template_data.unit_ids, return_counts=True)
|
|
200
|
+
have_groups = np.any(id_counts > 1)
|
|
201
|
+
coarse_objective = matching_cfg.coarse_objective and have_groups
|
|
202
|
+
|
|
203
|
+
lrt = svd_compress_templates(
|
|
204
|
+
template_data,
|
|
205
|
+
min_channel_amplitude=matching_cfg.template_min_channel_amplitude,
|
|
206
|
+
rank=matching_cfg.template_svd_compression_rank,
|
|
207
|
+
computation_cfg=computation_cfg,
|
|
208
|
+
)
|
|
209
|
+
cupt = compressed_upsampled_templates(
|
|
210
|
+
lrt.temporal_components,
|
|
211
|
+
ptps=np.ptp(template_data.templates, axis=1).max(1),
|
|
212
|
+
max_upsample=matching_cfg.template_temporal_upsampling_factor,
|
|
213
|
+
n_upsamples_map=matching_cfg.upsampling_compression_map,
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
if coarse_objective:
|
|
217
|
+
obj_td = template_data.coarsen()
|
|
218
|
+
obj_lrt = svd_compress_templates(
|
|
219
|
+
obj_td,
|
|
220
|
+
min_channel_amplitude=matching_cfg.template_min_channel_amplitude,
|
|
221
|
+
rank=matching_cfg.template_svd_compression_rank,
|
|
222
|
+
)
|
|
223
|
+
else:
|
|
224
|
+
obj_td = obj_lrt = None
|
|
225
|
+
|
|
226
|
+
T_samples = recording.get_num_samples()
|
|
227
|
+
dt = matching_cfg.chunk_length_samples
|
|
228
|
+
chunk_starts = np.arange(0, T_samples, dt)
|
|
229
|
+
chunk_ends = np.minimum(chunk_starts + dt, T_samples)
|
|
230
|
+
chunk_centers_samples = (chunk_starts + chunk_ends) / 2
|
|
231
|
+
chunk_centers_s = recording.sample_index_to_time(chunk_centers_samples)
|
|
232
|
+
geom = recording.get_channel_locations()
|
|
233
|
+
if coarse_objective:
|
|
234
|
+
pconv_td = obj_td
|
|
235
|
+
pconv_lrt = obj_lrt
|
|
236
|
+
else:
|
|
237
|
+
pconv_td = template_data
|
|
238
|
+
pconv_lrt = lrt
|
|
239
|
+
assert pconv_td is not None
|
|
240
|
+
assert pconv_lrt is not None
|
|
241
|
+
drifting = motion_est is not None and np.any(motion_est.displacement)
|
|
242
|
+
pairwise_conv_db = CompressedPairwiseConv.from_template_data(
|
|
243
|
+
save_folder / "pconv.h5",
|
|
244
|
+
template_data=pconv_td,
|
|
245
|
+
low_rank_templates=pconv_lrt,
|
|
246
|
+
template_data_b=template_data if coarse_objective else None,
|
|
247
|
+
low_rank_templates_b=lrt if coarse_objective else None,
|
|
248
|
+
compressed_upsampled_temporal=cupt,
|
|
249
|
+
chunk_time_centers_s=chunk_centers_s,
|
|
250
|
+
motion_est=motion_est if drifting else None,
|
|
251
|
+
geom=geom,
|
|
252
|
+
computation_cfg=computation_cfg,
|
|
253
|
+
overwrite=overwrite,
|
|
254
|
+
)
|
|
255
|
+
return cls(
|
|
256
|
+
low_rank_templates=lrt,
|
|
257
|
+
compressed_upsampled_temporal=cupt,
|
|
258
|
+
refractory_radius_frames=matching_cfg.refractory_radius_frames,
|
|
259
|
+
trough_offset_samples=template_data.trough_offset_samples,
|
|
260
|
+
geom=geom,
|
|
261
|
+
registered_geom=template_data.registered_geom,
|
|
262
|
+
registered_template_depths_um=template_data.registered_depths_um(),
|
|
263
|
+
pconv_db=pairwise_conv_db,
|
|
264
|
+
motion_est=motion_est,
|
|
265
|
+
dtype=dtype,
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
def data_at_time(
|
|
269
|
+
self,
|
|
270
|
+
t_s: float,
|
|
271
|
+
scaling: bool,
|
|
272
|
+
inv_lambda: float,
|
|
273
|
+
scale_min: float,
|
|
274
|
+
scale_max: float,
|
|
275
|
+
) -> "CompressedUpsampledChunkTemplateData":
|
|
276
|
+
if self.drifting:
|
|
277
|
+
shifts, padded_spatial_sing = templates_at_time(
|
|
278
|
+
t_s,
|
|
279
|
+
self.b.spatial_sing,
|
|
280
|
+
self.geom,
|
|
281
|
+
registered_template_depths_um=self.registered_template_depths_um,
|
|
282
|
+
registered_geom=self.rgeom,
|
|
283
|
+
motion_est=self.motion_est,
|
|
284
|
+
return_pitch_shifts=True,
|
|
285
|
+
geom_kdtree=self.geom_kdtree,
|
|
286
|
+
match_distance=self.match_rad,
|
|
287
|
+
return_padded=True,
|
|
288
|
+
fill_value=0.0,
|
|
289
|
+
)
|
|
290
|
+
shifts = torch.asarray(shifts, device=self.device)
|
|
291
|
+
padded_spatial_sing = torch.asarray(padded_spatial_sing, device=self.device)
|
|
292
|
+
else:
|
|
293
|
+
shifts = None
|
|
294
|
+
padded_spatial_sing = self.b.padded_spatial_sing
|
|
295
|
+
|
|
296
|
+
normsq_chan = padded_spatial_sing.square().sum(dim=1)
|
|
297
|
+
main_channels = normsq_chan.argmax(dim=1)
|
|
298
|
+
normsq = normsq_chan.sum(dim=1)
|
|
299
|
+
|
|
300
|
+
if self.drifting and self.coarse_objective:
|
|
301
|
+
obj_shifts, obj_spatial_sing = templates_at_time(
|
|
302
|
+
t_s,
|
|
303
|
+
self.b.spatial_sing,
|
|
304
|
+
self.geom,
|
|
305
|
+
registered_template_depths_um=self.registered_template_depths_um,
|
|
306
|
+
registered_geom=self.rgeom,
|
|
307
|
+
motion_est=self.motion_est,
|
|
308
|
+
return_pitch_shifts=True,
|
|
309
|
+
geom_kdtree=self.geom_kdtree,
|
|
310
|
+
match_distance=self.match_rad,
|
|
311
|
+
return_padded=False,
|
|
312
|
+
)
|
|
313
|
+
obj_shifts = torch.asarray(shifts, device=self.device)
|
|
314
|
+
obj_spatial_sing = torch.asarray(obj_spatial_sing, device=self.device)
|
|
315
|
+
obj_normsq = obj_spatial_sing.square().sum(dim=(1, 2))
|
|
316
|
+
else:
|
|
317
|
+
obj_shifts = shifts
|
|
318
|
+
obj_spatial_sing = padded_spatial_sing[..., :-1]
|
|
319
|
+
obj_normsq = normsq
|
|
320
|
+
|
|
321
|
+
return CompressedUpsampledChunkTemplateData(
|
|
322
|
+
coarse_objective=self.coarse_objective,
|
|
323
|
+
grouping=self.have_groups,
|
|
324
|
+
upsampling=self.upsampling,
|
|
325
|
+
scaling=scaling,
|
|
326
|
+
needs_fine_pass=self.have_groups or self.upsampling,
|
|
327
|
+
comp_up_max=self.comp_up_max,
|
|
328
|
+
n_templates=self.n_templates,
|
|
329
|
+
obj_n_templates=self.obj_n_templates,
|
|
330
|
+
spike_length_samples=self.spike_length_samples,
|
|
331
|
+
up_factor=self.b.cup_index.shape[1],
|
|
332
|
+
inv_lambda=torch.tensor(inv_lambda, device=normsq.device),
|
|
333
|
+
scale_min=torch.tensor(scale_min, device=normsq.device),
|
|
334
|
+
scale_max=torch.tensor(scale_max, device=normsq.device),
|
|
335
|
+
obj_normsq=obj_normsq,
|
|
336
|
+
obj_temporal_comps=self.b.obj_temporal_comps,
|
|
337
|
+
obj_spatial_sing=obj_spatial_sing,
|
|
338
|
+
temporal_comps=self.b.temporal_comps,
|
|
339
|
+
spatial_sing=padded_spatial_sing[..., :-1],
|
|
340
|
+
padded_spatial_sing=padded_spatial_sing,
|
|
341
|
+
cup_temporal=self.b.cup_temporal,
|
|
342
|
+
normsq=normsq,
|
|
343
|
+
cup_index=self.b.cup_index,
|
|
344
|
+
cup_map=self.b.cup_map,
|
|
345
|
+
cup_ix_to_up_ix=self.b.cup_ix_to_up_ix,
|
|
346
|
+
coarse_index=self.b.coarse_index,
|
|
347
|
+
group_index=self.b.group_index,
|
|
348
|
+
unit_ids=self.b.unit_ids,
|
|
349
|
+
fine_to_coarse=self.b.fine_to_coarse,
|
|
350
|
+
main_channels=main_channels,
|
|
351
|
+
conv_lags=self.b.conv_lags,
|
|
352
|
+
refrac_ix=self.b.refrac_ix,
|
|
353
|
+
rank_ix=self.b.rank_ix,
|
|
354
|
+
time_ix=self.b.time_ix,
|
|
355
|
+
chan_ix=self.b.chan_ix,
|
|
356
|
+
pconv_db=self.pconv_db,
|
|
357
|
+
shifts_a=shifts,
|
|
358
|
+
shifts_b=obj_shifts,
|
|
359
|
+
)
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
@dataclass(kw_only=True, slots=True, frozen=True, repr=False, eq=False)
|
|
363
|
+
class CompressedUpsampledChunkTemplateData(ChunkTemplateData):
|
|
364
|
+
coarse_objective: bool
|
|
365
|
+
grouping: bool
|
|
366
|
+
upsampling: bool
|
|
367
|
+
scaling: bool
|
|
368
|
+
needs_fine_pass: bool
|
|
369
|
+
comp_up_max: int
|
|
370
|
+
n_templates: int
|
|
371
|
+
obj_n_templates: int
|
|
372
|
+
spike_length_samples: int
|
|
373
|
+
up_factor: int
|
|
374
|
+
inv_lambda: Tensor
|
|
375
|
+
scale_min: Tensor
|
|
376
|
+
scale_max: Tensor
|
|
377
|
+
|
|
378
|
+
# objective props
|
|
379
|
+
obj_normsq: Tensor
|
|
380
|
+
obj_temporal_comps: Tensor
|
|
381
|
+
obj_spatial_sing: Tensor
|
|
382
|
+
temporal_comps: Tensor
|
|
383
|
+
spatial_sing: Tensor
|
|
384
|
+
padded_spatial_sing: Tensor
|
|
385
|
+
cup_temporal: Tensor
|
|
386
|
+
normsq: Tensor
|
|
387
|
+
|
|
388
|
+
# indexing
|
|
389
|
+
cup_index: Tensor
|
|
390
|
+
cup_map: Tensor
|
|
391
|
+
cup_ix_to_up_ix: Tensor
|
|
392
|
+
coarse_index: Tensor
|
|
393
|
+
group_index: Tensor | None
|
|
394
|
+
unit_ids: Tensor
|
|
395
|
+
fine_to_coarse: Tensor
|
|
396
|
+
main_channels: Tensor
|
|
397
|
+
conv_lags: Tensor
|
|
398
|
+
refrac_ix: Tensor
|
|
399
|
+
rank_ix: Tensor
|
|
400
|
+
time_ix: Tensor
|
|
401
|
+
chan_ix: Tensor
|
|
402
|
+
|
|
403
|
+
# pconv
|
|
404
|
+
pconv_db: PconvBase
|
|
405
|
+
shifts_a: Tensor | None
|
|
406
|
+
shifts_b: Tensor | None
|
|
407
|
+
|
|
408
|
+
def convolve(self, traces, padding=0, out=None):
|
|
409
|
+
"""Convolve the objective templates with traces."""
|
|
410
|
+
return convolve_lowrank(
|
|
411
|
+
traces,
|
|
412
|
+
self.obj_spatial_sing,
|
|
413
|
+
self.obj_temporal_comps,
|
|
414
|
+
padding=padding,
|
|
415
|
+
out=out,
|
|
416
|
+
)
|
|
417
|
+
|
|
418
|
+
def subtract_conv(self, conv, peaks, padding=0, batch_size=256, sign=-1):
|
|
419
|
+
if not peaks.n_spikes:
|
|
420
|
+
return
|
|
421
|
+
assert peaks.times is not None
|
|
422
|
+
assert peaks.template_inds is not None
|
|
423
|
+
for batch_start in range(0, peaks.n_spikes, batch_size):
|
|
424
|
+
batch_end = min(batch_start + batch_size, peaks.n_spikes)
|
|
425
|
+
temp_inds_b = peaks.template_inds[batch_start:batch_end]
|
|
426
|
+
if peaks.up_inds is None:
|
|
427
|
+
up_inds = torch.zeros_like(temp_inds_b)
|
|
428
|
+
else:
|
|
429
|
+
up_inds = peaks.up_inds[batch_start:batch_end]
|
|
430
|
+
if peaks.scalings is None:
|
|
431
|
+
scalings = None
|
|
432
|
+
else:
|
|
433
|
+
scalings = peaks.scalings[batch_start:batch_end]
|
|
434
|
+
template_indices_a, pconvs, which_b = self.pconv_db.query(
|
|
435
|
+
template_indices_a=None,
|
|
436
|
+
template_indices_b=temp_inds_b,
|
|
437
|
+
upsampling_indices_b=up_inds,
|
|
438
|
+
shifts_a=self.shifts_a,
|
|
439
|
+
shifts_b=self.shifts_b[temp_inds_b]
|
|
440
|
+
if self.shifts_b is not None
|
|
441
|
+
else None,
|
|
442
|
+
scalings_b=scalings,
|
|
443
|
+
)
|
|
444
|
+
pconvs = pconvs.to(conv.device)
|
|
445
|
+
times_sub = peaks.times[batch_start:batch_end][which_b]
|
|
446
|
+
ix_template = template_indices_a[:, None]
|
|
447
|
+
ix_time = times_sub[:, None] + (padding + self.conv_lags)[None, :]
|
|
448
|
+
add_at_(conv, (ix_template, ix_time), pconvs, sign=sign)
|
|
449
|
+
|
|
450
|
+
def subtract(self, traces, peaks, sign=-1):
|
|
451
|
+
"""Subtract templates from traces."""
|
|
452
|
+
if not peaks.n_spikes:
|
|
453
|
+
return
|
|
454
|
+
assert peaks.times is not None
|
|
455
|
+
assert peaks.template_inds is not None
|
|
456
|
+
if peaks.up_inds is None:
|
|
457
|
+
assert self.cup_map.shape[1] == 1
|
|
458
|
+
compressed_up_inds = self.cup_map[peaks.template_inds][:, 0]
|
|
459
|
+
else:
|
|
460
|
+
compressed_up_inds = self.cup_map[peaks.template_inds, peaks.up_inds]
|
|
461
|
+
if peaks.scalings is not None:
|
|
462
|
+
batch_templates = torch.einsum(
|
|
463
|
+
"n,nrc,ntr->ntc",
|
|
464
|
+
peaks.scalings,
|
|
465
|
+
self.spatial_sing[peaks.template_inds],
|
|
466
|
+
self.cup_temporal[compressed_up_inds],
|
|
467
|
+
)
|
|
468
|
+
else:
|
|
469
|
+
batch_templates = torch.bmm(
|
|
470
|
+
self.cup_temporal[compressed_up_inds],
|
|
471
|
+
self.spatial_sing[peaks.template_inds],
|
|
472
|
+
)
|
|
473
|
+
time_ix = peaks.times[:, None, None] + self.time_ix[None, :, None]
|
|
474
|
+
add_at_(
|
|
475
|
+
traces, (time_ix, self.chan_ix[None, None, :]), batch_templates, sign=sign
|
|
476
|
+
)
|
|
477
|
+
|
|
478
|
+
def fine_match(
|
|
479
|
+
self, *, peaks: MatchingPeaks, residual: Tensor, conv: Tensor, padding: int = 0
|
|
480
|
+
):
|
|
481
|
+
"""Determine superres ids, temporal upsampling, and scaling
|
|
482
|
+
|
|
483
|
+
Given coarse matches (unit ids at times) and the current residual,
|
|
484
|
+
pick the best superres template, the best temporal offset, and the
|
|
485
|
+
best amplitude scaling.
|
|
486
|
+
|
|
487
|
+
We used to upsample the objective to figure out the temporal upsampling,
|
|
488
|
+
but with superres in the picture we are now not computing the objective
|
|
489
|
+
using the same templates that we temporally upsample. So, instead
|
|
490
|
+
we use a greedy strategy: first pick the best (non-temporally upsampled)
|
|
491
|
+
superres template, then pick the upsampling and scaling at the same time.
|
|
492
|
+
These are all done by dotting everything and computing the objective,
|
|
493
|
+
which is probably more expensive than what we had before.
|
|
494
|
+
|
|
495
|
+
Returns
|
|
496
|
+
-------
|
|
497
|
+
time_shifts : Optional[array]
|
|
498
|
+
up_inds : Optional[array]
|
|
499
|
+
scalings : Optional[array]
|
|
500
|
+
template_inds : array
|
|
501
|
+
objs : array
|
|
502
|
+
"""
|
|
503
|
+
if not self.needs_fine_pass:
|
|
504
|
+
return peaks
|
|
505
|
+
if not peaks.n_spikes:
|
|
506
|
+
return peaks
|
|
507
|
+
del conv, padding # unused
|
|
508
|
+
|
|
509
|
+
if self.coarse_objective or self.upsampling:
|
|
510
|
+
residual_snips = grab_spikes_full(
|
|
511
|
+
residual,
|
|
512
|
+
peaks.times,
|
|
513
|
+
trough_offset=0,
|
|
514
|
+
spike_length_samples=self.spike_length_samples + 1,
|
|
515
|
+
)
|
|
516
|
+
else:
|
|
517
|
+
residual_snips = None
|
|
518
|
+
|
|
519
|
+
if self.coarse_objective:
|
|
520
|
+
assert residual_snips is not None
|
|
521
|
+
# TODO best I came up with, but it still syncs
|
|
522
|
+
# can refactor with a jagged/nested tensor. not prioritizing since
|
|
523
|
+
# grouped is rare for now.
|
|
524
|
+
superres_ix = self.coarse_index[peaks.template_inds]
|
|
525
|
+
dup_ix, column_ix = (superres_ix < self.n_templates).nonzero(as_tuple=True)
|
|
526
|
+
template_inds = superres_ix[dup_ix, column_ix]
|
|
527
|
+
convs = torch.einsum(
|
|
528
|
+
"ntr,ntc,nrc->n",
|
|
529
|
+
self.temporal_comps[template_inds],
|
|
530
|
+
residual_snips[dup_ix],
|
|
531
|
+
self.spatial_sing[template_inds],
|
|
532
|
+
)
|
|
533
|
+
norms = self.normsq[template_inds]
|
|
534
|
+
objs = convs.new_full(superres_ix.shape, -torch.inf)
|
|
535
|
+
objs[dup_ix, column_ix] = torch.add(norms.neg(), convs, alpha=2.0)
|
|
536
|
+
objs, best_column_ix = objs.max(dim=1)
|
|
537
|
+
row_ix = torch.arange(best_column_ix.numel(), device=best_column_ix.device)
|
|
538
|
+
template_inds = superres_ix[row_ix, best_column_ix]
|
|
539
|
+
else:
|
|
540
|
+
template_inds = peaks.template_inds
|
|
541
|
+
norms = self.normsq[template_inds]
|
|
542
|
+
objs = peaks.scores
|
|
543
|
+
|
|
544
|
+
if not self.upsampling:
|
|
545
|
+
return MatchingPeaks(
|
|
546
|
+
times=peaks.times,
|
|
547
|
+
obj_template_inds=peaks.obj_template_inds,
|
|
548
|
+
template_inds=template_inds,
|
|
549
|
+
scalings=peaks.scalings,
|
|
550
|
+
scores=objs,
|
|
551
|
+
)
|
|
552
|
+
assert residual_snips is not None
|
|
553
|
+
assert template_inds is not None
|
|
554
|
+
|
|
555
|
+
# get the objective for snips now and one step back
|
|
556
|
+
# TODO: jagged tensor? no-compression mode?
|
|
557
|
+
comp_up_ix = self.cup_index[template_inds]
|
|
558
|
+
dup_ix, column_ix = (comp_up_ix < self.comp_up_max).nonzero(as_tuple=True)
|
|
559
|
+
comp_up_indices = comp_up_ix[dup_ix, column_ix]
|
|
560
|
+
temps_t = self.cup_temporal[comp_up_indices]
|
|
561
|
+
temps_s = self.spatial_sing[template_inds[dup_ix]]
|
|
562
|
+
snips_dup_dt = residual_snips[dup_ix].unfold(1, self.spike_length_samples, 1)
|
|
563
|
+
convs = torch.einsum("ndct,ntr,nrc->nd", snips_dup_dt, temps_t, temps_s)
|
|
564
|
+
norms = norms[dup_ix]
|
|
565
|
+
if self.scaling:
|
|
566
|
+
b = convs + self.inv_lambda
|
|
567
|
+
a = norms[:, None] + self.inv_lambda
|
|
568
|
+
scalings = b.div(a).clip_(self.scale_min, self.scale_max)
|
|
569
|
+
# 2sb - s^2a - 1/l
|
|
570
|
+
scalingsqa = scalings.square().mul_(-a)
|
|
571
|
+
objs = scalingsqa.addcmul_(scalings, b, value=2.0).sub_(self.inv_lambda)
|
|
572
|
+
del convs, scalingsqa
|
|
573
|
+
else:
|
|
574
|
+
objs = torch.add(-norms[:, None], convs, alpha=2.0)
|
|
575
|
+
scalings = None
|
|
576
|
+
del convs
|
|
577
|
+
# this is just for numerical duplicates encountered in testing.
|
|
578
|
+
objs += (column_ix == 0).float()[:, None] * 1e-5
|
|
579
|
+
objs_, better_dt = objs.max(dim=1)
|
|
580
|
+
objs = objs.new_full(comp_up_ix.shape, -torch.inf)
|
|
581
|
+
objs[dup_ix, column_ix] = objs_
|
|
582
|
+
objs, best_column_ix = objs.max(dim=1)
|
|
583
|
+
|
|
584
|
+
comp_up_indices = comp_up_ix.take_along_dim(
|
|
585
|
+
dim=1, indices=best_column_ix[:, None]
|
|
586
|
+
)
|
|
587
|
+
comp_up_indices = comp_up_indices[:, 0]
|
|
588
|
+
up_inds = self.cup_ix_to_up_ix[comp_up_indices]
|
|
589
|
+
|
|
590
|
+
# prev convs were one step earlier
|
|
591
|
+
time_shifts = comp_up_ix.new_full(comp_up_ix.shape, 0)
|
|
592
|
+
time_shifts[dup_ix, column_ix] += better_dt.long()
|
|
593
|
+
time_shifts = time_shifts.take_along_dim(dim=1, indices=best_column_ix[:, None])
|
|
594
|
+
time_shifts = time_shifts[:, 0]
|
|
595
|
+
if self.scaling:
|
|
596
|
+
assert scalings is not None
|
|
597
|
+
scalings_ = scalings.take_along_dim(indices=better_dt[:, None], dim=1)[:, 0]
|
|
598
|
+
scalings = scalings_.new_zeros(comp_up_ix.shape)
|
|
599
|
+
scalings[dup_ix, column_ix] = scalings_
|
|
600
|
+
scalings = scalings.take_along_dim(dim=1, indices=best_column_ix[:, None])
|
|
601
|
+
scalings = scalings[:, 0]
|
|
602
|
+
|
|
603
|
+
assert peaks.times is not None
|
|
604
|
+
times = peaks.times + time_shifts
|
|
605
|
+
up_half = self.up_factor // 2
|
|
606
|
+
time_shifts = (up_inds > up_half).long().neg_()
|
|
607
|
+
return MatchingPeaks(
|
|
608
|
+
times=times,
|
|
609
|
+
obj_template_inds=peaks.obj_template_inds,
|
|
610
|
+
template_inds=template_inds,
|
|
611
|
+
up_inds=up_inds,
|
|
612
|
+
scalings=scalings,
|
|
613
|
+
scores=objs,
|
|
614
|
+
time_shifts=time_shifts,
|
|
615
|
+
)
|
|
616
|
+
|
|
617
|
+
def get_clean_waveforms(
|
|
618
|
+
self,
|
|
619
|
+
peaks: MatchingPeaks,
|
|
620
|
+
channels: Tensor,
|
|
621
|
+
channel_index: Tensor,
|
|
622
|
+
add_into: Tensor | None = None,
|
|
623
|
+
):
|
|
624
|
+
if not peaks.n_spikes:
|
|
625
|
+
return add_into
|
|
626
|
+
assert peaks.template_inds is not None
|
|
627
|
+
spatial = self.padded_spatial_sing[
|
|
628
|
+
peaks.template_inds[:, None, None],
|
|
629
|
+
self.rank_ix[None, :, None],
|
|
630
|
+
channel_index[channels][:, None, :],
|
|
631
|
+
]
|
|
632
|
+
if peaks.scalings is not None:
|
|
633
|
+
spatial.mul_(peaks.scalings[:, None, None])
|
|
634
|
+
if peaks.up_inds is None:
|
|
635
|
+
assert self.cup_map.shape[1] == 1
|
|
636
|
+
comp_up_ix = self.cup_map[peaks.template_inds][:, 0]
|
|
637
|
+
else:
|
|
638
|
+
comp_up_ix = self.cup_map[peaks.template_inds, peaks.up_inds]
|
|
639
|
+
temporal = self.cup_temporal[comp_up_ix]
|
|
640
|
+
if add_into is None:
|
|
641
|
+
return temporal.bmm(spatial)
|
|
642
|
+
else:
|
|
643
|
+
return add_into.baddbmm_(temporal, spatial)
|
|
644
|
+
|
|
645
|
+
def _enforce_refractory(self, mask, peaks, offset=0, value=-torch.inf):
|
|
646
|
+
if not peaks.n_spikes:
|
|
647
|
+
return
|
|
648
|
+
assert peaks.times is not None
|
|
649
|
+
time_ix = peaks.times[:, None] + (self.refrac_ix[None, :] + offset)
|
|
650
|
+
if self.coarse_objective:
|
|
651
|
+
assert peaks.obj_template_inds is not None
|
|
652
|
+
row_ix = peaks.obj_template_inds[:, None]
|
|
653
|
+
elif self.grouping:
|
|
654
|
+
assert peaks.template_inds is not None
|
|
655
|
+
assert self.group_index is not None
|
|
656
|
+
row_ix = self.group_index[peaks.template_inds]
|
|
657
|
+
row_ix = row_ix[:, :, None]
|
|
658
|
+
time_ix = time_ix[:, None, :]
|
|
659
|
+
else:
|
|
660
|
+
assert peaks.template_inds is not None
|
|
661
|
+
row_ix = peaks.template_inds[:, None]
|
|
662
|
+
mask[row_ix, time_ix] = value
|
|
663
|
+
|
|
664
|
+
def reconstruct_up_templates(self):
|
|
665
|
+
up_comps = self.cup_temporal[self.cup_map].cpu()
|
|
666
|
+
return torch.einsum("nrc,nutr->nutc", self.spatial_sing.cpu(), up_comps)
|
|
667
|
+
|
|
668
|
+
|
|
669
|
+
def handle_template_groups(obj_unit_ids, unit_ids, coarse_objective: bool):
|
|
670
|
+
"""Grouped templates in objective
|
|
671
|
+
|
|
672
|
+
If not coarse_objective, then several rows of the objective may
|
|
673
|
+
belong to the same unit. They must be handled together when imposing
|
|
674
|
+
refractory conditions.
|
|
675
|
+
"""
|
|
676
|
+
_u, fine_to_coarse, counts = unit_ids.unique(
|
|
677
|
+
return_inverse=True, return_counts=True
|
|
678
|
+
)
|
|
679
|
+
max_group_size = counts.max()
|
|
680
|
+
have_groups = max_group_size > 1
|
|
681
|
+
|
|
682
|
+
if not have_groups:
|
|
683
|
+
assert not coarse_objective
|
|
684
|
+
return have_groups, None, None, None
|
|
685
|
+
|
|
686
|
+
if have_groups and not coarse_objective:
|
|
687
|
+
# refractory enforcement helper array for grouped non-coarse objective
|
|
688
|
+
# like a channel index, sort of
|
|
689
|
+
# this is a n_templates x group_size array that maps each
|
|
690
|
+
# template index to the set of other template indices that
|
|
691
|
+
# are part of its group. so that the array is not ragged,
|
|
692
|
+
# we pad rows with -1s when their group is smaller than the
|
|
693
|
+
# largest group.
|
|
694
|
+
_u, counts = unit_ids.unique(return_counts=True)
|
|
695
|
+
assert torch.equal(_u, unit_ids)
|
|
696
|
+
assert (_u >= 0).all()
|
|
697
|
+
group_index = torch.full((len(unit_ids), counts.max()), -1)
|
|
698
|
+
for j, u in enumerate(unit_ids):
|
|
699
|
+
(row,) = torch.nonzero(unit_ids == u, as_tuple=True)
|
|
700
|
+
group_index[j, : len(row)] = row
|
|
701
|
+
|
|
702
|
+
return have_groups, group_index, None, None
|
|
703
|
+
|
|
704
|
+
# coarse objective with groups
|
|
705
|
+
assert obj_unit_ids is not None
|
|
706
|
+
assert have_groups # otherwise coarse_objective would be false.
|
|
707
|
+
_u, fine_to_coarse, counts = torch.unique(
|
|
708
|
+
unit_ids, return_counts=True, return_inverse=True
|
|
709
|
+
)
|
|
710
|
+
assert torch.equal(_u, obj_unit_ids)
|
|
711
|
+
assert (_u >= 0).all()
|
|
712
|
+
|
|
713
|
+
coarse_index = torch.full((len(obj_unit_ids), counts.max()), len(unit_ids))
|
|
714
|
+
for j, u in enumerate(obj_unit_ids):
|
|
715
|
+
(group,) = torch.nonzero(unit_ids == u, as_tuple=True)
|
|
716
|
+
coarse_index[j, : len(group)] = group
|
|
717
|
+
|
|
718
|
+
return have_groups, None, fine_to_coarse, coarse_index
|