CellRank for directed singlecell fate mapping¶
CellRank is a toolkit to uncover cellular dynamics based on Markov state modeling of singlecell data. It contains two main modules: kernels compute cellcell transition probabilities and estimators generate hypothesis based on these. Our kernels work with a variety of input data including RNA velocity (see [Manno18] and [Bergen20]), cellular similarity (both transcriptomic and spatial) and pseudotime, among others. Our VelocityKernel takes into account uncertainty in the velocities and allows you to aggregate the shortrange fate relations given by RNA velocity into longer trends along the phenotypic manifold. Our main estimator is Generalized Perron Cluster Cluster Analysis (GPCCA) [GPCCA18] which coarsegrains the Markov chain into a set of macrostates which represent initial, terminal and intermediate states. For each transient cell, we compute its fate probability towards any terminal state. We show an example of such a fate map in the figure above, which has been computed using the data of [Panc19]. CellRank combines kernels and estimators with a powerful plotting API, enabling you to visualize e.g. smooth gene expression trends along lineages or fateinformed circular embeddings, to name just a few.
CellRank scales to large cell numbers, is fully compatible with scanpy and scvelo and is easy to use.
Getting started with CellRank¶
If you’re new to CellRank, make sure to go though the basic tutorial which introduces you to CellRank’s highlevel API. Most biological systems require a bit more control, so be sure to check out the kernels and estimators tutorial which allows to unlock the full power of CellRank. If you want to see individual functions in action, visit our gallery.
Latest additions¶
1.3.0 20210329¶
This release includes some major additions which make CellRank more applicable with and without RNA velocity information. In particular, it includes:
Additions
Add new kernel
cellrank.tl.kernels.CytoTRACEKernel
which computes cellcell transition probabilities based on the CytoTRACE score [Cyto20], a measure of differentiation potential, PR 527.Add external API
cellrank.external
with a stationary optimal transport kernelcellrank.external.kernels.OTKernel
contributed from [Zhang21], as well as a contributing guide, PR 522.Rename
cellrank.tl.kernels.PalantirKernel
tocellrank.tl.kernels.PseudotimeKernel
and add hard thresholding scheme inspired by [Setty19], a soft thresholding scheme inspired by [VIA21] and a custom scheme when computing the transition matrix, see e.g.cellrank.tl.kernels.SoftThresholdScheme
PR 514.Add more flexibility to
cellrank.tl.kernels.ConnectivityKernel
, allowing it to use any cellcell similarities fromanndata.AnnData.obsp
, such as spatial similarities fromsquidpy
[Palla21] PR 501.Revamp Pancreas Advanced tutorial to showcase CellRank’s modular structure of kernels and estimators. PR 32.
Add 2 new tutorials:
Beyond RNA velocity: shows how to use CellRank when no RNA velocity information is available. PR 32
Creating a new kernel: explains how to create your own custom kernel class that estimates cellcell transition probabilities PR 31.
Add projection of transition matrix onto an embedding
cellrank.tl.kernels.Kernel.compute_projection()
Add random walk simulation and visualization in an embedding
cellrank.tl.kernels.Kernel.plot_random_walks()
PR 537.Add
cellrank.tl.Lineage.priming_degree()
PR 502 which estimates a cell’s plasticity/differentiation potential based on ideas by [Setty19] and [Velten17].Add checks for transition matrix irreducibility PR 516.
Add Zebrafish development dataset from [Farrel18] PR 539.
Speedup stationary distribution calculation in
pygpcca
PR 22.
Bugfixes
CellRank’s key applications¶
compute initial & terminal as well as intermediate macrostates of your biological system
infer fate probabilities towards the terminal states for each individual cell
visualize gene expression trends along specific lineages while accounting for the continuous nature of fate determination
identify potential driver genes for each identified cellular trajectory
Why is it called “CellRank”?¶
CellRank does not rank cells, we gave the package this name because just like Google’s original PageRank algorithm, it works with Markov chains to aggregate relationships between individual objects (cells vs. websites) to learn about more global properties of the underlying dynamics (initial & terminal states and fate probabilities vs. website relevance).
Support¶
We welcome your feedback! Feel free to open an issue, send us an email or tweet if you encounter a bug, need our help or just want to make a comment/suggestion.
Contributing¶
We actively encourage any contribution! To get started, please check out both the contribution guide as well as the external API. CellRank’s modular structure makes it easy to contribute, be it a new method to compute cellcell transition probabilities (kernels), a new way to analyze a transition matrix (estimators) or an addition to the plotting API. If you’re thinking of contributing a new kernel, we have a kernel tutorial that guides you trough the process.
CellRank was developed in collaboration between the Theislab and the Peerlab.
Installation¶
CellRank requires Python version >= 3.6 to run. We recommend using Miniconda to manage the environments.
Bioconda¶
CellRank can be installed via:
conda install c condaforge c bioconda cellrank
# or with extra libraries, useful for large datasets
conda install c condaforge c bioconda cellrankkrylov
If an error occurs during conda install c condaforge c bioconda cellrankkrylov
, please consult the
Dependencies section below.
PyPI¶
CellRank is also available on PyPI:
pip install cellrank
# or with extra libraries, useful for large datasets
pip install 'cellrank[krylov]'
# or with external modules, see External API
pip install 'cellrank[external]'
If an error occurs during pip install 'cellrank[krylov]'
, please consult the Dependencies section below.
Development Version¶
To stay uptodate with the newest version, run:
pip install git+https://github.com/theislab/cellrank@dev
Dependencies¶
Some of the inference tasks that CellRank performs can be broken down to linear algebra problems. For example, we solve linear systems to compute fate probabilities and we compute partial Schur decompositions and find metastable states. For these computations to be scalable, we rely on highly optimized libraries which make use of sparsity structure, parallel implementations and efficient message passing implemented via PETSc and SLEPc. CellRank works without these as well, however, if you would like to apply it to large (>15k cells) datasets, we recommend you install them.
Below, we give details for installing both PETSc and SLEPc, in case you’ve had any issues when installing them through PyPI or Bioconda:
# note: conda alternatives are denoted by alt.
# update
sudo aptget update
sudo aptget upgrade
# install a message passing interface and mpi4py
sudo aptget install libopenmpidev # alt.: conda install c condaforge openmpi
pip install user mpi4py # alt.: conda install c anaconda mpi4py
# install petsc and and petsc4py
pip install user petsc # alt.: conda install c condaforge petsc
pip install user petsc4py # alt.: conda install c condaforge petsc4py
# install slepsc and slepsc4py
pip install user slepc # alt.: conda install c condaforge slepc
pip install user slepc4py # alt.: conda install c condaforge slepc4py
During installation of petsc, petsc4py, slepc, and slepc4py the following error might appear several times:
ERROR: Failed building wheel for [insert package name here]
but this doesn’t matter if the installer finally tells you:
Successfully installed [insert package name here]
On Mac OS, install MPICH as a message passing interface and then proceed as above, using either pip or the installation instructions given on the PETSc and SLEPc websites. The SLEPc homepage even offers a video tutorial explaining the installation.
If after reading this, you still can’t proceed with the installation, feel free to open a GitHub issue.
Jupyter Notebook¶
To run the tutorials in a notebook locally, please install:
pip install notebook
and run jupyter notebook
in the terminal. If you get the error Not a directory: 'xdgsettings'
,
use jupyter notebook nobrowser
instead and open the url manually (or use this
bugfix). Alternatively,
you can run all tutorials interactively directly in your browser using binder. Just click the
binder button at the top of each tutorial.
If you run into issues, feel free to open a GitHub issue or send us an email .
API¶
Import CellRank as:
import cellrank as cr
Once velocities and the velocity graph have been computed using either scvelo or velocyto, CellRank offers two modes to interact with its core functionality:
high level mode, essentially calling
cellrank.tl.terminal_states()
,cellrank.tl.initial_states()
andcellrank.tl.lineages()
. See our CellRank basics tutorial.low level mode, interacting directly with the kernels defined in
cellrank.tl.kernels.Kernel
and the estimatorscellrank.tl.estimators.GPCCA
orcellrank.tl.estimators.CFLARE
. The division into kernels and estimators ensures that CellRank in broadly applicable, no matter how you have computed your transition matrix. See our Kernels and estimators tutorial.
Additionally, there is a set of plotting functions which can be used downstream of either analysis mode.
The utilities are mainly for fitting continuous models to gene expression data
and are utilized in some of the plotting functions, like cellrank.pl.gene_trends()
.
Tools¶
This module offers a highlevel API to compute cell fates and driver genes.

Compute a transition matrix based on a combination of RNA Velocity and transcriptomic or spatial similarity. 

Find initial states of a dynamic process of single cells based on RNA velocity [Manno18]. 

Find terminal states of a dynamic process of single cells based on RNA velocity [Manno18]. 

Compute probabilistic lineage assignment using RNA velocity. 

Compute driver genes per lineage. 
Kernels¶
Kernels are part of the lowlevel API and are used to estimate celltocell transitions.

Kernel which computes a transition matrix based on RNA velocity. 

Kernel which computes transition probabilities based on similarities among cells. 

Kernel which computes directed transition probabilities based on a KNN graph and pseudotime. 

Kernel which computes directed transition probabilities based on a KNN graph and the CytoTRACE score [Cyto20]. 
Kernel which contains a precomputed transition matrix. 
Estimators¶
Estimators predict cell fates using the transitions derived from Kernels.

Generalized Perron Cluster Cluster Analysis [GPCCA18] as implemented in pyGPCCA. 

Compute the initial/terminal states of a Markov chain via spectral heuristics. 
Plotting¶

Plot initial states uncovered by 

Plot terminal states uncovered by 

Plot lineages that were uncovered using 

Plot lineage drivers that were uncovered using 

Plot absorption probabilities on a circular embedding as done in [Velten17]. 

Plot gene expression trends along lineages. 

Plot a heatmap of smoothed gene expression along specified lineages. 

Cluster gene expression trends within a lineage and plot the clusters. 

Plot aggregate lineage probabilities at a cluster level. 

Plot a graph, visualizing incoming and outgoing edges or selftransitions. 
Utilities¶

Fit Generalized Additive Models (GAMs) using 

Wrapper around R’s mgcv package for fitting Generalized Additive Models (GAMs). 

Wrapper around 

Class representing an already fitted model. 

Model representing a failure of the original 
Reading¶

Read file and return 
Datasets¶

Development of the murine pancreas at E15.5 from [Panc19]. 

Regeneration of murine lung epithelial cells at 13 time points from [Lung20]. 

Reprogramming of mouse embryonic fibroblasts to induced endoderm progenitors at 8 time points from [Morris18]. 

Zebrafish embryogenesis assayed using dropseq, restricted to the axial mesoderm lineage from [Farrel18]. 

Development of the murine pancreas at E15.5 from [Panc19], preprocessed according to the basic tutorial. 
External API¶
This is a collection of external tools that are also accessible through CellRank. If you wish to contribute, please refer to our contributing guide.
Kernels¶

Stationary optimal transport kernel from [Zhang21]. 
Estimators¶
Models¶
Classes¶
Estimators¶
GPCCA¶

class
cellrank.tl.estimators.
GPCCA
(obj, inplace=True, read_from_adata=False, obsp_key=None, g2m_key='G2M_score', s_key='S_score', write_to_adata=True, key=None)[source]¶ Generalized Perron Cluster Cluster Analysis [GPCCA18] as implemented in pyGPCCA.
Coarsegrains a discrete Markov chain into a set of macrostates and computes coarsegrained transition probabilities among the macrostates. Each macrostate corresponds to an area of the state space, i.e. to a subset of cells. The assignment is soft, i.e. each cell is assigned to every macrostate with a certain weight, where weights sum to one per cell. Macrostates are computed by maximizing the ‘crispness’ which can be thought of as a measure for minimal overlap between macrostates in a certain innerproduct sense. Once the macrostates have been computed, we project the large transition matrix onto a coarsegrained transition matrix among the macrostates via a Galerkin projection. This projection is based on invariant subspaces of the original transition matrix which are obtained using the real Schur decomposition [GPCCA18].
 Parameters
obj¶ (
Union
[KernelExpression
, ~AnnData,spmatrix
,ndarray
]) – Either acellrank.tl.kernels.Kernel
object, ananndata.AnnData
object which stores the transition matrix in.obsp
attribute ornumpy
orscipy
array.inplace¶ (
bool
) – Whether to modifyadata
object inplace or make a copy.read_from_adata¶ (
bool
) – Whether to read available attributes inadata
, if present.obsp_key¶ (
Optional
[str
]) – Key inobj.obsp
whenobj
is ananndata.AnnData
object.g2m_key¶ (
Optional
[str
]) – Key inadata
.obs
. Can be used to detect cellcycle driven start or endpoints.s_key¶ (
Optional
[str
]) – Key inadata
.obs
. Can be used to detect cellcycle driven start or endpoints.write_to_adata¶ (
bool
) – Whether to write the transition matrix toadata
.obsp
and the parameters toadata
.uns
.key¶ (
Optional
[str
]) – Key used when writing transition matrix toadata
. If None, thekey
is set to ‘T_bwd’ ifbackward
is True, else ‘T_fwd’. Only used whenwrite_to_adata=True
.

compute_macrostates
(n_states=None, n_cells=30, use_min_chi=False, cluster_key=None, en_cutoff=0.7, p_thresh=1e15)[source]¶ Compute the macrostates.
 Parameters
n_states¶ (
Union
[int
,Tuple
[int
,int
],List
[int
],Dict
[str
,int
],None
]) – Number of macrostates. If None, use the eigengap heuristic.n_cells¶ (
Optional
[int
]) – Number of most likely cells from each macrostate to select.use_min_chi¶ (
bool
) – Whether to usepygpcca.GPCCA.minChi()
to calculate the number of macrostates. If True,n_states
corresponds to a closed interval [min, max] inside of which the potentially optimal number of macrostates is searched.cluster_key¶ (
Optional
[str
]) – If a key to cluster labels is given, names and colors of the states will be associated with the clusters.en_cutoff¶ (
Optional
[float
]) – Ifcluster_key
is given, this parameter determines when an approximate recurrent class will be labeled as ‘Unknown’, based on the entropy of the distribution of cells over transcriptomic clusters.p_thresh¶ (
float
) – If cell cycle scores were provided, a Wilcoxon ranksum test is conducted to identify cellcycle states. If the test returns a positive statistic and a pvalue smaller thanp_thresh
, a warning will be issued.
 Returns
Nothing, but updates the following fields:
macrostates_memberships
macrostates
schur
coarse_T
coarse_stationary_distribution
 Return type

set_terminal_states_from_macrostates
(names=None, n_cells=30)[source]¶ Manually select terminal states from macrostates.
 Parameters
names¶ (
Union
[Sequence
[str
],Mapping
[str
,str
],str
,None
]) – Names of the macrostates to be marked as terminal. Multiple states can be combined using ‘,’, such as["Alpha, Beta", "Epsilon"]
. If adict
, keys correspond to the names of the macrostates and the values to the new names. If None, select all macrostates.n_cells¶ (
int
) – Number of most likely cells from each macrostate to select.
 Returns
Nothing, just updates the following fields:
terminal_states_probabilities
terminal_states
 Return type

compute_terminal_states
(method='stability', n_cells=30, alpha=1, stability_threshold=0.96, n_states=None)[source]¶ Automatically select terminal states from macrostates.
 Parameters
One of following:
’eigengap’  select the number of states based on the eigengap of the transition matrix.
’eigengap_coarse’  select the number of states based on the eigengap of the diagonal of the coarsegrained transition matrix.
’top_n’  select top
n_states
based on the probability of the diagonal of the coarsegrained transition matrix.’stability’  select states which have a stability index >=
stability_threshold
. The stability index is given by the diagonal elements of the coarsegrained transition matrix.
n_cells¶ (
int
) – Number of most likely cells from each macrostate to select.alpha¶ (
Optional
[float
]) – Weight given to the deviation of an eigenvalue from one. Used whenmethod='eigengap'
ormethod='eigengap_coarse'
.stability_threshold¶ (
float
) – Threshold used whenmethod='stability'
.n_states¶ (
Optional
[int
]) – Numer of states used whenmethod='top_n'
.
 Returns
Nothing, just updates the following fields:
terminal_states_probabilities
terminal_states
 Return type

compute_gdpt
(n_components=10, key_added='gdpt_pseudotime', **kwargs)[source]¶ Compute generalized Diffusion pseudotime from [Haghverdi16] using the real Schur decomposition.
 Parameters
 Returns
