Skip to content

BlackrockRawIO: read .nev spike waveforms lazily - #1955

Merged
h-mayorquin merged 1 commit into
NeuralEnsemble:masterfrom
h-mayorquin:blackrock_lazy_waveform_reading
Nov 8, 2026
Merged

h-mayorquin merged 1 commit into
NeuralEnsemble:masterfrom
h-mayorquin:blackrock_lazy_waveform_reading

Conversation

@h-mayorquin

Copy link
Copy Markdown
Contributor

This is the second step of the plan I described in #1931 after #1940 read the headers through an open file. Right now _read_nev_data copies every spike packet with its waveform at parse. This PR keeps a small table per spike instead (timestamp, electrode, unit and the position of its packet in the file) and get_spike_raw_waveforms reads the waveforms of the requested unit from those positions. It also selects by t_start and t_stop before reading so a time range only reads the waveforms inside it.

On the 2.24 GB Trellis file from #1941 the memory held after parse goes from 2.46 GB to 0.44 GB. Parse goes from 54 s to 33 s, the timestamps of all 512 units from 85 s to 21 s and their waveforms from 93 s to 49 s (Linux laptop, spinning disk). The spikes and waveforms are the same as master on that file and on every Blackrock recording on gin.

The peak during parse is still about the size of the file. Finding the spikes needs the packet id of every packet and those are read through a memory map of the whole file. The kernel can drop these pages but they count as resident memory of the process. What I would like to do after this:

  1. Read the packets in chunks with file reads instead of the memory map. The peak would then follow what the reader holds and the .nev parse would work on any file object, a step towards remote reading without a second code path as discussed in Reading Blackrock recordings from fsspec URLs: is remote reading in scope for rawio? #1931. This needs _get_event_segment_ids to work from the arrays of each packet type instead of the whole file. That is the function the reordering proposed in Are the segments of a block ordered in time? #1937 will change. So I will wait for that PR.
  2. Make the table smaller. The segment id is stored as int64 per spike, 8 of its 19 bytes.

This should close #1941.

@h-mayorquin

Copy link
Copy Markdown
Contributor Author

@cboulay could you take a look

@cboulay cboulay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is the shape I hoped for. I checked it against master on every Blackrock file in ephy_testing_data that opens (39 of them). Spike timestamps and waveforms are identical for every unit and segment, both with no time range and with each half of the segment as t_start/t_stop. The Blackrock tests pass locally.

One thing for the follow-ups: the peak during parse is not only the memory map. _get_event_segment_ids builds event_segment_ids as an int64 per packet, not per spike, and masks keeps one bool array per packet type alive through the loop. That is 8 bytes per packet plus 1 per type, about 15 bytes per packet for 3.0 with its 7 types. On our 90 GB .nev files (about 0.9 billion packets) that is around 13 GB before any page of the map counts. If _get_event_segment_ids returned the boundary positions instead, each type could get its ids with np.searchsorted(boundaries, positions, side="right") on its own positions, and no array would be the length of the file. That also fits your chunked-read step, since a chunk only needs the boundaries found so far.

Smaller things:

  • nev_data["Spikes"] no longer has a waveform field. The attribute isn't private and our code read it, so it is worth a line in the release notes.
  • Each get_spike_raw_waveforms call maps the whole data region again. Harmless on 64-bit, just noting it.
  • A test that reads a unit's waveforms with t_start/t_stop and compares them with a slice of the full read would pin the new order (slice, then read).

cboulay added a commit to cboulay/python-neo that referenced this pull request Nov 7, 2026
…ork only)

