Skip to content

Uniform handling for stream/channel identifier changes across readers #1879

Open

Description

@h-mayorquin

This is a follow-up of #1604 ("Consistency of stream_index in neo.rawio") and comes from a discussion with @zm711 in #1874.

We need to provide consistency in the API for users to address our data-fetching methods (get_analogsignal_chunk, get_spike_timestamps, get_event_timestamps, rescale_signal_raw_to_float, and the future timestamps API discussed in #1773). Currently the API for most methods exposes addressing by index (stream_index, channel_indexes, spike_channel_index, event_channel_index) and, for signals, by id/name (channel_ids, channel_names). Both have changed before (Neuralynx #1786 / #1679, OpenEphysBinary #1624 / #1668 / #1753, SpikeGLX #1683 / #1661, Intan #1854 / #1558, Axon #1868 / #1874, EDF #1604, NeuroNexus #1711, Maxwell #1579, Plexon #1547) and this probably broke some user workflows. This is an ongoing effort, and while we try to provide backward compatibility (see the acrobatics I did on SpikeGLX to append the sync streams last and keep the stream order stable), it is on a best-effort basis and I am unsure how good a job we have been doing.

I would like to improve this and offer a contract that users can rely on. My proposal is that we do that only by id and not by index. The reason is that supporting index would 1) double the work and 2) force us to couple the API to our internal structures (an index is usually an internal detail of how the data is represented). Within that scope I think we should introduce new methods that are both stable and addressed only by id:

