meanap.pipeline.spike_detection

MEA spike detection algorithms — Python port of MATLAB WATERS.

Implements threshold-based and wavelet CWT-based detection, matching the behaviour of the MATLAB detectSpikesCWT / detectSpikesThreshold / detectSpikesWavelet functions in Functions/WATERS-master/.

Key differences from MATLAB

  • Wavelet CWT uses a custom FFT-based implementation with the bior1.5 wavelet function obtained via PyWavelets’ cascade algorithm. Results should be highly similar but not bitwise identical to MATLAB’s Wavelet Toolbox CWT.

  • Threshold detection is an exact port and should give near-identical results.

Functions

align_peaks(spike_frames, trace[, win, ...])

Align spike frames to the negative peak within ±win frames.

bandpass_filter(trace, fs[, low, high])

3rd-order Butterworth bandpass filter, matching MATLAB's filtfilt.

detect_spikes_recording(dat, channels, fs[, ...])

Run spike detection on all channels of a single recording.

detect_spikes_threshold(trace, multiplier, ...)

Threshold-based spike detection.

detect_spikes_wavelet(signal, fs_hz[, ...])

Wavelet CWT spike detection.

Classes

SpikeDetectionParams([fs, thresholds, ...])

Parameters matching the MATLAB Params struct for spike detection.

SpikeDetectionResult(spike_times, ...)

Results for a single recording.

class meanap.pipeline.spike_detection.SpikeDetectionParams(fs=12500.0, thresholds=<factory>, wname_list=<factory>, cost_list=<factory>, spikes_method='bior1p5', wid_ms=(0.4, 0.8), n_scales=5, filter_low_pass=600.0, filter_high_pass=6150.0, ref_period_ms=1.0, n_spikes=10000, min_peak_thr_mult=-5.0, max_peak_thr_mult=-100.0, pos_peak_thr_mult=15.0, remove_artifacts=False, unit='s', grd=<factory>)[source]

Bases: object

Parameters matching the MATLAB Params struct for spike detection.

Parameters:
  • fs (float)

  • thresholds (list[float])

  • wname_list (list[str])

  • cost_list (list[float])

  • spikes_method (str)

  • wid_ms (tuple[float, float])

  • n_scales (int)

  • filter_low_pass (float)

  • filter_high_pass (float)

  • ref_period_ms (float)

  • n_spikes (int)

  • min_peak_thr_mult (float)

  • max_peak_thr_mult (float)

  • pos_peak_thr_mult (float)

  • remove_artifacts (bool)

  • unit (str)

  • grd (list[int])

cost_list: list[float]
filter_high_pass: float = 6150.0
filter_low_pass: float = 600.0
fs: float = 12500.0
grd: list[int]
max_peak_thr_mult: float = -100.0
min_peak_thr_mult: float = -5.0
n_scales: int = 5
n_spikes: int = 10000
pos_peak_thr_mult: float = 15.0
ref_period_ms: float = 1.0
remove_artifacts: bool = False
spikes_method: str = 'bior1p5'
thresholds: list[float]
unit: str = 's'
wid_ms: tuple[float, float] = (0.4, 0.8)
wname_list: list[str]
class meanap.pipeline.spike_detection.SpikeDetectionResult(spike_times, spike_waveforms, thresholds, channels, fs)[source]

Bases: NamedTuple

Results for a single recording.

Parameters:
  • spike_times (dict[int, dict[str, ndarray]])

  • spike_waveforms (dict[int, dict[str, ndarray]])

  • thresholds (dict[int, dict[str, float]])

  • channels (ndarray)

  • fs (float)

channels: ndarray

Alias for field number 3

fs: float

Alias for field number 4

spike_times: dict[int, dict[str, ndarray]]

Alias for field number 0

spike_waveforms: dict[int, dict[str, ndarray]]

Alias for field number 1

thresholds: dict[int, dict[str, float]]

Alias for field number 2

meanap.pipeline.spike_detection.align_peaks(spike_frames, trace, win=10, min_peak_thr_mult=-5.0, max_peak_thr_mult=-100.0, pos_peak_thr_mult=15.0, remove_artifacts=False)[source]

Align spike frames to the negative peak within ±win frames.

Approximate port of MATLAB’s alignPeaks().

Returns:

  • aligned_frames (1-D int array)

  • waveforms ((n_spikes, 2*win+1) array of spike waveforms)

Parameters:
  • spike_frames (ndarray)

  • trace (ndarray)

  • win (int)

  • min_peak_thr_mult (float)

  • max_peak_thr_mult (float)

  • pos_peak_thr_mult (float)

  • remove_artifacts (bool)

Return type:

tuple[ndarray, ndarray]

meanap.pipeline.spike_detection.bandpass_filter(trace, fs, low=600.0, high=8000.0)[source]

3rd-order Butterworth bandpass filter, matching MATLAB’s filtfilt.

Parameters:
  • trace (ndarray)

  • fs (float)

  • low (float)

  • high (float)

Return type:

ndarray

meanap.pipeline.spike_detection.detect_spikes_recording(dat, channels, fs, params=None)[source]

Run spike detection on all channels of a single recording.

Parameters:
  • dat ((n_samples, n_channels) array)

  • channels ((n_channels,) channel ID array)

  • fs (sampling frequency in Hz)

  • params (detection parameters (defaults match the example data MATLAB run))

Return type:

SpikeDetectionResult

meanap.pipeline.spike_detection.detect_spikes_threshold(trace, multiplier, ref_period_ms, fs, filter_flag=False, absolute_threshold=None, threshold_window=(0.0, 1.0))[source]

Threshold-based spike detection.

Exact Python port of detectSpikesThreshold.m.

Parameters:
  • trace (1-D array — voltage trace (already filtered if filter_flag=False))

  • multiplier (threshold = median - multiplier * MAD / 0.6745)

  • ref_period_ms (refractory period in milliseconds)

  • fs (sampling frequency in Hz)

  • filter_flag (if True apply bandpass filter first)

  • absolute_threshold (if given, use this instead of the MAD threshold)

  • threshold_window ((start, end) as fractions of recording [0, 1])

Returns:

  • spike_frames (1-D int array of spike frame indices (0-based))

  • threshold (the threshold value used)

Return type:

tuple[ndarray, float]

meanap.pipeline.spike_detection.detect_spikes_wavelet(signal, fs_hz, wid_ms=(0.4, 0.8), ns=5, option='l', L=-0.12, wname='bior1.5')[source]

Wavelet CWT spike detection.

Port of MATLAB detectSpikesWavelet(). Signal should already be filtered (bandpass).

Parameters:
  • signal (1-D array — filtered voltage trace (zero-mean))

  • fs_hz (sampling frequency in Hz)

  • wid_ms ((min, max) expected spike width in milliseconds)

  • ns (number of CWT scales)

  • option ('l' (liberal) or 'c' (conservative))

  • L (Bayesian cost factor (typically -0.12))

  • wname (wavelet name ('bior1.5' supported))

Returns:

spike_frames

Return type:

1-D int array of spike frame indices (0-based)