Skip to content

Blackrock: read packets written out of order in timestamp order - #1958

Open
cboulay wants to merge 1 commit into
NeuralEnsemble:masterfrom
cboulay:nev-nsx-reordered-packets-v2
Open

cboulay wants to merge 1 commit into
NeuralEnsemble:masterfrom
cboulay:nev-nsx-reordered-packets-v2

Conversation

@cboulay

@cboulay cboulay commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

As discussed in #1937. Packets in Blackrock files are not always written in the order of their timestamps, and today BlackrockRawIO reads every step back as a gap or a clock reset. This reads them in timestamp order instead, so a step back is a reset only when it can't be explained by packets out of order. Closes #1755.

Two sources of out-of-order packets:

  • Central and a Gemini Hub (spec 3.0): Central sends up to 300 samples (10 ms) per UDP datagram, and the recording PC can write datagrams out of order (I traced it to the firmware; see Are the segments of a block ordered in time? #1937). The timestamps are right; only the place in the file is wrong.
  • Older .nev files (BlackrockIO can't read my nev files. #1755, spec 2.3): spikes on different electrodes are written up to 15 ms out of order relative to one another, while each electrode's own spikes are in order. That file reads as 178 segments against 1 in its ns6, and can't be loaded with the ns6. This is the case where @h-mayorquin suggested in BlackrockIO can't read my nev files. #1755 to "only segment if the data within the channel suffers a big jump".

The changes:

  • PTP .nsX: at a step back, the samples within 10 ms around it are sorted by timestamp. If no sample moves by more than 10 ms and the sorted samples lie on the sampling grid (each one half a period to two periods after the one before: no duplicate, overlap or gap), they are read in that order, and the forward steps they made as written are not gaps. The grid is the criterion; the 10 ms only bounds the search. Any other step back stays inside the segment, as on master since BlackrockRawIO: extend gap_tolerance_ms to the standard nsx formats #1880. Only the few rows involved are remapped, in _get_analogsignal_chunk and _get_blackrock_timestamps; the memory maps are unchanged.
  • .nev: a spike is compared only with the previous spike on its electrode; other packets are compared with the previous packet of their type, as since Blackrock: detect NEV clock resets per packet type #1924. A step back of up to 10 ms is a packet written late, not a reset, and each unit's spikes are returned in timestamp order. A larger step back is a reset. A reset sends every electrode's clock back, so it is still found (the rule from Adapting BlackrockIO for new features of version 2.3 #523), and it is placed at the largest step back between consecutive spikes since that electrode's previous spike. That way the spikes of other electrodes written after the reset are in the new segment. For the .nev there is no grid, so the 10 ms is the criterion itself, as Heberto noted in Are the segments of a block ordered in time? #1937.
  • Both are counted, in nsx_reordered_packets (per nsX) and nev_reordered_packets (late packets within a segment), with one warning each.

On every Blackrock recording in ephy_testing_data, segments and spikes are the same as on master except for the two PTP files below (one recording in two copies) and the reordered recording mentioned at the end. That includes the reset, pause and multi-block files.

Tests: no new test functions.

  • blackrock_ptp_with_missing_samples misses no samples: its ns4 and ns6 have runs of 10 and 30 samples written after the next run, and its .nev has a spike written 1.7 ms after a later spike on the same electrode. So test_get_blackrock_timestamps_ptp_with_gaps and test_gap_tolerance_ms_parameter now use version_3_0/time_structure/pause_and_resume, a PTP file with a real 3.6 s gap. They pass on master too.
  • test_ptp_backward_steps_stay_inside_the_segment (samples_out_of_order_in_ptp_timestamps, the same streams) also checks the counts and that samples and timestamps come in timestamp order, whole and in windows that start or end inside a moved run. It also checks that a step back that does not sort into the grid stays as written: in a copy of the ns6, one sample written 13 ms early and one written twice.
  • test_nev_spikes_at_segment_edges_ptp also checks that the late spike is not a reset (one segment) and that each unit's spikes are in timestamp order.
  • test_nev_segments_ignore_order_between_packet_types also checks, with synthetic spikes, that a spike stepping back 10 ms is not a reset, 10 ms plus one tick is, and a spike on another electrode 15 ms behind is not compared.

The file attached to #1755 now reads as one segment and loads with its ns6. It may be worth adding to ephy_testing_data, with the reporter's permission. I also have a signal-generator recording from a Gemini Hub and an NSP, with a copy that has packets moved out of order the way the recording PC was seen to write them; both read the same. It is in a zip, with the script that makes the copy, at https://github.com/cboulay/python-neo/releases/tag/test-data-blackrock-ptp-comments.

This should unblock the chunked .nev reading Heberto described in #1955. It touches the same loop in _get_event_segment_ids as #1959.

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).

This branch has not been deployed

No deployments
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.

BlackrockIO can't read my nev files.

1 participant