scviva.tools.vision.VisionToolsAccessor#

class scviva.tools.vision.VisionToolsAccessor(va)[source]#

Lower-level VISION tool functions accessible as va.tl.<method>.

Created automatically by VisionAnalysis; users do not instantiate it directly.

Parameters:
va VisionAnalysis

The analysis object whose AnnData is used when adata is not passed.

Methods table

apply_micro_clustering([adata, ...])

Partition cells into micro-clusters (supercells) via Louvain + K-means.

cluster_cells_tree([adata, target, obs_key])

Cluster cells by their position in a phylogenetic tree.

compute_knn_weights([adata, obsm_key, K, ...])

Compute VISION exponential KNN weights and store in adata.obsp.

compute_plasticity_scores([adata])

Compute per-cell Fitch-Hartigan parsimony plasticity scores.

generate_projections([adata, ...])

Generate 2D visualisation projections and store in adata.obsm.

pool_matrix([adata, pools, layer_key, ...])

Pool adata.X (or a layer) into per-micro-cluster mean expression.

pool_metadata([adata, pools, uns_key, ...])

Pool adata.obs into per-micro-cluster metadata.

rank_genes_groups([adata])

Rank genes for characterizing groups.

Methods

VisionToolsAccessor.apply_micro_clustering(adata=None, *, cells_per_partition=10, filter_input=True, filter_threshold=None, filter_num_mad=2.0, latent_space_key=None, K=None, uns_key='micro_clusters', random_state=0)[source]#

Partition cells into micro-clusters (supercells) via Louvain + K-means.

Forwards to apply_micro_clustering(). adata defaults to the AnnData held by the analysis object.

Mirrors R VISION’s applyMicroClustering.

If latent_space_key is provided and present in adata.obsm, those coordinates are used directly. Otherwise a lightweight internal PCA (max 10 components) is computed on log-transformed, filtered expression.

Parameters:
adata AnnData | None

Annotated data matrix.

cells_per_partition int

Target number of cells per micro-cluster, by default 10.

filter_input bool

Apply threshold + fano gene filters before internal PCA, by default True. Ignored when latent_space_key is provided.

filter_threshold int | None

Minimum expressing-cell count for the threshold filter. Defaults to round(0.05 * n_cells) (matching R VISION’s applyMicroClustering default of 5% of cells) when None.

filter_num_mad float

MAD multiplier for the fano filter, by default 2.0.

latent_space_key str | None

Key in adata.obsm for pre-computed latent-space coordinates. When None (default), coordinates are computed internally.

K int | None

Number of KNN neighbors for the Louvain graph. Defaults to min(round(sqrt(n_cells)), 30), matching R VISION (no lower floor – R’s own formula has none either).

uns_key str

Key under which the pool mapping is stored in adata.uns, by default "micro_clusters".

random_state int

Seed for the internal PCA, Louvain clustering, and K-means readjustment steps, by default 0. Passing the same value across runs makes the whole micro-clustering pipeline reproducible.

Return type:

Pools

Returns:

dict mapping str → list[str] pools["microcluster_<i>"] is the list of obs_names belonging to that micro-cluster.

VisionToolsAccessor.cluster_cells_tree(adata=None, *, newick, target=10, obs_key='VISION_Clusters_Tree')[source]#

Cluster cells by their position in a phylogenetic tree.

Forwards to cluster_cells_tree(). adata defaults to the AnnData held by the analysis object.

Mirrors R VISION’s tree-based clustering path in clusterCells() (methods-Vision.R), which calls maxSizeCladewiseTreeCluster (Microclusters.R) when a tree is available.

The algorithm greedily splits the largest sub-clade until √n_cells partitions exist, then merges small partitions back down to target using tree-distance (trivial_dist) as the merge criterion. Results are stored as a categorical column in adata.obs[obs_key].

Parameters:
adata AnnData | None

AnnData whose obs_names must match leaf labels in newick.

newick str

Newick-format tree string (or the value of adata.uns["vision_tree"]).

target int

Desired number of clusters. Default 10, matching R VISION’s default.

obs_key str

Column name written to adata.obs. Default "VISION_Clusters_Tree".

Return type:

None

Returns:

None Modifies adata in-place, adding a categorical adata.obs[obs_key] column.

VisionToolsAccessor.compute_knn_weights(adata=None, *, obsm_key='X_pca', K=None, obsp_key='weights', exact=False)[source]#

Compute VISION exponential KNN weights and store in adata.obsp.

Forwards to compute_knn_weights_anndata(). adata defaults to the AnnData held by the analysis object.

Reads the latent space from adata.obsm[obsm_key] and writes the resulting (n_cells, n_cells) sparse weight matrix to adata.obsp[obsp_key]. This replaces the L1-normalised scanpy connectivities previously used by visionpy as a proxy for VISION weights.

Parameters:
adata AnnData | None

Annotated data matrix. Must have obsm_key in adata.obsm (e.g. produced by compute_latent_space()).

obsm_key str

Key in adata.obsm for the latent-space coordinates, by default "X_pca".

K int | None

Number of neighbors. Defaults to round(sqrt(n_cells)).

obsp_key str

Key under which to store the weight matrix in adata.obsp, by default "weights".

exact bool

If True, use sklearn’s exact neighbor search instead of pynndescent. See find_knn().

Return type:

AnnData

Returns:

AnnData The input adata modified in-place with adata.obsp[obsp_key] populated.

Raises:

KeyError – If obsm_key is not present in adata.obsm.

VisionToolsAccessor.compute_plasticity_scores(adata=None, *, newick)[source]#

Compute per-cell Fitch-Hartigan parsimony plasticity scores.

Forwards to compute_plasticity_scores(). adata defaults to the AnnData held by the analysis object.

For every categorical variable in adata.obs, a {var}_plasticity column is added to adata.obs. The score for cell c is the average normalised Fitch-Hartigan parsimony across all internal nodes on the path from the tree root to c.

  • High plasticity: the metadata label switches often along the lineage path → the trait is labile / plastic.

  • Low plasticity: the label is conserved along the lineage path.

Mirrors R VISION’s computePlasticityScores().

Parameters:
adata AnnData | None

Must have obs_names matching tree leaf labels.

newick str

Newick-format tree string (or the adata.uns["vision_tree"] value).

Return type:

None

Returns:

None Modifies adata in-place, adding one {var}_plasticity column to adata.obs for each categorical variable found.

VisionToolsAccessor.generate_projections(adata=None, *, projection_methods=('tSNE30', 'tSNE10', 'UMAP', 'ISOMap', 'ICA', 'RBFPCA'), obsm_key='X_pca', K=None, projection_genes=None)[source]#

Generate 2D visualisation projections and store in adata.obsm.

Forwards to generate_projections(). adata defaults to the AnnData held by the analysis object.

Mirrors R VISION’s generateProjectionsInner. Dispatches each method to the appropriate input:

  • "ICA" — filtered log-transformed expression (cells × genes), not the latent space, to match R VISION’s behaviour.

  • "UMAP" — scanpy neighbour graph built on adata.obsm[obsm_key].

  • "tSNE30", "tSNE10", "ISOMap", "RBFPCA" — adata.obsm[obsm_key].

Results are stored in adata.obsm[f"X_{{method}}"] (e.g. "X_tSNE30", "X_UMAP", "X_RBFPCA").

Parameters:
adata AnnData | None

Annotated data matrix. Must have obsm_key in adata.obsm (produced by compute_latent_space()).

projection_methods Sequence[str]

Ordered list of projection names. Choose from "tSNE30", "tSNE10", "UMAP", "ISOMap", "ICA", "RBFPCA". Default: all six methods.

obsm_key str

Key in adata.obsm for the latent-space coordinates used by UMAP, tSNE, and ISOMap, by default "X_pca".

K int | None

Number of neighbours for UMAP and ISOMap. Defaults to round(sqrt(n_cells)).

projection_genes Sequence[str] | None

Subset of genes to use for the ICA projection. When None, all genes in adata.var_names are used.

Return type:

dict[str, np.ndarray]

Returns:

dict mapping str → ndarray of shape (n_cells, 2) Same mapping as the adata.obsm entries written by this function.

Raises:
  • KeyError – If obsm_key is not present in adata.obsm.

  • ValueError – If an unrecognised method name is requested.

VisionToolsAccessor.pool_matrix(adata=None, *, pools=None, layer_key=None, uns_key='micro_clusters', result_uns_key='pool_expression')[source]#

Pool adata.X (or a layer) into per-micro-cluster mean expression.

Forwards to pool_matrix_anndata(). adata defaults to the AnnData held by the analysis object.

Convenience wrapper around pool_matrix() that reads pools from adata.uns when pools is not provided explicitly.

Parameters:
adata AnnData | None

pools Pools | None

Pool mapping. Reads from adata.uns[uns_key] when None.

layer_key str | None

Layer to pool. Uses adata.X when None.

uns_key str

Key in adata.uns for the pool mapping.

result_uns_key str

Key in adata.uns where the result is stored.

Return type:

np.ndarray

Returns:

ndarray of shape (n_pools, n_genes)

VisionToolsAccessor.pool_metadata(adata=None, *, pools=None, uns_key='micro_clusters', result_uns_key='pool_metadata')[source]#

Pool adata.obs into per-micro-cluster metadata.

Forwards to pool_metadata_anndata(). adata defaults to the AnnData held by the analysis object.

Convenience wrapper around pool_metadata().

Parameters:
adata AnnData | None

pools Pools | None

Pool mapping. Reads from adata.uns[uns_key] when None.

uns_key str

Key in adata.uns for the pool mapping.

result_uns_key str

Key in adata.uns where the result is stored.

Return type:

pd.DataFrame

Returns:

pd.DataFrame of shape (n_pools, ≥n_obs_vars)

VisionToolsAccessor.rank_genes_groups(adata=None, **kwargs)[source]#

Rank genes for characterizing groups.

Forwards to rank_genes_groups() (imported lazily because it depends on scanpy). adata defaults to the AnnData held by the analysis object.

Parameters:
adata AnnData | None

AnnData to use. Defaults to the analysis object’s AnnData.

**kwargs

Keyword arguments forwarded to rank_genes_groups() (e.g. groupby, method, reference, n_genes).