Two notes:

  • I am omitting block_index, segment_index, i_start/i_stop, t_start/t_stop and similar arguments from the signatures above; they stay the same in the new methods. This is only about how we address streams and channels. (The _by_id names are placeholders, up for discussion.)
  • block_index and segment_index stay as indices on purpose. segment_index has no stable id to map to, because the number of segments is user-selection-dependent (it depends on the gap tolerance in the proposed timing API, #1773), so index is the only meaningful handle. block_index is a different axis that does not suffer this identifier churn, and multi-block is rare, so backward compatibility there is essentially free.

This will give us stable methods while keeping the current index-based ones, which are called inside them.

All of that said, it is a fact that the id/string identifiers need to change from time to time. Two examples:

  1. We decided to move from streams grouped operationally (same sampling rate, dtype, and number of samples) to semantically meaningful streams, e.g. separating channels that have different physical units (the EDF case, where an EEG channel in uV and a respiration channel in a different unit end up in the same stream today) or by purpose (separating the sync channel in SpikeGLX, #1683). So we need a way to change things, and this has been happening.

  2. We discovered on SpikeGadgets (#1847) that our names for the Neuropixels channels were wrong because they had been derived from the Intan case, so we needed to change them to be faithful to the acquisition (I described the exact id/name change here).

So on top of the new contract I also propose that we add machinery that works as a backward-compatibility layer: a stream and channel legacy map that captures old string identifiers, maps them to the new ones, and throws a warning. When this is not possible, e.g. the retired stream's channels are now split across streams with different sampling rates (so no single coherent chunk exists) or a channel was removed entirely, it should throw an informative error instead.

As a concrete example, I think the current EDF streams should be more specialized to be semantically meaningful. EDF currently groups channels by sampling rate, so one stream can mix an EEG channel in uV with a channel in a different unit. The proposal is to group by physical unit so each stream is homogeneous, and attach a legacy map from the old sampling-rate-based stream_id to the new unit-based streams, so old lookups still resolve and emit a warning pointing to the new streams.

Illustrative snippets (abstract, not final API)

EDF, group by physical unit and map the retired stream id:

# today: one stream per sampling rate, so units get mixed
streams = [("256Hz", "0")]            # stream "0" = EEG (uV) + SpO2 (%) together

# proposed: one stream per physical unit, plus a legacy map for the old id
streams = [("EEG", "eeg"), ("SpO2", "spo2")]
self._legacy_stream_mapper = LegacyStreamMapper({
    "0": {"channel_ids": [...], "since": "0.15",
          "reason": "streams now grouped by physical unit"},
})
# a lookup of the retired stream "0" still resolves, with a DeprecationWarning.

Axon, keep the real channel name and map the retired one:

# today: padding-stripping mangles multi-word names
name = raw.replace(b" ", b"")         # "IN 1" -> "IN1"

# proposed: keep the real name, map the retired one
name = raw.strip()                    # "IN 1"
self._legacy_channel_mapper = LegacyChannelMapper({
    "IN1": {"channel_id": "IN 1", "since": "0.15",
            "reason": "names now keep interior spaces"},
})
# addressing by the old "IN1" still resolves (warning); a removed channel raises.

Activity

  1. zm711 commented on Jul 15, 2026

    @zm711
    Contributor

    I like this in general, but let's see what @alejoe91 and @apdavison say since they weigh in the most on these things. We will badger @samuelgarcia to look at this too.

  2. h-mayorquin commented on Jul 15, 2026

    @h-mayorquin
    ContributorAuthor

    Let's discuss this in a meeting. I think that's the right place for it.

  3. h-mayorquin commented on Oct 12, 2026

    @h-mayorquin
    ContributorAuthor

    @zm711 following up on the id-only contract proposed here after our meeting discussion.

    I took a look at what the code enforces right now concerning uniqueness of ids, and the documentation contradicts itself.

    The user guide says that channel ids are not guaranteed to be unique and that index is the only unambiguous way to select channels:

    There are 3 ways to select a subset of channels: by index (0 based), by id or by name.
    By index is unambiguous 0 to n-1 (inclusive), whereas for some IOs channel_names
    (and sometimes channel_ids) are not guaranteed to be unique.
    In such cases, using names or ids may raise an error.

    However, the module docstring of baserawio.py says that channel_id must be unique within a stream:

    Channels within a stream can be accessed be either their channel_id, which must be unique within
    a stream, or by their channel_index, which is a 0 based index to all channels within the stream.

    And this is enforced. Every call to parse_header() runs _check_stream_signal_channel_characteristics() which throws an error if a stream has repeated channel ids:

    # also check that channel_id is unique inside a stream
    channel_ids = signal_channels[mask]["id"]
    if np.unique(channel_ids).size != channel_ids.size:
    raise ValueError(f"signal_channels do not have unique ids for stream {stream_index}")

    So I think the user guide is outdated for ids (as Sam said).

  4. h-mayorquin commented on Oct 12, 2026

    @h-mayorquin
    ContributorAuthor

    Now, I also took a look at the rest of the codebase and this is the state of affairs:

    • signal_streams and signal_buffers ids are documented as "must be unique" but they are only checked in the compliance tests, not at runtime:
      stream_ids = h["signal_streams"]["id"]
      if stream_ids.size > 0:
      assert stream_ids.size == np.unique(stream_ids).size
      buffer_ids = h["signal_buffers"]["id"]
      if buffer_ids.size > 0:
      assert buffer_ids.size == np.unique(buffer_ids).size
    • signal_channels ids are unique within a stream and checked at runtime (above). The same channel id can appear in two streams by design:
      Note that a single channel of recording may be represented within multiple streams, and such is
      the case for RawIOs which may have both unfiltered and filtered or downsampled versions of the
      signals from a single recording channel. In such a case, a single channel and channel_id may be
      represented by a different channel_index within different streams. Lists of channel_indexes are
    • spike_channels and event_channels ids are not documented as unique and are not checked anywhere:
      _spike_channel_dtype = [
      ("name", "U64"),
      ("id", "U64"),
      # for waveform
      ("wf_units", "U64"),
      ("wf_gain", "float64"),
      ("wf_offset", "float64"),
      ("wf_left_sweep", "int64"),
      ("wf_sampling_rate", "float64"),
      ]
      # in rawio event and epoch are handled the same way
      # except, that duration is `None` for events
      _event_channel_dtype = [
      ("name", "U64"),
      ("id", "U64"),
      ("type", "S5"), # epoch or event
      ]

    The header dtypes already describe the split I would like to make the explicit contract*, with id as "must be unique" and name as "not necessarily unique":

    _signal_buffer_dtype = [
    ("name", "U64"), # not necessarily unique
    ("id", "U64"), # must be unique
    ]
    # To be left an empty array if the concept of buffer is undefined for a reader.
    _signal_stream_dtype = [
    ("name", "U128"), # not necessarily unique
    ("id", "U64"), # must be unique
    (
    "buffer_id",
    "U64",
    ), # should be "" (empty string) when the stream is not nested under a buffer or the buffer is undefined for some reason.
    ]
    _signal_channel_dtype = [
    ("name", "U64"), # not necessarily unique
    ("id", "U64"), # must be unique
    ("sampling_rate", "float64"),

    That is:

    • id is the identity and must be unique (stream ids within the reader, signal channel ids within their stream, spike and event channel ids within the reader).
    • index is a positional detail of the current parsing and we don't promise to keep it stable across versions, as in point 2 of the issue.
    • name is the human readable label and does not need to be unique.
  5. zm711 commented on Oct 12, 2026

    @zm711
    Contributor

    So ID is unique, but without guarantees of format (ie is format-specific). My ID might be 1,2,3,4 or it might be 0,1,2,3 or do we guarantee that it is always 0,1,2,3 so I can fix my doc PR?

  6. h-mayorquin commented on Oct 13, 2026

    @h-mayorquin
    ContributorAuthor

    Yes, IDs are only unique. Intan for example as the format A-{number} so they are very different per format. In fact, if I could I will avoid integers as strings as in your example above but maybe some formats do have that format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions