scspecies.models.neighbors_workaround

scspecies.models.neighbors_workaround(adata, use_rep=None, n_neighbors=15, metric='euclidean')[source]

Compute the k-nearest-neighbors graph manually and store it in adata. Replacement for sc.pp.neighbors on M1/M2 chips to avoid kernel crashes.

Parameters:
  • adata (ad.AnnData) – Annotated data object.

  • use_rep (str) – Key in adata.obsm to use for neighbor search (e.g. ‘X_pca’), or None to use adata.X.

  • n_neighbors (int) – Number of nearest neighbors to use.

  • metric (str or None) – Distance metric to use (default ‘euclidean’).

Returns:

The same adata, with:
  • obsp[‘distances’] : sparse matrix of neighbor distances

  • obsp[‘connectivities’] : sparse binary connectivity matrix

  • uns[‘neighbors’] : dict of params & key names

Return type:

AnnData