Plot projection

This example shows how to project a transition matrix onto a low-dimensional embedding.

import scvelo as scv
import cellrank as cr

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

Out:

  0%|          | 0.00/140M [00:00<?, ?B/s]
  0%|          | 40.0k/140M [00:00<08:59, 272kB/s]
  0%|          | 176k/140M [00:00<03:46, 649kB/s]
  0%|          | 568k/140M [00:00<01:34, 1.56MB/s]
  2%|1         | 2.12M/140M [00:00<00:27, 5.20MB/s]
  4%|3         | 5.16M/140M [00:00<00:13, 10.8MB/s]
  6%|5         | 8.19M/140M [00:00<00:09, 14.2MB/s]
  8%|8         | 11.2M/140M [00:01<00:07, 17.5MB/s]
  9%|9         | 12.8M/140M [00:01<00:07, 16.8MB/s]
 11%|#1        | 15.7M/140M [00:01<00:06, 19.5MB/s]
 12%|#2        | 17.3M/140M [00:01<00:07, 18.4MB/s]
 14%|#4        | 20.2M/140M [00:01<00:06, 20.2MB/s]
 15%|#5        | 21.7M/140M [00:01<00:06, 18.9MB/s]
 18%|#7        | 24.6M/140M [00:01<00:05, 20.6MB/s]
 19%|#8        | 26.2M/140M [00:01<00:06, 19.3MB/s]
 21%|##        | 29.0M/140M [00:01<00:05, 20.8MB/s]
 22%|##1       | 30.6M/140M [00:02<00:05, 19.5MB/s]
 24%|##3       | 33.4M/140M [00:02<00:05, 20.8MB/s]
 25%|##5       | 35.1M/140M [00:02<00:05, 19.6MB/s]
 27%|##6       | 37.8M/140M [00:02<00:05, 20.8MB/s]
 28%|##8       | 39.5M/140M [00:02<00:05, 19.7MB/s]
 30%|###       | 42.2M/140M [00:02<00:04, 20.8MB/s]
 31%|###1      | 43.9M/140M [00:02<00:05, 19.7MB/s]
 33%|###3      | 46.7M/140M [00:02<00:04, 20.8MB/s]
 34%|###4      | 48.3M/140M [00:02<00:04, 19.6MB/s]
 36%|###6      | 51.1M/140M [00:03<00:04, 20.8MB/s]
 38%|###7      | 52.7M/140M [00:03<00:04, 19.7MB/s]
 40%|###9      | 55.6M/140M [00:03<00:04, 21.0MB/s]
 41%|####      | 57.2M/140M [00:03<00:04, 19.6MB/s]
 43%|####2     | 60.0M/140M [00:03<00:04, 20.9MB/s]
 44%|####3     | 61.6M/140M [00:03<00:04, 19.6MB/s]
 46%|####5     | 64.4M/140M [00:03<00:03, 20.9MB/s]
 47%|####7     | 66.0M/140M [00:03<00:03, 19.6MB/s]
 49%|####9     | 68.8M/140M [00:04<00:03, 21.1MB/s]
 50%|#####     | 70.4M/140M [00:04<00:03, 19.5MB/s]
 52%|#####2    | 73.3M/140M [00:04<00:03, 21.2MB/s]
 53%|#####3    | 74.9M/140M [00:04<00:03, 19.6MB/s]
 55%|#####5    | 77.7M/140M [00:04<00:03, 21.1MB/s]
 57%|#####6    | 79.3M/140M [00:04<00:03, 19.5MB/s]
 59%|#####8    | 82.1M/140M [00:04<00:02, 21.0MB/s]
 60%|#####9    | 83.7M/140M [00:04<00:03, 19.5MB/s]
 62%|######1   | 86.6M/140M [00:04<00:02, 21.1MB/s]
 63%|######2   | 88.2M/140M [00:05<00:02, 19.6MB/s]
 65%|######4   | 91.0M/140M [00:05<00:02, 21.0MB/s]
 66%|######6   | 92.6M/140M [00:05<00:02, 19.5MB/s]
 68%|######8   | 95.4M/140M [00:05<00:02, 21.0MB/s]
 69%|######9   | 97.0M/140M [00:05<00:02, 19.5MB/s]
 71%|#######1  | 99.8M/140M [00:05<00:02, 20.9MB/s]
 72%|#######2  | 101M/140M [00:05<00:02, 19.5MB/s]
 74%|#######4  | 104M/140M [00:05<00:01, 21.0MB/s]
 76%|#######5  | 106M/140M [00:05<00:01, 19.5MB/s]
 78%|#######7  | 109M/140M [00:06<00:01, 21.0MB/s]
 79%|#######8  | 110M/140M [00:06<00:01, 19.5MB/s]
 81%|########  | 113M/140M [00:06<00:01, 20.9MB/s]
 82%|########1 | 115M/140M [00:06<00:01, 19.4MB/s]
 84%|########3 | 118M/140M [00:06<00:01, 21.0MB/s]
 85%|########5 | 119M/140M [00:06<00:01, 19.6MB/s]
 87%|########6 | 122M/140M [00:06<00:00, 20.9MB/s]
 88%|########8 | 124M/140M [00:06<00:00, 19.6MB/s]
 90%|######### | 126M/140M [00:06<00:00, 20.8MB/s]
 91%|#########1| 128M/140M [00:07<00:00, 19.6MB/s]
 93%|#########3| 131M/140M [00:07<00:00, 20.9MB/s]
 94%|#########4| 132M/140M [00:07<00:00, 19.6MB/s]
 96%|#########6| 135M/140M [00:07<00:00, 20.9MB/s]
 98%|#########7| 137M/140M [00:07<00:00, 19.6MB/s]
100%|#########9| 140M/140M [00:07<00:00, 20.8MB/s]
100%|##########| 140M/140M [00:07<00:00, 19.0MB/s]

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 create the kernel using the high-level function cellrank.tl.transition_matrix(), but any instance of cellrank.tl.kernels.Kernel would do.

k = cr.tl.transition_matrix(
    adata, weight_connectivities=0.2, softmax_scale=4, show_progress_bar=False
)
k

Out:

((0.8 * <VelocityKernel>) + (0.2 * <ConnectivityKernel>))

Next, we compute the projection of the transition matrix onto an embedding, in this case UMAP. The projected transition matrix can be found in adata.obsm['T_fwd_umap'].

k.compute_projection(basis="umap")

The projected transition matrix can now be visualized using scvelo. Below we use scvelo.pl.velocity_embedding_stream() to visualize it as streamlines.

scv.pl.velocity_embedding_stream(adata, vkey="T_fwd", basis="umap")
clusters

Out:

/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/scvelo/plotting/utils.py:115: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
  ax = pl.figure(None, figsize, dpi=dpi).gca(projection=projection)

Another way of visualizing the transition matrix is to simulate random walks on Markov chain and plot them in an embedding, see Plot random walks for more information.

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

Estimated memory usage: 394 MB

Gallery generated by Sphinx-Gallery