Skip to content

What should to_reordered_spike_vector do? #4683

Closed

Description

@grahamfindlay

@alejoe91 @samuelgarcia @zm711 @chrishalcrow @h-mayorquin

Prior to #4606 , the vanilla spike vector was lexsorted on keys ("unit_index", "sample_index", "segment_index"). Now it is lexsorted on ("sample_index", "segment_index"). This raises a question about what the behavior of to_reordered_spike_vector should be.

Here is the function signature:

def to_reordered_spike_vector(
        self,
        lexsort=("sample_index", "segment_index", "unit_index"), 
        return_order=True,
        return_slices=True,
    ):
  • lexsort is the requested sort order, as keys would be passed to np.lexsort.
  • return_order=True requests that the numpy array needed to sort the spike vector (given the requested sort) is also returned.
  • return_slices=True: requests the numpy array of size (num_units, num_segments, 2) or (num_segments, num_units, 2) given the lexsort, where one can obtain the indices amin, amax of all the (segment, unit_index) values.

This function mainly exists to reorder the vanilla spike vector to ("sample_index", "segment_index", "unit_index"), so unit spike trains are compact in memory. In the current codebase, it is only ever called as to_reordered_spike_vector(lexsort=("sample_index", "segment_index", "unit_index"), ...). Nevertheless, it does allow users to request lexsort=("sample_index", "unit_index", "segment_index") or lexsort=("unit_index", "sample_index", "segment_index"). It is this last option that presents a dilemma.

Previously if a user requested lexsort=("unit_index", "sample_index", "segment_index") this just built the vanilla spike vector (or fetched it from the cache). There was nothing to reorder, and no compact 2D (unit, segment) table could possibly exist for that ordering, so return_order and return_slices had to be False.

Currently, if a user requests lexsort=("unit_index", "sample_index", "segment_index"), the same thing happens (vanilla spike vector retrieved), except now the spike vector that gets returned doesn't match the requested lexsort order. The function implies a tie-break that isn't there. This is the issue.

Question A: How to resolve this? Some options:

  1. Don't allow the user to request lexsort=("unit_index", "sample_index", "segment_index"). If they want the vanilla spike vector, make them call to_spike_vector.
  2. Don't allow the user to request lexsort=("unit_index", "sample_index", "segment_index"), but allow lexsort=("sample_index", "segment_index") as a way to get the vanilla spike vector.
  3. Allow the user to request lexsort=("unit_index", "sample_index", "segment_index"), and actually do the lexsorting (and allow return_order=True).

My vote would be for (1).

Question B (minor): Should lexsort=("sample_index", "unit_index", "segment_index") be allowed, since it seems to be seldom (if ever) used?

  1. Yes
  2. No

If it were not allowed, then we could refer to the reordered spike vector, rather than a reordered spike vector (and have to specify which ordering). If it stays allowed, I would like to mention what it is used for in the docstring.

My naive vote would be for (2), but I suspect there is a good reason for (1) that I am unaware of.

Activity

  1. alejoe91 commented on Jul 14, 2026

    @alejoe91
    Member

    Hi @grahamfindlay

    Thanks for the recap!

    Question A: I vote for (1) too. The to_reordered_spike_vector should only be used to get non-vanilla ordering

    Question B: I vote for (2). There is no harm in allowing it.

  2. h-mayorquin commented on Jul 20, 2026

    @h-mayorquin
    Contributor

    I would vote option 1 on Question A: to_reordered_spike_vector should never return the vanilla vector. If you want the time-ordered vector, call to_spike_vector. The pass-through branch only ever made sense because the vanilla vector used to be fully sorted in that order, and #4606 removed that; keeping it means advertising an order guarantee the data no longer satisfies.

    On Question B, dropping ("sample_index", "unit_index", "segment_index") is fine by me; no caller uses it and the unit-grouped ordering already handles segments via its slices table.

    More broadly, I think the confusion is terminological. I think a canonical order and remaping is doing us harm. There are two orderings we actually need, time_ordered and unit_grouped, plus a permutation to move between them (the same permutation that keeps per-spike extensions like amplitudes and locations aligned). I would rather name them as two first-class views, time_ordered_spikes() and unit_grouped_spikes(), than expose an open lexsort parameter that opens the door to questions like this one. I also think spike_vector is ambiguous in the same way, since the name does not say which ordering you get. I say "views" deliberately: they need not both be materialized copies, since some formats can offer one ordering natively or lazily (NWB's units table is already unit-grouped, zarr can serve ordered reads without a full in-memory sort).

    With named views, each method can be explicit about which ordering it needs to operate on, and each extractor can be specific about which view it materializes in memory. We could then compute a view on demand rather than always routing through a full spike_vector materialization to calculate everything, which is what makes the current path memory-inefficient.

  3. grahamfindlay commented on Jul 20, 2026

    @grahamfindlay
    CollaboratorAuthor

    @h-mayorquin I agree that simply having time_ordered and unit_grouped views, or something along those lines, would be better if we had a fresh start.

    Is there appetite for such a sweeping change? What would happen to to_reordered_spike_vector and, more importantly, to to-spike_vector? Make them thin wrappers for the time_ordered and unit_grouped spikes, and add deprecation warnings to them? Would a backward compatibility layer be needed to handle loading old spike vectors persisted to disk?

    If such a big change were welcome, I would be happy to work on it as I am able.

  4. h-mayorquin commented on Jul 20, 2026

    @h-mayorquin
    Contributor

    Thanks @grahamfindlay. I think we have a fair amount to iron out first. One design concern (at least for me) is how we support streaming, in particular avoiding reading the whole sorting into memory at initialization the way we do today (see #4702 which is related). With that in mind, I think it's worth continuing to develop the concrete use cases and documenting the rationale in issues, so we can take them into account once (and if) we reach consensus.

  5. grahamfindlay commented on Nov 21, 2026

    @grahamfindlay
    CollaboratorAuthor

    Closing, because this was mostly addressed in #4695, and what remains to discuss is now #4851.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions