Compute coarse-grained transition matrix

This example shows how to compute and plot the coarse-grained transition matrix among the set of macrostates.

We computed the macrostates using Generalized Perron Cluster Cluster Analysis [GPCCA18] [Reuter19], see the example Compute macrostates. The coarse-grained transition matrix shows transitional behavior among the set of macrostates and may be used to classify these states as initial, intermediate or terminal.

import cellrank as cr

adata = cr.datasets.pancreas_preprocessed("../example.h5ad")


AnnData object with n_obs × n_vars = 2531 × 2000
    obs: 'day', 'proliferation', 'G2M_score', 'S_score', 'phase', 'clusters_coarse', 'clusters', 'clusters_fine', 'louvain_Alpha', 'louvain_Beta', 'initial_size_unspliced', 'initial_size_spliced', 'initial_size', 'n_counts', 'velocity_self_transition', 'dpt_pseudotime'
    var: 'highly_variable_genes', 'gene_count_corr', 'means', 'dispersions', 'dispersions_norm', 'fit_r2', 'fit_alpha', 'fit_beta', 'fit_gamma', 'fit_t_', 'fit_scaling', 'fit_std_u', 'fit_std_s', 'fit_likelihood', 'fit_u0', 'fit_s0', 'fit_pval_steady', 'fit_steady_u', 'fit_steady_s', 'fit_variance', 'fit_alignment_scaling', 'velocity_genes'
    uns: 'clusters_colors', 'clusters_fine_colors', 'diffmap_evals', 'iroot', 'louvain_Alpha_colors', 'louvain_Beta_colors', 'neighbors', 'pca', 'recover_dynamics', 'velocity_graph', 'velocity_graph_neg', 'velocity_params'
    obsm: 'X_diffmap', 'X_pca', 'X_umap', 'velocity_umap'
    varm: 'PCs', 'loss'
    layers: 'Ms', 'Mu', 'fit_t', 'fit_tau', 'fit_tau_', 'spliced', 'unspliced', 'velocity', 'velocity_u'
    obsp: 'connectivities', 'distances'

First, we prepare the kernel using the high-level pipeline and the estimator.

k =
    adata, weight_connectivities=0.2, softmax_scale=4, show_progress_bar=False
g =

Next, we compute the Schur vectors. See Compute Schur vectors for more information.


Now we can compute the macrostates of the process, which also computes the coarse-grained transition matrix. Here, parameter cluster_key tries to associate the names of macrostates with the cluster labels. For more options, see Compute macrostates.

g.compute_macrostates(n_states=6, cluster_key="clusters")

We can now plot the coarse-grained transition matrix.

g.plot_coarse_T(text_kwargs={"fontsize": 10})
coarse-grained transition matrix


/home/docs/checkouts/ UserWarning: FixedFormatter should only be used together with FixedLocator

The coarse-grained transition matrix can also be used when setting the terminal states, see Compute terminal states using GPCCA.

Total running time of the script: ( 0 minutes 22.783 seconds)

Estimated memory usage: 711 MB

Gallery generated by Sphinx-Gallery