Nothing, just updates
adata
.obs[key_added]
with the computed pseudotime. Return type

plot_coarse_T
(show_stationary_dist=True, show_initial_dist=False, cmap='viridis', xtick_rotation=45, annotate=True, show_cbar=True, title=None, figsize=(8, 8), dpi=80, save=None, text_kwargs=mappingproxy({}), **kwargs)[source]¶ Plot the coarsegrained transition matrix between macrostates.
 Parameters
show_stationary_dist¶ (
bool
) – Whether to show the stationary distribution, if present.show_initial_dist¶ (
bool
) – Whether to show the initial distribution.cmap¶ (
Union
[str
,ListedColormap
]) – Colormap to use.annotate¶ (
bool
) – Whether to display the text on each cell.save¶ (
Union
[str
,Path
,None
]) – Filename where to save the plot.text_kwargs¶ (
Mapping
[str
,Any
]) – Keyword arguments formatplotlib.pyplot.text()
.kwargs¶ – Keyword arguments for
matplotlib.pyplot.imshow()
.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

fit
(n_lineages=None, cluster_key=None, keys=None, method='krylov', compute_absorption_probabilities=True, **kwargs)[source]¶ Run the pipeline, computing the macrostates, initial or terminal states and optionally the absorption probabilities.
It is equivalent to running:
if n_lineages is None or n_lineages == 1: compute_eigendecomposition(...) # get the stationary distribution if n_lineages > 1: compute_schur(...) compute_macrostates(...) if n_lineages is None: compute_terminal_states(...) else: set_terminal_states_from_macrostates(...) if compute_absorption_probabilities: compute_absorption_probabilities(...)
 Parameters
n_lineages¶ (
Optional
[int
]) – Number of lineages. If None, it will be determined automatically.cluster_key¶ (
Optional
[str
]) – Match computed states against precomputed clusters to annotate the states. For this, provide a key fromadata
.obs
where cluster labels have been computed.keys¶ (
Optional
[Sequence
[str
]]) – Determines which initial or terminal states to use by passing their names. Further, initial or terminal states can be combined. If e.g. the terminal states are [‘Neuronal_1’, ‘Neuronal_1’, ‘Astrocytes’, ‘OPC’], then passingkeys=['Neuronal_1, Neuronal_2', 'OPC']
means that the two neuronal terminal states are treated as one and the ‘Astrocyte’ state is excluded.method¶ (
str
) – Method to use when computing the Schur decomposition. Valid options are: ‘krylov’ or ‘brandts’.compute_absorption_probabilities¶ (
bool
) – Whether to compute the absorption probabilities or only the initial or terminal states.kwargs¶ – Keyword arguments for
cellrank.tl.estimators.GPCCA.compute_macrostates()
.
 Returns
Nothing, just makes available the following fields:
macrostates_memberships
macrostates
terminal_states_probabilities
terminal_states
absorption_probabilities
priming_degree
 Return type

property
absorption_probabilities
¶ Absorption probabilities.
 Return type
Lineage

property
adata
¶ Annotated data object.
 Returns
Annotated data object.
 Return type

property
coarse_T
¶ Coarsegrained transition matrix.
 Return type
DataFrame

property
coarse_initial_distribution
¶ Coarse initial distribution.
 Return type
Series

property
coarse_stationary_distribution
¶ Coarse stationary distribution.
 Return type
Series

compute_absorption_probabilities
(keys=None, check_irreducibility=False, solver='gmres', use_petsc=True, time_to_absorption=None, n_jobs=None, backend='loky', show_progress_bar=True, tol=1e06, preconditioner=None)¶ Compute absorption probabilities of a Markov chain.
For each cell, this computes the probability of it reaching any of the approximate recurrent classes defined by
terminal_states
. Parameters
keys¶ (
Optional
[Sequence
[str
]]) – Keys defining the recurrent classes.check_irreducibility¶ (
bool
) – Check whether the transition matrix is irreducible.Solver to use for the linear problem. Options are ‘direct’, ‘gmres’, ‘lgmres’, ‘bicgstab’ or ‘gcrotmk’ when
use_petsc=False
or one ofpetsc4py.PETSc.KPS.Type
otherwise.Information on the
scipy
iterative solvers can be found inscipy.sparse.linalg()
or forpetsc4py
solver here.use_petsc¶ (
bool
) – Whether to use solvers frompetsc4py
orscipy
. Recommended for large problems. If no installation is found, defaults toscipy.sparse.linalg.gmres()
.time_to_absorption¶ (
Union
[str
,Sequence
[Union
[str
,Sequence
[str
]]],Dict
[Union
[str
,Sequence
[str
]],str
],None
]) –Whether to compute mean time to absorption and its variance to specific absorbing states.
If a
dict
, can be specified as{'Alpha': 'var', ...}
to also compute variance. In case when states are atuple
, time to absorption will be computed to the subset of these states, such as[('Alpha', 'Beta'), ...]
or{('Alpha', 'Beta'): 'mean', ...}
. Can be specified as'all'
to compute it to any absorbing state inkeys
, which is more efficient than listing all absorbing states.It might be beneficial to disable the progress bar as
show_progress_bar=False
, because many linear systems are being solved.n_jobs¶ (
Optional
[int
]) – Number of parallel jobs to use when using an iterative solver. Whenuse_petsc=True
or for quicklysolvable problems, we recommend higher number (>=8) of jobs in order to fully saturate the cores.backend¶ (
str
) – Which backend to use for multiprocessing. Seejoblib.Parallel
for valid options.show_progress_bar¶ (
bool
) – Whether to show progress bar when the solver isn’t a direct one.tol¶ (
float
) – Convergence tolerance for the iterative solver. The default is fine for most cases, only consider decreasing this for severely illconditioned matrices.preconditioner¶ (
Optional
[str
]) – Preconditioner to use, only available whenuse_petsc=True
. For available values, see here or the values of petsc4py.PETSc.PC.Type. We recommended ‘ilu’ preconditioner for badly conditioned problems.
 Returns
Nothing, but updates the following fields:
absorption_probabilities
 probabilities of being absorbed into the terminal states.lineage_absorption_times
 mean times until absorption to subset absorbing states and optionally their variances saved as'{lineage} mean'
and'{lineage} var'
, respectively, for each subset of absorbing states specified intime_to_absorption
.
 Return type

compute_eigendecomposition
(k=20, which='LR', alpha=1, only_evals=False, ncv=None)¶ Compute eigendecomposition of transition matrix.
Uses a sparse implementation, if possible, and only computes the top \(k\) eigenvectors to speed up the computation. Computes both left and right eigenvectors.
 Parameters
 Returns
Nothing, but updates the following field:
eigendecomposition
 Return type

compute_lineage_drivers
(lineages=None, method='fischer', cluster_key=None, clusters=None, layer='X', use_raw=False, confidence_level=0.95, n_perms=1000, seed=None, return_drivers=True, **kwargs)¶ Compute driver genes per lineage.
Correlates gene expression with lineage probabilities, for a given lineage and set of clusters. Often, it makes sense to restrict this to a set of clusters which are relevant for the specified lineages.
 Parameters
lineages¶ (
Union
[str
,Sequence
,None
]) – Either a set of lineage names fromabsorption_probabilities
.names or None, in which case all lineages are considered.Mode to use when calculating pvalues and confidence intervals. Can be one of:
’fischer’  use Fischer transformation [Fischer21].
’perm_test’  use permutation test.
cluster_key¶ (
Optional
[str
]) – Key fromadata
.obs
to obtain cluster annotations. These are considered forclusters
.clusters¶ (
Union
[str
,Sequence
,None
]) – Restrict the correlations to these clusters.use_raw¶ (
bool
) – Whether or not to useadata
.raw
to correlate gene expression. If using a layer other than.X
, this must be set to False.confidence_level¶ (
float
) – Confidence level for the confidence interval calculation. Must be in [0, 1].n_perms¶ (
int
) – Number of permutations to use whenmethod='perm_test'
.seed¶ (
Optional
[int
]) – Random seed whenmethod='perm_test'
.return_drivers¶ (
bool
) – Whether to return the drivers. This also contains the lower and upperconfidence_level
confidence interval bounds.show_progress_bar¶ – Whether to show a progress bar. Disabling it may slightly improve performance.
n_jobs¶ – Number of parallel jobs. If 1, use all available cores. If None or 1, the execution is sequential.
backend¶ – Which backend to use for parallelization. See
joblib.Parallel
for valid options.
 Return type
Optional
[DataFrame
] Returns

Dataframe of shape
(n_genes, n_lineages * 5)
containing the following columns, 1 for each lineage:{lineage} corr
 correlation between the gene expression and absorption probabilities.{lineage} pval
 calculated pvalues for doublesided test.{lineage} qval
 corrected pvalues using BenjaminiHochberg method at level 0.05.{lineage} ci low
 lower bound of theconfidence_level
correlation confidence interval.{lineage} ci high
 upper bound of theconfidence_level
correlation confidence interval.
Only if
return_drivers=True
. None –
Updates
adata
.var
oradata
.raw.var
, dependinguse_raw
with:'{direction} {lineage} corr'
 the potential lineage drivers.'{direction} {lineage} qval'
 the corrected pvalues.
Updates the following fields:
lineage_drivers
 same as the returned values.

References
 Fischer21
Fisher, R. A. (1921), On the “probable error” of a coefficient of correlation deduced from a small sample., Metron 1 3–32.

compute_lineage_priming
(method='kl_divergence', early_cells=None)¶ Compute the degree of lineage priming.
This method computes how naive vs. committed each individual cell is. It returns a score where 0 stands for naive and 1 stands for committed.
 Parameters
method¶ (
Literal
[‘kl_divergence’, ‘entropy’]) –The method used to compute the degree of lineage priming. Valid options are:
’kl_divergence’: as in [Velten17], computes KLdivergence between the fate probabilities of a cell and the average fate probabilities. Computation of average fate probabilities can be restricted to a set of userdefined
early_cells
.’entropy’: as in [Setty19], computes entropy over a cell’s fate probabilities.
Cell ids or a mask marking early cells. If None, use all cells. Only used when
method='kl_divergence'
. If adict
, the key species a cluster key inanndata.AnnData.obs
and the values specify cluster labels containing early cells. Returns
 Return type
The priming degree.

compute_partition
()¶ Compute communication classes for the Markov chain.
 Returns
Nothing, but updates the following fields:
recurrent_classes
transient_classes
is_irreducible
 Return type

compute_schur
(n_components=10, initial_distribution=None, method='krylov', which='LR', alpha=1)¶ Compute the Schur decomposition.
 Parameters
initial_distribution¶ (
Optional
[ndarray
]) – Input probability distribution over all cells. If None, uniform is chosen.Method for calculating the Schur vectors. Valid options are: ‘krylov’ or ‘brandts’. For benefits of each method, see
pygpcca.GPCCA
.The former is an iterative procedure that computes a partial, sorted Schur decomposition for large, sparse matrices whereas the latter computes a full sorted Schur decomposition of a dense matrix.
which¶ (
str
) – Eigenvalues are in general complex. ‘LR’  largest real part, ‘LM’  largest magnitude.alpha¶ (
float
) – Used to compute the eigengap.alpha
is the weight given to the deviation of an eigenvalue from one.
 Returns
Nothing, but updates the following fields:
schur
schur_matrix
eigendecomposition
 Return type

copy
()¶ Return a copy of self, including the underlying
adata
object. Return type
BaseEstimator

property
is_irreducible
¶ Whether the Markov chain is irreducible or not.

property
kernel
¶ Underlying kernel.
 Return type
KernelExpression

property
lineage_absorption_times
¶ Lineage absorption times.
 Return type
DataFrame

property
lineage_drivers
¶ Lineage drivers.
 Return type
DataFrame

property
macrostates
¶ Macrostates.
 Return type
Series

property
macrostates_memberships
¶ Macrostates memberships.
 Return type
Lineage

plot_absorption_probabilities
(data, prop, discrete=False, lineages=None, cluster_key=None, mode='embedding', time_key='latent_time', title=None, same_plot=False, cmap='viridis', **kwargs)¶ Plot discrete states or probabilities in an embedding.
 Parameters
discrete¶ (
bool
) – Whether to plot in discrete or continuous mode.lineages¶ (
Union
[str
,Sequence
[str
],None
]) – Plot only these lineages. If None, plot all lineages.cluster_key¶ (
Optional
[str
]) – Key fromadata
.obs
for plotting categorical observations.Can be either ‘embedding’ or ‘time’:
’embedding’  plot the embedding while coloring in the absorption probabilities.
’time’  plot the pseudotime on xaxis and the absorption probabilities on yaxis.
time_key¶ (
str
) – Key fromadata
.obs
to use as a pseudotime ordering of the cells.title¶ (
Optional
[str
]) – Either None, in which case titles are'{to,from} {terminal,initial} {state}'
, or an array of titles, one per lineage.same_plot¶ (
bool
) – Whether to plot the lineages on the same plot using color gradients whenmode='embedding'
.cmap¶ (
Union
[str
,ListedColormap
]) – Colormap to use.basis¶ – Basis to use when
mode='embedding'
. If None, use ‘umap’.kwargs¶ – Keyword arguments for
scvelo.pl.scatter()
.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

plot_eigendecomposition
(left=False, *args, **kwargs)¶ Plot eigenvectors in an embedding.
 Parameters
use¶ – Which or how many vectors are to be plotted.
abs_value¶ – Whether to take the absolute value before plotting.
cluster_key¶ – Key in
adata
.obs
for plotting categorical observations.basis¶ – Basis to use when
mode='embedding'
. If None, use ‘umap’.kwargs¶ – Keyword arguments for
scvelo.pl.scatter()
.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

plot_lineage_drivers
(lineage, n_genes=8, ncols=None, use_raw=False, title_fmt='{gene} qval={qval:.4e}', figsize=None, dpi=None, save=None, **kwargs)¶ Plot lineage drivers discovered by
compute_lineage_drivers()
. Parameters
lineage¶ (
str
) – Lineage for which to plot the driver genes.use_raw¶ (
bool
) – Whether to look inadata
.raw.var
oradata
.var
.title_fmt¶ (
str
) – Title format. Possible keywords include {gene}, {qval}, {corr} for gene name, qvalue and correlation, respectively.figsize¶ (
Optional
[Tuple
[float
,float
]]) – Size of the figure.save¶ (
Union
[str
,Path
,None
]) – Filename where to save the plot.kwargs¶ – Keyword arguments for
scvelo.pl.scatter()
.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

plot_macrostates
(data, prop, discrete=False, lineages=None, cluster_key=None, mode='embedding', time_key='latent_time', title=None, same_plot=False, cmap='viridis', **kwargs)¶ Plot discrete states or probabilities in an embedding.
 Parameters
discrete¶ (
bool
) – Whether to plot in discrete or continuous mode.lineages¶ (
Union
[str
,Sequence
[str
],None
]) – Plot only these lineages. If None, plot all lineages.cluster_key¶ (
Optional
[str
]) – Key fromadata
.obs
for plotting categorical observations.Can be either ‘embedding’ or ‘time’:
’embedding’  plot the embedding while coloring in the absorption probabilities.
’time’  plot the pseudotime on xaxis and the absorption probabilities on yaxis.
time_key¶ (
str
) – Key fromadata
.obs
to use as a pseudotime ordering of the cells.title¶ (
Optional
[str
]) – Either None, in which case titles are'{to,from} {terminal,initial} {state}'
, or an array of titles, one per lineage.same_plot¶ (
bool
) – Whether to plot the lineages on the same plot using color gradients whenmode='embedding'
.cmap¶ (
Union
[str
,ListedColormap
]) – Colormap to use.basis¶ – Basis to use when
mode='embedding'
. If None, use ‘umap’.kwargs¶ – Keyword arguments for
scvelo.pl.scatter()
.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

plot_schur
(vectors, prop, use=None, abs_value=False, cluster_key=None, **kwargs)¶ Plot vectors in an embedding.
 Parameters
use¶ (
Union
[int
,Tuple
[int
],List
[int
],None
]) – Which or how many vectors are to be plotted.abs_value¶ (
bool
) – Whether to take the absolute value before plotting.cluster_key¶ (
Optional
[str
]) – Key inadata
.obs
for plotting categorical observations.basis¶ – Basis to use when
mode='embedding'
. If None, use ‘umap’.kwargs¶ – Keyword arguments for
scvelo.pl.scatter()
.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

