Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
**kwargs,
**paga_kwargs,
)
)
savefig_or_show(dpi=dpi, save=save, show=show)
if show is False:
return ax
else:
color, layer, vkey, basis = colors[0], layers[0], vkeys[0], basis
color = default_color(adata) if color is None else color
size = default_size(adata) / 2 if size is None else size
paga_groups = adata.uns["paga"]["groups"]
_adata = (
adata[groups_to_bool(adata, groups, groupby=paga_groups)]
if groups is not None and paga_groups in adata.obs.keys()
else adata
)
if isinstance(node_colors, dict):
paga_kwargs["colorbar"] = False
if isinstance(node_colors, str) and node_colors in adata.obsm.keys():
props = dict()
for name in adata.obs[paga_groups].cat.categories:
mask = (adata.obs[paga_groups] == name).values
props[name] = np.nanmean(adata.obsm[node_colors][mask], axis=0)
node_colors = adata.obsm[node_colors].colors
paga_kwargs["colors"] = {
i: dict(zip(node_colors, prop)) for i, prop in enumerate(props.values())
}
vkey = [key for key in lkeys if "velocity" in key and "_u" not in key]
color, color_map = kwargs.pop("c", color), kwargs.pop("cmap", color_map)
colors = make_unique_list(color, allow_array=True)
layers, vkeys = make_unique_list(layer), make_unique_list(vkey)
if V is None:
for key in vkeys:
if recompute or velocity_embedding_changed(adata, basis=basis, vkey=key):
velocity_embedding(adata, basis=basis, vkey=key)
color, layer, vkey = colors[0], layers[0], vkeys[0]
color = default_color(adata) if color is None else color
if X_grid is None or V_grid is None:
_adata = (
adata[groups_to_bool(adata, groups, groupby=color)]
if groups is not None and color in adata.obs.keys()
else adata
)
comps, obsm = get_components(components, basis), _adata.obsm
X_emb = np.array(obsm[f"X_{basis}"][:, comps]) if X is None else X[:, :2]
V_emb = np.array(obsm[f"{vkey}_{basis}"][:, comps]) if V is None else V[:, :2]
X_grid, V_grid = compute_velocity_on_grid(
X_emb=X_emb,
V_emb=V_emb,
density=1,
smooth=smooth,
min_mass=min_mass,
n_neighbors=n_neighbors,
autoscale=False,
adjust_for_stream=True,
cutoff_perc=cutoff_perc,
logg.info("computing terminal states", r=True)
strings_to_categoricals(adata)
if groupby is not None:
logg.warn(
"Only set groupby, when you have evident distinct clusters/lineages,"
" each with an own root and end point."
)
kwargs.update({"self_transitions": self_transitions})
categories = [None]
if groupby is not None and groups is None:
categories = adata.obs[groupby].cat.categories
for cat in categories:
groups = cat if cat is not None else groups
cell_subset = groups_to_bool(adata, groups=groups, groupby=groupby)
_adata = adata if groups is None else adata[cell_subset]
connectivities = get_connectivities(_adata, "distances")
T = transition_matrix(_adata, vkey=vkey, backward=True, **kwargs)
eigvecs_roots = eigs(T, eps=eps, perc=[2, 98], random_state=random_state)[1]
roots = csr_matrix.dot(connectivities, eigvecs_roots).sum(1)
roots = scale(np.clip(roots, 0, np.percentile(roots, 98)))
roots = verify_roots(_adata, roots)
write_to_obs(adata, "root_cells", roots, cell_subset)
T = transition_matrix(_adata, vkey=vkey, backward=False, **kwargs)
eigvecs_ends = eigs(T, eps=eps, perc=[2, 98], random_state=random_state)[1]
ends = csr_matrix.dot(connectivities, eigvecs_ends).sum(1)
ends = scale(np.clip(ends, 0, np.percentile(ends, 98)))
write_to_obs(adata, "end_points", ends, cell_subset)
if show is False:
return ax
else:
if projection == "3d":
from mpl_toolkits.mplot3d import Axes3D
ax, show = get_ax(ax, show, figsize, dpi, projection)
color, layer, vkey, basis = colors[0], layers[0], vkeys[0], bases[0]
color = default_color(adata) if color is None else color
color_map = default_color_map(adata, color) if color_map is None else color_map
size = default_size(adata) / 2 if size is None else size
if use_raw is None and "Ms" not in adata.layers.keys():
use_raw = True
_adata = (
adata[groups_to_bool(adata, groups, groupby=color)]
if groups is not None and color in adata.obs.keys()
else adata
)
quiver_kwargs = {
"scale": scale,
"cmap": color_map,
"angles": "xy",
"scale_units": "xy",
"edgecolors": "k",
"linewidth": 0.1,
"width": None,
}
if basis in adata.var_names:
if use_raw:
x = adata[:, basis].layers["spliced"]
adata.layers[f'{vkey}_u'] = np.ones(adata.shape) * np.nan
adata.layers[f'{vkey}_u'][:, gene_subset] = wt
if filter_genes and len(set(vgenes)) > 1:
adata._inplace_subset_var(vgenes)
elif mode in {'steady_state', 'deterministic', 'stochastic'}:
categories = adata.obs[groupby].cat.categories \
if groupby is not None and groups is None and groups_for_fit is None else [None]
for cat in categories:
groups = cat if cat is not None else groups
cell_subset = groups_to_bool(adata, groups, groupby)
_adata = adata if groups is None else adata[cell_subset]
velo = Velocity(_adata, groups_for_fit=groups_for_fit, groupby=groupby, constrain_ratio=constrain_ratio,
min_r2=min_r2, r2_adjusted=r2_adjusted, use_raw=use_raw)
velo.compute_deterministic(fit_offset=fit_offset, perc=perc)
if mode == 'stochastic':
if filter_genes and len(set(velo._velocity_genes)) > 1:
adata._inplace_subset_var(velo._velocity_genes)
residual = velo._residual[:, velo._velocity_genes]
_adata = adata if groups is None else adata[cell_subset]
velo = Velocity(_adata, residual=residual, groups_for_fit=groups_for_fit, groupby=groupby, constrain_ratio=constrain_ratio)
velo.compute_stochastic(fit_offset, fit_offset2, mode, perc=perc)
write_residuals(adata, vkey, velo._residual, cell_subset)
write_residuals(adata, f'variance_{vkey}', velo._residual2, cell_subset)
write_pars(adata, vkey, velo.get_pars(), velo.get_pars_names(), add_key=cat)
if groupby is not None:
logg.warn(
"Only set groupby, when you have evident distinct clusters/lineages,"
" each with an own root and end point."
)
categories = (
adata.obs[groupby].cat.categories
if groupby is not None and groups is None
else [None]
)
for cat in categories:
groups = cat if cat is not None else groups
if root_key is None or end_key is None:
terminal_states(adata, vkey, groupby, groups)
root_key, end_key = "root_cells", "end_points"
cell_subset = groups_to_bool(adata, groups=groups, groupby=groupby)
data = adata.copy() if cell_subset is None else adata[cell_subset].copy()
if "allow_kendall_tau_shift" not in kwargs:
kwargs["allow_kendall_tau_shift"] = True
vpt = VPT(data, n_dcs=n_dcs, **kwargs)
if use_velocity_graph:
T = data.uns[f"{vkey}_graph"] - data.uns[f"{vkey}_graph_neg"]
vpt._connectivities = T + T.T
vpt.compute_transitions()
vpt.compute_eigen(n_comps=n_dcs)
vpt.set_iroot(root_key)
vpt.compute_pseudotime()
dpt_root = vpt.pseudotime
vkey = [key for key in lkeys if "velocity" in key and "_u" not in key]
color, color_map = kwargs.pop("c", color), kwargs.pop("cmap", color_map)
colors = make_unique_list(color, allow_array=True)
layers, vkeys = make_unique_list(layer), make_unique_list(vkey)
if V is None:
for key in vkeys:
if recompute or velocity_embedding_changed(adata, basis=basis, vkey=key):
velocity_embedding(adata, basis=basis, vkey=key)
color, layer, vkey = colors[0], layers[0], vkeys[0]
color = default_color(adata) if color is None else color
if X_grid is None or V_grid is None:
_adata = (
adata[groups_to_bool(adata, groups, groupby=color)]
if groups is not None and color in adata.obs.keys()
else adata
)
comps, obsm = get_components(components, basis), _adata.obsm
X_emb = np.array(obsm[f"X_{basis}"][:, comps]) if X is None else X[:, :2]
V_emb = np.array(obsm[f"{vkey}_{basis}"][:, comps]) if V is None else V[:, :2]
X_grid, V_grid = compute_velocity_on_grid(
X_emb=X_emb,
V_emb=V_emb,
density=density,
autoscale=autoscale,
smooth=smooth,
n_neighbors=n_neighbors,
min_mass=min_mass,
)