Skip to content

Collect fast array routines in one module, each tested against a reference #4963

Todo

Description

@galenlynch

Several recent PRs replace a numpy or scipy call with a faster implementation of the same operation. @JoeZiminski suggested on #4855 and #4854 that these live in one module, each tested directly against the routine it replaces. I agree, and I'd like to settle the layout here before #4854 sets a precedent.

Proposal

  • One module, such as spikeinterface.core.numerics. I'd avoid kernels, which already means filter taps in preprocessing, and algorithms, since most entries are operations like a median rather than algorithms.
  • One file per operation, not per backend. numerics/median.py would hold the public row_median, its numba path, and the rules choosing between that path and np.median (dtype, channel count). Split into numpy/ and numba/, a reader would find the fast path and its reference in different directories, and the dispatch rules in neither.
  • Numba stays optional. Each file imports it lazily and falls back to the reference.
  • Each test compares against the reference over a stated input domain: dtypes, value ranges and edge cases such as NaN, inf and saturated channels. The test docstring lists them.
  • Only generic array routines move. Code built on SpikeInterface structures, such as the amplitude-scaling collision fit, which reads the spike vector and sparsity, stays with its caller.

Everything in the module would compute the same result as its reference, to rounding or a stated tolerance. A step that deliberately computes something different, such as a FIR instead of an IIR filter, is a choice of method and is tested against an accuracy spec in its own module.

Candidates

operation now in reference
row median preprocessing/_row_median.py (#4854) np.median
FIR convolution by overlap-save preprocessing/fir_filter.py (#4855) scipy.signal.oaconvolve
per-channel FIR for the phase shift preprocessing/phase_shift.py (#4563) scipy.ndimage.correlate1d per channel
NNLS on a Gram matrix postprocessing/_amplitude_scalings_numba.py (#4870) scipy.optimize.nnls

Questions

  1. Is spikeinterface.core.numerics the right name and place?
  2. Should the numba code already in about 15 modules, such as correlograms.py and template_similarity.py, move too, or only new routines? Code tied to the spike vector, such as the correlograms, would stay under the rule above.

If the layout works, I'll move the median there in #4854 and the others in a follow-up.

Activity

  1. added
    core
    refactor
    on Nov 30, 2026
  2. JoeZiminski commented on Dec 1, 2026

    @JoeZiminski
    Contributor

    Cheers for this @galenlynch, I agree it makes sense for numba and numpy to share files, makes dispatch and comparing the algorithms easier. Agree with everything else. I think numerics is okay for a name, I see the problems with kernels, algorithms and don't have any other ideas.

    I think it would be nice to move algorithms here as much as possible, wherever there is a clear reference implementation in a major package, and the main reason for the reimplementation is speed rather than gain of function. For example correlograms.py could be compared against spike-count arrays & np.correlate. In this case its a little more complicated as the function makes some explicit decisions about how bin edges are handled, in general the comparison is easier if there is a 1:1 relationship between the reimplementation and the reference function (e.g. median, nnls). Maybe we could maintain a list here and do the clear-cut cases first?

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

Metadata

Metadata

Assignees

Labels

corerefactor

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions