Repository navigation
Blackrock: detect NEV clock resets per packet type - #1924
Conversation
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
left a comment
There was a problem hiding this comment.
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.
|
@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).
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:
I changed the spike filter and LNC during the recording, but no
|
|
[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:
Once the gin PR is merged, can you point the test here to that file? |
|
The gin PR is merged, so the test here can read |
antonsoo
left a comment
There was a problem hiding this comment.
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.
…ket-type # Conflicts: # neo/test/rawiotest/test_blackrockrawio.py
h-mayorquin
left a comment
There was a problem hiding this comment.
Ok, I changed the tests to the one on gin and that works. As that was my only quest I will be merging this.
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).
BlackrockRawIOsplit a spec 2.3/3.0.nevinto 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_idson 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.