Fork-only per python-neo#1931 (remote reading stays out of rawio for now). Rebuilt on
upstream's open-file headers (NeuralEnsemble#1940), merged NSx blocks (NeuralEnsemble#1880) and lazy NEV waveforms
(NeuralEnsemble#1955), replacing the streaming series of blackrock-integration-2026-11-07.

- A filename with "://" opens through fsspec (storage_options); local files keep every
  upstream code path, memory maps included. fsspec is the optional "remote" extra.
- Files open through _open/_exists/_get_file_size, so NeuralEnsemble#1940's header reads work remotely.
- Signal chunks and PTP timestamps of a remote file read only the requested rows, block by
  block for a segment that merges several (_read_remote_chunk, _read_rows).
- A remote PTP .nsX finds its gaps and steps back with a scan in pieces (_scan_ptp_ticks),
  with the whole-array arithmetic, so the segments and gap report are the same.
- A remote NEV (but 2.3, which needs every packet to match segments with the NSx) is scanned
  in pieces at open: events are kept, spikes are summarized (units, counts per segment and
  unit, segment times) in _nev_spikes; segment boundaries follow _get_event_segment_ids
  (per packet type, comments skipped, packets up to 10 ms late not resets). One unit's spikes
  are read with another pass (_spike_records, NeuralEnsemble#1955's table). Unmatched packets are counted
  across the pieces.
- _iter_nev_pieces, _nev_masks_and_types and _spike_waveforms stay for connectome-curate,
  which streams every spike in one pass. iter_spikes() is gone (NeuralEnsemble#1931: not wanted upstream).

Dropped from the old branch: the buffer description for standard NSx (unused; NeuralEnsemble#1880's merged
segments are not one region) and the gap-scan helper in rawio.utils (the scan now reuses
_detect_timestamp_gaps).
cboulay added a commit to cboulay/python-neo that referenced this pull request Nov 7, 2026
cboulay added a commit to cboulay/python-neo that referenced this pull request Nov 7, 2026
…ork only)

Fork-only per python-neo#1931 (remote reading stays out of rawio for now). Rebuilt on
upstream's open-file headers (NeuralEnsemble#1940), merged NSx blocks (NeuralEnsemble#1880) and lazy NEV waveforms
(NeuralEnsemble#1955), replacing the streaming series of blackrock-integration-2026-11-07.

- A filename with "://" opens through fsspec (storage_options); local files keep every
  upstream code path, memory maps included. fsspec is the optional "remote" extra.
- Files open through _open/_exists/_get_file_size, so NeuralEnsemble#1940's header reads work remotely.
- Signal chunks and PTP timestamps of a remote file read only the requested rows, block by
  block for a segment that merges several (_read_remote_chunk, _read_rows).
- A remote PTP .nsX finds its gaps and steps back with a scan in pieces (_scan_ptp_ticks),
  with the whole-array arithmetic, so the segments and gap report are the same.
- A remote NEV (but 2.3, which needs every packet to match segments with the NSx) is scanned
  in pieces at open: events are kept, spikes are summarized (units, counts per segment and
  unit, segment times) in _nev_spikes; segment boundaries follow _get_event_segment_ids
  (per packet type, comments skipped, packets up to 10 ms late not resets). One unit's spikes
  are read with another pass (_spike_records, NeuralEnsemble#1955's table). Unmatched packets are counted
  across the pieces.
- _iter_nev_pieces, _nev_masks_and_types and _spike_waveforms stay for connectome-curate,
  which streams every spike in one pass. iter_spikes() is gone (NeuralEnsemble#1931: not wanted upstream).

Dropped from the old branch: the buffer description for standard NSx (unused; NeuralEnsemble#1880's merged
segments are not one region) and the gap-scan helper in rawio.utils (the scan now reuses
_detect_timestamp_gaps).
cboulay added a commit to cboulay/python-neo that referenced this pull request Nov 8, 2026
cboulay added a commit to cboulay/python-neo that referenced this pull request Nov 8, 2026
…ork only)

Fork-only per python-neo#1931 (remote reading stays out of rawio for now). Rebuilt on
upstream's open-file headers (NeuralEnsemble#1940), merged NSx blocks (NeuralEnsemble#1880) and lazy NEV waveforms
(NeuralEnsemble#1955), replacing the streaming series of blackrock-integration-2026-11-07.

- A filename with "://" opens through fsspec (storage_options); local files keep every
  upstream code path, memory maps included. fsspec is the optional "remote" extra.
- Files open through _open/_exists/_get_file_size, so NeuralEnsemble#1940's header reads work remotely.
- Signal chunks and PTP timestamps of a remote file read only the requested rows, block by
  block for a segment that merges several (_read_remote_chunk, _read_rows).
- A remote PTP .nsX finds its gaps and steps back with a scan in pieces (_scan_ptp_ticks),
  with the whole-array arithmetic, so the segments and gap report are the same.
- A remote NEV (but 2.3, which needs every packet to match segments with the NSx) is scanned
  in pieces at open: events are kept, spikes are summarized (units, counts per segment and
  unit, segment times) in _nev_spikes; segment boundaries follow _get_event_segment_ids
  (per packet type, comments skipped, packets up to 10 ms late not resets). One unit's spikes
  are read with another pass (_spike_records, NeuralEnsemble#1955's table). Unmatched packets are counted
  across the pieces.
- _iter_nev_pieces, _nev_masks_and_types and _spike_waveforms stay for connectome-curate,
  which streams every spike in one pass. iter_spikes() is gone (NeuralEnsemble#1931: not wanted upstream).

Dropped from the old branch: the buffer description for standard NSx (unused; NeuralEnsemble#1880's merged
segments are not one region) and the gap-scan helper in rawio.utils (the scan now reuses
_detect_timestamp_gaps).
@h-mayorquin

Copy link
Copy Markdown
Contributor Author

Thanks

I think the test you suggested should be a general check. It applies to every reader that returns waveforms and right now none of them is tested with a time range. I opened an issue to add it to the compliance tests:
#1960

Concerning the memory during parse, I agree, I have a solution let me open a PR just now, I will tag you.

On your two smaller points:

  • Yes about the notes, nev_data["Spikes"] no longer has the waveform field. But nev_data is not documented and I don't think it was meant to be public. Many attributes of the readers are public without being part of the API, and I would like to make them private at some point.
  • Mapping the data region on every call is fine for now.

@h-mayorquin
h-mayorquin merged commit 57f8d78 into NeuralEnsemble:master Nov 8, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BlackrockRawIO: read .nev spike waveforms lazily instead of copying them at parse

2 participants