plot_schur_matrix
(title='schur matrix', cmap='viridis', figsize=None, dpi=80, save=None, **kwargs)¶ Plot the Schur matrix.
 Parameters
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

plot_spectrum
(n=None, real_only=False, show_eigengap=True, show_all_xticks=True, legend_loc=None, title=None, figsize=(5, 5), dpi=100, save=None, marker='.', **kwargs)¶ Plot the top eigenvalues in real or complex plane.
 Parameters
n¶ (
Optional
[int
]) – Number of eigenvalues to show. If None, show all that have been computed.real_only¶ (
bool
) – Whether to plot only the real part of the spectrum.show_eigengap¶ (
bool
) – When real_only=True, this determines whether to show the inferred eigengap as a dotted line.show_all_xticks¶ (
bool
) – When real_only=True, this determines whether to show the indices of all eigenvalues on the xaxis.legend_loc¶ (
Optional
[str
]) – Location parameter for the legend.figsize¶ (
Optional
[Tuple
[float
,float
]]) – Size of the figure.save¶ (
Union
[str
,Path
,None
]) – Filename where to save the plot.marker¶ (
str
) – Marker symbol used, valid options can be found inmatplotlib.markers
.kwargs¶ – Keyword arguments for
matplotlib.pyplot.scatter()
.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

plot_terminal_states
(data, prop, discrete=False, lineages=None, cluster_key=None, mode='embedding', time_key='latent_time', title=None, same_plot=False, cmap='viridis', **kwargs)¶ Plot discrete states or probabilities in an embedding.
 Parameters
discrete¶ (
bool
) – Whether to plot in discrete or continuous mode.lineages¶ (
Union
[str
,Sequence
[str
],None
]) – Plot only these lineages. If None, plot all lineages.cluster_key¶ (
Optional
[str
]) – Key fromadata
.obs
for plotting categorical observations.Can be either ‘embedding’ or ‘time’:
’embedding’  plot the embedding while coloring in the absorption probabilities.
’time’  plot the pseudotime on xaxis and the absorption probabilities on yaxis.
time_key¶ (
str
) – Key fromadata
.obs
to use as a pseudotime ordering of the cells.title¶ (
Optional
[str
]) – Either None, in which case titles are'{to,from} {terminal,initial} {state}'
, or an array of titles, one per lineage.same_plot¶ (
bool
) – Whether to plot the lineages on the same plot using color gradients whenmode='embedding'
.cmap¶ (
Union
[str
,ListedColormap
]) – Colormap to use.basis¶ – Basis to use when
mode='embedding'
. If None, use ‘umap’.kwargs¶ – Keyword arguments for
scvelo.pl.scatter()
.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

property
priming_degree
¶ Priming degree.
 Return type
Series

static
read
(fname)¶ Deserialize self from a file.

property
recurrent_classes
¶ Recurrent classes of the Markov chain.

rename_terminal_states
(new_names, update_adata=True)¶ Rename the names of
terminal_states
.

set_terminal_states
(labels, cluster_key=None, en_cutoff=None, p_thresh=None, add_to_existing=False, **kwargs)¶ Set the approximate recurrent classes, if they are known a priori.
 Parameters
labels¶ (
Union
[Series
,Dict
[str
,Sequence
[Any
]]]) – Either a categoricalpandas.Series
with index as cell names, where NaN marks marks a cell belonging to a transient state or adict
, where each key is the name of the recurrent class and values are list of cell names.cluster_key¶ (
Optional
[str
]) – If a key to cluster labels is given,terminal_states
will be associated with these for naming and colors.en_cutoff¶ (
Optional
[float
]) – Ifcluster_key
is given, this parameter determines when an approximate recurrent class will be labeled as ‘Unknown’, based on the entropy of the distribution of cells over transcriptomic clusters.p_thresh¶ (
Optional
[float
]) – If cell cycle scores were provided, a Wilcoxon ranksum test is conducted to identify cellcycle states. If the test returns a positive statistic and a pvalue smaller thanp_thresh
, a warning will be issued.add_to_existing¶ (
bool
) – Whether to add these categories to existing ones. Cells already belonging to recurrent classes will be updated if there’s an overlap. Throws an error if previous approximate recurrent classes have not been calculated.
 Returns
Nothing, but updates the following fields:
terminal_states_probabilities
terminal_states
 Return type

property
terminal_states
¶ Terminal states.
 Return type
Series

property
terminal_states_probabilities
¶ Terminal states probabilities.
 Return type
Series

property
transient_classes
¶ Transient classes of the Markov chain.
CFLARE¶

class
cellrank.tl.estimators.
CFLARE
(obj, inplace=True, read_from_adata=False, obsp_key=None, g2m_key='G2M_score', s_key='S_score', write_to_adata=True, key=None)[source]¶ Compute the initial/terminal states of a Markov chain via spectral heuristics.
This estimator uses the left eigenvectors of the transition matrix to filter to a set of recurrent cells and the right eigenvectors to cluster this set of cells into discrete groups.
 Parameters
obj¶ (
Union
[KernelExpression
, ~AnnData,spmatrix
,ndarray
]) – Either acellrank.tl.kernels.Kernel
object, ananndata.AnnData
object which stores the transition matrix in.obsp
attribute ornumpy
orscipy
array.inplace¶ (
bool
) – Whether to modifyadata
object inplace or make a copy.read_from_adata¶ (
bool
) – Whether to read available attributes inadata
, if present.obsp_key¶ (
Optional
[str
]) – Key inobj.obsp
whenobj
is ananndata.AnnData
object.g2m_key¶ (
Optional
[str
]) – Key inadata
.obs
. Can be used to detect cellcycle driven start or endpoints.s_key¶ (
Optional
[str
]) – Key inadata
.obs
. Can be used to detect cellcycle driven start or endpoints.write_to_adata¶ (
bool
) – Whether to write the transition matrix toadata
.obsp
and the parameters toadata
.uns
.key¶ (
Optional
[str
]) – Key used when writing transition matrix toadata
. If None, thekey
is set to ‘T_bwd’ ifbackward
is True, else ‘T_fwd’. Only used whenwrite_to_adata=True
.

compute_terminal_states
(use=None, percentile=98, method='kmeans', cluster_key=None, n_clusters_kmeans=None, n_neighbors=20, resolution=0.1, n_matches_min=0, n_neighbors_filtering=15, basis=None, n_comps=5, scale=False, en_cutoff=0.7, p_thresh=1e15)[source]¶ Find approximate recurrent classes of the Markov chain.
Filter to obtain recurrent states in left eigenvectors. Cluster to obtain approximate recurrent classes in right eigenvectors.
 Parameters
use¶ (
Union
[int
,Tuple
[int
],List
[int
],range
,None
]) – Which or how many first eigenvectors to use as features for clustering/filtering. If None, use the eigengap statistic.percentile¶ (
Optional
[int
]) – Threshold used for filtering out cells which are most likely transient states. Cells which are in the lowerpercentile
percent of each eigenvector will be removed from the data matrix.method¶ (
str
) – Method to be used for clustering. Must be one of ‘louvain’, ‘leiden’ or ‘kmeans’.cluster_key¶ (
Optional
[str
]) – If a key to cluster labels is given,terminal_states
will get associated with these for naming and colors.n_clusters_kmeans¶ (
Optional
[int
]) – If None, this is set touse + 1
.n_neighbors¶ (
int
) – If we use ‘louvain’ or ‘leiden’ for clustering cells, we need to build a KNN graph. This is the \(K\) parameter for that, the number of neighbors for each cell.resolution¶ (
float
) – Resolution parameter for ‘louvain’ or ‘leiden’ clustering. Should be chosen relatively small.n_matches_min¶ (
Optional
[int
]) – Filters out cells which don’t have at least n_matches_min neighbors from the same class. This filters out some cells which are transient but have been misassigned.n_neighbors_filtering¶ (
int
) – Parameter for filtering cells. Cells are filtered out if they don’t have at leastn_matches_min
neighbors among theirn_neighbors_filtering
nearest cells.basis¶ (
Optional
[str
]) – Key from :paramref`adata`.obsm
to be used as additional features for the clustering.n_comps¶ (
int
) – Number of embedding components to be use whenbasis
is not None.scale¶ (
bool
) – Scale to zscores. Consider using this if appending embedding to features.en_cutoff¶ (
Optional
[float
]) – Ifcluster_key
is given, this parameter determines when an approximate recurrent class will be labeled as ‘Unknown’, based on the entropy of the distribution of cells over transcriptomic clusters.p_thresh¶ (
float
) – If cell cycle scores were provided, a Wilcoxon ranksum test is conducted to identify cellcycle states. If the test returns a positive statistic and a pvalue smaller thanp_thresh
, a warning will be issued.
 Returns
Nothing, but updates the following fields:
terminal_states_probabilities
terminal_states
 Return type

fit
(n_lineages, keys=None, cluster_key=None, compute_absorption_probabilities=True, **kwargs)[source]¶ Run the pipeline, computing the initial or terminal states and optionally the absorption probabilities.
It is equivalent to running:
compute_eigendecomposition(...) compute_terminal_states(...) compute_absorption_probabilities(...)
 Parameters
n_lineages¶ (
Optional
[int
]) – Number of lineages. If None, it will be determined automatically.cluster_key¶ (
Optional
[str
]) – Match computed states against precomputed clusters to annotate the states. For this, provide a key fromadata
.obs
where cluster labels have been computed.keys¶ (
Optional
[Sequence
[str
]]) – Determines which initial or terminal states to use by passing their names. Further, initial or terminal states can be combined. If e.g. the terminal states are [‘Neuronal_1’, ‘Neuronal_1’, ‘Astrocytes’, ‘OPC’], then passingkeys=['Neuronal_1, Neuronal_2', 'OPC']
means that the two neuronal terminal states are treated as one and the ‘Astrocyte’ state is excluded.kwargs¶ – Keyword arguments for
compute_terminal_states()
, such asn_cells
.
 Returns
Nothing, just makes available the following fields:
terminal_states_probabilities
terminal_states
absorption_probabilities
priming_degree
 Return type

property
absorption_probabilities
¶ Absorption probabilities.
 Return type
Lineage

property
adata
¶ Annotated data object.
 Returns
Annotated data object.
 Return type

compute_absorption_probabilities
(keys=None, check_irreducibility=False, solver='gmres', use_petsc=True, time_to_absorption=None, n_jobs=None, backend='loky', show_progress_bar=True, tol=1e06, preconditioner=None)¶ Compute absorption probabilities of a Markov chain.
For each cell, this computes the probability of it reaching any of the approximate recurrent classes defined by
terminal_states
. Parameters
keys¶ (
Optional
[Sequence
[str
]]) – Keys defining the recurrent classes.check_irreducibility¶ (
bool
) – Check whether the transition matrix is irreducible.Solver to use for the linear problem. Options are ‘direct’, ‘gmres’, ‘lgmres’, ‘bicgstab’ or ‘gcrotmk’ when
use_petsc=False
or one ofpetsc4py.PETSc.KPS.Type
otherwise.Information on the
scipy
iterative solvers can be found inscipy.sparse.linalg()
or forpetsc4py
solver here.use_petsc¶ (
bool
) – Whether to use solvers frompetsc4py
orscipy
. Recommended for large problems. If no installation is found, defaults toscipy.sparse.linalg.gmres()
.time_to_absorption¶ (
Union
[str
,Sequence
[Union
[str
,Sequence
[str
]]],Dict
[Union
[str
,Sequence
[str
]],str
],None
]) –Whether to compute mean time to absorption and its variance to specific absorbing states.
If a
dict
, can be specified as{'Alpha': 'var', ...}
to also compute variance. In case when states are atuple
, time to absorption will be computed to the subset of these states, such as[('Alpha', 'Beta'), ...]
or{('Alpha', 'Beta'): 'mean', ...}
. Can be specified as'all'
to compute it to any absorbing state inkeys
, which is more efficient than listing all absorbing states.It might be beneficial to disable the progress bar as
show_progress_bar=False
, because many linear systems are being solved.n_jobs¶ (
Optional
[int
]) – Number of parallel jobs to use when using an iterative solver. Whenuse_petsc=True
or for quicklysolvable problems, we recommend higher number (>=8) of jobs in order to fully saturate the cores.backend¶ (
str
) – Which backend to use for multiprocessing. Seejoblib.Parallel
for valid options.show_progress_bar¶ (
bool
) – Whether to show progress bar when the solver isn’t a direct one.tol¶ (
float
) – Convergence tolerance for the iterative solver. The default is fine for most cases, only consider decreasing this for severely illconditioned matrices.preconditioner¶ (
Optional
[str
]) – Preconditioner to use, only available whenuse_petsc=True
. For available values, see here or the values of petsc4py.PETSc.PC.Type. We recommended ‘ilu’ preconditioner for badly conditioned problems.
 Returns
Nothing, but updates the following fields:
absorption_probabilities
 probabilities of being absorbed into the terminal states.lineage_absorption_times
 mean times until absorption to subset absorbing states and optionally their variances saved as'{lineage} mean'
and'{lineage} var'
, respectively, for each subset of absorbing states specified intime_to_absorption
.
 Return type

compute_eigendecomposition
(k=20, which='LR', alpha=1, only_evals=False, ncv=None)¶ Compute eigendecomposition of transition matrix.
Uses a sparse implementation, if possible, and only computes the top \(k\) eigenvectors to speed up the computation. Computes both left and right eigenvectors.
 Parameters
 Returns
Nothing, but updates the following field:
eigendecomposition
 Return type

compute_lineage_drivers
(lineages=None, method='fischer', cluster_key=None, clusters=None, layer='X', use_raw=False, confidence_level=0.95, n_perms=1000, seed=None, return_drivers=True, **kwargs)¶ Compute driver genes per lineage.
Correlates gene expression with lineage probabilities, for a given lineage and set of clusters. Often, it makes sense to restrict this to a set of clusters which are relevant for the specified lineages.
 Parameters
lineages¶ (
Union
[str
,Sequence
,None
]) – Either a set of lineage names fromabsorption_probabilities
.names or None, in which case all lineages are considered.Mode to use when calculating pvalues and confidence intervals. Can be one of:
’fischer’  use Fischer transformation [Fischer21].
’perm_test’  use permutation test.
cluster_key¶ (
Optional
[str
]) – Key fromadata
.obs
to obtain cluster annotations. These are considered forclusters
.clusters¶ (
Union
[str
,Sequence
,None
]) – Restrict the correlations to these clusters.use_raw¶ (
bool
) – Whether or not to useadata
.raw
to correlate gene expression. If using a layer other than.X
, this must be set to False.confidence_level¶ (
float
) – Confidence level for the confidence interval calculation. Must be in [0, 1].n_perms¶ (
int
) – Number of permutations to use whenmethod='perm_test'
.seed¶ (
Optional
[int
]) – Random seed whenmethod='perm_test'
.return_drivers¶ (
bool
) – Whether to return the drivers. This also contains the lower and upperconfidence_level
confidence interval bounds.show_progress_bar¶ – Whether to show a progress bar. Disabling it may slightly improve performance.
n_jobs¶ – Number of parallel jobs. If 1, use all available cores. If None or 1, the execution is sequential.
backend¶ – Which backend to use for parallelization. See
joblib.Parallel
for valid options.
 Return type
Optional
[DataFrame
] Returns

Dataframe of shape
(n_genes, n_lineages * 5)
containing the following columns, 1 for each lineage:{lineage} corr
 correlation between the gene expression and absorption probabilities.{lineage} pval
 calculated pvalues for doublesided test.{lineage} qval
 corrected pvalues using BenjaminiHochberg method at level 0.05.{lineage} ci low
 lower bound of theconfidence_level
correlation confidence interval.{lineage} ci high
 upper bound of theconfidence_level
correlation confidence interval.
Only if
return_drivers=True
. None –
Updates
adata
.var
oradata
.raw.var
, dependinguse_raw
with:'{direction} {lineage} corr'
 the potential lineage drivers.'{direction} {lineage} qval'
 the corrected pvalues.
Updates the following fields:
lineage_drivers
 same as the returned values.

References
 Fischer21
Fisher, R. A. (1921), On the “probable error” of a coefficient of correlation deduced from a small sample., Metron 1 3–32.

compute_lineage_priming
(method='kl_divergence', early_cells=None)¶ Compute the degree of lineage priming.
This method computes how naive vs. committed each individual cell is. It returns a score where 0 stands for naive and 1 stands for committed.
 Parameters
