Repository navigation
Todo
Description
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 avoidkernels, which already means filter taps inpreprocessing, andalgorithms, since most entries are operations like a median rather than algorithms. - One file per operation, not per backend.
numerics/median.pywould hold the publicrow_median, its numba path, and the rules choosing between that path andnp.median(dtype, channel count). Split intonumpy/andnumba/, 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
- Is
spikeinterface.core.numericsthe right name and place? - Should the numba code already in about 15 modules, such as
correlograms.pyandtemplate_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
JoeZiminski commentedon Dec 1, 2026
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.pycould 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?