def velocity_embedding(data, basis=None, vkey='<STR_LIT>', scale=<NUM_LIT:10>, self_transitions=True, use_negative_cosines=True,<EOL>direct_projection=None, pca_transform=None, retain_scale=False, autoscale=True, all_comps=True,<EOL>T=None, copy=False):
adata = data.copy() if copy else data<EOL>if basis is None:<EOL><INDENT>keys = [key for key in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>'] if '<STR_LIT>' + key in adata.obsm.keys()]<EOL>if len(keys) > <NUM_LIT:0>: basis = keys[-<NUM_LIT:1>]<EOL>else: raise ValueError('<STR_LIT>')<EOL><DEDENT>if '<STR_LIT>' + basis not in adata.obsm_keys():<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>logg.info('<STR_LIT>', r=True)<EOL>if pca_transform is None and direct_projection is None:<EOL><INDENT>pca_transform = True if '<STR_LIT>' in basis else False<EOL><DEDENT>if '<STR_LIT>' in basis and (direct_projection or pca_transform):<EOL><INDENT>V = adata.layers[vkey]<EOL>PCs = adata.varm['<STR_LIT>'] if all_comps else adata.varm['<STR_LIT>'][:, :<NUM_LIT:2>]<EOL>if vkey + '<STR_LIT>' in adata.var.keys():<EOL><INDENT>V = V[:, adata.var[vkey + '<STR_LIT>']]<EOL>PCs = PCs[adata.var[vkey + '<STR_LIT>']]<EOL><DEDENT>nans = np.isnan(V.sum(<NUM_LIT:0>))<EOL>if np.any(nans):<EOL><INDENT>V = V[:, ~nans]<EOL>PCs = PCs[~nans]<EOL><DEDENT>X_emb = adata.obsm['<STR_LIT>' + basis]<EOL>V_emb = (V - V.mean(<NUM_LIT:0>)).dot(PCs)<EOL><DEDENT>else:<EOL><INDENT>X_emb = adata.obsm['<STR_LIT>' + basis] if all_comps else adata.obsm['<STR_LIT>' + basis][:, :<NUM_LIT:2>]<EOL>V_emb = np.zeros(X_emb.shape)<EOL>T = transition_matrix(adata, vkey=vkey, scale=scale, self_transitions=self_transitions,<EOL>use_negative_cosines=use_negative_cosines) if T is None else T<EOL>T.setdiag(<NUM_LIT:0>)<EOL>T.eliminate_zeros()<EOL>densify = adata.n_obs < <NUM_LIT><EOL>TA = T.A if densify else None<EOL>with warnings.catch_warnings():<EOL><INDENT>warnings.simplefilter("<STR_LIT:ignore>")<EOL>for i in range(adata.n_obs):<EOL><INDENT>indices = T[i].indices<EOL>dX = X_emb[indices] - X_emb[i, None]  <EOL>if not retain_scale: dX /= norm(dX)[:, None]<EOL>dX[np.isnan(dX)] = <NUM_LIT:0>  <EOL>probs = TA[i, indices] if densify else T[i].data<EOL>V_emb[i] = probs.dot(dX) - probs.mean() * dX.sum(<NUM_LIT:0>)  <EOL><DEDENT><DEDENT>if retain_scale:<EOL><INDENT>delta = T.dot(adata.X) - adata.X<EOL>if issparse(delta): delta = delta.A<EOL>cos_proj = (adata.layers[vkey] * delta).sum(<NUM_LIT:1>) / norm(delta)<EOL>V_emb *= np.clip(cos_proj[:, None] * <NUM_LIT:10>, <NUM_LIT:0>, <NUM_LIT:1>)<EOL><DEDENT><DEDENT>if autoscale: V_emb /= (<NUM_LIT:3> * quiver_autoscale(X_emb, V_emb))<EOL>vkey += '<STR_LIT:_>' + basis<EOL>adata.obsm[vkey] = V_emb<EOL>logg.info('<STR_LIT>', time=True, end='<STR_LIT:U+0020>' if settings.verbosity > <NUM_LIT:2> else '<STR_LIT:\n>')<EOL>logg.hint(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' + vkey + '<STR_LIT>')<EOL>return adata if copy else None<EOL>
Computes the single cell velocities in the embedding

    Arguments
    ---------
    data: :class:`~anndata.AnnData`
        Annotated data matrix.
    basis: `str` (default: `'tsne'`)
        Which embedding to use.
    vkey: `str` (default: `'velocity'`)
        Name of velocity estimates to be used.
    scale: `int` (default: 10)
        Scale parameter of gaussian kernel for transition matrix.
    self_transitions: `bool` (default: `True`)
        Whether to allow self transitions, based on the confidences of transitioning to neighboring cell.
    use_negative_cosines: `bool` (default: `True`)
        Whether to use not only positive, but also negative cosines and use those transitions to the opposite way.
    direct_projection: `bool` (default: `True`)
        Whether to directly project the velocities into PCA space, thus skipping velocity graph.
    pca_transform: `bool` (default: `None`)
        same as direct_projection (deprecated)
    retain_scale: `bool` (default: `False`)
        Whether to retain scale from high dimensional space in embedding.
    autoscale: `bool` (default: `True`)
        Whether to scale the embedded velocities by a scalar multiplier,
        which simply ensures that the arrows in the embedding are properly scaled.
    all_comps: `bool` (default: `True`)
        Whether to compute the velocities on all embedding components or just the first two.
    T: `csr_matrix` (default: `None`)
        Allows the user to directly pass a transition matrix.

    Returns
    -------
    Returns or updates `adata` with the attributes
    velocity_basis: `.obsm`
        coordinates of velocity projection on embedding