def velocity(data, vkey='<STR_LIT>', mode=None, fit_offset=False, fit_offset2=False, filter_genes=False,<EOL>groups=None, groupby=None, groups_for_fit=None, use_raw=False, perc=[<NUM_LIT:5>, <NUM_LIT>], copy=False):
adata = data.copy() if copy else data<EOL>if not use_raw and '<STR_LIT>' not in adata.layers.keys(): moments(adata)<EOL>logg.info('<STR_LIT>', r=True)<EOL>strings_to_categoricals(adata)<EOL>categories = adata.obs[groupby].cat.categoriesif groupby is not None and groups is None and groups_for_fit is None else [None]<EOL>for cat in categories:<EOL><INDENT>groups = cat if cat is not None else groups<EOL>cell_subset = groups_to_bool(adata, groups, groupby)<EOL>_adata = adata if groups is None else adata[cell_subset]<EOL>velo = Velocity(_adata, groups_for_fit=groups_for_fit, groupby=groupby, use_raw=use_raw)<EOL>velo.compute_deterministic(fit_offset, perc=perc)<EOL>if any([mode is not None and mode in item for item in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']]):<EOL><INDENT>if filter_genes and len(set(velo._velocity_genes)) > <NUM_LIT:1>:<EOL><INDENT>adata._inplace_subset_var(velo._velocity_genes)<EOL>residual = velo._residual[:, velo._velocity_genes]<EOL>_adata = adata if groups is None else adata[cell_subset]<EOL>velo = Velocity(_adata, residual=residual, groups_for_fit=groups_for_fit, groupby=groupby)<EOL><DEDENT>velo.compute_stochastic(fit_offset, fit_offset2, mode, perc=perc)<EOL><DEDENT>write_residuals(adata, vkey, velo._residual, cell_subset)<EOL>write_residuals(adata, '<STR_LIT>' + vkey, velo._residual2, cell_subset)<EOL>write_pars(adata, vkey, velo.get_pars(), velo.get_pars_names(), add_key=cat)<EOL>if filter_genes and len(set(velo._velocity_genes)) > <NUM_LIT:1>:<EOL><INDENT>adata._inplace_subset_var(velo._velocity_genes)<EOL><DEDENT><DEDENT>logg.info('<STR_LIT>', time=True, end='<STR_LIT:U+0020>' if settings.verbosity > <NUM_LIT:2> else '<STR_LIT:\n>')<EOL>logg.hint('<STR_LIT>' <EOL>'<STR_LIT>' + vkey + '<STR_LIT>')<EOL>return adata if copy else None<EOL>
Estimates velocities in a gene-specific manner

    Arguments
    ---------
    data: :class:`~anndata.AnnData`
        Annotated data matrix.
    vkey: `str` (default: `'velocity'`)
        Name under which to refer to the computed velocities for `velocity_graph` and `velocity_embedding`.
    mode: `'deterministic'`, `'stochastic'` or `'bayes'` (default: `'stochastic'`)
        Whether to run the estimation using the deterministic or stochastic model of transcriptional dynamics.
        `'bayes'` solves the stochastic model and accounts for heteroscedasticity, but is slower than `'stochastic'`.
    fit_offset: `bool` (default: `False`)
        Whether to fit with offset for first order moment dynamics.
    fit_offset2: `bool`, (default: `False`)
        Whether to fit with offset for second order moment dynamics.
    filter_genes: `bool` (default: `True`)
        Whether to remove genes that are not used for further velocity analysis.
    groups: `str`, `list` (default: `None`)
        Subset of groups, e.g. [‘g1’, ‘g2’, ‘g3’], to which velocity analysis shall be restricted.
    groupby: `str`, `list` or `np.ndarray` (default: `None`)
        Key of observations grouping to consider.
    groups_for_fit: `str`, `list` or `np.ndarray` (default: `None`)
        Subset of groups, e.g. [‘g1’, ‘g2’, ‘g3’], to which steady-state fitting shall be restricted.
    use_raw: `bool` (default: `False`)
        Whether to use raw data for estimation.
    perc: `float` (default: `None`)
        Percentile, e.g. 98, upon for extreme quantile fit (to better capture steady states for velocity estimation).
    copy: `bool` (default: `False`)
        Return a copy instead of writing to `adata`.

    Returns
    -------
    Returns or updates `adata` with the attributes
    velocity: `.layers`
        velocity vectors for each individual cell
    variance_velocity: `.layers`
        velocity vectors for the cell variances
    velocity_offset, velocity_beta, velocity_gamma, velocity_r2: `.var`
        parameters