Skip to main content

Crate dspk_sorter_kilosort

Crate dspk_sorter_kilosort 

Source
Expand description

The Kilosort family on the GPU: Kilosort4 (Pachitariu, Sridhar, Pennington & Stringer, Nature Methods 2024; feature ks4) and its fork EMUsort (O’Connell et al., eLife 2026; feature emusort, emusort). Written from the papers and the published defaults, their behaviour matched to upstream’s code by reading it; no code copied. GPL-3.0, as both upstreams.

Implemented stages: universal templates (wPCA / wTEMP) learned from the recording or loaded from the predefined wTEMP.npz (templates), universal-template spike detection with wPCA features on the device (detect), and the first graph-based clustering of those spikes into units (clustering), learned templates (learned), learned-template matching (matching), the clustering of the matched spikes with the refractory criterion, global merges ([merges]), duplicate-spike removal and good / mua labels, driven over a whole recording by runner (preprocessing fit: CAR, high-pass, local whitening), with Kilosort4’s drift correction (drift) and artifact blanking (artifacts). See the documentation.

Re-exports§

pub use clustering::cluster_spikes;
pub use clustering::ClusteringOptions;
pub use clustering::SpikeClusters;
pub use matching::TemplateMatcher;
pub use matching::MAX_PEELS;
pub use clustering::merges::global_merges;
pub use clustering::merges::GlobalMergeOptions;
pub use clustering::merges::MergedClusters;
pub use learned::learned_templates;
pub use learned::LearnedTemplates;
pub use learned::TemplateMergeOptions;
pub use emusort::Emusort;
pub use emusort::EmusortConfig;
pub use ks4::kilosort4_provenance;
pub use ks4::Kilosort4;
pub use drift::estimate_drift;
pub use drift::DriftCorrector;
pub use drift::DriftModel;
pub use drift::DriftOptions;
pub use drift::DriftSpike;
pub use detect::detect_universal;
pub use detect::CentreOptions;
pub use detect::TemplateCentres;
pub use detect::UniversalDetector;
pub use detect::UniversalSpike;
pub use runner::fit_kilosort4_preprocessing;
pub use runner::fit_preprocessing;
pub use runner::run_plan;
pub use runner::ChannelDelays;
pub use runner::FitSettings;
pub use runner::FittedPreprocessing;
pub use runner::Kilosort4Result;
pub use runner::RunPlan;
pub use templates::extract_clips;
pub use templates::learn_universal_templates;
pub use templates::learn_universal_templates_with_progress;
pub use templates::ClipOptions;
pub use templates::ClipScaling;
pub use templates::LearnOptions;
pub use templates::TemplateClustering;
pub use templates::UniversalTemplates;

Modules§

artifacts
Kilosort4’s artifact_threshold: a window holding a value at or above the threshold is taken to contain a recording artifact and zeroed (upstream zeroes the batch). Off by default, as upstream (∞).
clustering
Kilosort4’s first clustering of the universal-template spikes into units, written from the paper (Pachitariu et al. 2024, Methods: Template learning, Graph-based clustering, Hierarchical merging tree, Split/merge criteria, Bimodality of regression axis).
delays
Channel delays on the device (EMUsort’s remove_chan_delays, a stage of the shared runner): estimation (ChannelDelayEstimator) and removal (ChannelAligner). Batches never leave the device; the estimator downloads its [channels, channels, 2·max_lag + 1] cross-correlation once, at the end.
detect
Kilosort4 universal-template spike detection on the device.
drift
Kilosort4’s drift correction (paper, Methods, Drift correction; behaviour matched to upstream’s datashift.py and get_drift_matrix by reading them; no code copied).
emusort
EMUsort (O’Connell et al., eLife 2026): Kilosort4 adapted to motor-unit action potentials from high-density intramuscular arrays (Myomatrix), feature emusort. Written from the paper and the published defaults, its behaviour matched to upstream’s code (snel-repo/EMUsort a06bb60, ks4mods) by reading it; no code copied (GPL-3.0, as upstream).
ks4
Kilosort4 (feature ks4): the sorter entry point and its provenance. The stages and settings (crate::Kilosort4Config) are shared with EMUsort and live in the crate root.
learned
Learned templates for template matching, from the first clustering’s units (Kilosort4 paper, Methods, Template learning, written from its description):
matching
Learned-template matching with matching pursuit (Kilosort4 paper, Methods: Spike detection with learned templates and matching pursuit, Extracting PC features with background subtraction; written from its description). Per preprocessed window, on the device:
runner
Kilosort4-family runner (Kilosort4, and EMUsort through RunPlan::emusort): out-of-core halo-window streaming over a recording, every window processed on the device.
templates
Kilosort4 universal templates: the single-channel temporal basis wPCA ([n_pcs, nt]) and the universal templates wTEMP ([n_templates, nt]), learned from the recording (learn_universal_templates, Kilosort4’s default) or loaded from Kilosort4’s predefined wTEMP.npz (UniversalTemplates::from_npz).

Structs§

Kilosort4Config
Kilosort4 settings this crate uses, with upstream defaults (kilosort/parameters.py).

Constants§

KILOSORT4_WTEMP_ID
dspk-hub catalog id of Kilosort4’s predefined wTEMP.npz (dspk_hub::catalog::synapse).
LINE_NOISE_HZ
Line-noise frequency of the notch (Hz): 60 (50 outside the Americas).
NOTCH_Q
Default quality factor of the line-noise notch (−3 dB bandwidth f / 30: 2 Hz at 60 Hz).
TH_LEARNED
TH_UNIVERSAL
Detection thresholds (whitened σ) for the default 300–6000 Hz band. Upstream Kilosort4 uses 9 and 8 with a high-pass only; the band’s upper edge lowers the whitened noise, so the same thresholds detect ~36% more spikes. 10 and 9 give the same agreement with Kilosort4’s saved results as 9 and 8 with the high-pass (test recording, 2026-10-09; 11 and 10 miss spikes).
UPSTREAM_TH_LEARNED
UPSTREAM_TH_UNIVERSAL
Upstream Kilosort4’s thresholds (high-pass only, bandpass_high_hz = None).