"""Abstract base class for spike sorter backends.
Each backend implements the three-step pipeline: load recording, run
sorter, extract waveforms. The pipeline module (``pipeline.py``)
calls these methods and handles everything downstream (SpikeData
conversion, curation, compilation, figures).
To add a new sorter:
1. Create a new module in ``backends/`` (e.g. ``kilosort4.py``).
2. Subclass ``SorterBackend`` and implement all three methods.
3. Register the backend in ``backends/__init__.py``.
"""
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Any
from ..config import SortingPipelineConfig
[docs]
class SorterBackend(ABC):
"""Interface that each spike sorter backend must implement.
Parameters:
config (SortingPipelineConfig): Full pipeline configuration.
Backends read their relevant sub-configs (``config.recording``,
``config.sorter``, ``config.waveform``, ``config.execution``).
"""
[docs]
def __init__(self, config: SortingPipelineConfig) -> None:
self.config = config
[docs]
@abstractmethod
def load_recording(self, rec_path: Any):
"""Load and preprocess a single recording.
Handles format-specific loading (Maxwell ``.h5``, NWB, etc.),
gain/offset scaling, and bandpass filtering.
Parameters:
rec_path: Path to a recording file, a directory of files
to concatenate, or a pre-loaded BaseRecording object.
Returns:
recording: A SpikeInterface ``BaseRecording`` ready for
sorting (scaled, filtered, single-segment).
"""
[docs]
@abstractmethod
def sort(self, recording, rec_path, recording_dat_path, output_folder):
"""Run the spike sorter on a preprocessed recording.
Parameters:
recording: SpikeInterface ``BaseRecording`` from
``load_recording``.
rec_path: Original recording file path (for binary
conversion or metadata).
recording_dat_path (Path): Path for the binary ``.dat``
file (used by sorters that require pre-converted input).
output_folder (Path): Directory for sorter output files.
Returns:
sorting: A SpikeInterface ``BaseSorting`` with detected
units and spike trains.
"""
[docs]
def write_recording(self, recording: Any, dat_path: Any) -> None:
"""Convert a recording to the binary format needed by the sorter.
Not all sorters need this (some read recordings directly via
SpikeInterface). The default implementation is a no-op.
Parameters:
recording: SpikeInterface ``BaseRecording``.
dat_path (Path): Output binary file path.
"""
pass