method¶ (
Literal
[‘kl_divergence’, ‘entropy’]) –The method used to compute the degree of lineage priming. Valid options are:
’kl_divergence’: as in [Velten17], computes KLdivergence between the fate probabilities of a cell and the average fate probabilities. Computation of average fate probabilities can be restricted to a set of userdefined
early_cells
.’entropy’: as in [Setty19], computes entropy over a cell’s fate probabilities.
Cell ids or a mask marking early cells. If None, use all cells. Only used when
method='kl_divergence'
. If adict
, the key species a cluster key inanndata.AnnData.obs
and the values specify cluster labels containing early cells. Returns
 Return type
The priming degree.

compute_partition
()¶ Compute communication classes for the Markov chain.
 Returns
Nothing, but updates the following fields:
recurrent_classes
transient_classes
is_irreducible
 Return type

copy
()¶ Return a copy of self, including the underlying
adata
object. Return type
BaseEstimator

property
is_irreducible
¶ Whether the Markov chain is irreducible or not.

property
kernel
¶ Underlying kernel.
 Return type
KernelExpression

property
lineage_absorption_times
¶ Lineage absorption times.
 Return type
DataFrame

property
lineage_drivers
¶ Lineage drivers.
 Return type
DataFrame

plot_absorption_probabilities
(data, prop, discrete=False, lineages=None, cluster_key=None, mode='embedding', time_key='latent_time', title=None, same_plot=False, cmap='viridis', **kwargs)¶ Plot discrete states or probabilities in an embedding.
 Parameters
discrete¶ (
bool
) – Whether to plot in discrete or continuous mode.lineages¶ (
Union
[str
,Sequence
[str
],None
]) – Plot only these lineages. If None, plot all lineages.cluster_key¶ (
Optional
[str
]) – Key fromadata
.obs
for plotting categorical observations.Can be either ‘embedding’ or ‘time’:
’embedding’  plot the embedding while coloring in the absorption probabilities.
’time’  plot the pseudotime on xaxis and the absorption probabilities on yaxis.
time_key¶ (
str
) – Key fromadata
.obs
to use as a pseudotime ordering of the cells.title¶ (
Optional
[str
]) – Either None, in which case titles are'{to,from} {terminal,initial} {state}'
, or an array of titles, one per lineage.same_plot¶ (
bool
) – Whether to plot the lineages on the same plot using color gradients whenmode='embedding'
.cmap¶ (
Union
[str
,ListedColormap
]) – Colormap to use.basis¶ – Basis to use when
mode='embedding'
. If None, use ‘umap’.kwargs¶ – Keyword arguments for
scvelo.pl.scatter()
.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

plot_eigendecomposition
(left=False, *args, **kwargs)¶ Plot eigenvectors in an embedding.
 Parameters
use¶ – Which or how many vectors are to be plotted.
abs_value¶ – Whether to take the absolute value before plotting.
cluster_key¶ – Key in
adata
.obs
for plotting categorical observations.basis¶ – Basis to use when
mode='embedding'
. If None, use ‘umap’.kwargs¶ – Keyword arguments for
scvelo.pl.scatter()
.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

plot_lineage_drivers
(lineage, n_genes=8, ncols=None, use_raw=False, title_fmt='{gene} qval={qval:.4e}', figsize=None, dpi=None, save=None, **kwargs)¶ Plot lineage drivers discovered by
compute_lineage_drivers()
. Parameters
lineage¶ (
str
) – Lineage for which to plot the driver genes.use_raw¶ (
bool
) – Whether to look inadata
.raw.var
oradata
.var
.title_fmt¶ (
str
) – Title format. Possible keywords include {gene}, {qval}, {corr} for gene name, qvalue and correlation, respectively.figsize¶ (
Optional
[Tuple
[float
,float
]]) – Size of the figure.save¶ (
Union
[str
,Path
,None
]) – Filename where to save the plot.kwargs¶ – Keyword arguments for
scvelo.pl.scatter()
.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

plot_spectrum
(n=None, real_only=False, show_eigengap=True, show_all_xticks=True, legend_loc=None, title=None, figsize=(5, 5), dpi=100, save=None, marker='.', **kwargs)¶ Plot the top eigenvalues in real or complex plane.
 Parameters
n¶ (
Optional
[int
]) – Number of eigenvalues to show. If None, show all that have been computed.real_only¶ (
bool
) – Whether to plot only the real part of the spectrum.show_eigengap¶ (
bool
) – When real_only=True, this determines whether to show the inferred eigengap as a dotted line.show_all_xticks¶ (
bool
) – When real_only=True, this determines whether to show the indices of all eigenvalues on the xaxis.legend_loc¶ (
Optional
[str
]) – Location parameter for the legend.figsize¶ (
Optional
[Tuple
[float
,float
]]) – Size of the figure.save¶ (
Union
[str
,Path
,None
]) – Filename where to save the plot.marker¶ (
str
) – Marker symbol used, valid options can be found inmatplotlib.markers
.kwargs¶ – Keyword arguments for
matplotlib.pyplot.scatter()
.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

plot_terminal_states
(data, prop, discrete=False, lineages=None, cluster_key=None, mode='embedding', time_key='latent_time', title=None, same_plot=False, cmap='viridis', **kwargs)¶ Plot discrete states or probabilities in an embedding.
 Parameters
discrete¶ (
bool
) – Whether to plot in discrete or continuous mode.lineages¶ (
Union
[str
,Sequence
[str
],None
]) – Plot only these lineages. If None, plot all lineages.cluster_key¶ (
Optional
[str
]) – Key fromadata
.obs
for plotting categorical observations.Can be either ‘embedding’ or ‘time’:
’embedding’  plot the embedding while coloring in the absorption probabilities.
’time’  plot the pseudotime on xaxis and the absorption probabilities on yaxis.
time_key¶ (
str
) – Key fromadata
.obs
to use as a pseudotime ordering of the cells.title¶ (
Optional
[str
]) – Either None, in which case titles are'{to,from} {terminal,initial} {state}'
, or an array of titles, one per lineage.same_plot¶ (
bool
) – Whether to plot the lineages on the same plot using color gradients whenmode='embedding'
.cmap¶ (
Union
[str
,ListedColormap
]) – Colormap to use.basis¶ – Basis to use when
mode='embedding'
. If None, use ‘umap’.kwargs¶ – Keyword arguments for
scvelo.pl.scatter()
.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

property
priming_degree
¶ Priming degree.
 Return type
Series

static
read
(fname)¶ Deserialize self from a file.

property
recurrent_classes
¶ Recurrent classes of the Markov chain.

rename_terminal_states
(new_names, update_adata=True)¶ Rename the names of
terminal_states
.

set_terminal_states
(labels, cluster_key=None, en_cutoff=None, p_thresh=None, add_to_existing=False, **kwargs)¶ Set the approximate recurrent classes, if they are known a priori.
 Parameters
labels¶ (
Union
[Series
,Dict
[str
,Sequence
[Any
]]]) – Either a categoricalpandas.Series
with index as cell names, where NaN marks marks a cell belonging to a transient state or adict
, where each key is the name of the recurrent class and values are list of cell names.cluster_key¶ (
Optional
[str
]) – If a key to cluster labels is given,terminal_states
will be associated with these for naming and colors.en_cutoff¶ (
Optional
[float
]) – Ifcluster_key
is given, this parameter determines when an approximate recurrent class will be labeled as ‘Unknown’, based on the entropy of the distribution of cells over transcriptomic clusters.p_thresh¶ (
Optional
[float
]) – If cell cycle scores were provided, a Wilcoxon ranksum test is conducted to identify cellcycle states. If the test returns a positive statistic and a pvalue smaller thanp_thresh
, a warning will be issued.add_to_existing¶ (
bool
) – Whether to add these categories to existing ones. Cells already belonging to recurrent classes will be updated if there’s an overlap. Throws an error if previous approximate recurrent classes have not been calculated.
 Returns
Nothing, but updates the following fields:
terminal_states_probabilities
terminal_states
 Return type

property
terminal_states
¶ Terminal states.
 Return type
Series

property
terminal_states_probabilities
¶ Terminal states probabilities.
 Return type
Series

property
transient_classes
¶ Transient classes of the Markov chain.
Kernels¶
Velocity Kernel¶

class
cellrank.tl.kernels.
VelocityKernel
(adata, backward=False, vkey='velocity', xkey='Ms', gene_subset=None, compute_cond_num=False, check_connectivity=False)[source]¶ Kernel which computes a transition matrix based on RNA velocity.
This borrows ideas from both [Manno18] and [Bergen20]. In short, for each cell i, we compute transition probabilities \(p_{i, j}\) to each cell j in the neighborhood of i. The transition probabilities are computed as a multinomial logistic regression where the weights \(w_j\) (for all j) are given by the vector that connects cell i with cell j in gene expression space, and the features \(x_i\) are given by the velocity vector \(v_i\) of cell i.
 Parameters
adata¶ (
anndata.AnnData
) – Annotated data object.vkey¶ (
str
) – Key inadata
.uns
where the velocities are stored.xkey¶ (
str
) – Key inadata
.layers
where expected gene expression counts are stored.gene_subset¶ (
Optional
[Iterable
]) – List of genes to be used to compute transition probabilities. By default, genes fromadata
.var['velocity_genes']
are used.compute_cond_num¶ (
bool
) – Whether to compute condition number of the transition matrix. Note that this might be costly, since it does not use sparse implementation.check_connectivity¶ (
bool
) – Check whether the underlying KNN graph is connected.

compute_transition_matrix
(mode='deterministic', backward_mode='transpose', scheme='correlation', softmax_scale=None, n_samples=1000, seed=None, check_irreducibility=False, **kwargs)[source]¶ Compute transition matrix based on velocity directions on the local manifold.
For each cell, infer transition probabilities based on the cell’s velocityextrapolated cell state and the cell states of its K nearest neighbors.
 Parameters
How to compute transition probabilities. Valid options are:
’deterministic’  deterministic computation that doesn’t propagate uncertainty.
’monte_carlo’  Monte Carlo average of randomly sampled velocity vectors.
’stochastic’  second order approximation, only available when
jax
is installed.’sampling’  sample 1 transition matrix from the velocity distribution.
Only matters if initialized as
backward
=True. Valid options are:’transpose’  compute transitions from neighboring cells j to cell i.
’negate’  negate the velocity vector.
softmax_scale¶ (
Optional
[float
]) – Scaling parameter for the softmax. If None, it will be estimated using1 / median(correlations)
. The idea behind this is to scale the softmax to counteract everything tending to orthogonality in high dimensions.scheme¶ (
Union
[str
,Callable
]) –Similarity scheme between cells as described in [Li2020]. Can be one of the following:
’dot_product’:
cellrank.tl.kernels.DotProductScheme
.’cosine’:
cellrank.tl.kernels.CosineScheme
.’correlation’:
cellrank.tl.kernels.CorrelationScheme
.
Alternatively, any function can be passed as long as it follows the signature of
cellrank.tl.kernels.SimilaritySchemeABC.__call__()
.n_samples¶ (
int
) – Number of bootstrap samples whenmode='monte_carlo'
.seed¶ (
Optional
[int
]) – Set the seed for random state when the method requiresn_samples
.check_irreducibility¶ (
bool
) – Optional check for irreducibility of the final transition matrix.show_progress_bar¶ – Whether to show a progress bar. Disabling it may slightly improve performance.
n_jobs¶ – Number of parallel jobs. If 1, use all available cores. If None or 1, the execution is sequential.
backend¶ – Which backend to use for parallelization. See
joblib.Parallel
for valid options.
 Returns
Makes available the following fields:
transition_matrix
.logits
.
 Return type

property
logits
¶ Array of shape
(n_cells, n_cells)
containing the logits. Return type
csr_matrix
Cosine similarity scheme¶

class
cellrank.tl.kernels.
CosineScheme
[source]¶ Cosine similarity scheme as defined in eq. (4.7) of [Li2020].
\(v(s_i, s_j) = g(cos(\delta_{i, j}, v_i))\)
where \(v_i\) is the velocity vector of cell \(i\), \(\delta_{i, j}\) corresponds to the transcriptional displacement between cells \(i\) and \(j\) and \(g\) is a softmax function with some scaling parameter.

__call__
(v, D, softmax_scale=1.0)¶ Compute transition probability of a cell to its nearest neighbors using RNA velocity.
 Parameters
v¶ (
ndarray
) – Array of shape(n_genes,)
or(n_neighbors, n_genes)
containing the velocity vector(s). The second case is used for the backward process.D¶ (
ndarray
) – Array of shape(n_neighbors, n_genes)
corresponding to the transcriptomic displacement of the current cell with respect to ist nearest neighbors.softmax_scale¶ (
float
) – Scaling factor for the softmax function.
 Returns
The probability and logits arrays of shape
(n_neighbors,)
. Return type

hessian
(v, D, softmax_scale=1.0)¶ Compute the Hessian.
 Parameters
 Returns
The full Hessian of shape
(n_neighbors, n_genes, n_genes)
or only its diagonal of shape(n_neighbors, n_genes)
. Return type

Correlation scheme¶

class
cellrank.tl.kernels.
CorrelationScheme
[source]¶ Pearson correlation scheme as defined in eq. (4.8) of [Li2020].
\(v(s_i, s_j) = g(corr(\delta_{i, j}, v_i))\)
where \(v_i\) is the velocity vector of cell \(i\), \(\delta_{i, j}\) corresponds to the transcriptional displacement between cells \(i\) and \(j\) and \(g\) is a softmax function with some scaling parameter.

__call__
(v, D, softmax_scale=1.0)¶ Compute transition probability of a cell to its nearest neighbors using RNA velocity.
 Parameters
v¶ (
ndarray
) – Array of shape(n_genes,)
or(n_neighbors, n_genes)
containing the velocity vector(s). The second case is used for the backward process.D¶ (
ndarray
) – Array of shape(n_neighbors, n_genes)
corresponding to the transcriptomic displacement of the current cell with respect to ist nearest neighbors.softmax_scale¶ (
float
) – Scaling factor for the softmax function.
 Returns
The probability and logits arrays of shape
(n_neighbors,)
. Return type

hessian
(v, D, softmax_scale=1.0)¶ Compute the Hessian.
 Parameters
 Returns
The full Hessian of shape
(n_neighbors, n_genes, n_genes)
or only its diagonal of shape(n_neighbors, n_genes)
. Return type

Dot product scheme¶

class
cellrank.tl.kernels.
DotProductScheme
[source]¶ Dot product scheme as defined in eq. (4.9) of [Li2020].
\(v(s_i, s_j) = g(\delta_{i, j}^T v_i)\)
where \(v_i\) is the velocity vector of cell \(i\), \(\delta_{i, j}\) corresponds to the transcriptional displacement between cells \(i\) and \(j\) and \(g\) is a softmax function with some scaling parameter.

__call__
(v, D, softmax_scale=1.0)¶ Compute transition probability of a cell to its nearest neighbors using RNA velocity.
 Parameters
v¶ (
ndarray
) – Array of shape(n_genes,)
or(n_neighbors, n_genes)
containing the velocity vector(s). The second case is used for the backward process.D¶ (
ndarray
) – Array of shape(n_neighbors, n_genes)
corresponding to the transcriptomic displacement of the current cell with respect to ist nearest neighbors.softmax_scale¶ (
float
) – Scaling factor for the softmax function.
 Returns
The probability and logits arrays of shape
(n_neighbors,)
. Return type

hessian
(v, D, softmax_scale=1.0)¶ Compute the Hessian.
 Parameters
 Returns
The full Hessian of shape
(n_neighbors, n_genes, n_genes)
or only its diagonal of shape(n_neighbors, n_genes)
. Return type

Connectivity Kernel¶

class
cellrank.tl.kernels.
ConnectivityKernel
(adata, backward=False, conn_key='connectivities', compute_cond_num=False, check_connectivity=False)[source]¶ Kernel which computes transition probabilities based on similarities among cells.
As a measure of similarity, we currently support:
transcriptomic similarities, computed using e.g.
scanpy.pp.neighbors()
, see [Wolf18].spatial similarities, computed using e.g.
squidpy.gr.spatial_neighbors()
, see [Palla21].
The resulting transition matrix is symmetric and thus cannot be used to learn about the direction of the biological process. To include this direction, consider combining with a velocityderived transition matrix via
cellrank.tl.kernels.VelocityKernel
.Optionally, we apply a density correction as described in [Coifman05], where we use the implementation of [Haghverdi16].
 Parameters
