dspk.synapse.clustering
Clustering of feature points (n, d) (spike features, PCs, positions): k-means, HDBSCAN and
Gaussian mixtures on the runtime’s device; density peaks and KDE valley merging on the host; and
the random choice of peaks sorters cluster on. Labels are int64 (-1: noise / outlier).
density_peaks |
Density-peaks clustering (Rodriguez & Laio, 2014), on the host. |
gmm |
A Gaussian mixture on the device, its number of components chosen by BIC. |
hdbscan |
HDBSCAN clustering on the device, as the hdbscan package (excess of mass). |
kde_merge |
Over-clusters with k-means, then merges clusters with no density valley between them (host). |
kmeans |
k-means clustering on the device. |
subsample_peaks |
Chooses peaks at random, as sorters do before clustering. |
Functions
Section titled “Functions”density_peaks
Section titled “density_peaks”density_peaks( points, *, cutoff_distance: float, num_clusters: int, max_points: int | None = None,) -> dictDensity-peaks clustering (Rodriguez & Laio, 2014), on the host.
Parameters
points:array_like
Feature points(n, d)(spike features, principal components, positions), converted to float32 on the host; 1-D is(n, 1).cutoff_distance:float
Neighbourhood radius of the density.num_clusters:int
Clusters (the points of highest density times distance).max_points:int, defaultNone
Bounds the O(n²) work with an evenly strided subsample.
Returns
dict—labels,centers(exemplar indices),densities,deltas.
gmm( points, *, k_min: int = DEFAULT_GMM_K_MIN, k_max: int = DEFAULT_GMM_K_MAX, covariance: str = 'full', max_iterations: int = DEFAULT_GMM_MAX_ITERATIONS, tolerance: float = DEFAULT_GMM_TOLERANCE, regularization: float = DEFAULT_GMM_REGULARIZATION, outlier_mahalanobis_sq: float | None = None, mask=None, runtime: str | None = None,) -> dictA Gaussian mixture on the device, its number of components chosen by BIC.
Parameters
points:array_like
Feature points(n, d)(spike features, principal components, positions), converted to float32 on the host; 1-D is(n, 1).k_min:int, defaultDEFAULT_GMM_K_MIN(1)
Components tried,k_mintok_maxincluded.k_max:int, defaultDEFAULT_GMM_K_MAX(8)
Components tried,k_mintok_maxincluded.covariance:{"full", "diagonal", "masked"}, default'full'
Covariance model;"masked"is KlustaKwik’s masked EM and needsmask.max_iterations:int, defaultDEFAULT_GMM_MAX_ITERATIONS(100)
EM iterations.tolerance:float, defaultDEFAULT_GMM_TOLERANCE(0.001)
Convergence tolerance of the log-likelihood.regularization:float, defaultDEFAULT_GMM_REGULARIZATION(0.0001)
Added to each covariance’s diagonal.outlier_mahalanobis_sq:float, defaultNone
Points beyond this squared Mahalanobis distance get the label-1(25: beyond 5 sigma).mask:array_like, defaultNone
(n, d): a weight for each feature of each point ("masked"only).runtime:str, defaultNone
A runtime fromruntime.available; by default the current one.
Returns
dict—labels,weights,means,covariances,responsibilities,mahalanobis_sq,log_likelihood,bic.
hdbscan
Section titled “hdbscan”hdbscan( points, *, min_cluster_size: int = DEFAULT_MIN_CLUSTER_SIZE, allow_single_cluster: bool = False, runtime: str | None = None,) -> np.ndarrayHDBSCAN clustering on the device, as the hdbscan package (excess of mass).
Parameters
points:array_like
Feature points(n, d)(spike features, principal components, positions), converted to float32 on the host; 1-D is(n, 1).min_cluster_size:int, defaultDEFAULT_MIN_CLUSTER_SIZE(5)
Smallest group of points that counts as a cluster.allow_single_cluster:bool, defaultFalse
Allow all points to form one cluster.runtime:str, defaultNone
A runtime fromruntime.available; by default the current one.
Returns
np.ndarray—labels(n,)int64;-1for noise.
kde_merge
Section titled “kde_merge”kde_merge( points, *, initial_k: int, dip_threshold: float = DEFAULT_DIP_THRESHOLD, min_cluster_size: int = DEFAULT_KDE_MIN_CLUSTER_SIZE,) -> dictOver-clusters with k-means, then merges clusters with no density valley between them (host).
Parameters
points:array_like
Feature points(n, d)(spike features, principal components, positions), converted to float32 on the host; 1-D is(n, 1).initial_k:int
Clusters of the first k-means.dip_threshold:float, defaultDEFAULT_DIP_THRESHOLD(1.5)
Pairs whose dip score (0to3: the depth of the density valley between them) is below this merge.min_cluster_size:int, defaultDEFAULT_KDE_MIN_CLUSTER_SIZE(5)
Smaller clusters are merged into their nearest.
Returns
dict—labels,centroids.
kmeans
Section titled “kmeans”kmeans( points, k: int, *, n_init: int = DEFAULT_N_INIT, max_iter: int = DEFAULT_MAX_ITER, tol: float = DEFAULT_TOL, seed: int = 0, runtime: str | None = None,) -> dictk-means clustering on the device: k-means++ starts, the best of n_init runs.
Parameters
points:array_like
Feature points(n, d)(spike features, principal components, positions), converted to float32 on the host; 1-D is(n, 1).k:int
Clusters.n_init:int, defaultDEFAULT_N_INIT(10)
Runs from different starts; the one with the lowest inertia is kept.max_iter:int, defaultDEFAULT_MAX_ITER(300)
Iterations per run.tol:float, defaultDEFAULT_TOL(0.0001)
Convergence tolerance, relative to the data’s mean per-feature variance (as scikit-learn).seed:int, default0
Seed of the starts.runtime:str, defaultNone
A runtime fromruntime.available; by default the current one.
Returns
dict—labels(n,)int64,centers(k, d),inertia.
Examples
>>> import numpy as np>>> import dspk>>> rng = np.random.default_rng(0)>>> pts = np.concatenate([rng.normal(0, 1, (100, 2)), rng.normal(8, 1, (100, 2))])>>> out = dspk.synapse.clustering.kmeans(pts, 2)>>> len(set(out["labels"][:100])), len(set(out["labels"][100:]))(1, 1)subsample_peaks
Section titled “subsample_peaks”subsample_peaks( samples, channels, *, n_peaks: int, per_channel: bool = False, seed: int = 0,) -> np.ndarrayChooses peaks at random, as sorters do before clustering.
Parameters
samples:array_like
Sample of every peak.channels:array_like
Channel of every peak.n_peaks:int
Peaks to choose: in total, or on each channel withper_channel.per_channel:bool, defaultFalse
Choosen_peakson every channel.seed:int, default0
Seed of the choice.
Returns
np.ndarray— Indices of the chosen peaks, in sample order.