Repository navigation
Description
I was working under the assumption that the segments of a block are ordered in time, but looking at the documentation I can't find anything that says so. The closest is the Segment docstring, which says a segment holds data "sharing a common clock (time basis)" but nothing about how two segments relate.
In SpikeInterface, however, we seem to assume that they follow each other. Sorters that do not handle several segments ask the user to call si.concatenate_recordings, which joins the segments end to end in index order and drops their times:
https://github.com/SpikeInterface/spikeinterface/blob/93c612f51cca09ecafe2c23a283132cf0ad57869/src/spikeinterface/sorters/basesorter.py#L141-L145
I have also been relying on this in NeuroConv, where the units table joins the spike times of each unit across all segments into one array:
https://github.com/catalystneuro/neuroconv/blob/128578d4a78de3e115500cc8d349a6be44dcd4a7/src/neuroconv/tools/spikeinterface/spikeinterface.py#L2871-L2883
The purpose of this issue is that we agree on this. It can be closed once we make a decision and fix the documentation.
This is relevant because of Blackrock. The Blackrock clock can be reset in the middle of a recording, and right now BlackrockRawIO returns the part after the reset as a second segment that overlaps the first one in time. This is blackrock/segment/ResetCorrect from ephy_testing_data:
from neo.rawio import BlackrockRawIO
reader = BlackrockRawIO(filename="ephy_testing_data/blackrock/segment/ResetCorrect/reset", nsx_to_load=2)
reader.parse_header()
for seg_index in range(reader.segment_count(0)):
t_start = reader.segment_t_start(0, seg_index)
t_stop = reader.segment_t_stop(0, seg_index)
print(f"{seg_index=}, {t_start=:.4f}, {t_stop=:.4f}")seg_index=0, t_start=0.0000, t_stop=4.0200
seg_index=1, t_start=0.0032, t_stop=3.9842
I am wondering how to handle this better. I see two options:
- If segments are meant to be ordered in time: a reset starts a new block, and the documentation says that the segments of a block follow each other without overlapping.
- If they are not: a reset stays a new segment, the documentation says that segments can overlap, and code downstream (like the NeuroConv case above) has to stop assuming that they follow each other.
What do you think @alejoe91, @samuelgarcia and @zm711 ?
Activity
cboulay commentedon Oct 28, 2026
One data point from Blackrock spec 3.0 (PTP) files written by Central. Not every step back in timestamps is a clock reset, and treating them as resets is what produces most of the overlapping segments.
We found packets whose timestamps are correct but whose place in the file is wrong, most likely UDP datagrams reassembled out of order on the recording PC:
.ns6, one sample written 51 positions early;.ns3, one sample written 2 positions early;.nev, spikes written up to a few ms after later ones.Sorted by timestamp, they give a perfect sampling grid, with no duplicates and nothing missing. Today neo reads each of these as a gap or a reset. In a small test file this produced 4 segments instead of 2, and 7,681 of 31,752 spikes were lost. Comments stamped ahead of the spikes around them (#1924) are another source of false resets.
So I'd argue that before we decide what a reset means for blocks and segments, the reader should only call something a reset when it can't be explained as packets out of order. We have a change that reads these in timestamp order within a 10 ms window; anything larger, or overlapping, stays a reset. I'll open it as a PR stacked on #1924. With false resets out of the way, a true reset like
ResetCorrectis rare and unambiguous. My preference is that it starts a new block, so the segments of a block stay ordered and never overlap, which matches what SpikeInterface and NeuroConv already assume.h-mayorquin commentedon Oct 28, 2026
I agree that we need some tolerance and that not everything out of order is a reset (as you found for the nev in #1924). Initially I was thinking about adding yet another parameter so users could specify this but on a second thought I think it is better to handle it internally.
But I think we should be very principled about it, otherwise we could make a release that leaves users without recourse. In that vein, why did you choose 10 ms as the backward tolerance? What would be a good principle to move forward? You know the format and the acquisition better than us, so if you are convinced that it is a good value I can settle for that.
cboulay commentedon Oct 28, 2026
In recent weeks, I have told my colleagues multiple times to "never sort Blackrock data! The order is correct; if anything, the timestamp is wrong due to a clock adjustment."
Then I found that Trellis data where we'd occasionally get a subset of channels feeding into the data stream with a bit of a delay. "Fine, I guess their hardware works differently," I thought.
Now, I'm seeing out-of-order packets in the data streams saved by Central. I still can't explain it. It's the first time I've seen it, and it's only coming from one Gemini Hub at one user's site. I can't reproduce it. Claude is telling me it's because the user's network card is spreading the incoming UDP packets over several CPU queues and can reassemble out-of-order. I didn't know that was a thing. Sounds like a hallucination.
And the out-of-order packets in the .ns3 stream happen every 1.111 seconds. Surely that's got to be a bug in the code, either the hub or Central, right?
Anyway, putting my existential crisis aside for a moment, these data exist and we should be able to load them. Supporting these data certainly complicates loading the files by stream. Unfortunately, I don't have much of a choice there because I have other data where I have a single .nev file that is 90 GB and several in the 40 GB range, and I'm curating these data in the cloud on job runners with only 5 GB RAM.
cboulay commentedon Oct 28, 2026
To come back to your question, I chose 10 msec because it had to be larger than 1 msec as that's the average duration of data that fits into a datagram when running at 30 kHz, and I don't think we'd see this if running slower than 30 kHz. The largest out-of-order jump I've seen so far has been in spiking data at ~ 3 ms. Claude suggested 7 ms. I rounded up.
cboulay commentedon Oct 28, 2026
One more thing worth making clear: the 10 msec window isn't the criterion, it's just a search bounds. The criteria are that the out of order samples must fit into a timestamp gap, and after reordering we restore the timestamp grid.
h-mayorquin commentedon Nov 6, 2026
@cboulay (sorry for the late answer!) the 10 ms heuristic makes sense to me. As I understand it, the real principle is the grid: misplaced samples are only moved back when they fill empty positions and the sorted timestamps come back as a complete grid. The 10 ms is a heuristic from the format (one datagram holds at most 300 samples) that bounds the search, and we can rely on it. Let's document it as such.
I like the grid idea as I think it can go beyond Blackrock. Other formats could use the same check with their own search bound, which we should keep small for performance.
For the
.nevthere is no grid though, so there the 10 ms is the criterion itself. I think that is fine for now given how far apart resets and misplaced packets are (seconds against a few ms) but maybe we can think on something better on the future.h-mayorquin commentedon Nov 6, 2026
I discussed this with Sam a couple of days ago and we discussed it in the neo developers meeting today. First, Sam stated that the reason neo never said that the segments of a block are ordered in time is that in some formats we can't tell how the segments relate in time. It was not because of downstream analysis reasons.
From today's meeting we think that the segments of a block should follow each other in time without overlapping, and that when a format can't tell us how two pieces relate in time (like a Blackrock reset, where the time between the segments is lost) they go into different blocks. Across blocks we don't assume any time relation (@alejoe91 correct me if I am misremembering!).
I think this is a clean policy to follow. Things within a block are on the same clock and we provide the time organization from the format. When we can't infer that, let's move the data to other blocks.
cboulay commentedon Nov 6, 2026
FYI, last night I was able to reproduce the problem semi-reliably on my Windows machine, and trace the source of it in the Gemini Firmware. It's not really a bug in the firmware so much as a weird way that Windows behaves when it sees a certain datagram split, but the problem can be avoided by a change in firmware. Here is how I propose to fix it for online clients until a new firmware is released: CerebusOSS/CereLink#217
What are the implications for offline? Well, so far I only see this split-reordering when we're shipping a lot of data -- at least 30 samples per millisecond. If we are running a 30 kHz stream and nothing else, we see 3 samples that show up 0.9 msec too early. This looks like a 0.9-msec gap until we look ahead 3 samples and we see the 27 missing samples.
What if we're running 30 kHz + 2 kHz? In that 30-sample datagram, we should expect to find 1-2 samples from the 2 kHz stream (and 28-29 from the 30 kHz). If one of those two 2-kHz samples is in the small part of the split, it will get moved forward. So we'll see a 1.0-msec gap where we expect to see 0.5-msec, until we look ahead 1 sample (in the .ns3) and see the missing sample.
I think 10 msec is generous for the continuous data but it's still low enough to not catch false positives, especially if we add the resample-on-grid heuristic.
For spikes, I guess it depends on the spike rate and what else is filling the network buffer. If 30 kHz is being streamed then we'll probably see spike gaps < 1 ms. But if 30 kHz is not being streamed, then I guess the upper bound is how long a datagram can stick around before a sysprotocolmonitor packet flushes it. The largest I've seen is 3 msec.
Blackrock: read packets written out of order in timestamp order
Blackrock: read packets written out of order in timestamp order