adata¶ (
anndata.AnnData
) – Annotated data object.conn_key¶ (
str
) – Key inanndata.AnnData.obsp
to obtain the connectivity matrix, describing cellcell similarity.compute_cond_num¶ (
bool
) – Whether to compute condition number of the transition matrix. Note that this might be costly, since it does not use sparse implementation.check_connectivity¶ (
bool
) – Check whether the underlying KNN graph is connected.

compute_transition_matrix
(density_normalize=True)[source]¶ Compute transition matrix based on transcriptomic similarity.
Uses symmetric, weighted KNN graph to compute symmetric transition matrix. The connectivities are computed using
scanpy.pp.neighbors()
. Depending on the parameters used there, they can be UMAP connectivities or gaussiankernelbased connectivities with adaptive kernel width.
Pseudotime Kernel¶

class
cellrank.tl.kernels.
PseudotimeKernel
(adata, backward=False, time_key='dpt_pseudotime', compute_cond_num=False, check_connectivity=False, **kwargs)[source]¶ Kernel which computes directed transition probabilities based on a KNN graph and pseudotime.
The KNN graph contains information about the (undirected) connectivities among cells, reflecting their similarity. Pseudotime can be used to either remove edges that point against the direction of increasing pseudotime (see [Setty19], or to downweight them (see [VIA21]).
Optionally, we apply a density correction as described in [Coifman05], where we use the implementation of [Haghverdi16].
 Parameters

compute_transition_matrix
(threshold_scheme='soft', frac_to_keep=0.3, b=20.0, nu=1.0, percentile=95, density_normalize=True, check_irreducibility=False, **kwargs)[source]¶ Compute transition matrix based on KNN graph and pseudotemporal ordering.
Depending on the choice of the thresholding_scheme, this is based on ideas by either Palantir (see [Setty19]) or VIA (see [VIA21]).
When using a ‘hard’ thresholding scheme, this based on ideas by Palantir (see [Setty19]) which removes some edges that point against the direction of increasing pseudotime. To avoid disconnecting the graph, it does not remove all edges that point against the direction of increasing pseudotime but keeps the ones that point to cells inside a close radius. This radius is chosen according to the local cell density.
When using a ‘soft’ thresholding scheme, this is based on ideas by VIA (see [VIA21]) which downweights edges that points against the direction of increasing pseudotime. Essentially, the further “behind” a query cell is in pseudotime with respect to the current reference cell, the more penalized will be its graphconnectivity.
 Parameters
frac_to_keep¶ (
float
) – The fract_to_keep * n_neighbors closest neighbors (according to graph connectivities) are kept, no matter whether they lie in the pseudotemporal past or future. This is done to ensure that the graph remains connected. Only used when threshold_scheme=’hard’.b¶ (
float
) – The growth rate of generalized logistic function. Only used when threshold_scheme=’soft’.nu¶ (
float
) – Affects near which asymptote maximum growth occurs. Only used when threshold_scheme=’soft’.perc¶ – Percentile by which to clip the connectivities. Only used when threshold_scheme=’soft’.
density_normalize¶ (
bool
) – Whether or not to use the underlying KNN graph for density normalization.check_irreducibility¶ (
bool
) – Optional check for irreducibility of the final transition matrix.
 Returns
Makes
transition_matrix
available. Return type

property
pseudotime
¶ Pseudotemporal ordering of cells.
 Return type
array
Hard threshold scheme¶

class
cellrank.tl.kernels.
HardThresholdScheme
[source]¶ Thresholding scheme inspired by Palantir [Setty19].
Note that this won’t exactly reproduce the original Palantir results, for three reasons:
Palantir computes the KNN graph in a scaled space of diffusion components.
Palantir uses its own pseudotime to bias the KNN graph which is not implemented here.
Palantir uses a slightly different mechanism to ensure the graph remains connected when removing edges that point into the “pseudotime past”.

__call__
(cell_pseudotime, neigh_pseudotime, neigh_conn, n_neighs, frac_to_keep=0.3)[source]¶ Convert the undirected graph of cellcell similarities into a directed one by removing “past” edges.
This uses a pseudotemporal measure to remove graphedges that point into the pseudotimepast. For each cell, it keeps the closest neighbors, even if they are in the pseudotime past, to make sure the graph remains connected.
 Parameters
neigh_pseudotime¶ (
ndarray
) – Array of shape(n_neighbors,)
containing pseudotimes of neighbors.neigh_conn¶ (
ndarray
) – Array of shape(n_neighbors,)
containing connectivities of the current cell and its neighbors.frac_to_keep¶ (
float
) – The fract_to_keep * n_neighbors closest neighbors (according to graph connectivities) are kept, no matter whether they lie in the pseudotemporal past or future.
 Returns
 Return type
Array of shape
(n_neighbors,)
containing the biased connectivities.
Soft threshold scheme¶

class
cellrank.tl.kernels.
SoftThresholdScheme
[source]¶ Thresholding scheme inspired by [VIA21].
The idea is to downweight edges that points against the direction of increasing pseudotime. Essentially, the further “behind” a query cell is in pseudotime with respect to the current reference cell, the more penalized will be its graphconnectivity.

__call__
(cell_pseudotime, neigh_pseudotime, neigh_conn, b=20.0, nu=1.0, perc=95)[source]¶ Bias the connectivities by downweighting ones to past cells.
This function uses generalized logistic regression to weight the past connectivities.
 Parameters
neigh_pseudotime¶ (
ndarray
) – Array of shape(n_neighbors,)
containing pseudotimes of neighbors.neigh_conn¶ (
ndarray
) – Array of shape(n_neighbors,)
containing connectivities of the current cell and its neighbors.b¶ (
float
) – The growth rate of generalized logistic function.nu¶ (
float
) – Affects near which asymptote maximum growth occurs.perc¶ (
Optional
[int
]) – Percentile by which to clip the connectivities.
 Returns
 Return type
Array of shape
(n_neighbors,)
containing the biased connectivities.

CytoTRACE Kernel¶

class
cellrank.tl.kernels.
CytoTRACEKernel
(adata, backward=False, layer='Ms', aggregation='mean', use_raw=False, compute_cond_num=False, check_connectivity=False)[source]¶ Kernel which computes directed transition probabilities based on a KNN graph and the CytoTRACE score [Cyto20].
The KNN graph contains information about the (undirected) connectivities among cells, reflecting their similarity. CytoTRACE can be used to estimate cellular plasticity and in turn, a pseudotemporal ordering of cells from more plastic to less plastic states. This kernel internally uses the
cellrank.tl.kernels.PseudotimeKernel
to direct the KNN graph on the basis of the CytoTRACEderived pseudotime.Optionally, we apply a density correction as described in [Coifman05], where we use the implementation of [Haghverdi16].
 Parameters
adata¶ (
anndata.AnnData
) – Annotated data object.layer¶ (
str
) – Key inanndata.AnnData.layers
or ‘X’ foranndata.AnnData.X
from where to get the expression.aggregation¶ (
Literal
[‘mean’, ‘median’, ‘hmean’, ‘gmean’]) –How to aggregate expression of the topcorrelating genes. Valid options are:
’mean’: arithmetic mean.
’median’: median.
’gmean’: geometric mean.
’hmean’: harmonic mean.
compute_cond_num¶ (
bool
) – Whether to compute condition number of the transition matrix. Note that this might be costly, since it does not use sparse implementation.
Examples
Workflow:
import scvelo as scv import cellrank as cr adata = cr.datasets.pancreas() sc.pp.filter_genes(adata, min_cells=10) adata.raw = adata.copy() sc.pp.normalize_total(adata) sc.pp.log1p(adata) sc.pp.highly_variable_genes(adata) if 'spliced' not in adata.layers or 'unspliced' not in adata.layers: # use the following trick to get scvelo's moments function working adata.layers['spliced'] = adata.X adata.layers['unspliced'] = adata.X scv.pp.moments(adata, n_pcs=None, n_neighbors=None)

compute_cytotrace
(layer='Ms', aggregation='mean', use_raw=False)[source]¶ Reimplementation of the CytoTRACE algorithm [Cyto20] to estimate cellular plasticity.
Computes the number of genes expressed per cell and ranks genes according to their correlation with this measure. Next, it selects to topcorrelating genes and aggregates their (imputed) expression to obtain the CytoTRACE score. A high score stands for high differentiation potential (naive, plastic cells) and a low score stands for low differentiation potential (mature, differentiation cells).
Note that this will not exactly reproduce the results of the original CytoTRACE algorithm [Cyto20] because we allow for any normalization and imputation techniques whereas CytoTRACE has buildin specific methods for that.
 Parameters
layer¶ (
str
) – Key inanndata.AnnData.layers
or ‘X’ foranndata.AnnData.X
from where to get the expression.aggregation¶ (
Literal
[‘mean’, ‘median’, ‘hmean’, ‘gmean’]) –How to aggregate expression of the topcorrelating genes. Valid options are:
’mean’: arithmetic mean.
’median’: median.
’gmean’: geometric mean.
’hmean’: harmonic mean.
use_raw¶ (
bool
) – Whether to use theanndata.AnnData.raw
to compute the number of genes expressed per cell (#genes/cell) and the correlation of gene expression across cells with #genes/cell.
 Return type
 Returns
Nothing, just modifies
anndata.AnnData.obs
with the following keys –‘ct_score’: the normalized CytoTRACE score.
’ct_pseudotime’: associated pseudotime, essentially 1  CytoTRACE score.
’ct_num_exp_genes’: the number of genes expressed per cell, basis of the CytoTRACE score.
It also modifies
anndata.AnnData.var
with the following keys –‘ct_gene_corr’: the correlation as specified above.
’ct_correlates’: indication of the genes used to compute the CytoTRACE score, i.e. the ones that correlated best with ‘num_exp_genes’.

compute_transition_matrix
(threshold_scheme='soft', frac_to_keep=0.3, b=20.0, nu=1.0, percentile=95, density_normalize=True, check_irreducibility=False, **kwargs)¶ Compute transition matrix based on KNN graph and pseudotemporal ordering.
Depending on the choice of the thresholding_scheme, this is based on ideas by either Palantir (see [Setty19]) or VIA (see [VIA21]).
When using a ‘hard’ thresholding scheme, this based on ideas by Palantir (see [Setty19]) which removes some edges that point against the direction of increasing pseudotime. To avoid disconnecting the graph, it does not remove all edges that point against the direction of increasing pseudotime but keeps the ones that point to cells inside a close radius. This radius is chosen according to the local cell density.
When using a ‘soft’ thresholding scheme, this is based on ideas by VIA (see [VIA21]) which downweights edges that points against the direction of increasing pseudotime. Essentially, the further “behind” a query cell is in pseudotime with respect to the current reference cell, the more penalized will be its graphconnectivity.
 Parameters
frac_to_keep¶ (
float
) – The fract_to_keep * n_neighbors closest neighbors (according to graph connectivities) are kept, no matter whether they lie in the pseudotemporal past or future. This is done to ensure that the graph remains connected. Only used when threshold_scheme=’hard’.b¶ (
float
) – The growth rate of generalized logistic function. Only used when threshold_scheme=’soft’.nu¶ (
float
) – Affects near which asymptote maximum growth occurs. Only used when threshold_scheme=’soft’.perc¶ – Percentile by which to clip the connectivities. Only used when threshold_scheme=’soft’.
density_normalize¶ (
bool
) – Whether or not to use the underlying KNN graph for density normalization.check_irreducibility¶ (
bool
) – Optional check for irreducibility of the final transition matrix.
 Returns
Makes
transition_matrix
available. Return type
Precomputed Kernel¶

class
cellrank.tl.kernels.
PrecomputedKernel
(transition_matrix=None, adata=None, backward=False, compute_cond_num=False)[source]¶ Kernel which contains a precomputed transition matrix.
 Parameters
transition_matrix¶ (
Union
[ndarray
,spmatrix
,KernelExpression
,str
,None
]) – Rownormalized transition matrix or a key inadata
.obsp
or acellrank.tl.kernels.KernelExpression
with the computed transition matrix. If None, try to determine the key based onbackward
.adata¶ (
anndata.AnnData
) – Annotated data object.compute_cond_num¶ (
bool
) – Whether to compute condition number of the transition matrix. Note that this might be costly, since it does not use sparse implementation.
Models¶
GAM¶

class
cellrank.ul.models.
GAM
(adata, n_knots=6, spline_order=3, distribution='gamma', link='log', max_iter=2000, expectile=None, grid=None, spline_kwargs=mappingproxy({}), **kwargs)[source]¶ Fit Generalized Additive Models (GAMs) using
pygam
. Parameters
adata¶ (
anndata.AnnData
) – Annotated data object.spline_order¶ (
int
) – Order of the splines, i.e. 3 for cubic splines.distribution¶ (
str
) – Name of the distribution. Available distributions can be found here.link¶ (
str
) – Name of the link function. Available link functions can be found here.max_iter¶ (
int
) – Maximum number of iterations for optimization.expectile¶ (
Optional
[float
]) – Expectile forpygam.pygam.ExpectileGAM
. This forces the distribution to be ‘normal’ and link function to ‘identity’. Must be in interval (0, 1).grid¶ (
Union
[str
,Mapping
,None
]) – Whether to perform a grid search. Keys correspond to a parameter names and values to range to be searched. If ‘default’, use the default grid. If None, don’t perform a grid search.kwargs¶ – Keyword arguments for
pygam.pygam.GAM
.

fit
(x=None, y=None, w=None, **kwargs)[source]¶ Fit the model.
 Parameters
x¶ (
Optional
[ndarray
]) – Independent variables, array of shape (n_samples, 1). If None, usex
.y¶ (
Optional
[ndarray
]) – Dependent variables, array of shape (n_samples, 1). If None, usey
.w¶ (
Optional
[ndarray
]) – Optional weights ofx
, array of shape (n_samples,). If None, usew
.kwargs¶ – Keyword arguments for underlying
model
’s fitting function.
 Returns
Fits the model and returns self.
 Return type

predict
(x_test=None, key_added='_x_test', **kwargs)[source]¶ Run the prediction.
 Parameters
 Returns
Updates and returns the following:
y_test
 Prediction values of shape (n_samples,) forx_test
.
 Return type

property
adata
¶ Annotated data object.
 Returns
adata – Annotated data object.
 Return type

property
conf_int
¶ Array of shape (n_samples, 2) containing the lower and upper bounds of the confidence interval.
 Return type

confidence_interval
(x_test=None, **kwargs)[source]¶ Calculate the confidence interval.
 Parameters
x_test¶ (
Optional
[ndarray
]) – Array of shape (n_samples,) used for confidence interval calculation. If None, usex_test
.kwargs¶ – Keyword arguments for underlying
model
’s confidence method or fordefault_confidence_interval()
.
 Returns
Updates the following fields:
conf_int
 Array of shape (n_samples, 2) containing the lower and upper bounds of the confidence interval.
 Return type

default_confidence_interval
(x_test=None, **kwargs)¶ Calculate the confidence interval, if the underlying
model
has no method for it.This formula is taken from [DeSalvo70], eq. 5.
 Parameters
x_test¶ (
Optional
[ndarray
]) – Array of shape (n_samples,) used for confidence interval calculation. If None, usex_test
.kwargs¶ – Keyword arguments for underlying
model
’s confidence method or fordefault_confidence_interval()
.
 Returns
Updates the following fields:
conf_int
 Array of shape (n_samples, 2) containing the lower and upper bounds of the confidence interval.x_hat
 Filtered independent variables used when calculating default confidence interval, usually same asx
.y_hat
 Filtered dependent variables used when calculating default confidence interval, usually same asy
.
 Return type
References
 DeSalvo70
DeSalvo, J. S. (1970), Standard Error of Forecast in Multiple Regression: Proof of a Useful Result., RAND Corporation.

plot
(figsize=(8, 5), same_plot=False, hide_cells=False, perc=None, abs_prob_cmap=<matplotlib.colors.ListedColormap object>, cell_color=None, lineage_color='black', alpha=0.8, lineage_alpha=0.2, title=None, size=15, lw=2, cbar=True, margins=0.015, xlabel='pseudotime', ylabel='expression', conf_int=True, lineage_probability=False, lineage_probability_conf_int=False, lineage_probability_color=None, obs_legend_loc='best', dpi=None, fig=None, ax=None, return_fig=False, save=None, **kwargs)¶ Plot the smoothed gene expression.
 Parameters
same_plot¶ (
bool
) – Whether to plot all trends in the same plot.perc¶ (
Optional
[Tuple
[float
,float
]]) – Percentile by which to clip the absorption probabilities.abs_prob_cmap¶ (
ListedColormap
) – Colormap to use when coloring in the absorption probabilities.cell_color¶ (
Optional
[str
]) – Key inanndata.AnnData.obs
oranndata.AnnData.var_names
used for coloring the cells.lineage_alpha¶ (
float
) – Alpha channel for lineage confidence intervals.lineage_probability¶ (
bool
) – Whether to show smoothed lineage probability as a dashed line. Note that this will require 1 additional model fit.lineage_probability_conf_int¶ (
Union
[bool
,float
]) – Whether to compute and show smoothed lineage probability confidence interval. Ifself
iscellrank.ul.models.GAMR
, it can also specify the confidence level, the default is 0.95. Only used whenshow_lineage_probability=True
.lineage_probability_color¶ (
Optional
[str
]) – Color to use when plotting the smoothedlineage_probability
. If None, it’s the same aslineage_color
. Only used whenshow_lineage_probability=True
.obs_legend_loc¶ (
Optional
[str
]) – Location of the legend whencell_color
corresponds to a categorical variable.fig¶ (
Optional
[Figure
]) – Figure to use, if None, create a new one.ax¶ (
matplotlib.axes.Axes
) – Ax to use, if None, create a new one.save¶ (
Optional
[str
]) – Filename where to save the plot. If None, just shows the plots.kwargs¶ – Keyword arguments for
matplotlib.axes.Axes.legend()
, e.g. to disable the legend, specifyloc=None
. Only available whenshow_lineage_probability=True
.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

prepare
(gene, lineage, backward=False, time_range=None, data_key='X', time_key='latent_time', use_raw=False, threshold=None, weight_threshold=(0.01, 0.01), filter_cells=None, n_test_points=200)¶ Prepare the model to be ready for fitting.
 Parameters
gene¶ (
str
) – Gene inadata
.var_names
or inadata
.raw.var_names
.lineage¶ (
Optional
[str
]) – Name of a lineage inadata
.obsm[lineage_key]
. If None, all weights will be set to 1.time_range¶ (
Union
[float
,Tuple
[float
,float
],None
]) –Specify start and end times:
data_key¶ (
str
) – Key inadata
.layers
or ‘X’ foradata
.X
. Ifuse_raw=True
, it’s always set to ‘X’.time_key¶ (
str
) – Key inadata
.obs
where the pseudotime is stored.threshold¶ (
Optional
[float
]) – Consider only cells with weights >threshold
when estimating the test endpoint. If None, use the median of the weights.weight_threshold¶ (
Union
[float
,Tuple
[float
,float
]]) – Set all weights belowweight_threshold
toweight_threshold
if afloat
, or to the second value, if atuple
.filter_cells¶ (
Optional
[float
]) – Filter out all cells with expression values lower than this threshold.n_test_points¶ (
int
) – Number of test points. If None, use the original points based onthreshold
.
 Returns
Nothing, but updates the following fields:
x
 Filtered independent variables of shape (n_filtered_cells, 1) used for fitting.y
 Filtered dependent variables of shape (n_filtered_cells, 1) used for fitting.w
 Filtered weights of shape (n_filtered_cells,) used for fitting.x_all
 Unfiltered independent variables of shape (n_cells, 1).y_all
 Unfiltered dependent variables of shape (n_cells, 1).w_all
 Unfiltered weights of shape (n_cells,).x_test
 Independent variables of shape (n_samples, 1) used for prediction.prepared
 Whether the model is prepared for fitting.
 Return type

property
prepared
¶ Whether the model is prepared for fitting.

static
read
(fname)¶ Deserialize self from a file.

write
(fname, ext='pickle')¶ Serialize self to a file.

property
x
¶ Filtered independent variables of shape (n_filtered_cells, 1) used for fitting.
 Return type

property
x_hat
¶ Filtered independent variables used when calculating default confidence interval, usually same as
x
. Return type

property
x_test
¶ Independent variables of shape (n_samples, 1) used for prediction.
 Return type

property
y
¶ Filtered dependent variables of shape (n_filtered_cells, 1) used for fitting.
 Return type

property
y_hat
¶ Filtered dependent variables used when calculating default confidence interval, usually same as
y
. Return type
SKLearnModel¶

class
cellrank.ul.models.
SKLearnModel
(adata, model, weight_name=None, ignore_raise=False)[source]¶ Wrapper around
sklearn.base.BaseEstimator
. Parameters
adata¶ (
anndata.AnnData
) – Annotated data object.model¶ (
BaseEstimator
) – Instance of the underlyingsklearn
estimator, such assklearn.svm.SVR
.weight_name¶ (
Optional
[str
]) – Name of the weight argument formodel
.fit
. If None, to determine it automatically. If and empty string, no weights will be used.ignore_raise¶ (
bool
) – Do not raise an exception if weight argument is not found in the fitting function ofmodel
. This is useful in case when weight is passed in**kwargs
and cannot be determined from signature.

fit
(x=None, y=None, w=None, **kwargs)[source]¶ Fit the model.
 Parameters
x¶ (
Optional
[ndarray
]) – Independent variables, array of shape (n_samples, 1). If None, usex
.y¶ (
Optional
[ndarray
]) – Dependent variables, array of shape (n_samples, 1). If None, usey
.w¶ (
Optional
[ndarray
]) – Optional weights ofx
, array of shape (n_samples,). If None, usew
.kwargs¶ – Keyword arguments for underlying
model
’s fitting function.
 Returns
Fits the model and returns self.
 Return type

predict
(x_test=None, key_added='_x_test', **kwargs)[source]¶ Run the prediction.
 Parameters
 Returns
Updates and returns the following:
y_test
 Prediction values of shape (n_samples,) forx_test
.
 Return type

confidence_interval
(x_test=None, **kwargs)[source]¶ Calculate the confidence interval.
Use
default_confidence_interval()
function if underlyingmodel
has not method for confidence interval calculation. Parameters
x_test¶ (
Optional
[ndarray
]) – Array of shape (n_samples,) used for confidence interval calculation. If None, usex_test
.kwargs¶ – Keyword arguments for underlying
model
’s confidence method or fordefault_confidence_interval()
.
 Returns
Updates the following fields:
conf_int
 Array of shape (n_samples, 2) containing the lower and upper bounds of the confidence interval.
 Return type

property
model
¶ The underlying
sklearn.base.BaseEstimator
. Return type

property
adata
¶ Annotated data object.
 Returns
adata – Annotated data object.
 Return type

property
conf_int
¶ Array of shape (n_samples, 2) containing the lower and upper bounds of the confidence interval.
 Return type

default_confidence_interval
(x_test=None, **kwargs)¶ Calculate the confidence interval, if the underlying
model
has no method for it.This formula is taken from [DeSalvo70], eq. 5.
 Parameters
x_test¶ (
Optional
[ndarray
]) – Array of shape (n_samples,) used for confidence interval calculation. If None, usex_test
.kwargs¶ – Keyword arguments for underlying
model
’s confidence method or fordefault_confidence_interval()
.
 Returns
Updates the following fields:
conf_int
 Array of shape (n_samples, 2) containing the lower and upper bounds of the confidence interval.x_hat
 Filtered independent variables used when calculating default confidence interval, usually same asx
.y_hat
 Filtered dependent variables used when calculating default confidence interval, usually same asy
.
 Return type
References
 DeSalvo70
DeSalvo, J. S. (1970), Standard Error of Forecast in Multiple Regression: Proof of a Useful Result., RAND Corporation.

plot
(figsize=(8, 5), same_plot=False, hide_cells=False, perc=None, abs_prob_cmap=<matplotlib.colors.ListedColormap object>, cell_color=None, lineage_color='black', alpha=0.8, lineage_alpha=0.2, title=None, size=15, lw=2, cbar=True, margins=0.015, xlabel='pseudotime', ylabel='expression', conf_int=True, lineage_probability=False, lineage_probability_conf_int=False, lineage_probability_color=None, obs_legend_loc='best', dpi=None, fig=None, ax=None, return_fig=False, save=None, **kwargs)¶ Plot the smoothed gene expression.
 Parameters
same_plot¶ (
bool
) – Whether to plot all trends in the same plot.perc¶ (
Optional
[Tuple
[float
,float
]]) – Percentile by which to clip the absorption probabilities.abs_prob_cmap¶ (
ListedColormap
) – Colormap to use when coloring in the absorption probabilities.cell_color¶ (
Optional
[str
]) – Key inanndata.AnnData.obs
oranndata.AnnData.var_names
used for coloring the cells.lineage_alpha¶ (
float
) – Alpha channel for lineage confidence intervals.lineage_probability¶ (
bool
) – Whether to show smoothed lineage probability as a dashed line. Note that this will require 1 additional model fit.lineage_probability_conf_int¶ (
Union
[bool
,float
]) – Whether to compute and show smoothed lineage probability confidence interval. Ifself
iscellrank.ul.models.GAMR
, it can also specify the confidence level, the default is 0.95. Only used whenshow_lineage_probability=True
.lineage_probability_color¶ (
Optional
[str
]) – Color to use when plotting the smoothedlineage_probability
. If None, it’s the same aslineage_color
. Only used whenshow_lineage_probability=True
.obs_legend_loc¶ (
Optional
[str
]) – Location of the legend whencell_color
corresponds to a categorical variable.fig¶ (
Optional
[Figure
]) – Figure to use, if None, create a new one.ax¶ (
matplotlib.axes.Axes
) – Ax to use, if None, create a new one.save¶ (
Optional
[str
]) – Filename where to save the plot. If None, just shows the plots.kwargs¶ – Keyword arguments for
matplotlib.axes.Axes.legend()
, e.g. to disable the legend, specifyloc=None
. Only available whenshow_lineage_probability=True
.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

prepare
(gene, lineage, backward=False, time_range=None, data_key='X', time_key='latent_time', use_raw=False, threshold=None, weight_threshold=(0.01, 0.01), filter_cells=None, n_test_points=200)¶ Prepare the model to be ready for fitting.
 Parameters
gene¶ (
str
) – Gene inadata
.var_names
or inadata
.raw.var_names
.lineage¶ (
Optional
[str
]) – Name of a lineage inadata
.obsm[lineage_key]
. If None, all weights will be set to 1.time_range¶ (
Union
[float
,Tuple
[float
,float
],None
]) –Specify start and end times:
data_key¶ (
str
) – Key inadata
.layers
or ‘X’ foradata
.X
. Ifuse_raw=True
, it’s always set to ‘X’.time_key¶ (
str
) – Key inadata
.obs
where the pseudotime is stored.threshold¶ (
Optional
[float
]) – Consider only cells with weights >threshold
when estimating the test endpoint. If None, use the median of the weights.weight_threshold¶ (
Union
[float
,Tuple
[float
,float
]]) – Set all weights belowweight_threshold
toweight_threshold
if afloat
, or to the second value, if atuple
.filter_cells¶ (
Optional
[float
]) – Filter out all cells with expression values lower than this threshold.n_test_points¶ (
int
) – Number of test points. If None, use the original points based onthreshold
.
 Returns
Nothing, but updates the following fields:
x
 Filtered independent variables of shape (n_filtered_cells, 1) used for fitting.y
 Filtered dependent variables of shape (n_filtered_cells, 1) used for fitting.w
 Filtered weights of shape (n_filtered_cells,) used for fitting.x_all
 Unfiltered independent variables of shape (n_cells, 1).y_all
 Unfiltered dependent variables of shape (n_cells, 1).w_all
 Unfiltered weights of shape (n_cells,).x_test
 Independent variables of shape (n_samples, 1) used for prediction.prepared
 Whether the model is prepared for fitting.
 Return type

property
prepared
¶ Whether the model is prepared for fitting.

static
read
(fname)¶ Deserialize self from a file.

write
(fname, ext='pickle')¶ Serialize self to a file.

property
x
¶ Filtered independent variables of shape (n_filtered_cells, 1) used for fitting.
 Return type

property
x_hat
¶ Filtered independent variables used when calculating default confidence interval, usually same as
x
. Return type

property
x_test
¶ Independent variables of shape (n_samples, 1) used for prediction.
 Return type

property
y
¶ Filtered dependent variables of shape (n_filtered_cells, 1) used for fitting.
 Return type

property
y_hat
¶ Filtered dependent variables used when calculating default confidence interval, usually same as
y
. Return type
GAMR¶

class
cellrank.ul.models.
GAMR
(adata, n_knots=5, distribution='gaussian', basis='cr', knotlocs='auto', offset='default', smoothing_penalty=1.0, **kwargs)[source]¶ Wrapper around R’s mgcv package for fitting Generalized Additive Models (GAMs).
 Parameters
adata¶ (
anndata.AnnData
) – Annotated data object.distribution¶ (
str
) – Distribution family in rpy2.robjects.r, such as ‘gaussian’ or ‘nb’ for negative binomial. If ‘nb’, raw count data inadata
.raw
is always used.basis¶ (
str
) – Basis for the smoothing term. See here for valid options.Position of the knots. Can be one of the following:
’auto’  let mgcv handle the knot positions.
’density’  position the knots based on the density of the pseudotime.
offset¶ (
Union
[str
,ndarray
,None
]) – Offset term for the GAM. Only available whendistribution='nb'
. If ‘default’, it is calculated according to [Robinson10]. The values are saved inadata
.obs['cellrank_offset']
. If None, no offset is used.smoothing_penalty¶ (
float
) – Penalty for the smoothing term. The larger the value, the smoother the fitted curve.kwargs¶ – Keyword arguments for
gam.control
. See here for reference.

prepare
(*args, **kwargs)[source]¶ Prepare the model to be ready for fitting. This also removes the zero and negative weights and prepares the design matrix.
 Parameters
gene¶ – Gene in
adata
.var_names
or inadata
.raw.var_names
.lineage¶ – Name of a lineage in
adata
.obsm[lineage_key]
. If None, all weights will be set to 1.backward¶ – Direction of the process.
time_range¶ –
Specify start and end times:
data_key¶ – Key in
adata
.layers
or ‘X’ foradata
.X
. Ifuse_raw=True
, it’s always set to ‘X’.time_key¶ – Key in
adata
.obs
where the pseudotime is stored.use_raw¶ – Whether to access
adata
.raw
or not.threshold¶ – Consider only cells with weights >
threshold
when estimating the test endpoint. If None, use the median of the weights.weight_threshold¶ – Set all weights below
weight_threshold
toweight_threshold
if afloat
, or to the second value, if atuple
.filter_cells¶ – Filter out all cells with expression values lower than this threshold.
n_test_points¶ – Number of test points. If None, use the original points based on
threshold
.
 Returns
Nothing, but updates the following fields:
x
 Filtered independent variables of shape (n_filtered_cells, 1) used for fitting.y
 Filtered dependent variables of shape (n_filtered_cells, 1) used for fitting.w
 Filtered weights of shape (n_filtered_cells,) used for fitting.x_all
 Unfiltered independent variables of shape (n_cells, 1).y_all
 Unfiltered dependent variables of shape (n_cells, 1).w_all
 Unfiltered weights of shape (n_cells,).x_test
 Independent variables of shape (n_samples, 1) used for prediction.prepared
 Whether the model is prepared for fitting.
 Return type

fit
(x=None, y=None, w=None, **kwargs)[source]¶ Fit the model.
 Parameters
x¶ (
Optional
[ndarray
]) – Independent variables, array of shape (n_samples, 1). If None, usex
.y¶ (
Optional
[ndarray
]) – Dependent variables, array of shape (n_samples, 1). If None, usey
.w¶ (
Optional
[ndarray
]) – Optional weights ofx
, array of shape (n_samples,). If None, usew
.kwargs¶ – Keyword arguments for underlying
model
’s fitting function.
 Returns
Fits the model and returns self. Updates the following fields by filtering out 0 weights
w
:x
 Filtered independent variables of shape (n_filtered_cells, 1) used for fitting.y
 Filtered dependent variables of shape (n_filtered_cells, 1) used for fitting.w
 Filtered weights of shape (n_filtered_cells,) used for fitting.
 Return type

predict
(x_test=None, key_added='_x_test', level=None, **kwargs)[source]¶ Run the prediction. This method can also compute the confidence interval.
 Parameters
x_test¶ (
Optional
[ndarray
]) – Array of shape (n_samples,) used for prediction. If None, usex_test
.key_added¶ (
str
) – Attribute name where to save thex_test
for later use. If None, don’t save it.kwargs¶ – Keyword arguments for underlying
model
’s prediction method.level¶ (
Optional
[float
]) – Confidence level for confidence interval calculation. If None, don’t compute the confidence interval. Must be in the interval [0, 1].
 Returns
Updates and returns the following:
y_test
 Prediction values of shape (n_samples,) forx_test
.
 Return type

confidence_interval
(x_test=None, level=0.95, **kwargs)[source]¶ Calculate the confidence interval. Internally, this method calls
cellrank.ul.models.GAMR.predict()
to extract the confidence interval, if needed. Parameters
 Returns
Updates the following fields:
conf_int
 Array of shape (n_samples, 2) containing the lower and upper bounds of the confidence interval.
 Return type

property
adata
¶ Annotated data object.
 Returns
adata – Annotated data object.
 Return type

property
conf_int
¶ Array of shape (n_samples, 2) containing the lower and upper bounds of the confidence interval.
 Return type

default_confidence_interval
(x_test=None, **kwargs)¶ Calculate the confidence interval, if the underlying
model
has no method for it.This formula is taken from [DeSalvo70], eq. 5.
 Parameters
x_test¶ (
Optional
[ndarray
]) – Array of shape (n_samples,) used for confidence interval calculation. If None, usex_test
.kwargs¶ – Keyword arguments for underlying
model
’s confidence method or fordefault_confidence_interval()
.
 Returns
Updates the following fields:
conf_int
 Array of shape (n_samples, 2) containing the lower and upper bounds of the confidence interval.x_hat
 Filtered independent variables used when calculating default confidence interval, usually same asx
.y_hat
 Filtered dependent variables used when calculating default confidence interval, usually same asy
.
 Return type
References
 DeSalvo70
DeSalvo, J. S. (1970), Standard Error of Forecast in Multiple Regression: Proof of a Useful Result., RAND Corporation.

plot
(figsize=(8, 5), same_plot=False, hide_cells=False, perc=None, abs_prob_cmap=<matplotlib.colors.ListedColormap object>, cell_color=None, lineage_color='black', alpha=0.8, lineage_alpha=0.2, title=None, size=15, lw=2, cbar=True, margins=0.015, xlabel='pseudotime', ylabel='expression', conf_int=True, lineage_probability=False, lineage_probability_conf_int=False, lineage_probability_color=None, obs_legend_loc='best', dpi=None, fig=None, ax=None, return_fig=False, save=None, **kwargs)¶ Plot the smoothed gene expression.
 Parameters
same_plot¶ (
bool
) – Whether to plot all trends in the same plot.perc¶ (
Optional
[Tuple
[float
,float
]]) – Percentile by which to clip the absorption probabilities.abs_prob_cmap¶ (
ListedColormap
) – Colormap to use when coloring in the absorption probabilities.cell_color¶ (
Optional
[str
]) – Key inanndata.AnnData.obs
oranndata.AnnData.var_names
used for coloring the cells.lineage_alpha¶ (
float
) – Alpha channel for lineage confidence intervals.lineage_probability¶ (
bool
) – Whether to show smoothed lineage probability as a dashed line. Note that this will require 1 additional model fit.lineage_probability_conf_int¶ (
Union
[bool
,float
]) – Whether to compute and show smoothed lineage probability confidence interval. Ifself
iscellrank.ul.models.GAMR
, it can also specify the confidence level, the default is 0.95. Only used whenshow_lineage_probability=True
.lineage_probability_color¶ (
Optional
[str
]) – Color to use when plotting the smoothedlineage_probability
. If None, it’s the same aslineage_color
. Only used whenshow_lineage_probability=True
.obs_legend_loc¶ (
Optional
[str
]) – Location of the legend whencell_color
corresponds to a categorical variable.fig¶ (
Optional
[Figure
]) – Figure to use, if None, create a new one.ax¶ (
matplotlib.axes.Axes
) – Ax to use, if None, create a new one.save¶ (
Optional
[str
]) – Filename where to save the plot. If None, just shows the plots.kwargs¶ – Keyword arguments for
matplotlib.axes.Axes.legend()
, e.g. to disable the legend, specifyloc=None
. Only available whenshow_lineage_probability=True
.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

property
prepared
¶ Whether the model is prepared for fitting.

static
read
(fname)¶ Deserialize self from a file.

write
(fname, ext='pickle')¶ Serialize self to a file.

property
x
¶ Filtered independent variables of shape (n_filtered_cells, 1) used for fitting.
 Return type

property
x_hat
¶ Filtered independent variables used when calculating default confidence interval, usually same as
x
. Return type

property
x_test
¶ Independent variables of shape (n_samples, 1) used for prediction.
 Return type

property
y
¶ Filtered dependent variables of shape (n_filtered_cells, 1) used for fitting.
 Return type

property
y_hat
¶ Filtered dependent variables used when calculating default confidence interval, usually same as
y
. Return type
Base Classes¶
BaseEstimator¶

class
cellrank.tl.estimators.
BaseEstimator
(obj, inplace=True, read_from_adata=False, obsp_key=None, g2m_key='G2M_score', s_key='S_score', write_to_adata=True, key=None)[source]¶ Base class for all estimators.
 Parameters
obj¶ (
Union
[KernelExpression
, ~AnnData,spmatrix
,ndarray
]) – Either acellrank.tl.kernels.Kernel
object, ananndata.AnnData
object which stores the transition matrix in.obsp
attribute ornumpy
orscipy
array.inplace¶ (
bool
) – Whether to modifyadata
object inplace or make a copy.read_from_adata¶ (
bool
) – Whether to read available attributes inadata
, if present.obsp_key¶ (
Optional
[str
]) – Key inobj.obsp
whenobj
is ananndata.AnnData
object.g2m_key¶ (
Optional
[str
]) – Key inadata
.obs
. Can be used to detect cellcycle driven start or endpoints.s_key¶ (
Optional
[str
]) – Key inadata
.obs
. Can be used to detect cellcycle driven start or endpoints.write_to_adata¶ (
bool
) – Whether to write the transition matrix toadata
.obsp
and the parameters toadata
.uns
.key¶ (
Optional
[str
]) – Key used when writing transition matrix toadata
. If None, thekey
is set to ‘T_bwd’ ifbackward
is True, else ‘T_fwd’. Only used whenwrite_to_adata=True
.

set_terminal_states
(labels, cluster_key=None, en_cutoff=None, p_thresh=None, add_to_existing=False, **kwargs)[source]¶ Set the approximate recurrent classes, if they are known a priori.
 Parameters
labels¶ (
Union
[Series
,Dict
[str
,Sequence
[Any
]]]) – Either a categoricalpandas.Series
with index as cell names, where NaN marks marks a cell belonging to a transient state or adict
, where each key is the name of the recurrent class and values are list of cell names.cluster_key¶ (
Optional
[str
]) – If a key to cluster labels is given,terminal_states
will be associated with these for naming and colors.en_cutoff¶ (
Optional
[float
]) – Ifcluster_key
is given, this parameter determines when an approximate recurrent class will be labeled as ‘Unknown’, based on the entropy of the distribution of cells over transcriptomic clusters.p_thresh¶ (
Optional
[float
]) – If cell cycle scores were provided, a Wilcoxon ranksum test is conducted to identify cellcycle states. If the test returns a positive statistic and a pvalue smaller thanp_thresh
, a warning will be issued.add_to_existing¶ (
bool
) – Whether to add these categories to existing ones. Cells already belonging to recurrent classes will be updated if there’s an overlap. Throws an error if previous approximate recurrent classes have not been calculated.
 Returns
Nothing, but updates the following fields:
terminal_states_probabilities
terminal_states
 Return type

compute_absorption_probabilities
(keys=None, check_irreducibility=False, solver='gmres', use_petsc=True, time_to_absorption=None, n_jobs=None, backend='loky', show_progress_bar=True, tol=1e06, preconditioner=None)[source]¶ Compute absorption probabilities of a Markov chain.
For each cell, this computes the probability of it reaching any of the approximate recurrent classes defined by
terminal_states
. Parameters
keys¶ (
Optional
[Sequence
[str
]]) – Keys defining the recurrent classes.check_irreducibility¶ (
bool
) – Check whether the transition matrix is irreducible.Solver to use for the linear problem. Options are ‘direct’, ‘gmres’, ‘lgmres’, ‘bicgstab’ or ‘gcrotmk’ when
use_petsc=False
or one ofpetsc4py.PETSc.KPS.Type
otherwise.Information on the
scipy
iterative solvers can be found inscipy.sparse.linalg()
or forpetsc4py
solver here.use_petsc¶ (
bool
) – Whether to use solvers frompetsc4py
orscipy
. Recommended for large problems. If no installation is found, defaults toscipy.sparse.linalg.gmres()
.time_to_absorption¶ (
Union
[str
,Sequence
[Union
[str
,Sequence
[str
]]],Dict
[Union
[str
,Sequence
[str
]],str
],None
]) –Whether to compute mean time to absorption and its variance to specific absorbing states.
If a
dict
, can be specified as{'Alpha': 'var', ...}
to also compute variance. In case when states are atuple
, time to absorption will be computed to the subset of these states, such as[('Alpha', 'Beta'), ...]
or{('Alpha', 'Beta'): 'mean', ...}
. Can be specified as'all'
to compute it to any absorbing state inkeys
, which is more efficient than listing all absorbing states.It might be beneficial to disable the progress bar as
show_progress_bar=False
, because many linear systems are being solved.n_jobs¶ (
Optional
[int
]) – Number of parallel jobs to use when using an iterative solver. Whenuse_petsc=True
or for quicklysolvable problems, we recommend higher number (>=8) of jobs in order to fully saturate the cores.backend¶ (
str
) – Which backend to use for multiprocessing. Seejoblib.Parallel
for valid options.show_progress_bar¶ (
bool
) – Whether to show progress bar when the solver isn’t a direct one.tol¶ (
float
) – Convergence tolerance for the iterative solver. The default is fine for most cases, only consider decreasing this for severely illconditioned matrices.preconditioner¶ (
Optional
[str
]) – Preconditioner to use, only available whenuse_petsc=True
. For available values, see here or the values of petsc4py.PETSc.PC.Type. We recommended ‘ilu’ preconditioner for badly conditioned problems.
 Returns
Nothing, but updates the following fields:
absorption_probabilities
 probabilities of being absorbed into the terminal states.lineage_absorption_times
 mean times until absorption to subset absorbing states and optionally their variances saved as'{lineage} mean'
and'{lineage} var'
, respectively, for each subset of absorbing states specified intime_to_absorption
.
 Return type

compute_lineage_priming
(method='kl_divergence', early_cells=None)[source]¶ Compute the degree of lineage priming.
This method computes how naive vs. committed each individual cell is. It returns a score where 0 stands for naive and 1 stands for committed.
 Parameters
method¶ (
Literal
[‘kl_divergence’, ‘entropy’]) –The method used to compute the degree of lineage priming. Valid options are:
’kl_divergence’: as in [Velten17], computes KLdivergence between the fate probabilities of a cell and the average fate probabilities. Computation of average fate probabilities can be restricted to a set of userdefined
early_cells
.’entropy’: as in [Setty19], computes entropy over a cell’s fate probabilities.
Cell ids or a mask marking early cells. If None, use all cells. Only used when
method='kl_divergence'
. If adict
, the key species a cluster key inanndata.AnnData.obs
and the values specify cluster labels containing early cells. Returns
 Return type
The priming degree.

compute_lineage_drivers
(lineages=None, method='fischer', cluster_key=None, clusters=None, layer='X', use_raw=False, confidence_level=0.95, n_perms=1000, seed=None, return_drivers=True, **kwargs)[source]¶ Compute driver genes per lineage.
Correlates gene expression with lineage probabilities, for a given lineage and set of clusters. Often, it makes sense to restrict this to a set of clusters which are relevant for the specified lineages.
 Parameters
lineages¶ (
Union
[str
,Sequence
,None
]) – Either a set of lineage names fromabsorption_probabilities
.names or None, in which case all lineages are considered.Mode to use when calculating pvalues and confidence intervals. Can be one of:
’fischer’  use Fischer transformation [Fischer21].
’perm_test’  use permutation test.
cluster_key¶ (
Optional
[str
]) – Key fromadata
.obs
to obtain cluster annotations. These are considered forclusters
.clusters¶ (
Union
[str
,Sequence
,None
]) – Restrict the correlations to these clusters.use_raw¶ (
bool
) – Whether or not to useadata
.raw
to correlate gene expression. If using a layer other than.X
, this must be set to False.confidence_level¶ (
float
) – Confidence level for the confidence interval calculation. Must be in [0, 1].n_perms¶ (
int
) – Number of permutations to use whenmethod='perm_test'
.seed¶ (
Optional
[int
]) – Random seed whenmethod='perm_test'
.return_drivers¶ (
bool
) – Whether to return the drivers. This also contains the lower and upperconfidence_level
confidence interval bounds.show_progress_bar¶ – Whether to show a progress bar. Disabling it may slightly improve performance.
n_jobs¶ – Number of parallel jobs. If 1, use all available cores. If None or 1, the execution is sequential.
backend¶ – Which backend to use for parallelization. See
joblib.Parallel
for valid options.
 Return type
Optional
[DataFrame
] Returns

Dataframe of shape
(n_genes, n_lineages * 5)
containing the following columns, 1 for each lineage:{lineage} corr
 correlation between the gene expression and absorption probabilities.{lineage} pval
 calculated pvalues for doublesided test.{lineage} qval
 corrected pvalues using BenjaminiHochberg method at level 0.05.{lineage} ci low
 lower bound of theconfidence_level
correlation confidence interval.{lineage} ci high
 upper bound of theconfidence_level
correlation confidence interval.
Only if
return_drivers=True
. None –
Updates
adata
.var
oradata
.raw.var
, dependinguse_raw
with:'{direction} {lineage} corr'
 the potential lineage drivers.'{direction} {lineage} qval'
 the corrected pvalues.
Updates the following fields:
lineage_drivers
 same as the returned values.

References
 Fischer21
Fisher, R. A. (1921), On the “probable error” of a coefficient of correlation deduced from a small sample., Metron 1 3–32.

plot_lineage_drivers
(lineage, n_genes=8, ncols=None, use_raw=False, title_fmt='{gene} qval={qval:.4e}', figsize=None, dpi=None, save=None, **kwargs)[source]¶ Plot lineage drivers discovered by
compute_lineage_drivers()
. Parameters
lineage¶ (
str
) – Lineage for which to plot the driver genes.use_raw¶ (
bool
) – Whether to look inadata
.raw.var
oradata
.var
.title_fmt¶ (
str
) – Title format. Possible keywords include {gene}, {qval}, {corr} for gene name, qvalue and correlation, respectively.figsize¶ (
Optional
[Tuple
[float
,float
]]) – Size of the figure.save¶ (
Union
[str
,Path
,None
]) – Filename where to save the plot.kwargs¶ – Keyword arguments for
scvelo.pl.scatter()
.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

fit
(keys=None, compute_absorption_probabilities=True, **kwargs)[source]¶ Run the pipeline.
 Parameters
 Returns
Nothing, just makes available the following fields:
terminal_states_probabilities
terminal_states
absorption_probabilities
priming_degree
 Return type
Kernel¶

class
cellrank.tl.kernels.
Kernel
(adata, backward=False, compute_cond_num=False, check_connectivity=False, **kwargs)[source]¶ A base class from which all kernels are derived.
These kernels read from a given AnnData object, usually the KNN graph and additional variables, to compute a weighted, directed graph. Every kernel object has a direction. The kernels defined in the derived classes are not strictly kernels in the mathematical sense because they often only take one input argument  however, they build on other functions which have computed a similarity based on two input arguments. The role of the kernels defined here is to add directionality to these symmetric similarity relations or to transform them.
 Parameters
adata¶ (
anndata.AnnData
) – Annotated data object.compute_cond_num¶ (
bool
) – Whether to compute the condition number of the transition matrix. For large matrices, this can be very slow.check_connectivity¶ (
bool
) – Check whether the underlying KNN graph is connected.kwargs¶ – Keyword arguments which can specify key to be read from
adata
object.

property
adata
¶ Annotated data object.
 Returns
Annotated data object.
 Return type

compute_projection
(basis='umap', key_added=None, copy=False)¶ Compute a projection of the transition matrix in the embedding.
The projected matrix can be then visualized as:
scvelo.pl.velocity_embedding(adata, vkey='T_fwd', basis='umap')
 Parameters
basis¶ (
str
) – Basis inanndata.AnnData.obsm
for which to compute the projection.key_added¶ (
Optional
[str
]) – If not None andcopy=False
, save the result toadata
.obsm['{key_added}']
. Otherwise, save the result to ‘T_fwd_{basis}’ or T_bwd_{basis}, depending on the direction.copy¶ (
bool
) – Whether to return the projection or modifyadata
inplace.
 Return type
 Returns
If
copy=True
, the projection array of shape (n_cells, n_components).Otherwise, it modifies
anndata.AnnData.obsm
with a key based onkey_added
.

abstract
compute_transition_matrix
(*args, **kwargs)¶ Compute a transition matrix.

property
condition_number
¶ Condition number of the transition matrix.

abstract
copy
()¶ Return a copy of itself. Note that the underlying
adata
object is not copied. Return type
KernelExpression

property
kernels
¶ Get the kernels of the kernel expression, except for constants.
 Return type
List
[Kernel
]

property
params
¶ Parameters which are used to compute the transition matrix.

plot_random_walks
(n_sims, max_iter=0.25, seed=None, successive_hits=0, start_ixs=None, stop_ixs=None, basis='umap', cmap='gnuplot', linewidth=1.0, linealpha=0.3, ixs_legend_loc=None, n_jobs=None, backend='loky', show_progress_bar=True, figsize=None, dpi=None, save=None, **kwargs)¶ Plot random walks in an embedding.
This method simulates random walks on the Markov chain defined though the corresponding transition matrix. The method is intended to give qualitative rather than quantitative insights into the transition matrix. Random walks are simulated by iteratively choosing the next cell based on the current cell’s transition probabilities.
 Parameters
max_iter¶ (
Union
[int
,float
]) – Maximum number of steps of a random walk. If afloat
, it can be specified as a fraction of the number of cells.successive_hits¶ (
int
) – Number of successive hits in thestop_ixs
required to stop prematurely.start_ixs¶ (
Union
[Sequence
[str
],Dict
[str
,Union
[str
,Sequence
[str
]]],None
]) – Cells from which to sample the starting points. If None, use all cells. Can be specified as either adict
with a key corresponding to cluster key inanndata.AnnData.obs
and values to clusters or just a sequence of cell ids inanndata.AnnData.obs_names
. For example{'clusters': ['Ngn3 low EP', 'Ngn3 high EP']}
means that starting points for random walks will be samples uniformly from the these clusters.stop_ixs¶ (
Union
[Sequence
[str
],Dict
[str
,Union
[str
,Sequence
[str
]]],None
]) – Cells which when hit, the random walk is terminated. If None, terminate aftermax_iters
. Can be specified as either adict
with a key corresponding to cluster key inanndata.AnnData.obs
and values to clusters or just a sequence of cell ids inanndata.AnnData.obs_names
. For example{'clusters': ['Alpha', 'Beta']}
andsuccesive_hits=3
means that the random walk will stop prematurely after cells in the above specified clusters have been visited successively 3 times in a row.basis¶ (
str
) – Basis inanndata.AnnData.obsm
to use as an embedding.cmap¶ (
Union
[str
,LinearSegmentedColormap
]) – Colormap for the random walk lines.ixs_legend_loc¶ (
Optional
[str
]) – Legend location for the start/top indices.show_progress_bar¶ (
bool
) – Whether to show a progress bar. Disabling it may slightly improve performance.n_jobs¶ (
Optional
[int
]) – Number of parallel jobs. If 1, use all available cores. If None or 1, the execution is sequential.backend¶ (
str
) – Which backend to use for parallelization. Seejoblib.Parallel
for valid options.figsize¶ (
Optional
[Tuple
[float
,float
]]) – Size of the figure.save¶ (
Union
[str
,Path
,None
]) – Filename where to save the plot.kwargs¶ (
Any
) – Keyword arguments forscvelo.pl.scatter()
.
 Return type
 Returns
None – Nothing, just plots the figure. Optionally saves it based on
save
.For each random walk, the first (last) cell is marked though a black (yellow) dot.

static
read
(fname)¶ Deserialize self from a file.

property
transition_matrix
¶ Return rownormalized transition matrix.
If not present, it is computed, if all the underlying kernels have been initialized.

write
(fname, ext='pickle')¶ Serialize self to a file.

write_to_adata
(key=None)¶ Write the transition matrix and parameters used for computation to the underlying
adata
object. Parameters
key¶ (
Optional
[str
]) – Key used when writing transition matrix toadata
. If None, thekey
is set to ‘T_bwd’ ifbackward
is True, else ‘T_fwd’. Returns
Updates the
adata
with the following fields:.obsp['{key}']
 the transition matrix..uns['{key}_params']
 parameters used for calculation.
 Return type
Similarity scheme¶

class
cellrank.tl.kernels.
SimilaritySchemeABC
[source]¶ Base class for all similarity schemes.

abstract
__call__
(v, D, softmax_scale=1.0)[source]¶ Compute transition probability of a cell to its nearest neighbors using RNA velocity.
 Parameters
v¶ (
ndarray
) – Array of shape(n_genes,)
or(n_neighbors, n_genes)
containing the velocity vector(s). The second case is used for the backward process.D¶ (
ndarray
) – Array of shape(n_neighbors, n_genes)
corresponding to the transcriptomic displacement of the current cell with respect to ist nearest neighbors.softmax_scale¶ (
float
) – Scaling factor for the softmax function.
 Returns
The probability and logits arrays of shape
(n_neighbors,)
. Return type

abstract
Threshold scheme¶

class
cellrank.tl.kernels.
ThresholdSchemeABC
[source]¶ Base class for all connectivity biasing schemes.

abstract
__call__
(cell_pseudotime, neigh_pseudotime, neigh_conn, **kwargs)[source]¶ Calculate biased connections for a given cell.
 Parameters
 Returns
 Return type
Array of shape
(n_neighbors,)
containing the biased connectivities.

abstract
BaseModel¶

class
cellrank.ul.models.
BaseModel
(adata, model)[source]¶ Base class for all model classes.
 Parameters
adata¶ (
anndata.AnnData
) – Annotated data object.model¶ (
Any
) – The underlying model that is used for fitting and prediction.

property
prepared
¶ Whether the model is prepared for fitting.

property
adata
¶ Annotated data object.
 Returns
adata – Annotated data object.
 Return type

property
x
¶ Filtered independent variables of shape (n_filtered_cells, 1) used for fitting.
 Return type

property
y
¶ Filtered dependent variables of shape (n_filtered_cells, 1) used for fitting.
 Return type

property
x_test
¶ Independent variables of shape (n_samples, 1) used for prediction.
 Return type

property
x_hat
¶ Filtered independent variables used when calculating default confidence interval, usually same as
x
. Return type

property
y_hat
¶ Filtered dependent variables used when calculating default confidence interval, usually same as
y
. Return type

property
conf_int
¶ Array of shape (n_samples, 2) containing the lower and upper bounds of the confidence interval.
 Return type

prepare
(gene, lineage, backward=False, time_range=None, data_key='X', time_key='latent_time', use_raw=False, threshold=None, weight_threshold=(0.01, 0.01), filter_cells=None, n_test_points=200)[source]¶ Prepare the model to be ready for fitting.
 Parameters
gene¶ (
str
) – Gene inadata
.var_names
or inadata
.raw.var_names
.lineage¶ (
Optional
[str
]) – Name of a lineage inadata
.obsm[lineage_key]
. If None, all weights will be set to 1.time_range¶ (
Union
[float
,Tuple
[float
,float
],None
]) –Specify start and end times:
data_key¶ (
str
) – Key inadata
.layers
or ‘X’ foradata
.X
. Ifuse_raw=True
, it’s always set to ‘X’.time_key¶ (
str
) – Key inadata
.obs
where the pseudotime is stored.threshold¶ (
Optional
[float
]) – Consider only cells with weights >threshold
when estimating the test endpoint. If None, use the median of the weights.weight_threshold¶ (
Union
[float
,Tuple
[float
,float
]]) – Set all weights belowweight_threshold
toweight_threshold
if afloat
, or to the second value, if atuple
.filter_cells¶ (
Optional
[float
]) – Filter out all cells with expression values lower than this threshold.n_test_points¶ (
int
) – Number of test points. If None, use the original points based onthreshold
.
 Returns
Nothing, but updates the following fields:
x
 Filtered independent variables of shape (n_filtered_cells, 1) used for fitting.y
 Filtered dependent variables of shape (n_filtered_cells, 1) used for fitting.w
 Filtered weights of shape (n_filtered_cells,) used for fitting.x_all
 Unfiltered independent variables of shape (n_cells, 1).y_all
 Unfiltered dependent variables of shape (n_cells, 1).w_all
 Unfiltered weights of shape (n_cells,).x_test
 Independent variables of shape (n_samples, 1) used for prediction.prepared
 Whether the model is prepared for fitting.
 Return type

abstract
fit
(x=None, y=None, w=None, **kwargs)[source]¶ Fit the model.
 Parameters
x¶ (
Optional
[ndarray
]) – Independent variables, array of shape (n_samples, 1). If None, usex
.y¶ (
Optional
[ndarray
]) – Dependent variables, array of shape (n_samples, 1). If None, usey
.w¶ (
Optional
[ndarray
]) – Optional weights ofx
, array of shape (n_samples,). If None, usew
.kwargs¶ – Keyword arguments for underlying
model
’s fitting function.
 Returns
Fits the model and returns self.
 Return type

abstract
predict
(x_test=None, key_added='_x_test', **kwargs)[source]¶ Run the prediction.
 Parameters
 Returns
Updates and returns the following:
y_test
 Prediction values of shape (n_samples,) forx_test
.
 Return type

abstract
confidence_interval
(x_test=None, **kwargs)[source]¶ Calculate the confidence interval.
Use
default_confidence_interval()
function if underlyingmodel
has not method for confidence interval calculation. Parameters
x_test¶ (
Optional
[ndarray
]) – Array of shape (n_samples,) used for confidence interval calculation. If None, usex_test
.kwargs¶ – Keyword arguments for underlying
model
’s confidence method or fordefault_confidence_interval()
.
 Returns
Updates the following fields:
conf_int
 Array of shape (n_samples, 2) containing the lower and upper bounds of the confidence interval.
 Return type

default_confidence_interval
(x_test=None, **kwargs)[source]¶ Calculate the confidence interval, if the underlying
model
has no method for it.This formula is taken from [DeSalvo70], eq. 5.
 Parameters
x_test¶ (
Optional
[ndarray
]) – Array of shape (n_samples,) used for confidence interval calculation. If None, usex_test
.kwargs¶ – Keyword arguments for underlying
model
’s confidence method or fordefault_confidence_interval()
.
 Returns
Updates the following fields:
conf_int
 Array of shape (n_samples, 2) containing the lower and upper bounds of the confidence interval.x_hat
 Filtered independent variables used when calculating default confidence interval, usually same asx
.y_hat
 Filtered dependent variables used when calculating default confidence interval, usually same asy
.
 Return type
References
 DeSalvo70
DeSalvo, J. S. (1970), Standard Error of Forecast in Multiple Regression: Proof of a Useful Result., RAND Corporation.

plot
(figsize=(8, 5), same_plot=False, hide_cells=False, perc=None, abs_prob_cmap=<matplotlib.colors.ListedColormap object>, cell_color=None, lineage_color='black', alpha=0.8, lineage_alpha=0.2, title=None, size=15, lw=2, cbar=True, margins=0.015, xlabel='pseudotime', ylabel='expression', conf_int=True, lineage_probability=False, lineage_probability_conf_int=False, lineage_probability_color=None, obs_legend_loc='best', dpi=None, fig=None, ax=None, return_fig=False, save=None, **kwargs)[source]¶ Plot the smoothed gene expression.
 Parameters
same_plot¶ (
bool
) – Whether to plot all trends in the same plot.perc¶ (
Optional
[Tuple
[float
,float
]]) – Percentile by which to clip the absorption probabilities.abs_prob_cmap¶ (
ListedColormap
) – Colormap to use when coloring in the absorption probabilities.cell_color¶ (
Optional
[str
]) – Key inanndata.AnnData.obs
oranndata.AnnData.var_names
used for coloring the cells.lineage_alpha¶ (
float
) – Alpha channel for lineage confidence intervals.lineage_probability¶ (
bool
) – Whether to show smoothed lineage probability as a dashed line. Note that this will require 1 additional model fit.lineage_probability_conf_int¶ (
Union
[bool
,float
]) – Whether to compute and show smoothed lineage probability confidence interval. Ifself
iscellrank.ul.models.GAMR
, it can also specify the confidence level, the default is 0.95. Only used whenshow_lineage_probability=True
.lineage_probability_color¶ (
Optional
[str
]) – Color to use when plotting the smoothedlineage_probability
. If None, it’s the same aslineage_color
. Only used whenshow_lineage_probability=True
.obs_legend_loc¶ (
Optional
[str
]) – Location of the legend whencell_color
corresponds to a categorical variable.fig¶ (
Optional
[Figure
]) – Figure to use, if None, create a new one.ax¶ (
matplotlib.axes.Axes
) – Ax to use, if None, create a new one.save¶ (
Optional
[str
]) – Filename where to save the plot. If None, just shows the plots.kwargs¶ – Keyword arguments for
matplotlib.axes.Axes.legend()
, e.g. to disable the legend, specifyloc=None
. Only available whenshow_lineage_probability=True
.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type
Lineage¶

class
cellrank.tl.
Lineage
(input_array: numpy.ndarray, *, names: Iterable[str], colors: Optional[Iterable[ColorLike]] = None)[source]¶ Lightweight
numpy.ndarray
wrapper that adds names and colors. Parameters

property
T
¶ Transpose of self.

priming_degree
(method='kl_divergence', early_cells=None)[source]¶ Compute the degree of lineage priming.
This method computes how naive vs. committed each individual cell is. It returns a score where 0 stands for naive and 1 stands for committed.
 Parameters
method¶ (
Literal
[‘kl_divergence’, ‘entropy’]) –The method used to compute the degree of lineage priming. Valid options are:
’kl_divergence’: as in [Velten17], computes KLdivergence between the fate probabilities of a cell and the average fate probabilities. Computation of average fate probabilities can be restricted to a set of userdefined
early_cells
.’entropy’: as in [Setty19], computes entropy over a cell’s fate probabilities.
early_cells¶ (
Optional
[ndarray
]) – Cell ids or a mask marking early cells. If None, use all cells. Only used whenmethod='kl_divergence'
.
 Returns
 Return type
The priming degree.

plot_pie
(reduction, title=None, legend_loc='on data', legend_kwargs=mappingproxy({}), figsize=None, dpi=None, save=None, **kwargs)[source]¶ Plot a pie chart visualizing aggregated lineage probabilities.
 Parameters
reduction¶ (
Callable
) – Function that will be applied lineagewise.legend_loc¶ (
Optional
[str
]) – Location of the legend. If None, it is not shown.legend_kwargs¶ (
Mapping
) – Keyword arguments formatplotlib.axes.Axes.legend()
.figsize¶ (
Optional
[Tuple
[float
,float
]]) – Size of the figure.save¶ (
Union
[str
,Path
,None
]) – Filename where to save the plot.
 Returns
Nothing, just plots the figure. Optionally saves it based on
save
. Return type

reduce
(*keys, mode='dist', dist_measure='mutual_info', normalize_weights='softmax', softmax_scale=1, return_weights=False)[source]¶ Subset states and normalize them so that they again sum to 1.
 Parameters
keys¶ (
str
) – List of keys that define the states, to which this object will be reduced by projecting the values of the other states.mode¶ (
str
) – Whether to use a distance measure to compute weights  ‘dist’, or just rescale  ‘scale’.dist_measure¶ (
Literal
[‘cosine_sim’, ‘wasserstein_dist’, ‘kl_div’, ‘js_div’, ‘mutual_inf’, ‘equal’]) –Used to quantify similarity between query and reference states. Valid options are:
’cosine_sim’  cosine similarity.
’wasserstein_dist’  Wasserstein distance.
’kl_div’  Kullback–Leibler divergence.
’js_div’  Jensen–Shannon divergence.
’mutual_inf’  mutual information.
’equal’  equally redistribute the mass among the rest.
normalize_weights¶ (
Literal
[‘scale’, ‘softmax’]) –How to rownormalize the weights. Valid options are:
’scale’  divide by the sum.
’softmax’ use a softmax.
softmax_scale