DARTsort 0.3.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. dartsort/__init__.py +68 -0
  2. dartsort/cli.py +105 -0
  3. dartsort/clustering/__init__.py +3 -0
  4. dartsort/clustering/cluster_util.py +392 -0
  5. dartsort/clustering/clustering.py +765 -0
  6. dartsort/clustering/clustering_features.py +202 -0
  7. dartsort/clustering/density.py +1299 -0
  8. dartsort/clustering/forward_backward.py +219 -0
  9. dartsort/clustering/gmm/mixture.py +5790 -0
  10. dartsort/clustering/gmm/stable_features.py +993 -0
  11. dartsort/clustering/kmeans.py +448 -0
  12. dartsort/clustering/merge.py +501 -0
  13. dartsort/clustering/modes.py +214 -0
  14. dartsort/clustering/refine_util.py +191 -0
  15. dartsort/clustering/relocate.py +109 -0
  16. dartsort/clustering/split.py +1285 -0
  17. dartsort/config.py +288 -0
  18. dartsort/detect/__init__.py +2 -0
  19. dartsort/detect/detect.py +449 -0
  20. dartsort/detect/detect_filters.py +37 -0
  21. dartsort/evaluate/__init__.py +1 -0
  22. dartsort/evaluate/analysis.py +594 -0
  23. dartsort/evaluate/comparison.py +534 -0
  24. dartsort/evaluate/hybrid_util.py +599 -0
  25. dartsort/evaluate/noise_recording_tools.py +184 -0
  26. dartsort/evaluate/sim_template_tools.py +842 -0
  27. dartsort/evaluate/simkit.py +840 -0
  28. dartsort/evaluate/simlib.py +270 -0
  29. dartsort/localize/__init__.py +0 -0
  30. dartsort/localize/localize_torch.py +291 -0
  31. dartsort/localize/localize_util.py +313 -0
  32. dartsort/main.py +657 -0
  33. dartsort/peel/__init__.py +6 -0
  34. dartsort/peel/grab.py +208 -0
  35. dartsort/peel/matching.py +628 -0
  36. dartsort/peel/matching_util/__init__.py +3 -0
  37. dartsort/peel/matching_util/compressed_upsampled.py +718 -0
  38. dartsort/peel/matching_util/drifty.py +791 -0
  39. dartsort/peel/matching_util/matching_base.py +616 -0
  40. dartsort/peel/matching_util/pairwise.py +359 -0
  41. dartsort/peel/matching_util/pairwise_util.py +1317 -0
  42. dartsort/peel/peel_base.py +1000 -0
  43. dartsort/peel/running_template.py +1498 -0
  44. dartsort/peel/subtract.py +1123 -0
  45. dartsort/peel/threshold.py +471 -0
  46. dartsort/peel/universal.py +191 -0
  47. dartsort/pretrained/default_temporal_kernel.npy +0 -0
  48. dartsort/pretrained/detect_np1.pt +0 -0
  49. dartsort/pretrained/detect_np2.pt +0 -0
  50. dartsort/pretrained/glasbey1024.npz +0 -0
  51. dartsort/pretrained/offset_trained_denoiser.pt +0 -0
  52. dartsort/pretrained/offset_trained_denoiser_added_temp.pt +0 -0
  53. dartsort/pretrained/offset_trained_denoiser_added_temp_boost.pt +0 -0
  54. dartsort/pretrained/original_single_chan_denoiser.pt +0 -0
  55. dartsort/pretrained/single_chan_denoiser.pt +0 -0
  56. dartsort/templates/__init__.py +11 -0
  57. dartsort/templates/get_templates.py +771 -0
  58. dartsort/templates/postprocess_util.py +555 -0
  59. dartsort/templates/realignment.py +466 -0
  60. dartsort/templates/superres_util.py +204 -0
  61. dartsort/templates/template_util.py +518 -0
  62. dartsort/templates/templates.py +528 -0
  63. dartsort/transform/__init__.py +2 -0
  64. dartsort/transform/_multichan_denoiser_kit.py +650 -0
  65. dartsort/transform/all_transformers.py +58 -0
  66. dartsort/transform/amortized_localization.py +503 -0
  67. dartsort/transform/amplitudes.py +197 -0
  68. dartsort/transform/decollider.py +635 -0
  69. dartsort/transform/enforce_decrease.py +158 -0
  70. dartsort/transform/localize.py +86 -0
  71. dartsort/transform/matching_denoiser.py +44 -0
  72. dartsort/transform/pipeline.py +391 -0
  73. dartsort/transform/single_channel_denoiser.py +153 -0
  74. dartsort/transform/single_channel_templates.py +98 -0
  75. dartsort/transform/supervised_denoiser.py +486 -0
  76. dartsort/transform/temporal_pca.py +413 -0
  77. dartsort/transform/transform_base.py +255 -0
  78. dartsort/util/__init__.py +0 -0
  79. dartsort/util/cli_util.py +179 -0
  80. dartsort/util/data_util.py +1128 -0
  81. dartsort/util/drift_util.py +1054 -0
  82. dartsort/util/internal_config.py +1074 -0
  83. dartsort/util/interpolation_util.py +1012 -0
  84. dartsort/util/job_util.py +19 -0
  85. dartsort/util/logging_util.py +91 -0
  86. dartsort/util/main_util.py +235 -0
  87. dartsort/util/more_operators.py +269 -0
  88. dartsort/util/multiprocessing_util.py +211 -0
  89. dartsort/util/nn_util.py +461 -0
  90. dartsort/util/noise_util.py +1461 -0
  91. dartsort/util/peel_util.py +223 -0
  92. dartsort/util/py_util.py +213 -0
  93. dartsort/util/registration_util.py +128 -0
  94. dartsort/util/residual_util.py +30 -0
  95. dartsort/util/sparse_util.py +81 -0
  96. dartsort/util/spikeio.py +227 -0
  97. dartsort/util/spiketorch.py +1198 -0
  98. dartsort/util/testing_util/__init__.py +0 -0
  99. dartsort/util/testing_util/matching_debug_util.py +517 -0
  100. dartsort/util/testing_util/mixture_testing_util.py +291 -0
  101. dartsort/util/torch_optimization_util.py +156 -0
  102. dartsort/util/torch_util.py +75 -0
  103. dartsort/util/universal_util.py +269 -0
  104. dartsort/util/waveform_util.py +809 -0
  105. dartsort/vis/__init__.py +19 -0
  106. dartsort/vis/analysis_plots.py +392 -0
  107. dartsort/vis/colors.py +36 -0
  108. dartsort/vis/gt.py +392 -0
  109. dartsort/vis/layout.py +149 -0
  110. dartsort/vis/metrics_util.py +23 -0
  111. dartsort/vis/mixture.py +1489 -0
  112. dartsort/vis/over_time.py +514 -0
  113. dartsort/vis/recanim.py +429 -0
  114. dartsort/vis/scatterplots.py +779 -0
  115. dartsort/vis/sorting.py +163 -0
  116. dartsort/vis/unit.py +917 -0
  117. dartsort/vis/unit_comparison.py +1017 -0
  118. dartsort/vis/versus.py +300 -0
  119. dartsort/vis/vismain.py +537 -0
  120. dartsort/vis/waveforms.py +357 -0
  121. dartsort-0.3.5.dist-info/METADATA +93 -0
  122. dartsort-0.3.5.dist-info/RECORD +125 -0
  123. dartsort-0.3.5.dist-info/WHEEL +5 -0
  124. dartsort-0.3.5.dist-info/entry_points.txt +2 -0
  125. dartsort-0.3.5.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1317 @@
