dspk.synapse.matching
Templates and matching: lag-maximised cosine similarity of templates, merge suggestions, and matching pursuit of a recording against templates (on the runtime’s device).
Templates are (units, rows, samples); row r of unit u is recording channel
channels[u, r] (default: rows are channels 0..rows).
matching_pursuit |
Finds spikes by subtracting the best-fitting template, pass after pass, on the device. |
suggest_merges |
Pairs of units whose templates are alike enough to be one unit. |
template_similarity |
Cosine similarity of every pair of templates, each at its best lag. |
Functions
Section titled “Functions”matching_pursuit
Section titled “matching_pursuit”matching_pursuit( data, templates, *, min_amplitude_scale: float = DEFAULT_MIN_AMPLITUDE_SCALE, max_amplitude_scale: float = DEFAULT_MAX_AMPLITUDE_SCALE, min_explained_energy: float = DEFAULT_MIN_EXPLAINED_ENERGY, max_passes: int = DEFAULT_MAX_PASSES, channels=None, runtime: str | None = None,) -> dictFinds spikes by subtracting the best-fitting template, pass after pass, on the device.
Parameters
data:array_like
Signal(channels, samples)in µV, converted to float32 on the host.templates:array_like
(units, rows, samples), or(units, samples)for one row each.min_amplitude_scale:float, defaultDEFAULT_MIN_AMPLITUDE_SCALE(0.65)
Range a template may be scaled by to fit.max_amplitude_scale:float, defaultDEFAULT_MAX_AMPLITUDE_SCALE(1.45)
Range a template may be scaled by to fit.min_explained_energy:float, defaultDEFAULT_MIN_EXPLAINED_ENERGY(500.0)
Least energy (µV²) a fit must remove to count; rescale it with the data’s unit.max_passes:int, defaultDEFAULT_MAX_PASSES(4)
Passes over the data.channels:array_like, defaultNone
(units, rows): the recording channel of each template row; by default rows are channels0..rows. Only rows on shared channels are compared.runtime:str, defaultNone
A runtime fromruntime.available; by default the current one.
Returns
dict of np.ndarray—samples(troughs),units,amplitudes(scales),scores.
suggest_merges
Section titled “suggest_merges”suggest_merges( templates, *, max_lag_samples: int = DEFAULT_MAX_LAG_SAMPLES, threshold: float = DEFAULT_MERGE_THRESHOLD, channels=None,) -> list[tuple[int, int, float]]Pairs of units whose templates are alike enough to be one unit.
Parameters
templates:array_like
(units, rows, samples), or(units, samples)for one row each.max_lag_samples:int, defaultDEFAULT_MAX_LAG_SAMPLES
Lags tried, up to this many samples each way.threshold:float, defaultDEFAULT_MERGE_THRESHOLD
Least similarity of a suggested pair.channels:array_like, defaultNone
(units, rows): the recording channel of each template row; by default rows are channels0..rows. Only rows on shared channels are compared.
Returns
list of (int, int, float)—(i, j, similarity)of each pair.
template_similarity
Section titled “template_similarity”template_similarity( templates, *, max_lag_samples: int = DEFAULT_MAX_LAG_SAMPLES, channels=None,) -> np.ndarrayCosine similarity of every pair of templates, each at its best lag.
Parameters
templates:array_like
(units, rows, samples), or(units, samples)for one row each.max_lag_samples:int, defaultDEFAULT_MAX_LAG_SAMPLES
Lags tried, up to this many samples each way.channels:array_like, defaultNone
(units, rows): the recording channel of each template row; by default rows are channels0..rows. Only rows on shared channels are compared.
Returns
np.ndarray—(units, units), in[-1, 1].
Examples
>>> import numpy as np>>> import dspk>>> t = np.sin(np.linspace(0, 6, 60, dtype=np.float32))>>> sim = dspk.synapse.matching.template_similarity(np.stack([t, np.roll(t, 3)]))>>> bool(sim[0, 1] > 0.9)True