# cellrank.kernels.VelocityKernel#

class cellrank.kernels.VelocityKernel(adata, backward=False, xkey='Ms', vkey='velocity', **kwargs)[source]#

Kernel which computes a transition matrix based on RNA velocity.

This borrows ideas from both and . 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:

## Attributes table#

 adata Annotated data object. backward Direction of the process. connectivities Underlying connectivity matrix. kernels Underlying base kernels. logits Array of shape (n_cells, n_cells) containing not row-normalized transition matrix. params Parameters which are used to compute the transition matrix. shape (n_cells, n_cells). transition_matrix Row-normalized transition matrix.

## Methods table#

 compute_transition_matrix([model, ...]) Compute transition matrix based on velocity directions on the local manifold. copy(*[, deep]) Return a copy of self. from_adata(adata, key[, copy]) Read kernel object saved using write_to_adata(). plot_projection([basis, key_added, ...]) Plot transition_matrix as a stream or a grid plot. plot_random_walks([n_sims, max_iter, seed, ...]) Plot random walks in an embedding. plot_single_flow(cluster, cluster_key, time_key) Visualize outgoing flow from a cluster of cells . read(fname[, adata, copy]) De-serialize self from a file. write(fname[, write_adata, ext]) Serialize self to a file. write_to_adata([key, copy]) Write the transition matrix and parameters used for computation to the underlying adata object.

## Attributes#

Annotated data object.

Return type:

AnnData

### backward#

VelocityKernel.backward#

Direction of the process.

Return type:

bool

### connectivities#

VelocityKernel.connectivities#

Underlying connectivity matrix.

Return type:

csr_matrix

### kernels#

VelocityKernel.kernels#

Underlying base kernels.

Return type:

Tuple[KernelExpression, ...]

### logits#

VelocityKernel.logits#

Array of shape (n_cells, n_cells) containing not row-normalized transition matrix.

Return type:

### params#

VelocityKernel.params#

Parameters which are used to compute the transition matrix.

Return type:

### shape#

VelocityKernel.shape#

(n_cells, n_cells).

Return type:

### transition_matrix#

VelocityKernel.transition_matrix#

Row-normalized transition matrix.

Return type:

## Methods#

### compute_transition_matrix#

VelocityKernel.compute_transition_matrix(model=VelocityModel.DETERMINISTIC, backward_mode=BackwardMode.TRANSPOSE, similarity=Similarity.CORRELATION, softmax_scale=None, n_samples=1000, seed=None, **kwargs)[source]#

Compute transition matrix based on velocity directions on the local manifold.

For each cell, infer transition probabilities based on the cell’s velocity-extrapolated cell state and the cell states of its K nearest neighbors.

Parameters:
• mode

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.

• backward_mode (Literal[‘transpose’, ‘negate’]) –

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 using 1 / median(correlations). The idea behind this is to scale the softmax to counter everything tending to orthogonality in high dimensions.

• scheme

Similarity measure between cells as described in . Can be one of the following:

• ’correlation’ - cellrank.kernels.utils.Correlation.

• ’cosine’ - cellrank.kernels.utils.Cosine.

• ’dot_product’ - cellrank.kernels.utils.DotProduct.

Alternatively, any function can be passed as long as it follows the signature of cellrank.kernels.utils.SimilarityABC.__call__().

• n_samples (int) – Number of samples when mode = 'monte_carlo'.

• seed (Optional[int]) – Random seed when mode = 'monte_carlo'.

• 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.

• kwargs (Any) – Keyword arguments for the underlying model.

Return type:

VelocityKernel

Returns:

Self and updates transition_matrix, logits and params.

### copy#

VelocityKernel.copy(*, deep=False)#

Return a copy of self.

Return type:

Kernel

Read kernel object saved using write_to_adata().

Parameters:
Return type:

Kernel

Returns:

The kernel with explicitly initialized properties:

### plot_projection#

VelocityKernel.plot_projection(basis='umap', key_added=None, recompute=False, stream=True, connectivities=None, **kwargs)#

Plot transition_matrix as a stream or a grid plot.

Parameters:
Return type:

None

Returns:

Nothing, just plots and modifies anndata.AnnData.obsm with a key based on key_added.

### plot_random_walks#

VelocityKernel.plot_random_walks(n_sims=100, 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:
Return type:

None

Returns:

Nothing, just plots the figure. Optionally saves it based on save. For each random walk, the first/last cell is marked by the start/end colors of cmap.

### plot_single_flow#

VelocityKernel.plot_single_flow(cluster, cluster_key, time_key, clusters=None, time_points=None, min_flow=0, remove_empty_clusters=True, ascending=False, legend_loc='upper right out', alpha=0.8, xticks_step_size=1, figsize=None, dpi=None, save=None, show=True)#

Visualize outgoing flow from a cluster of cells .

Parameters:
Return type:
Returns:

The axes object, if show = False. Nothing, just plots the figure. Optionally saves it based on save.

Notes

This function is a Python re-implementation of the following original R function with some minor stylistic differences. This function will not recreate the results from , because there, the Metacell model was used to compute the flow, whereas here the transition matrix is used.

De-serialize self from a file.

Parameters:
Return type:

IOMixin

Returns:

The de-serialized object.

### write#

Serialize self to a file.

Parameters:
Return type:

None

Returns:

Nothing, just writes itself to a file using pickle.

Write the transition matrix and parameters used for computation to the underlying adata object.

Parameters:

key (Optional[str]) – Key used when writing transition matrix to adata. If None, the key will be determined automatically.

Return type:

None

Returns:

Updates the adata with the following fields:

• .obsp['{key}'] - the transition matrix.

• .uns['{key}_params'] - parameters used for the calculation.