Skip to content

Remove segment_index from the spike vector(s) #4816

Open

Description

@h-mayorquin

I am reviewing #4809 (optimize SpikeRetriever and RAM usage for extensions) and I want to bring back the topic of simplifying the spike vector (time ordered and unit ordered) data model. A lot of the memory cost on @alejoe91's benchmark is the spike vector itself and of that 1/3 is the segment index and the segment index is unnecessary. This is an edge case (most recordings are not multi-segment) and the field is only a convenience, syntactic sugar. These are the main places where we use it:

  1. To find the boundaries of a segment (the sorting already caches them as segment_slices, see to_spike_vector(return_slices=True)):

    s0, s1 = np.searchsorted(spikes["segment_index"], [segment_index, segment_index + 1])

    The same np.searchsorted is repeated three more times in waveform_tools.py and in basesorting.py, zarrextractors.py, sorting_tools.py and remove_excess_spikes.py.

  2. To mask the spikes of one unit in one segment (this could be a slice of the segment plus a mask on unit_index):

    spike_indices_in_seg = np.flatnonzero(
    (spikes["segment_index"] == segment_index) & (spikes["unit_index"] == unit_index)
    )

  3. As a sort key when building a sorting from peaks (the slices could be computed once at construction instead):

    order = np.lexsort((spikes["sample_index"], spikes["segment_index"]))

As a side effect this would remove the branching for the mono-segment case, like the special cases that #4809 adds to SpikeRetriever, PeakRetriever and _get_spike_vector_segment_slices.

The cost is a big refactor: using the slices wherever we rely on np.searchsorted (which I also think would be more efficient) and a compatibility path for the sortings that were saved with the field.

What do you think? Is there something I am missing?

Activity

  1. alejoe91 commented on Oct 5, 2026

    @alejoe91
    Member

    I agree that the segment_index adds a lot of extra complexity for very niche use cases.

    @samuelgarcia agrees too, but we need to discuss how to properly implement this!

  2. added
    questionGeneral question regarding SI
    coreChanges to core module
    discussionGeneral discussions and community feedback
    and removed
    questionGeneral question regarding SI
    on Oct 6, 2026
  3. JoeZiminski commented on Oct 31, 2026

    @JoeZiminski
    Contributor

    That's a nice idea, so instead a list of slices is maintained to get the spikes-by-segment? Am I correct the ordering is SEGMENT TIME UNIT in the time-ordered array, and becomes UNIT SEGMENT TIME in the unit-ordered array? but all units are from the same segment, and in segment order? so the slice transform should be fairly straightforward?

    Personally I would consider a long-term goal to remove the segment infrastructure. As you say its a rarely used case, its not fully supported by the codebase (e.g. motion estimation, KS4) but there is a lot of complexity and machinery added in the codebase to support it. I think for the most part the segment functionality could be fairly easily handled by the user working with a list of recordings?)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    coreChanges to core modulediscussionGeneral discussions and community feedback

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions