Skip to content

Blackrock: detect NEV clock resets per packet type - #1924

Merged
h-mayorquin merged 4 commits into
NeuralEnsemble:masterfrom
cboulay:nev-reset-per-packet-type
Nov 6, 2026
Merged

h-mayorquin merged 4 commits into
NeuralEnsemble:masterfrom
cboulay:nev-reset-per-packet-type

Conversation

@cboulay

@cboulay cboulay commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

BlackrockRawIO split a spec 2.3/3.0 .nev into a new segment whenever a packet's timestamp was earlier than the previous handled packet's, whatever the packets' types. The file does not order packets of different types: a comment is stamped when the user starts writing it but written to the file when they finish, so it can be a few ms later than the spikes written after it. Each such comment looked like a clock reset, so a recording with comments was split into many segments (in one real 3.0 file: 43 segments against 1 in the .ns3), and spikes outside the first segment were not returned.

This compares each packet only with the previous packet of its own type. A real reset sends every type's clock back, so each type jumps at its first packet afterwards; a jump counts only if the packet it is compared with is in the current segment, so one reset is still one boundary. Reset comments ("critical load restart") work as before.

Behavior on the existing test files is unchanged (same "3 undocumented segments" for the PTP-with-gaps file). The new test calls _get_event_segment_ids on synthetic packets and fails on the old code.

Idea and the original per-type implementation (for a streaming reader) are from Colin McNabb at Blackrock Neurotech.

A NEV packet started a new segment whenever its timestamp was earlier than
the previous handled packet's, whatever the type. The file does not order
packets of different types: a comment is stamped when the user starts
writing it but written when they finish, so it can be later than the spikes
written after it, and each such comment split the recording.

Compare each packet only with the previous packet of its own type. A real
reset sends every type's clock back, so a jump counts only if the packet it
is compared with is in the current segment: one reset is one boundary.

@h-mayorquin h-mayorquin 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.

I took a look at this and the solution, comparing each packet only with the previous packet of its own type to decide the resets, seems fine to me. I measured it and it does not make the reading slower (on a 2 GB spec 2.3 .nev the segment detection goes from 0.35 s to 0.25 s and the whole parse_header stays at about 15 s).

I am thinking about a more general solution for this in #1937 but in the meantime I think this is fine.

One point to discuss before we move forward. I already sent you an email with a data request for something else, but it would be great to have test data for this one too. I could not find any spec 3.0 file where comments split the recording like this (I wonder why). Is there any chance we could get a cut of the 3.0 file with 43 segments that you mention in the description? I can anonymize the metadata fields and shuffle the data if sharing it is a problem: what we need is only the structure of the packets.

Otherwise, we need to re-run the tests as I think the failures are unrelated.

@cboulay

cboulay commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

@h-mayorquin I can't share the recording with 43 segments, but I made a short one that shows the same thing, using a signal generator. There's no participant data in it, so no need to anonymize: https://github.com/cboulay/python-neo/releases/download/test-data-blackrock-ptp-comments/blackrock_ptp_reordered.zip (14 MB).

datafile001/ was recorded from a Gemini Hub1 and an NSP through Central, spec 3.0 (PTP), about 40 s long. Read .nev only (nsx_to_load=[]):

