pypi piaso-tools 1.2.9
PIASO v1.2.9

41 minutes ago

piaso-tools 1.2.9

pip install -U piaso-tools

Requires Python 3.10 or later, cytome>=0.3.9 and cosg>=1.2.1. Wheels for Linux x86_64, CPython 3.10 to 3.12.

The neighbour graph's default search is PIASO's deterministic search: neighbors(backend='auto', graph_backend='auto')

  • What changes. piaso.tl.neighbors now finds the neighbours with PIASO's own parallel search (new, in Rust) and builds the graph with PIASO's Rust builder, when the extension has them: backend='auto' and graph_backend='auto' are the new defaults. For one input and one random_state the graph is the same on any number of threads, and the same with the AVX2+FMA kernels as with the portable kernels that ARM64 processors and x86 processors without AVX2 use (checked on x86-64 by forcing the portable path; every wheel's CI job runs a fixed-input check). The search is exact up to 16,384 cells (backend='exact' at any size) and approximate above, with higher recall than pynndescent: on whole-mouse-brain nuclei, 99.99% of the 14 true nearest neighbours against 96.75% at 100,000 nuclei, and 98.2% against 88.4 to 89.1% at 2.3 million. At 2.3 million nuclei and 20 threads it adds about 1.1 GB of memory against 2.15 GB for pynndescent 0.6.0, and runs about 2 to 2.5 times faster than pynndescent once compiled (no clear gain at 4 threads). Metrics: 'euclidean' (also 'l2'), 'cosine', 'sqeuclidean'.
  • Size of the change against 1.2.8. On 100,000 nuclei, every argument at its default: 23.6% of the cells have at least one different neighbour (0.45 of 14 on average), and Leiden at its defaults agrees with 1.2.8's labels at ARI 0.944 (53 clusters in both). The Rust graph builder alone changes the weights only at float32 rounding; the difference comes from the search. Every step that calls neighbors with its defaults changes with it: leiden_local; runGDR's own clustering; stitchSpace's clusters inside each batch; and the integration map predictCellTypeByGDR returns (its predictions use no search).
  • The way back. neighbors(..., backend='pynndescent', graph_backend='umap-learn') with a fixed n_threads (pynndescent's neighbours depend on its thread count) gives the graph of 1.2.8 bit for bit. leiden_local and runGDR call neighbors themselves, and take the same choice through neighbors_kwargs (new), which reaches every search they run: neighbors_kwargs={'backend': 'pynndescent', 'graph_backend': 'umap-learn', 'n_threads': 0} gives their labels and embeddings of 1.2.8 ('n_threads': 0 leaves the search's thread count unset, as in 1.2.8; keep NUMBA_NUM_THREADS as it was). Only the search's own choices are taken there; the embedding, n_neighbors, the seed and the graph's name stay the function's own, and naming them is an error. runGDR records neighbors_kwargs with its parameters when given. stitchSpace and predictCellTypeByGDR take neighbors_kwargs too (stitchSpace records it).
  • Platforms. Where the extension lacks the search (a build from source without the Rust extension), 'auto' warns and falls back to pynndescent and umap-learn, the searches of 1.2.8. On x86 processors without AVX2/FMA (rare; some virtual machines hide them) the search takes its portable path: the same results, but 10 to 24 times slower in our tests.
  • Threads. n_threads is handed to the Rust search and graph builder (0 uses RAYON_NUM_THREADS or all cores); the result does not depend on it. leiden_local(n_threads=...) now hands its setting to each group's search, and runGDR's parallel per-batch paths give each batch's search its share of the cores, as they already did for Leiden.
  • New settings of the deterministic search: knn_n_clusters, knn_nprobe (24), knn_refine_rounds (2), knn_exact (False). backend takes 'auto', 'deterministic' (alias 'rust'), 'exact' and 'pynndescent'; graph_backend takes 'auto', 'rust' and 'umap-learn'.

Fixed: runGDR on a store with several batches runs each batch's SVD on one BLAS thread with PyPI's numpy too

  • The first stage runs the batches side by side in threads (with max_workers above 1 and a thread-safe numba layer, the default of a pip install). Each batch's SVD set its one-BLAS-thread limit from inside its thread, and the OpenBLAS that PyPI's numpy and scipy ship (0.3.31 and 0.3.34 measured) ignores a limit set from a thread other than the main one: the SVD's dense steps then ran on the machine's BLAS thread count, and their last bits (up to 2e-12) depended on it. runGDR now sets the limit before the batches start. Runtime and memory are unchanged (36,000 cells in three batches), and where the limit already applied (OpenBLAS 0.3.23, 0.3.26) the results do not change.

piaso.tl.neighbors(n_threads=...), and a graph of another modality is not replaced without a word

  • n_threads (new, default 0): threads for the nearest-neighbour search, the same name as in leiden, runSVD and runGDR. The deterministic search, the new default (above), gives the same graph on any number of threads. With backend='pynndescent' it is handed to NNDescent as n_jobs, at most numba's NUMBA_NUM_THREADS, which numba does not let it exceed (left at 0, pynndescent is called as in 1.2.8); the neighbours pynndescent finds depend on the number of threads it runs on, so its graph reproduces exactly at a fixed thread count: pass n_threads=1 (or keep NUMBA_NUM_THREADS fixed) where it must be the same on any machine. piaso.tl.leiden and piaso.tl.umap are the same on any number of threads given the graph; the graph itself is what depends on them.
  • On a cytome, two modalities and one graph name. With key_added=None the graph is stored as connectivities / distances whatever the modality (the names umap and runGDR read), so running neighbors for a second modality replaced the first one's graph, and nothing said so. The modality a graph was built for is now recorded beside it, and replacing a graph built for another modality warns: pass key_added (for example 'ATAC') to keep both. Where the graph is written does not change.

leiden_local(feature_selection=...): the features inside each group, as a choice

  • feature_selection (new, default None): 'dataset', the whole dataset's selection (the highly_variable column of the feature table), or 'none', every feature. It applies to dr_method='X_svd'.
  • Left unset, each group of an ATAC or tiles store is embedded on the whole dataset's selection, or on every feature when the store has none, as in 1.2.8; an RNA store and an AnnData: as before, the stored selection. On the E18 example store (4,091 nuclei, its four largest groups) with the neighbour search of 1.2.8 (neighbors_kwargs, above), the groups were identical to 1.2.8's.

leiden_local(embedding_seed=...): one seed for the group's GDR and Harmony

  • Inside each group, dr_method='X_gdr' runs runGDR and 'X_pca_harmony' / 'X_svd_full_harmony' run Harmony, each with a seed of its own (1927 and 0) whatever random_state is. embedding_seed (new, default None) gives both the same seed. Left unset, both run as before and the labels do not change.

score() and kamon: control genes for a dataset with one dominant cell type (ctrl_groupby, off by default)

  • What it is for. A set's score is the mean of its genes less the mean of control genes that match them in mean and variance over all cells. In a dataset where one cell type is most of the cells, those statistics are that cell type's, so the controls of its markers are genes that are themselves high in it, and its markers hardly stand above them in its own cells.
  • The option. piaso.tl.score(..., ctrl_groupby=...) and piaso.tl.kamon(..., ctrl_groupby=...) take a grouping of the cells: an array with one value per cell, or the name of a column (adata.obs, or a cytome's cells table), for example a coarse clustering. No labels are needed. Each gene's mean and variance, for the matching of control genes only, are then taken on a subsample with at most ctrl_group_cap cells (300) from each group of at least ctrl_group_min cells (20), drawn with ctrl_group_seed (0).
  • What does not change. The layer scored and its normalization over all cells, the number of candidate genes, the control sets and their seed, the exclusion of a set's own genes, the smoothing in kamon: all as before. Every cell is scored. Without ctrl_groupby (the default) every score is what it was, value for value, on an AnnData and on a cytome; with it, the two give the same scores.
  • What it needs. A grouping in which the dominant cell type does not hold most of the groups: a coarse clustering serves better than a fine one. It cannot be combined with precomputed_knn. If no group has ctrl_group_min cells, the usual matching is used and a warning says so. The subsample's rows are read into memory (at most ctrl_group_cap times the number of groups).
  • Where one cell type dominates. In a mouse cerebellum (29,587 nuclei, 80% of them granule cells), a gene-by-nucleus matrix scored with the markers of the Allen whole mouse brain atlas, the grouping being 15 coarse clusters. The granule cells' own set had the highest score in 52% of the granule cells with the usual matching and in 99% with the option. kamon's accuracy after smoothing went from 0.77 to 0.96, and over the nuclei that are not granule cells from 0.85 to 0.83. That dataset's labels are our own annotation of its RNA.
  • Where none does. Seven datasets in which the largest cell type holds 15% to 39% of the nuclei, each grouped by its own coarse clusters and annotated with published marker tables, from two matrices of each dataset: 34 comparisons. Accuracy after smoothing changed by -0.02 to +0.03 (median +0.002) and macro F1 rose in most, the small cell types gaining most. But the option is not neutral cell type by cell type: in three comparisons accuracy or macro F1 fell by more than 0.01, in two of them because one cell type lost much (naive B cells with one study's markers, mural cells of the heart with another's). That is why it is an option for datasets with a dominant cell type and not the default.

Fixed: leiden_local with dr_method 'X_pca', 'X_gdr' or 'X_pca_harmony' clusters each group on its own embedding

  • Who is affected: versions up to 1.2.8. Labels from leiden_local with one of these three methods, on an AnnData that had an obsm['X_pca'] or on a store that held an embedding named X_pca. Such a call clustered every group on that embedding, the whole dataset's cut to the group's cells, and said nothing. 'X_pca' was the function's default up to 1.2.4, so a call of that time that named no dr_method is one of them. A call on an object without an obsm['X_pca'] stopped with KeyError: 'X_pca' and made no labels.
  • Not affected: dr_method 'X_svd' (the default since 1.2.5), 'X_svd_full' and 'X_svd_full_harmony', on an AnnData or on a store; the leiden_local tutorial, which uses 'X_svd_full'.
  • What was wrong. The three methods compute each group's own INFOG embedding (INFOG normalization and gene selection on the group's cells, then the SVD) and cluster the group on it, or on the GDR or Harmony embedding built from it. That embedding was stored under a name the later steps did not read, and a group's working copy carried the object's own embeddings, so the later steps found the object's X_pca under the name they read.
  • What changed, and the labels of the three methods change with it.
    • The group's embedding is stored under the name the later steps read.
    • A group's working copy starts without the object's embeddings, graphs and neighbors records, for every dr_method. A step that reads a name nothing inside the group wrote now stops with a KeyError; it cannot read the object's array in silence again. The object's own embeddings and graphs are neither read nor changed.
    • The group's INFOG embedding is seeded by random_state. It ran with a seed of its own (1927) whatever random_state was, although random_state is documented as the seed of the SVD.
  • To make the old labels again. leiden_local(adata, use_rep='X_pca') (new, below) does what dr_method='X_pca' did on an object with obsm['X_pca'], and gives identical labels against the uncorrected code. Releases up to 1.1.0 clustered with scanpy's neighbors and Leiden, and releases up to 1.2.5 with igraph's Leiden (backend='igraph'), so their labels are made again exactly only by installing that release (pip install piaso-tools==<version>). That is also the only way for 'X_gdr' and 'X_pca_harmony', whose GDR and Harmony steps were built on the object's embedding too.
  • 'X_gdr' does not use batch_key, and now says so. Each group's GDR is built on all the group's cells together, without batches; only 'X_pca_harmony' and 'X_svd_full_harmony' use the batch column. A call that gives 'X_gdr' a batch_key gets one UserWarning saying this. Its labels are unchanged.

leiden_local(use_rep=...): cluster each group on an embedding the object already has

  • What it does. With use_rep no group is embedded again: each is clustered on its rows of an existing embedding of the whole object, obsm[use_rep] of an AnnData or the embedding of that name of a store (found as piaso.tl.neighbors finds it), neighbors and then Leiden. The matrix is not read, so it is fast and a store is not converted. Left unset, nothing changes.
  • What it is for. A clustering inside each group in the whole object's space; it does not recover structure that embedding merged, which is what embedding a group again is for. And the old labels of 'X_pca' (above).
  • What it leaves unused. dr_method is not used, and a call that names another one than the default is told so once; min_cells has no effect, as in the other in-memory runs. A name the object does not have is a KeyError that lists the embeddings it has.

Don't miss a new piaso-tools release

NewReleases is sending notifications on new releases.