def rank_velocity_genes(data, vkey='<STR_LIT>', n_genes=<NUM_LIT:10>, groupby=None, match_with=None, resolution=None,<EOL>min_counts=None, min_r2=None, min_dispersion=None, copy=False):
adata = data.copy() if copy else data<EOL>if groupby is None or groupby == '<STR_LIT>':<EOL><INDENT>velocity_clusters(adata, vkey=vkey, match_with=match_with, resolution=resolution)<EOL>groupby = '<STR_LIT>'<EOL><DEDENT>logg.info('<STR_LIT>', r=True)<EOL>tmp_filter = np.ones(adata.n_vars, dtype=bool)<EOL>if vkey + '<STR_LIT>' in adata.var.keys():<EOL><INDENT>tmp_filter &= adata.var[vkey + '<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in adata.layers.keys():<EOL><INDENT>n_counts = (adata.layers['<STR_LIT>'] > <NUM_LIT:0>).sum(<NUM_LIT:0>)<EOL>n_counts = n_counts.A1 if issparse(adata.layers['<STR_LIT>']) else n_counts<EOL>min_counts = min(<NUM_LIT:50>, np.percentile(n_counts, <NUM_LIT:50>)) if min_counts is None else min_counts<EOL>tmp_filter &= (n_counts > min_counts)<EOL><DEDENT>if '<STR_LIT>' in adata.var.keys():<EOL><INDENT>r2 = adata.var.velocity_r2<EOL>min_r2 = <NUM_LIT> if min_r2 is None else min_r2  <EOL>tmp_filter &= (r2 > min_r2)<EOL><DEDENT>if '<STR_LIT>' in adata.var.keys():<EOL><INDENT>dispersions = adata.var.dispersions_norm<EOL>min_dispersion = <NUM_LIT:0> if min_dispersion is None else min_dispersion  <EOL>tmp_filter &= (dispersions > min_dispersion)<EOL><DEDENT>X = adata[:, tmp_filter].layers[vkey]<EOL>groups, groups_masks = select_groups(adata[:, tmp_filter], key=groupby)<EOL>n_groups = groups_masks.shape[<NUM_LIT:0>]<EOL>sizes = groups_masks.sum(<NUM_LIT:1>)<EOL>mean, var = np.zeros((n_groups, X.shape[<NUM_LIT:1>])), np.zeros((n_groups, X.shape[<NUM_LIT:1>]))<EOL>for i, mask in enumerate(groups_masks): mean[i], var[i] = get_mean_var(X[mask])<EOL>rankings_gene_names, rankings_gene_scores, indices = [], [], []<EOL>for i in range(n_groups):<EOL><INDENT>mask_rest = ~groups_masks[i]<EOL>mean_rest, var_rest = get_mean_var(X[mask_rest])<EOL>size_rest = sizes[i]  <EOL>scores = (mean[i] - mean_rest) / np.sqrt(var[i] / sizes[i] + var_rest / size_rest)<EOL>scores = np.nan_to_num(scores)<EOL>if n_genes > X.shape[<NUM_LIT:1>]: n_genes = X.shape[<NUM_LIT:1>]<EOL>idx = np.argpartition(scores, -n_genes)[-n_genes:]<EOL>idx = idx[np.argsort(scores[idx])[::-<NUM_LIT:1>]]<EOL>rankings_gene_names.append(adata[:, tmp_filter].var_names[idx].values)<EOL>rankings_gene_scores.append(scores[idx])<EOL><DEDENT>rankings_gene_names = np.array([list(n) for n in rankings_gene_names])<EOL>rankings_gene_scores = np.array([list(n) for n in rankings_gene_scores])<EOL>all_names = rankings_gene_names.T.flatten()<EOL>all_scores = rankings_gene_scores.T.flatten()<EOL>vscore = np.zeros(adata.n_vars, dtype=np.int)<EOL>for i, name in enumerate(adata.var_names):<EOL><INDENT>if name in all_names: vscore[i] = all_scores[np.where(name == all_names)[<NUM_LIT:0>][<NUM_LIT:0>]]<EOL><DEDENT>adata.var['<STR_LIT>'] = vscore<EOL>key = '<STR_LIT>'<EOL>if key not in adata.uns.keys(): adata.uns[key] = {}<EOL>adata.uns[key] ={'<STR_LIT>': np.rec.fromarrays([n for n in rankings_gene_names], dtype=[(rn, '<STR_LIT>') for rn in groups]),<EOL>'<STR_LIT>': np.rec.fromarrays([n.round(<NUM_LIT:2>) for n in rankings_gene_scores], dtype=[(rn, '<STR_LIT>') for rn in groups]),<EOL>'<STR_LIT>': {'<STR_LIT>': groupby, '<STR_LIT>': '<STR_LIT>', '<STR_LIT>': '<STR_LIT>', '<STR_LIT>': True}}<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>' + key + '<STR_LIT>')<EOL>return adata if copy else None<EOL>
Rank genes for velocity characterizing groups.

    Arguments
    ----------
    data : :class:`~anndata.AnnData`
        Annotated data matrix.
    vkey: `str` (default: `'velocity'`)
        Key of velocities computed in `tl.velocity`
    n_genes : `int`, optional (default: 100)
        The number of genes that appear in the returned tables.
    groupby: `str`, `list` or `np.ndarray` (default: `None`)
        Key of observations grouping to consider.
    min_counts: `float` (default: None)
        Minimum count of genes for consideration.
    min_r2: `float` (default: None)
        Minimum r2 value of genes for consideration.
    min_dispersion: `float` (default: None)
        Minimum dispersion norm value of genes for consideration.
    copy: `bool` (default: `False`)
        Return a copy instead of writing to data.

    Returns
    -------
    Returns or updates `data` with the attributes
    rank_velocity_genes : `.uns`
        Structured array to be indexed by group id storing the gene
        names. Ordered according to scores.
    velocity_score : `.var`
        Storing the score for each gene for each group. Ordered according to scores.