upstream master this PR (+ #1925)
Hub1-datafile001.nev 2 segments, 31,749 spikes returned 1 segment, 31,752
NSP-datafile001.nev 1 segment 1 segment

Hub1's split happens at a comment. Central stamps a comment when its event begins and writes it a few ms later, so the spike written next is earlier than the comment. The firmware stamps comments with the newest sample time, while spikes wait in a buffer of about 115 samples, so comments lead by 3 to 4 ms. That also explains the 3 spikes lost at the segment edge (#1925).

The same files also cover what you asked for by email:

  • a pause and resume (0xFFF9 reasons 2 and 3, plus 0 and 1);
  • a button trigger (0xFFFC, on the NSP);
  • comments.

I changed the spike filter and LNC during the recording, but no 0xFFFA was written. I don't yet know what Central needs to produce one.

make_reordered.py derives a copy whose packets are written out of order: timestamps right, position in the file wrong. We've seen this in real Hub recordings from Central. That's for a separate PR stacked on this one.

@h-mayorquin

h-mayorquin commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

[EDIT, I previously pasted the wrong draft]

Thanks for making this recording. It covers several cases that we did not have data for.

I stubbed it down and it is on gin now:
https://gin.g-node.org/NeuralEnsemble/ephy_testing_data/pulls/193

blackrock/version_3_0/time_structure/comment_written_before_earlier_spikes keeps the comments and the 96 spikes stamped before them. On master the .nev alone splits into two segments and with the .ns5 it returns 384 of its 960 spikes. With this PR it is one segment with all 960 spikes in both cases. The pause and resume and the button press are in their own stubs in the same gin PR.

Once the gin PR is merged, can you point the test here to that file?

@h-mayorquin

Copy link
Copy Markdown
Contributor

The gin PR is merged, so the test here can read blackrock/version_3_0/time_structure/comment_written_before_earlier_spikes now.

@antonsoo antonsoo 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.

Tested ece1d4ac against c5d0cfbd, plus a local merge with current master d50b9b95 retaining both added test methods.

On the shared signal-generator recording, NEV-only Hub1 reading changes from 2 segments / 31,749 spikes to 1 segment / 31,752. An independent struct decoder confirms every returned timestamp and waveform byte across all 99 units. The added synthetic test passes, as do 80 independent known-epoch cases covering asynchronous packet types, documented/undocumented resets and unsigned timestamps. All 32 selected native RawIO compliance checks pass on the current-master composition.

One integration case to cover while updating the recording test: loading the companion NS5 across its pause. With the composed branch:

from neo.rawio import BlackrockRawIO
r = BlackrockRawIO(
    filename="blackrock_ptp_reordered/datafile001/Hub1-datafile001.nev",
    nsx_to_load=[5], gap_tolerance_ms=0,
)
r.parse_header()
print([sum(r.get_spike_timestamps(seg_index=s, spike_channel_index=u).size
           for u in range(r.spike_channels_count()))
       for s in range(r.segment_count(0))])
# [31752, 0]

Decoding the NS5 sample timestamps directly gives two recording intervals containing 528 and 31,224 NEV spikes, with none outside those intervals. The second segment currently gets no spikes. Master already assigns them incorrectly ([25608, 6144]), so this is an existing PTP pause-reconciliation gap exposed by removing the false NEV boundary, rather than an objection to comparing packets within each type. _match_nsx_and_nev_segment_ids() currently only handles spec 2.3.

The older PR head additionally loses three trailing spikes with NS5 loaded; merging master includes #1925 and restores those, so that loss does not need another fix here. This is a scoped recording/segmentation check, not a whole-PR approval.

@h-mayorquin h-mayorquin 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.

Ok, I changed the tests to the one on gin and that works. As that was my only quest I will be merging this.

@h-mayorquin
h-mayorquin merged commit 540abbe into NeuralEnsemble:master Nov 6, 2026
3 checks passed
@cboulay
cboulay deleted the nev-reset-per-packet-type branch November 6, 2026 21:26
cboulay added a commit to cboulay/python-neo that referenced this pull request Nov 8, 2026
Central sends up to 300 samples (10 ms) per UDP datagram and the recording PC can write
datagrams out of order: the packets' timestamps are right, only their place in the file is not
(NeuralEnsemble#1937). Older .nev files also hold spikes of different electrodes out of order relative to one
another, by up to 15 ms, each electrode's own spikes in order (NeuralEnsemble#1755).

- PTP .nsX: a step back in the timestamps whose packets sort, within 10 ms, into an even grid
  (no duplicate, overlap or gap) is read in timestamp order: the few rows involved are remapped
  in _get_analogsignal_chunk and the timestamps, and the steps they made as written are no gaps.
  Other steps back stay inside the segment, as before. The grid is the criterion; 10 ms only
  bounds the search.
- NEV: a spike is compared only with the previous spike on its electrode (other packets with the
  previous packet of their type, as since NeuralEnsemble#1924). A step back of up to 10 ms is a packet written
  late, not a clock reset; each unit's spikes come in timestamp order. A larger step back is a
  reset, placed at the largest step back between consecutive packets of the type since that
  electrode's previous spike, so the spikes of other electrodes written after the reset are in
  the new segment.
- Counted in nsx_reordered_packets / nev_reordered_packets (late packets within a segment), with
  one warning each.

Tests: no new test functions. The "ptp with missing samples" file misses no samples (its ns4/ns6
have runs of 10/30 samples written after the next run, and its NEV a spike written after a later
one on its electrode), so the two gap tests now use pause_and_resume, a PTP file with a real 3.6 s
gap. The reordering is asserted in the existing tests on samples_out_of_order_in_ptp_timestamps
(and a copy with steps back that do not sort into the grid), on the missing-samples NEV, and with
synthetic spikes in test_nev_segments_ignore_order_between_packet_types (the 10 ms bound, and
electrodes out of order relative to one another).
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.

3 participants