Skip to content

Add copy_sorting option to SortingAnalyzer.create_memory#4668

Open
h-mayorquin wants to merge 3 commits into
SpikeInterface:mainfrom
h-mayorquin:add_copy_sorting_to_create_memory
Open

Add copy_sorting option to SortingAnalyzer.create_memory#4668
h-mayorquin wants to merge 3 commits into
SpikeInterface:mainfrom
h-mayorquin:add_copy_sorting_to_create_memory

Conversation

@h-mayorquin

@h-mayorquin h-mayorquin commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

create_memory always materializes the sorting into an in-memory NumpySorting via from_sorting(..., copy_spike_vector=True), which requests the whole spike vector up front and discards any laziness the sorting provides. This adds a copy_sorting parameter (default True, so existing behavior is unchanged); with copy_sorting=False the given sorting is kept as-is, so it stays lazy through analyzer construction and its spike times are read only on demand. This pairs directly with #4662, which keeps the NwbSortingExtractor spike vector lazy (nothing read until first requested): without this option create_memory immediately re-materializes exactly what #4662 made lazy, so the two are needed together for a streamed sorting to reach an analyzer without a full spike read. The immediate consumer is the streaming read_nwb_sorting_analyzer reader (#4645).

The payoff is building analyzers over large or streamed sortings where the spike vector is often never touched (template-and-metric analysis, batch surveys, memory-constrained builds). Materializing costs roughly three times the on-disk size in RAM and grows with the recording. Querying IBL processed files from dandiset 000409 (Brain Wide Map) by metadata only (via lindi), a single probe insertion runs 16-37M spikes: session 6713a4a7-faed-4df2-acab-ee4e63326f8d has 898 units and 20.7M spikes (~0.50 GB materialized), and session 4364a246-f8d7-4ce7-ba23-a098104b96e4 has 1202 units and 36.6M spikes (~0.88 GB), against only 18-25 MB of templates that template-and-metric workflows actually use. A survey across the hundreds of Brain Wide Map sessions would otherwise pay gigabytes of RAM and hundreds of gigabytes of reads for spikes it immediately discards. The default preserves today's behavior.

When copy_sorting=False, create_memory keeps the given sorting as-is instead of
materializing it into a NumpySorting via copy_spike_vector=True. This lets a lazy
sorting (e.g. a streamed NWB sorting) stay lazy: its spike times are read on demand
rather than pulled up front, which matters for large remote sortings where a view
may need only a few units' trains. Default stays True, so existing behavior is
unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@h-mayorquin h-mayorquin self-assigned this Jul 9, 2026
@h-mayorquin h-mayorquin added enhancement New feature or request core Changes to core module Edinburgh hackathon 2026 PRs from Edinburgh hackathon 2026 labels Jul 9, 2026
@h-mayorquin h-mayorquin requested a review from samuelgarcia July 9, 2026 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Changes to core module Edinburgh hackathon 2026 PRs from Edinburgh hackathon 2026 enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant