Repository navigation
Description
@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,
):lexsortis the requested sort order, as keys would be passed tonp.lexsort.return_order=Truerequests 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 indicesamin, amaxof 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:
- Don't allow the user to request
lexsort=("unit_index", "sample_index", "segment_index"). If they want the vanilla spike vector, make them callto_spike_vector. - Don't allow the user to request
lexsort=("unit_index", "sample_index", "segment_index"), but allowlexsort=("sample_index", "segment_index")as a way to get the vanilla spike vector. - Allow the user to request
lexsort=("unit_index", "sample_index", "segment_index"), and actually do the lexsorting (and allowreturn_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?
- Yes
- 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
alejoe91 commentedon Jul 14, 2026
Hi @grahamfindlay
Thanks for the recap!
Question A: I vote for (1) too. The
to_reordered_spike_vectorshould only be used to get non-vanilla orderingQuestion B: I vote for (2). There is no harm in allowing it.
Restrict to_reordered_spike_vector to non-vanilla orderings (SpikeInt…
h-mayorquin commentedon Jul 20, 2026
I would vote option 1 on Question A:
to_reordered_spike_vectorshould never return the vanilla vector. If you want the time-ordered vector, callto_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_orderedandunit_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()andunit_grouped_spikes(), than expose an openlexsortparameter that opens the door to questions like this one. I also thinkspike_vectoris 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_vectormaterialization to calculate everything, which is what makes the current path memory-inefficient.grahamfindlay commentedon Jul 20, 2026
@h-mayorquin I agree that simply having
time_orderedandunit_groupedviews, 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_vectorand, more importantly, toto-spike_vector? Make them thin wrappers for thetime_orderedandunit_groupedspikes, 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.
h-mayorquin commentedon Jul 20, 2026
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.
Restrict to_reordered_spike_vector to non-vanilla orderings (SpikeInt…
Restrict to_reordered_spike_vector to non-vanilla orderings (SpikeInt…
grahamfindlay commentedon Nov 21, 2026
Closing, because this was mostly addressed in #4695, and what remains to discuss is now #4851.