1
+ from __future__ import annotations # allow forward type references
2
+
3
+ from collections import namedtuple
4
+ from dataclasses import dataclass, fields
5
+ from pathlib import Path
6
+ from typing import Iterator, Optional, Union
7
+
8
+ import h5py
9
+ import numpy as np
10
+ import torch
11
+ import torch.nn.functional as F
12
+ from scipy.spatial import KDTree
13
+ from scipy.spatial.distance import pdist
14
+ from tqdm.auto import tqdm
15
+
16
+ from ...templates.template_util import LowRankTemplates, CompressedUpsampledTemplates
17
+ from ...templates.templates import TemplateData
18
+ from ...util import drift_util
19
+ from ...util.multiprocessing_util import get_pool
20
+
21
+
22
+ def compressed_convolve_to_h5(
23
+ output_hdf5_filename,
24
+ template_data: TemplateData,
25
+ low_rank_templates: LowRankTemplates,
26
+ compressed_upsampled_temporal: CompressedUpsampledTemplates,
27
+ template_data_b: Optional[TemplateData] = None,
28
+ low_rank_templates_b: Optional[LowRankTemplates] = None,
29
+ chunk_time_centers_s: Optional[np.ndarray] = None,
30
+ motion_est=None,
31
+ geom: Optional[np.ndarray] = None,
32
+ conv_ignore_threshold=0.0,
33
+ coarse_approx_error_threshold=0.0,
34
+ min_spatial_cosine=0.0,
35
+ conv_batch_size=128,
36
+ units_batch_size=8,
37
+ overwrite=False,
38
+ device=None,
39
+ n_jobs=0,
40
+ show_progress=True,
41
+ ):
42
+ """Convolve all pairs of templates and store result in a .h5
43
+
44
+ See pairwise.CompressedPairwiseConvDB for how to read the
45
+ resulting convolutions back.
46
+
47
+ This runs compressed_convolve_pairs in a loop over chunks
48
+ of unit pairs, so that it's not all done in memory at one time,
49
+ and so that it can be done in parallel.
50
+ """
51
+ output_hdf5_filename = Path(output_hdf5_filename)
52
+ if not overwrite and output_hdf5_filename.exists():
53
+ with h5py.File(output_hdf5_filename, "r") as h5:
54
+ if "pconv_index" in h5:
55
+ return output_hdf5_filename
56
+ del h5
57
+ elif overwrite and output_hdf5_filename.exists():
58
+ output_hdf5_filename.unlink()
59
+
60
+ # construct indexing helpers
61
+ (
62
+ template_shift_index_a,
63
+ template_shift_index_b,
64
+ upsampled_shifted_template_index,
65
+ cooccurrence,
66
+ ) = construct_shift_indices(
67
+ chunk_time_centers_s,
68
+ geom,
69
+ template_data,
70
+ compressed_upsampled_temporal,
71
+ template_data_b=template_data_b,
72
+ motion_est=motion_est,
73
+ )
74
+
75
+ chunk_res_iterator = iterate_compressed_pairwise_convolutions(
76
+ template_data_a=template_data,
77
+ low_rank_templates_a=low_rank_templates,
78
+ template_data_b=template_data_b,
79
+ low_rank_templates_b=low_rank_templates_b,
80
+ compressed_upsampled_temporal=compressed_upsampled_temporal,
81
+ template_shift_index_a=template_shift_index_a,
82
+ template_shift_index_b=template_shift_index_b,
83
+ cooccurrence=cooccurrence,
84
+ upsampled_shifted_template_index=upsampled_shifted_template_index,
85
+ do_shifting=motion_est is not None,
86
+ geom=geom,
87
+ conv_ignore_threshold=conv_ignore_threshold,
88
+ min_spatial_cosine=min_spatial_cosine,
89
+ coarse_approx_error_threshold=coarse_approx_error_threshold,
90
+ max_shift="full",
91
+ conv_batch_size=conv_batch_size,
92
+ units_batch_size=units_batch_size,
93
+ device=device,
94
+ n_jobs=n_jobs,
95
+ show_progress=show_progress,
96
+ )
97
+
98
+ pconv_index = np.zeros(
99
+ (
100
+ template_shift_index_a.n_shifted_templates,
101
+ upsampled_shifted_template_index.n_upsampled_shifted_templates,
102
+ ),
103
+ dtype=np.int64,
104
+ )
105
+ n_pconvs = 1
106
+ with h5py.File(output_hdf5_filename, "w") as h5:
107
+ # resizeable pconv dataset
108
+ spike_length_samples = template_data.templates.shape[1]
109
+ pconv = h5.create_dataset(
110
+ "pconv",
111
+ dtype=np.float32,
112
+ shape=(1, 2 * spike_length_samples - 1),
113
+ maxshape=(None, 2 * spike_length_samples - 1),
114
+ chunks=(128, 2 * spike_length_samples - 1),
115
+ )
116
+
117
+ for chunk_res in chunk_res_iterator:
118
+ if chunk_res is None:
119
+ continue
120
+
121
+ # get shifted template indices for A
122
+ shifted_temp_ix_a = template_shift_index_a.template_shift_index[
123
+ chunk_res.template_indices_a,
124
+ chunk_res.shift_indices_a,
125
+ ]
126
+
127
+ # upsampled shifted template indices for B
128
+ up_shifted_temp_ix_b = (
129
+ upsampled_shifted_template_index.upsampled_shifted_template_index[
130
+ chunk_res.template_indices_b,
131
+ chunk_res.shift_indices_b,
132
+ chunk_res.upsampling_indices_b,
133
+ ]
134
+ )
135
+
136
+ # store new set of indices
137
+ new_pconv_indices = chunk_res.compression_index + n_pconvs
138
+ pconv_index[shifted_temp_ix_a, up_shifted_temp_ix_b] = new_pconv_indices
139
+
140
+ # store new pconvs
141
+ n_new_pconvs = chunk_res.compressed_conv.shape[0]
142
+ pconv.resize(n_pconvs + n_new_pconvs, axis=0)
143
+ pconv[n_pconvs:] = chunk_res.compressed_conv
144
+
145
+ n_pconvs += n_new_pconvs
146
+
147
+ # write fixed size outputs
148
+ h5.create_dataset("shifts_a", data=template_shift_index_a.all_pitch_shifts)
149
+ h5.create_dataset("shifts_b", data=template_shift_index_b.all_pitch_shifts)
150
+ h5.create_dataset(
151
+ "shifted_template_index_a",
152
+ data=template_shift_index_a.template_shift_index,
153
+ )
154
+ h5.create_dataset(
155
+ "upsampled_shifted_template_index_b",
156
+ data=upsampled_shifted_template_index.upsampled_shifted_template_index,
157
+ )
158
+ h5.create_dataset("pconv_index", data=pconv_index)
159
+
160
+ return output_hdf5_filename
161
+
162
+
163
+ def iterate_compressed_pairwise_convolutions(
164
+ template_data_a: TemplateData,
165
+ low_rank_templates_a: LowRankTemplates,
166
+ template_data_b: TemplateData,
167
+ low_rank_templates_b: LowRankTemplates,
168
+ compressed_upsampled_temporal: CompressedUpsampledTemplates,
169
+ template_shift_index_a: drift_util.TemplateShiftIndex,
170
+ template_shift_index_b: drift_util.TemplateShiftIndex,
171
+ cooccurrence: np.ndarray,
172
+ upsampled_shifted_template_index: UpsampledShiftedTemplateIndex,
173
+ do_shifting: bool = True,
174
+ geom: Optional[np.ndarray] = None,
175
+ conv_ignore_threshold=0.0,
176
+ coarse_approx_error_threshold=0.0,
177
+ min_spatial_cosine=0.0,
178
+ max_shift="full",
179
+ amplitude_scaling_variance=0.0,
180
+ amplitude_scaling_boundary=0.5,
181
+ reduce_deconv_resid_decrease=False,
182
+ ignore_empty_channels=False,
183
+ distance_kind="rms",
184
+ conv_batch_size=128,
185
+ units_batch_size=8,
186
+ device=None,
187
+ n_jobs=0,
188
+ show_progress=True,
189
+ ) -> Iterator[Optional[CompressedConvResult]]:
190
+ """A generator of CompressedConvResults capturing all pairs of templates
191
+
192
+ Runs the function compressed_convolve_pairs on chunks of units.
193
+
194
+ This is a helper function for parallelizing computation of cross correlations
195
+ between pairs of templates. There are too many to store all the results in
196
+ memory, so this is a generator yielding a chunk at a time. Callers may
197
+ process the results differently.
198
+ """
199
+ reg_geom = template_data_a.registered_geom
200
+ if template_data_b is None:
201
+ template_data_b = template_data_a
202
+ assert low_rank_templates_b is None
203
+ low_rank_templates_b = low_rank_templates_a
204
+ if reg_geom is None:
205
+ assert template_data_b.registered_geom is None
206
+ else:
207
+ assert template_data_b.registered_geom is not None
208
+ assert np.array_equal(reg_geom, template_data_b.registered_geom)
209
+
210
+ # construct drift-related helper data if needed
211
+ geom_kdtree = reg_geom_kdtree = match_distance = None
212
+ if do_shifting:
213
+ assert geom is not None
214
+ geom_kdtree = KDTree(geom)
215
+ reg_geom_kdtree = KDTree(reg_geom)
216
+ match_distance = pdist(geom).min() / 2
217
+
218
+ # make chunks
219
+ units_a = np.unique(template_data_a.unit_ids)
220
+ units_b = np.unique(template_data_b.unit_ids)
221
+ jobs = []
222
+ for start_a in range(0, units_a.size, units_batch_size):
223
+ end_a = min(start_a + units_batch_size, units_a.size)
224
+ for start_b in range(0, units_b.size, units_batch_size):
225
+ end_b = min(start_b + units_batch_size, units_b.size)
226
+ jobs.append((units_a[start_a:end_a], units_b[start_b:end_b]))
227
+
228
+ # worker kwargs
229
+ kwargs = dict(
230
+ template_data_a=template_data_a,
231
+ template_data_b=template_data_b,
232
+ low_rank_templates_a=low_rank_templates_a,
233
+ low_rank_templates_b=low_rank_templates_b,
234
+ compressed_upsampled_temporal=compressed_upsampled_temporal,
235
+ template_shift_index_a=template_shift_index_a,
236
+ template_shift_index_b=template_shift_index_b,
237
+ upsampled_shifted_template_index=upsampled_shifted_template_index,
238
+ cooccurrence=cooccurrence,
239
+ geom=geom,
240
+ reg_geom=reg_geom,
241
+ geom_kdtree=geom_kdtree,
242
+ reg_geom_kdtree=reg_geom_kdtree,
243
+ do_shifting=do_shifting,
244
+ match_distance=match_distance,
245
+ conv_ignore_threshold=conv_ignore_threshold,
246
+ min_spatial_cosine=min_spatial_cosine,
247
+ coarse_approx_error_threshold=coarse_approx_error_threshold,
248
+ max_shift=max_shift,
249
+ batch_size=conv_batch_size,
250
+ amplitude_scaling_variance=amplitude_scaling_variance,
251
+ amplitude_scaling_boundary=amplitude_scaling_boundary,
252
+ reduce_deconv_resid_decrease=reduce_deconv_resid_decrease,
253
+ distance_kind=distance_kind,
254
+ ignore_empty_channels=ignore_empty_channels,
255
+ )
256
+
257
+ n_jobs, Executor, context, rank_queue = get_pool(n_jobs, with_rank_queue=True) # type: ignore
258
+ with Executor(
259
+ n_jobs,
260
+ mp_context=context,
261
+ initializer=_conv_worker_init,
262
+ initargs=(rank_queue, device, kwargs),
263
+ ) as pool:
264
+ it = pool.map(_conv_job, jobs)
265
+ if show_progress:
266
+ it = tqdm(
267
+ it,
268
+ smoothing=0.01,
269
+ desc="Pairwise convolution",
270
+ unit="pair block",
271
+ total=len(jobs),
272
+ )
273
+ yield from it
274
+
275
+
276
+ @dataclass
277
+ class CompressedConvResult:
278
+ """Main return type of compressed_convolve_pairs
279
+
280
+ If reduce_deconv_resid_decrease=True, a DeconvResidResult is returned.
281
+
282
+ After convolving a bunch of template pairs, some convolutions
283
+ may be zero. Let n_pairs be the number of nonzero convolutions.
284
+ We don't store the zero ones.
285
+ """
286
+
287
+ # arrays of shape n_pairs,
288
+ # For each convolved pair, these document which templates were
289
+ # in the pair, what their relative shifts were, and what the
290
+ # upsampling was (we only upsample the RHS)
291
+ template_indices_a: np.ndarray
292
+ template_indices_b: np.ndarray
293
+ shift_indices_a: np.ndarray
294
+ shift_indices_b: np.ndarray
295
+ upsampling_indices_b: np.ndarray
296
+
297
+ # another one of shape n_pairs
298
+ # maps a pair index to the corresponding convolution index
299
+ # some convolutions are duplicates, so this array contains
300
+ # many duplicate entries in the range 0, ..., n_convs-1
301
+ compression_index: np.ndarray
302
+
303
+ # this one has shape (n_convs, 2 * spike_length_samples - 1)
304
+ compressed_conv: np.ndarray
305
+
306
+
307
+ @dataclass
308
+ class DeconvResidResult:
309
+ """Return type of compressed_convolve_pairs
310
+
311
+ After convolving a bunch of template pairs, some convolutions
312
+ may be zero. Let n_pairs be the number of nonzero convolutions.
313
+ We don't store the zero ones.
314
+ """
315
+
316
+ # arrays of shape n_pairs,
317
+ # For each convolved pair, these document which templates were
318
+ # in the pair, what their relative shifts were, and what the
319
+ # upsampling was (we only upsample the RHS)
320
+ template_indices_a: np.ndarray
321
+ template_indices_b: np.ndarray
322
+
323
+ # norm after subtracting best upsampled/scaled/shifted B template from A template
324
+ deconv_resid_decreases: np.ndarray
325
+
326
+ # ints. B trough - A trough
327
+ shifts: np.ndarray
328
+
329
+ # for caller to implement different metrics
330
+ template_a_norms: np.ndarray
331
+ scalings: np.ndarray
332
+
333
+
334
+ def conv_to_resid(
335
+ # template_data_a: TemplateData,
336
+ low_rank_templates_a: LowRankTemplates,
337
+ low_rank_templates_b: LowRankTemplates,
338
+ conv_result: CompressedConvResult,
339
+ amplitude_scaling_variance=0.0,
340
+ amplitude_scaling_boundary=0.5,
341
+ ignore_empty_channels=True,
342
+ distance_kind="rms",
343
+ ) -> DeconvResidResult:
344
+ """Reduce pairwise convolutions to a scalar, the deconv objective.
345
+
346
+ The idea: we are trying to reconstruct templates a with templates b.
347
+ How much can we reduce the norm of template a by subtracting (scaled) b?
348
+ """
349
+ # decompress
350
+ pconvs = conv_result.compressed_conv[conv_result.compression_index]
351
+ full_length = pconvs.shape[1]
352
+ center = full_length // 2
353
+
354
+ # here, we just care about pairs of (superres) templates, not upsampling
355
+ # or shifting. so, get unique such pairs.
356
+ pairs = np.c_[conv_result.template_indices_a, conv_result.template_indices_b]
357
+ pairs = np.unique(pairs, axis=0)
358
+ n_pairs = len(pairs)
359
+
360
+ # for loop to reduce over all (upsampled etc) member templates
361
+ deconv_resid_norms = np.zeros(n_pairs)
362
+ shifts = np.zeros(n_pairs, dtype=np.int64)
363
+ template_indices_a, template_indices_b = pairs.T
364
+
365
+ # low rank template norms
366
+ # svs_a = low_rank_templates_a.singular_values[template_indices_a]
367
+ # svs_b = low_rank_templates_b.singular_values[template_indices_b]
368
+ # template_a_norms = (
369
+ # torch.square(svs_a).sum(1).numpy(force=True)
370
+ # )
371
+ # template_b_norms = (
372
+ # torch.square(svs_b).sum(1).numpy(force=True)
373
+ # )
374
+
375
+ # produce these on common channel sets
376
+ # N, R, C
377
+ spatial_a = low_rank_templates_a.spatial_components[template_indices_a]
378
+ spatial_b = low_rank_templates_b.spatial_components[template_indices_b]
379
+ # N, R
380
+ svs_a = low_rank_templates_a.singular_values[template_indices_a]
381
+ svs_b = low_rank_templates_b.singular_values[template_indices_b]
382
+ active_a = torch.any(spatial_a > 0, dim=1).to(svs_a)
383
+ if (
384
+ ignore_empty_channels
385
+ and low_rank_templates_b.spike_counts_by_channel is not None
386
+ ):
387
+ active_b = low_rank_templates_b.spike_counts_by_channel[template_indices_b]
388
+ active_b = active_b > 0
389
+ active_b = torch.from_numpy(active_b).to(svs_a)
390
+ active = active_a * active_b
391
+ else:
392
+ active = active_a
393
+ # active_b = torch.any(spatial_b > 0, dim=1).to(svs_b)
394
+ com_spatial_sing_a = (spatial_a * active[:, None, :]) * svs_a[:, :, None]
395
+ com_spatial_sing_b = (spatial_b * active[:, None, :]) * svs_b[:, :, None]
396
+ template_a_norms = com_spatial_sing_a.square().sum((1, 2)).numpy(force=True)
397
+ template_b_norms = com_spatial_sing_b.square().sum((1, 2)).numpy(force=True)
398
+
399
+ if distance_kind == "max":
400
+ ta = torch.bmm(
401
+ low_rank_templates_a.temporal_components[template_indices_a],
402
+ com_spatial_sing_a,
403
+ ).numpy(force=True)
404
+ tb = torch.bmm(
405
+ low_rank_templates_b.temporal_components[template_indices_b],
406
+ com_spatial_sing_b,
407
+ ).numpy(force=True)
408
+ template_a_norms_ret = np.abs(ta).max(axis=(1, 2))
409
+ elif distance_kind == "rms":
410
+ template_a_norms_ret = template_a_norms
411
+ elif distance_kind == "deconv":
412
+ template_a_norms_ret = np.ones_like(template_a_norms)
413
+ else:
414
+ assert False
415
+
416
+ # now, compute reduction in norm of A after matching by B
417
+ scalings = np.ones_like(template_a_norms)
418
+ for j, (ix_a, ix_b) in enumerate(pairs):
419
+ in_a = conv_result.template_indices_a == ix_a
420
+ in_b = conv_result.template_indices_b == ix_b
421
+ in_pair = np.flatnonzero(in_a & in_b)
422
+
423
+ # reduce over fine templates
424
+ pair_conv = pconvs[in_pair].max(axis=0)
425
+ lag_index = np.argmax(pair_conv)
426
+ best_conv = pair_conv[lag_index]
427
+ shifts[j] = lag_index - center
428
+
429
+ # figure out scaling
430
+ if amplitude_scaling_variance:
431
+ amp_scale_min = 1.0 / (1.0 + amplitude_scaling_boundary)
432
+ amp_scale_max = 1.0 + amplitude_scaling_boundary
433
+ inv_lambda = 1.0 / amplitude_scaling_variance
434
+ b = best_conv + inv_lambda
435
+ a = template_b_norms[j] + inv_lambda
436
+ scaling = (b / a).clip(amp_scale_min, amp_scale_max)
437
+ scalings[j] = scaling
438
+ else:
439
+ scaling = 1.0
440
+
441
+ if distance_kind in ("rms", "deconv"):
442
+ if amplitude_scaling_variance:
443
+ norm_reduction = 2.0 * scaling * b - np.square(scaling) * a - inv_lambda
444
+ else:
445
+ norm_reduction = 2.0 * best_conv - template_b_norms[j]
446
+ deconv_resid_norms[j] = template_a_norms[j] - norm_reduction
447
+ # deconv_resid_norms[j] /= template_a_norms[j]
448
+ elif distance_kind == "max":
449
+ deconv_resid_norms[j] = np.abs(ta[j] - scaling * tb[j]).max()
450
+ # deconv_resid_norms[j] /= np.abs(ta[j]).max()
451
+ else:
452
+ assert False
453
+
454
+ return DeconvResidResult(
455
+ template_indices_a,
456
+ template_indices_b,
457
+ deconv_resid_norms,
458
+ shifts,
459
+ template_a_norms_ret,
460
+ scalings,
461
+ )
462
+
463
+
464
+ def compressed_convolve_pairs(
465
+ template_data_a: TemplateData,
466
+ template_data_b: TemplateData,
467
+ low_rank_templates_a: LowRankTemplates,
468
+ low_rank_templates_b: LowRankTemplates,
469
+ compressed_upsampled_temporal: CompressedUpsampledTemplates,
470
+ template_shift_index_a: drift_util.TemplateShiftIndex,
471
+ template_shift_index_b: drift_util.TemplateShiftIndex,
472
+ upsampled_shifted_template_index: UpsampledShiftedTemplateIndex,
473
+ cooccurrence: np.ndarray,
474
+ geom: Optional[np.ndarray] = None,
475
+ reg_geom: Optional[np.ndarray] = None,
476
+ geom_kdtree: Optional[KDTree] = None,
477
+ reg_geom_kdtree: Optional[KDTree] = None,
478
+ do_shifting: bool = False,
479
+ match_distance: Optional[float] = None,
480
+ units_a: Optional[np.ndarray] = None,
481
+ units_b: Optional[np.ndarray] = None,
482
+ conv_ignore_threshold=0.0,
483
+ min_spatial_cosine=0.0,
484
+ coarse_approx_error_threshold=0.0,
485
+ amplitude_scaling_variance=0.0,
486
+ amplitude_scaling_boundary=0.5,
487
+ reduce_deconv_resid_decrease=False,
488
+ ignore_empty_channels=False,
489
+ distance_kind="rms",
490
+ max_shift="full",
491
+ batch_size=128,
492
+ device=None,
493
+ ) -> CompressedConvResult | DeconvResidResult | None:
494
+ """Compute compressed pairwise convolutions between template pairs
495
+
496
+ Takes as input all the template data and groups of pairs of units to convolve
497
+ (units_a,b). units_a,b are unit indices, not template indices (i.e., coarse
498
+ units, not superresolved bin indices).
499
+
500
+ Returns compressed convolutions between all units_a[i], units_b[j], for all
501
+ shifts, superres templates, and upsamples. Some of these may be zero or may
502
+ be duplicates, so the return value is a sparse representation. See below.
503
+ """
504
+ # what pairs, shifts, etc are we convolving?
505
+ shifted_temp_ix_a, temp_ix_a, shift_a, unit_a = handle_shift_indices(
506
+ units_a, template_data_a.unit_ids, template_shift_index_a
507
+ )
508
+ shifted_temp_ix_b, temp_ix_b, shift_b, unit_b = handle_shift_indices(
509
+ units_b, template_data_b.unit_ids, template_shift_index_b
510
+ )
511
+
512
+ # get (shifted) spatial components * singular values
513
+ spatial_singular_a = get_shifted_spatial_singular(
514
+ temp_ix_a,
515
+ shift_a,
516
+ template_shift_index_a,
517
+ low_rank_templates_a,
518
+ geom=geom,
519
+ registered_geom=reg_geom,
520
+ geom_kdtree=geom_kdtree,
521
+ match_distance=match_distance,
522
+ device=device,
523
+ do_shifting=do_shifting,
524
+ )
525
+ spatial_singular_b = get_shifted_spatial_singular(
526
+ temp_ix_b,
527
+ shift_b,
528
+ template_shift_index_b,
529
+ low_rank_templates_b,
530
+ geom=geom,
531
+ registered_geom=reg_geom,
532
+ geom_kdtree=geom_kdtree,
533
+ match_distance=match_distance,
534
+ device=device,
535
+ do_shifting=do_shifting,
536
+ )
537
+
538
+ # figure out pairs of shifted templates to convolve in a deduplicated way
539
+ pairs_ret = shift_deduplicated_pairs(
540
+ shifted_temp_ix_a,
541
+ shifted_temp_ix_b,
542
+ spatial_singular_a,
543
+ spatial_singular_b,
544
+ temp_ix_a,
545
+ temp_ix_b,
546
+ cooccurrence=cooccurrence,
547
+ shift_a=shift_a,
548
+ shift_b=shift_b,
549
+ conv_ignore_threshold=conv_ignore_threshold,
550
+ min_spatial_cosine=min_spatial_cosine,
551
+ geom=geom,
552
+ registered_geom=reg_geom,
553
+ reg_geom_kdtree=reg_geom_kdtree,
554
+ match_distance=match_distance,
555
+ )
556
+ if pairs_ret is None:
557
+ return None
558
+ ix_a, ix_b, compression_index, conv_ix, spatial_shift_ids = pairs_ret
559
+
560
+ # handle upsampling
561
+ # each pair will be duplicated by the b unit's number of upsampled copies
562
+
563
+ (
564
+ ix_b,
565
+ compression_index,
566
+ conv_ix,
567
+ conv_upsampling_indices_b,
568
+ conv_temporal_components_up_b, # Need to change this conv_temporal_components_up_b[conv_compressed_upsampled_ix_b]
569
+ conv_compressed_upsampled_ix_b,
570
+ compression_dup_ix,
571
+ ) = compressed_upsampled_pairs(
572
+ ix_b,
573
+ compression_index,
574
+ conv_ix,
575
+ temp_ix_b,
576
+ shifted_temp_ix_b,
577
+ upsampled_shifted_template_index,
578
+ compressed_upsampled_temporal,
579
+ )
580
+ ix_a = ix_a[compression_dup_ix]
581
+ spatial_shift_ids = spatial_shift_ids[compression_dup_ix]
582
+
583
+ # run convolutions
584
+ temporal_a = low_rank_templates_a.temporal_components[temp_ix_a]
585
+ pconv, kept = correlate_pairs_lowrank(
586
+ torch.as_tensor(spatial_singular_a, device=device),
587
+ torch.as_tensor(spatial_singular_b, device=device),
588
+ torch.as_tensor(temporal_a, device=device),
589
+ torch.as_tensor(conv_temporal_components_up_b, device=device),
590
+ ix_a=ix_a,
591
+ ix_b=ix_b,
592
+ conv_ix=conv_ix,
593
+ conv_compressed_upsampled_ix_b=conv_compressed_upsampled_ix_b,
594
+ max_shift=max_shift,
595
+ # conv_ignore_threshold=conv_ignore_threshold,
596
+ conv_ignore_threshold=0.0,
597
+ batch_size=batch_size,
598
+ restrict_to_active=reduce_deconv_resid_decrease,
599
+ )
600
+ if kept is not None:
601
+ conv_ix = conv_ix[kept]
602
+ if not conv_ix.shape[0]:
603
+ return None
604
+ kept_pairs = np.flatnonzero(np.isin(compression_index, kept))
605
+ compression_index = np.searchsorted(kept, compression_index[kept_pairs])
606
+ conv_ix = np.searchsorted(kept_pairs, conv_ix)
607
+ ix_a = ix_a[kept_pairs]
608
+ ix_b = ix_b[kept_pairs]
609
+ spatial_shift_ids = spatial_shift_ids[kept_pairs]
610
+ assert pconv.numel() > 0
611
+
612
+ # coarse approx
613
+ pconv, old_ix_to_new_ix = coarse_approximate(
614
+ pconv,
615
+ unit_a[ix_a[conv_ix]],
616
+ unit_b[ix_b[conv_ix]],
617
+ temp_ix_a[ix_a[conv_ix]],
618
+ spatial_shift_ids[conv_ix],
619
+ coarse_approx_error_threshold=coarse_approx_error_threshold,
620
+ )
621
+ compression_index = old_ix_to_new_ix[compression_index]
622
+ # above function invalidates the whole idea of conv_ix
623
+ del conv_ix
624
+
625
+ # recover metadata
626
+ temp_ix_a = temp_ix_a[ix_a]
627
+ shift_ix_a = np.searchsorted(template_shift_index_a.all_pitch_shifts, shift_a[ix_a])
628
+ temp_ix_b = temp_ix_b[ix_b]
629
+ shift_ix_b = np.searchsorted(template_shift_index_b.all_pitch_shifts, shift_b[ix_b])
630
+
631
+ res = CompressedConvResult(
632
+ template_indices_a=temp_ix_a,
633
+ template_indices_b=temp_ix_b,
634
+ shift_indices_a=shift_ix_a,
635
+ shift_indices_b=shift_ix_b,
636
+ upsampling_indices_b=conv_upsampling_indices_b[compression_index],
637
+ compression_index=compression_index,
638
+ compressed_conv=pconv.numpy(force=True),
639
+ )
640
+ if reduce_deconv_resid_decrease:
641
+ return conv_to_resid(
642
+ low_rank_templates_a,
643
+ low_rank_templates_b,
644
+ res,
645
+ amplitude_scaling_variance=amplitude_scaling_variance,
646
+ amplitude_scaling_boundary=amplitude_scaling_boundary,
647
+ distance_kind=distance_kind,
648
+ ignore_empty_channels=ignore_empty_channels,
649
+ )
650
+ return res
651
+
652
+
653
+ # -- helpers
654
+
655
+
656
+ def correlate_pairs_lowrank(
657
+ spatial_a,
658
+ spatial_b,
659
+ temporal_a,
660
+ temporal_b,
661
+ ix_a,
662
+ ix_b,
663
+ conv_ix,
664
+ conv_compressed_upsampled_ix_b,
665
+ max_shift="full",
666
+ conv_ignore_threshold=0.0,
667
+ batch_size=128,
668
+ restrict_to_active=False,
669
+ ):
670
+ """Convolve pairs of low rank templates
671
+
672
+ For each i, we want to convolve (temporal_a[i] @ spatial_a[i]) with
673
+ (temporal_b[i] @ spatial_b[i]). So, spatial_{a,b} and temporal_{a,b}
674
+ should contain lots of duplicates, since they are already representing
675
+ pairs.
676
+
677
+ Returns
678
+ -------
679
+ pconv, kept
680
+ """
681
+
682
+ # Now need to take ix_a/b[conv_ix] of spatial_a, spatial_b, temporal_a
683
+ _, rank, nchan = spatial_a.shape
684
+ _, rank_, nchan_ = spatial_b.shape
685
+ n_pairs = conv_ix.shape[0]
686
+ assert rank == rank_
687
+ assert nchan == nchan_
688
+ # assert n_pairs == n_pairs_
689
+ _, t, rank_ = temporal_a.shape
690
+ # assert n_pairs == n_pairs_
691
+ assert rank_ == rank
692
+ _, t_, rank_ = temporal_b.shape
693
+ n_pairs_ = conv_compressed_upsampled_ix_b.shape[0]
694
+ assert n_pairs == n_pairs_
695
+ assert t == t_
696
+ assert rank == rank_
697
+
698
+ if max_shift == "full":
699
+ max_shift = t - 1
700
+ elif max_shift == "valid":
701
+ max_shift = 0
702
+ elif max_shift == "same":
703
+ max_shift = t // 2
704
+ else:
705
+ assert isinstance(max_shift, int)
706
+
707
+ # batch over n_pairs for memory reasons
708
+ pconv = torch.zeros(
709
+ (n_pairs, 2 * max_shift + 1),
710
+ dtype=spatial_a.dtype,
711
+ device=spatial_a.device,
712
+ )
713
+ for istart in range(0, n_pairs, batch_size):
714
+ iend = min(istart + batch_size, n_pairs)
715
+ nbatch = iend - istart
716
+ ix = slice(istart, iend)
717
+
718
+ spatial_a_ = spatial_a[ix_a[conv_ix][ix]]
719
+ spatial_b_ = spatial_b[ix_b[conv_ix][ix]]
720
+ if restrict_to_active:
721
+ active_a = torch.any(spatial_a_ > 0, dim=1).to(spatial_a)
722
+ active_b = torch.any(spatial_b_ > 0, dim=1).to(spatial_b)
723
+ active = active_a * active_b
724
+ spatial_a_ = spatial_a_ * active[:, None, :]
725
+ spatial_b_ = spatial_b_ * active[:, None, :]
726
+
727
+ ta = temporal_a[ix_a[conv_ix][ix]]
728
+ tb = temporal_b[conv_compressed_upsampled_ix_b[ix]]
729
+ assert ta.shape == tb.shape == (nbatch, t, rank)
730
+
731
+ # # we want to convolve templates A and B. have to do rank^2
732
+ # because the basis is not shared. the conv kind of matvecs the
733
+ # rank^2 and leaves a rank dim behind.
734
+ # TODO: rework this whole thing.
735
+ sb_ta_sa = torch.einsum(
736
+ "ntr,nrc,nqc->rnqt", temporal_a[ix_a[conv_ix][ix]], spatial_a_, spatial_b_
737
+ )
738
+ pc = F.conv2d(sb_ta_sa, tb.mT[:, None], groups=nbatch, padding=(0, max_shift))
739
+ pconv[istart:iend] = torch.flip(pc.sum(0)[:, 0], dims=(1,))
740
+
741
+ # more stringent covisibility
742
+ if conv_ignore_threshold is not None:
743
+ max_val = pconv.reshape(n_pairs, -1).abs().max(dim=1).values
744
+ kept = max_val > conv_ignore_threshold
745
+ pconv = pconv[kept]
746
+ kept = np.flatnonzero(kept.numpy(force=True))
747
+ else:
748
+ kept = None
749
+
750
+ return pconv, kept
751
+
752
+
753
+ def construct_shift_indices(
754
+ chunk_time_centers_s,
755
+ geom,
756
+ template_data_a,
757
+ compressed_upsampled_temporal,
758
+ template_data_b=None,
759
+ motion_est=None,
760
+ ):
761
+ (
762
+ template_shift_index_a,
763
+ template_shift_index_b,
764
+ cooccurrence,
765
+ ) = drift_util.get_shift_and_unit_pairs(
766
+ chunk_time_centers_s,
767
+ geom,
768
+ template_data_a,
769
+ template_data_b=template_data_b,
770
+ motion_est=motion_est,
771
+ )
772
+ upsampled_shifted_template_index = get_upsampled_shifted_template_index(
773
+ template_shift_index_b, compressed_upsampled_temporal
774
+ )
775
+ return (
776
+ template_shift_index_a,
777
+ template_shift_index_b,
778
+ upsampled_shifted_template_index,
779
+ cooccurrence,
780
+ )
781
+
782
+
783
+ def handle_shift_indices(units, unit_ids, template_shift_index):
784
+ """Determine shifted template indices belonging to a set of units."""
785
+ shifted_temp_ix_to_unit = unit_ids[template_shift_index.shifted_temp_ix_to_temp_ix]
786
+ if units is None:
787
+ shifted_temp_ix = np.arange(template_shift_index.n_shifted_templates)
788
+ else:
789
+ shifted_temp_ix = np.flatnonzero(np.isin(shifted_temp_ix_to_unit, units))
790
+
791
+ shift = template_shift_index.shifted_temp_ix_to_shift[shifted_temp_ix]
792
+ temp_ix = template_shift_index.shifted_temp_ix_to_temp_ix[shifted_temp_ix]
793
+ unit = unit_ids[temp_ix]
794
+
795
+ return shifted_temp_ix, temp_ix, shift, unit
796
+
797
+
798
+ def get_shifted_spatial_singular(
799
+ temp_ix,
800
+ shift,
801
+ template_shift_index,
802
+ low_rank_templates,
803
+ geom=None,
804
+ registered_geom=None,
805
+ geom_kdtree=None,
806
+ match_distance=None,
807
+ device=None,
808
+ do_shifting=False,
809
+ ):
810
+ spatial_singular = (
811
+ low_rank_templates.spatial_components[temp_ix]
812
+ * low_rank_templates.singular_values[temp_ix][..., None]
813
+ )
814
+ if do_shifting:
815
+ spatial_singular = drift_util.get_waveforms_on_static_channels(
816
+ spatial_singular,
817
+ registered_geom,
818
+ n_pitches_shift=shift,
819
+ registered_geom=geom,
820
+ target_kdtree=geom_kdtree,
821
+ match_distance=match_distance,
822
+ fill_value=0.0,
823
+ )
824
+ spatial_singular = torch.as_tensor(spatial_singular, device=device)
825
+
826
+ return spatial_singular
827
+
828
+
829
+ def shift_deduplicated_pairs(
830
+ shifted_temp_ix_a,
831
+ shifted_temp_ix_b,
832
+ spatialsing_a,
833
+ spatialsing_b,
834
+ temp_ix_a,
835
+ temp_ix_b,
836
+ cooccurrence,
837
+ shift_a=None,
838
+ shift_b=None,
839
+ conv_ignore_threshold=0.0,
840
+ min_spatial_cosine=0.0,
841
+ geom=None,
842
+ registered_geom=None,
843
+ reg_geom_kdtree=None,
844
+ do_shifting=False,
845
+ match_distance=None,
846
+ ):
847
+ """Choose a set of pairs of indices from group A and B to convolve
848
+
849
+ Some pairs of shifted templates don't overlap, so we don't need to convolve them.
850
+ Some pairs of shifted templates never show up in the recording at the same time
851
+ (what this code calls "cooccurrence"), so we don't need to convolve them.
852
+
853
+ More complicated: for each shift, a certain set of registered template channels
854
+ survives. Given that the some set of visible channels has survived for a pair of
855
+ templates at shifts shift_a and shift_b, their cross-correlation at these shifts
856
+ is the same as the one at shift_a_prime and shift_b_prime if the same exact channels
857
+ survived at shift_a_prime and shift_b_prime and if
858
+ shift_a-shift_b == shift_a_prime-shift_b_prime.
859
+
860
+ Returns
861
+ -------
862
+ pair_ix_a, pair_ix_b
863
+ Size < original number of shifted templates a,b
864
+ The indices of shifted templates which overlap enough to be
865
+ co-visible. So, these are subsets of shifted_temp_ix_a,b
866
+ compression_index
867
+ Size == pair_ix_a,b size
868
+ Arrays with shape matching pair_ix_a,b, so that the xcorr of templates
869
+ shifted_temp_ix_a[pair_ix_a[i]], shifted_temp_ix_b[pair_ix_b[i]]
870
+ is the same as that of
871
+ shifted_temp_ix_a[pair_ix_a[conv_ix[compression_index[i]]],
872
+ pair_ix_b[conv_ix[compression_index[i]]]
873
+ conv_ix
874
+ Size < original number of shifted templates a,b
875
+ Pairs of templates which should actually be convolved
876
+ """
877
+ # check spatially overlapping
878
+ chan_amp_a = torch.sqrt(torch.square(spatialsing_a).sum(1))
879
+ chan_amp_b = torch.sqrt(torch.square(spatialsing_b).sum(1))
880
+ dot = chan_amp_a @ chan_amp_b.T
881
+ pair = dot > conv_ignore_threshold
882
+ if min_spatial_cosine:
883
+ norm_a = torch.sqrt(chan_amp_a.square().sum(1))
884
+ norm_b = torch.sqrt(chan_amp_b.square().sum(1))
885
+ cos = dot / (norm_a[:, None] * norm_b[None, :])
886
+ pair = pair & (cos > min_spatial_cosine)
887
+
888
+ pair = pair.cpu()
889
+
890
+ # co-occurrence
891
+ cooccurrence = cooccurrence[
892
+ shifted_temp_ix_a[:, None],
893
+ shifted_temp_ix_b[None, :],
894
+ ]
895
+ pair = pair & torch.as_tensor(cooccurrence, device=pair.device)
896
+
897
+ pair_ix_a, pair_ix_b = torch.nonzero(pair, as_tuple=True)
898
+ nco = pair_ix_a.numel()
899
+ if not nco:
900
+ return None
901
+
902
+ # if no shifting, deduplication is the identity
903
+ if not do_shifting:
904
+ nco_range = torch.arange(nco, device=pair_ix_a.device)
905
+ return (
906
+ pair_ix_a,
907
+ pair_ix_b,
908
+ nco_range,
909
+ nco_range,
910
+ np.zeros(nco, dtype=np.int64),
911
+ )
912
+
913
+ # shift deduplication. algorithm:
914
+ # 1 for each shifted template, determine the set of registered channels
915
+ # which it occupies
916
+ # 2 assign each such set an ID (an "active channel ID")
917
+ # - // then a pair of shifted templates' xcorr is a function of the pair
918
+ # // of active channel IDs and the difference of shifts
919
+ # 3 figure out the set of unique (active chan id a, active chan id b, shift diff a,b)
920
+ # combinations in each pair of units
921
+
922
+ # 1: get active channel neighborhoods as many-hot len(reg_geom)-vectors
923
+ active_chans_a = drift_util.get_waveforms_on_static_channels(
924
+ (chan_amp_a > 0).numpy(force=True),
925
+ geom,
926
+ n_pitches_shift=-shift_a,
927
+ registered_geom=registered_geom,
928
+ target_kdtree=reg_geom_kdtree,
929
+ match_distance=match_distance,
930
+ fill_value=0,
931
+ )
932
+ active_chans_b = drift_util.get_waveforms_on_static_channels(
933
+ (chan_amp_b > 0).numpy(force=True),
934
+ geom,
935
+ n_pitches_shift=-shift_b,
936
+ registered_geom=registered_geom,
937
+ target_kdtree=reg_geom_kdtree,
938
+ match_distance=match_distance,
939
+ fill_value=0,
940
+ )
941
+ # 2: assign IDs to each such vector
942
+ chanset_a, active_chan_ids_a = np.unique(
943
+ active_chans_a, axis=0, return_inverse=True
944
+ )
945
+ chanset_b, active_chan_ids_b = np.unique(
946
+ active_chans_b, axis=0, return_inverse=True
947
+ )
948
+
949
+ # 3
950
+ temp_ix_a = temp_ix_a[pair_ix_a]
951
+ temp_ix_b = temp_ix_b[pair_ix_b]
952
+ # get the relative shifts
953
+ shift_a = shift_a[pair_ix_a]
954
+ shift_b = shift_b[pair_ix_b]
955
+
956
+ # figure out combinations
957
+ _, spatial_shift_ids = np.unique(
958
+ np.c_[
959
+ active_chan_ids_a[pair_ix_a],
960
+ active_chan_ids_b[pair_ix_b],
961
+ shift_a - shift_b,
962
+ ],
963
+ axis=0,
964
+ return_inverse=True,
965
+ )
966
+ conv_determiners = np.c_[
967
+ temp_ix_a,
968
+ temp_ix_b,
969
+ spatial_shift_ids,
970
+ ]
971
+ # conv_ix: indices of unique determiners
972
+ # compression_index: which representative does each pair belong to
973
+ _, conv_ix, compression_index = np.unique(
974
+ conv_determiners, axis=0, return_index=True, return_inverse=True
975
+ )
976
+ conv_ix = np.atleast_1d(conv_ix)
977
+ compression_index = np.atleast_1d(compression_index)
978
+
979
+ return pair_ix_a, pair_ix_b, compression_index, conv_ix, spatial_shift_ids
980
+
981
+
982
+ UpsampledShiftedTemplateIndex = namedtuple(
983
+ "UpsampledShiftedTemplateIndex",
984
+ [
985
+ "n_upsampled_shifted_templates",
986
+ "upsampled_shifted_template_index",
987
+ "up_shift_temp_ix_to_shift_temp_ix",
988
+ "up_shift_temp_ix_to_temp_ix",
989
+ "up_shift_temp_ix_to_comp_up_ix",
990
+ ],
991
+ )
992
+
993
+
994
+ def get_upsampled_shifted_template_index(
995
+ template_shift_index, compressed_upsampled_temporal
996
+ ):
997
+ """Make a compressed index space for upsampled shifted templates
998
+
999
+ See also: template_util.{compressed_upsampled_templates,ComptessedUpsampledTemplates}.
1000
+
1001
+ The comp_up_ix / compressed upsampled template indices here are indices into that
1002
+ structure.
1003
+
1004
+ Returns
1005
+ -------
1006
+ UpsampledShiftedTemplateIndex
1007
+ named tuple with fields:
1008
+ upsampled_shifted_template_index : (n_templates, n_shifts, up_factor)
1009
+ Maps template_ix, shift_ix, up_ix -> compressed upsampled template index
1010
+ up_shift_temp_ix_to_shift_temp_ix
1011
+ up_shift_temp_ix_to_temp_ix
1012
+ up_shift_temp_ix_to_comp_up_ix
1013
+ """
1014
+ n_shifted_templates = template_shift_index.n_shifted_templates
1015
+ n_templates, n_shifts = template_shift_index.template_shift_index.shape
1016
+ max_upsample = compressed_upsampled_temporal.compressed_upsampling_map.shape[1]
1017
+
1018
+ cur_up_shift_temp_ix = 0
1019
+ # fill with an invalid index
1020
+ upsampled_shifted_template_index = np.full(
1021
+ (n_templates, n_shifts, max_upsample),
1022
+ n_shifted_templates * max_upsample,
1023
+ )
1024
+ usti2sti = []
1025
+ usti2ti = []
1026
+ usti2cui = []
1027
+ for i in range(n_templates):
1028
+ shifted_temps = template_shift_index.template_shift_index[i]
1029
+ valid_shifts = np.flatnonzero(shifted_temps < n_shifted_templates)
1030
+
1031
+ upsampled_temps = compressed_upsampled_temporal.compressed_upsampling_map[i]
1032
+ unique_comp_up_inds, inverse = np.unique(upsampled_temps, return_inverse=True)
1033
+
1034
+ for j in valid_shifts:
1035
+ up_shift_inds = cur_up_shift_temp_ix + np.arange(unique_comp_up_inds.size)
1036
+ upsampled_shifted_template_index[i, j] = up_shift_inds[inverse]
1037
+ cur_up_shift_temp_ix += up_shift_inds.size
1038
+
1039
+ usti2sti.extend([shifted_temps[j]] * up_shift_inds.size)
1040
+ usti2ti.extend([i] * up_shift_inds.size)
1041
+ usti2cui.extend(unique_comp_up_inds)
1042
+
1043
+ up_shift_temp_ix_to_shift_temp_ix = np.array(usti2sti)
1044
+ up_shift_temp_ix_to_temp_ix = np.array(usti2ti)
1045
+ up_shift_temp_ix_to_comp_up_ix = np.array(usti2cui)
1046
+
1047
+ return UpsampledShiftedTemplateIndex(
1048
+ up_shift_temp_ix_to_shift_temp_ix.size,
1049
+ upsampled_shifted_template_index,
1050
+ up_shift_temp_ix_to_shift_temp_ix,
1051
+ up_shift_temp_ix_to_temp_ix,
1052
+ up_shift_temp_ix_to_comp_up_ix,
1053
+ )
1054
+
1055
+
1056
+ def compressed_upsampled_pairs(
1057
+ ix_b,
1058
+ compression_index,
1059
+ conv_ix,
1060
+ temp_ix_b,
1061
+ shifted_temp_ix_b,
1062
+ upsampled_shifted_template_index,
1063
+ compressed_upsampled_temporal,
1064
+ ):
1065
+ """Add in upsampling to the set of pairs that need to be convolved
1066
+
1067
+ So far, ix_a,b, compression_index, and conv_ix are such that non-upsampled
1068
+ convolutions between templates ix_a[i], ix_b[i] equal that between templates
1069
+ ix_a[conv_ix[compression_index[i]]], ix_b[conv_ix[compression_index[i]]].
1070
+
1071
+ We will upsample the templates in the RHS (b) in a compressed way, so that
1072
+ each b index gets its own number of duplicates.
1073
+ """
1074
+ up_factor = compressed_upsampled_temporal.compressed_upsampling_map.shape[1]
1075
+ compression_dup_ix = slice(None)
1076
+ if up_factor == 1:
1077
+ upinds = np.zeros(len(conv_ix), dtype=np.int64)
1078
+ # temp_comps = compressed_upsampled_temporal.compressed_upsampled_templates[
1079
+ # np.atleast_1d(temp_ix_b[ix_b[conv_ix]])
1080
+ # ]
1081
+ return (
1082
+ ix_b,
1083
+ compression_index,
1084
+ conv_ix,
1085
+ upinds,
1086
+ compressed_upsampled_temporal.compressed_upsampled_templates,
1087
+ np.atleast_1d(temp_ix_b[ix_b[conv_ix]]),
1088
+ compression_dup_ix,
1089
+ )
1090
+
1091
+ # each conv_ix needs to be duplicated as many times as its b template has
1092
+ # upsampled copies
1093
+ conv_shifted_temp_ix_b = np.atleast_1d(shifted_temp_ix_b[ix_b[conv_ix]])
1094
+ upsampling_mask = (
1095
+ conv_shifted_temp_ix_b[:, None]
1096
+ == upsampled_shifted_template_index.up_shift_temp_ix_to_shift_temp_ix[None, :]
1097
+ )
1098
+ conv_up_i, up_shift_up_i = np.nonzero(upsampling_mask)
1099
+ conv_compressed_upsampled_ix = (
1100
+ upsampled_shifted_template_index.up_shift_temp_ix_to_comp_up_ix[up_shift_up_i]
1101
+ )
1102
+ conv_dup = conv_ix[conv_up_i]
1103
+ # And, all ix_{a,b}[i] such that compression_ix[i] lands in
1104
+ # that conv_ix need to be duplicated as well.
1105
+ dup_mask = conv_ix[compression_index][:, None] == conv_dup[None, :]
1106
+ if torch.is_tensor(dup_mask):
1107
+ dup_mask = dup_mask.numpy(force=True)
1108
+ compression_dup_ix, compression_index_up = np.nonzero(dup_mask)
1109
+ ix_b_up = ix_b[compression_dup_ix]
1110
+
1111
+ # the conv ix need to be offset to keep the relation with the pairs
1112
+ # ix_a[old i]
1113
+ # offsets = np.cumsum((conv_ix[:, None] == conv_dup[None, :]).sum(0))
1114
+ # offsets -= offsets[0]
1115
+ _, offsets = np.unique(compression_dup_ix, return_index=True)
1116
+ conv_ix_up = np.atleast_1d(offsets[conv_dup])
1117
+
1118
+ # which upsamples and which templates?
1119
+ conv_upsampling_indices_b = (
1120
+ compressed_upsampled_temporal.compressed_index_to_upsampling_index[
1121
+ conv_compressed_upsampled_ix
1122
+ ]
1123
+ )
1124
+
1125
+ # conv_temporal_components_up_b = compressed_upsampled_temporal.compressed_upsampled_templates
1126
+
1127
+ return (
1128
+ ix_b_up,
1129
+ compression_index_up,
1130
+ conv_ix_up,
1131
+ conv_upsampling_indices_b,
1132
+ compressed_upsampled_temporal.compressed_upsampled_templates,
1133
+ conv_compressed_upsampled_ix,
1134
+ compression_dup_ix,
1135
+ )
1136
+
1137
+
1138
+ def coarse_approximate(
1139
+ pconv,
1140
+ units_a,
1141
+ units_b,
1142
+ temp_ix_a,
1143
+ # shift_a,
1144
+ # shift_b,
1145
+ spatial_shift_ids,
1146
+ coarse_approx_error_threshold=0.0,
1147
+ ):
1148
+ """Try to replace fine (superres+temporally upsampled) convs with coarse ones
1149
+
1150
+ For each pair of convolved units, we first try to replace all of the pairwise
1151
+ convolutions between these units with their mean, respecting the shifts.
1152
+
1153
+ If that fails, we try to do this in a factorized way: for each superres unit a,
1154
+ try to replace all of its convolutions with unit b with their mean, respecting
1155
+ the shifts.
1156
+
1157
+ Above, "respecting the shifts" means we only do this within each shift-deduplication
1158
+ class, since changes in the sets of channels being convolved cause large changes
1159
+ in the cross correlation. pconv has already been deduplicated with respect to
1160
+ equivalent channel neighborhoods, so all that matters for that purpose is the
1161
+ shift difference.
1162
+
1163
+ This needs to tell the caller how to update its bookkeeping.
1164
+ """
1165
+ if not pconv.numel() or not coarse_approx_error_threshold:
1166
+ return pconv, np.arange(len(pconv))
1167
+
1168
+ new_pconv = []
1169
+ old_ix_to_new_ix = np.full(len(pconv), -1)
1170
+ cur_new_ix = 0
1171
+ # shift_diff = shift_a - shift_b
1172
+ for ua in np.unique(units_a):
1173
+ ina = np.flatnonzero(units_a == ua)
1174
+ partners_b = np.unique(units_b[ina])
1175
+ for ub in partners_b:
1176
+ inab = ina[units_b[ina] == ub]
1177
+ dshift = spatial_shift_ids[inab]
1178
+ for shift in np.unique(dshift):
1179
+ inshift = inab[dshift == shift]
1180
+
1181
+ convs = pconv[inshift]
1182
+ meanconv = convs.mean(dim=0, keepdims=True)
1183
+ if (convs - meanconv).abs().max() < coarse_approx_error_threshold:
1184
+ # do something
1185
+ new_pconv.append(meanconv)
1186
+ old_ix_to_new_ix[inshift] = cur_new_ix
1187
+ cur_new_ix += 1
1188
+ continue
1189
+ # else:
1190
+ # # if we don't want the factorized thing...
1191
+ # new_pconv.append(convs)
1192
+ # old_ix_to_new_ix[inshift] = np.arange(cur_new_ix, cur_new_ix + inshift.size)
1193
+ # cur_new_ix += inshift.size
1194
+ # continue
1195
+
1196
+ active_temp_a = temp_ix_a[inshift]
1197
+ unique_active_temp_a = np.unique(active_temp_a)
1198
+
1199
+ # TODO just upsampling dedup
1200
+ # active_temp_b = temp_ix_b[inshift]
1201
+ # unique_active_temp_b = np.unique(active_temp_b)
1202
+ # if unique_active_temp_a.size == unique_active_temp_b.size == 1:
1203
+ if unique_active_temp_a.size == 1:
1204
+ new_pconv.append(convs)
1205
+ old_ix_to_new_ix[inshift] = np.arange(
1206
+ cur_new_ix, cur_new_ix + inshift.size
1207
+ )
1208
+ cur_new_ix += inshift.size
1209
+ continue
1210
+
1211
+ for tixa in unique_active_temp_a:
1212
+ insup = active_temp_a == tixa
1213
+ supconvs = convs[insup]
1214
+
1215
+ meanconv = supconvs.mean(dim=0, keepdims=True)
1216
+ if (convs - meanconv).abs().max() < coarse_approx_error_threshold:
1217
+ new_pconv.append(meanconv)
1218
+ old_ix_to_new_ix[inshift[insup]] = cur_new_ix
1219
+ cur_new_ix += 1
1220
+ else:
1221
+ new_pconv.append(supconvs)
1222
+ old_ix_to_new_ix[inshift[insup]] = np.arange(
1223
+ cur_new_ix, cur_new_ix + insup.sum()
1224
+ )
1225
+ cur_new_ix += insup.sum()
1226
+
1227
+ new_pconv = torch.cat(new_pconv, out=pconv[:cur_new_ix])
1228
+ return new_pconv, old_ix_to_new_ix
1229
+
1230
+
1231
+ # -- parallelism helpers
1232
+
1233
+
1234
+ @dataclass
1235
+ class ConvWorkerContext:
1236
+ template_data_a: TemplateData
1237
+ template_data_b: TemplateData
1238
+ low_rank_templates_a: LowRankTemplates
1239
+ low_rank_templates_b: LowRankTemplates
1240
+ compressed_upsampled_temporal: CompressedUpsampledTemplates
1241
+ template_shift_index_a: drift_util.TemplateShiftIndex
1242
+ template_shift_index_b: drift_util.TemplateShiftIndex
1243
+ cooccurrence: np.ndarray
1244
+ upsampled_shifted_template_index: UpsampledShiftedTemplateIndex
1245
+ geom: Optional[np.ndarray] = None
1246
+ reg_geom: Optional[np.ndarray] = None
1247
+ geom_kdtree: Optional[KDTree] = None
1248
+ reg_geom_kdtree: Optional[KDTree] = None
1249
+ do_shifting: bool = False
1250
+ match_distance: Optional[float] = None
1251
+ conv_ignore_threshold: float = 0.0
1252
+ coarse_approx_error_threshold: float = 0.0
1253
+ distance_kind: str = "rms"
1254
+ min_spatial_cosine: float = 0.0
1255
+ amplitude_scaling_variance: float = 0.0
1256
+ amplitude_scaling_boundary: float = 0.5
1257
+ reduce_deconv_resid_decrease: bool = False
1258
+ max_shift: Union[int, str] = "full"
1259
+ ignore_empty_channels: bool = False
1260
+ batch_size: int = 128
1261
+ device: Optional[torch.device] = None
1262
+
1263
+ def __post_init__(self):
1264
+ # to device
1265
+ self.compressed_upsampled_temporal.compressed_upsampled_templates = (
1266
+ torch.as_tensor(
1267
+ self.compressed_upsampled_temporal.compressed_upsampled_templates,
1268
+ device=self.device,
1269
+ )
1270
+ )
1271
+ self.low_rank_templates_a.spatial_components = torch.as_tensor(
1272
+ self.low_rank_templates_a.spatial_components, device=self.device
1273
+ )
1274
+ self.low_rank_templates_a.singular_values = torch.as_tensor(
1275
+ self.low_rank_templates_a.singular_values, device=self.device
1276
+ )
1277
+ self.low_rank_templates_a.temporal_components = torch.as_tensor(
1278
+ self.low_rank_templates_a.temporal_components, device=self.device
1279
+ )
1280
+ self.low_rank_templates_b.spatial_components = torch.as_tensor(
1281
+ self.low_rank_templates_b.spatial_components, device=self.device
1282
+ )
1283
+ self.low_rank_templates_b.singular_values = torch.as_tensor(
1284
+ self.low_rank_templates_b.singular_values, device=self.device
1285
+ )
1286
+ self.low_rank_templates_b.temporal_components = torch.as_tensor(
1287
+ self.low_rank_templates_b.temporal_components, device=self.device
1288
+ )
1289
+
1290
+
1291
+ _conv_worker_context = None
1292
+
1293
+
1294
+ def _conv_worker_init(rank_queue, device, kwargs):
1295
+ global _conv_worker_context
1296
+
1297
+ my_rank = rank_queue.get()
1298
+ if device is None:
1299
+ device = "cuda" if torch.cuda.is_available() else "cpu"
1300
+ device = torch.device(device)
1301
+ if device.type == "cuda" and device.index is None:
1302
+ if torch.cuda.device_count() > 1:
1303
+ device = torch.device("cuda", index=my_rank % torch.cuda.device_count())
1304
+
1305
+ _conv_worker_context = ConvWorkerContext(device=device, **kwargs)
1306
+
1307
+
1308
+ def _conv_job(unit_chunk):
1309
+ global _conv_worker_context
1310
+ units_a, units_b = unit_chunk
1311
+ return compressed_convolve_pairs(
1312
+ units_a=units_a, units_b=units_b, **asdict_shallow(_conv_worker_context)
1313
+ )
1314
+
1315
+
1316
+ def asdict_shallow(obj):
1317
+ return {field.name: getattr(obj, field.name) for field in fields(obj)}