Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
sc.pp.neighbors(latent_with_true_labels)
sc.tl.umap(latent_with_true_labels)
sc.pl.umap(latent_with_true_labels, color=color,
save=f"_{data_name}_{cell_type}_latent_with_true_labels",
show=False)
sc.pp.neighbors(latent_with_fake_labels)
sc.tl.umap(latent_with_fake_labels)
sc.pl.umap(latent_with_fake_labels, color=color,
save=f"_{data_name}_{cell_type}_latent_with_fake_labels",
show=False)
sc.pp.neighbors(mmd_latent_with_true_labels)
sc.tl.umap(mmd_latent_with_true_labels)
sc.pl.umap(mmd_latent_with_true_labels, color=color,
save=f"_{data_name}_{cell_type}_mmd_latent_with_true_labels",
show=False)
sc.pp.neighbors(mmd_latent_with_fake_labels)
sc.tl.umap(mmd_latent_with_fake_labels)
sc.pl.umap(mmd_latent_with_fake_labels, color=color,
save=f"_{data_name}_{cell_type}_mmd_latent_with_fake_labels",
show=False)
sc.pl.violin(cell_type_adata, keys=top_100_genes[0], groupby='condition',
save=f"_{data_name}_{cell_type}_{top_100_genes[0]}",
show=False)
plt.close("all")
_, rec, mmd = network.get_reconstruction_error(net_valid_adata, condition_key)
row = [alpha, eta, z_dim, mmd_dim, beta, asw, nmi, ari, ebm, rec, mmd]
with open(f"./{filename}.csv", 'a') as file:
writer = csv.writer(file)
writer.writerow(row)
file.close()
os.makedirs(f"./results/Monitor/{filename}/", exist_ok=True)
sc.settings.figdir = f"./results/Monitor/{filename}/"
sc.pp.neighbors(mmd_latent)
sc.tl.umap(mmd_latent)
sc.pl.umap(mmd_latent, color=condition_key, frameon=False, title="", save=f"_trVAE_MMD_condition_{beta}.pdf")
sc.pl.umap(mmd_latent, color=cell_type_key, frameon=False, title="", save=f"_trVAE_MMD_cell_type_{beta}.pdf")
K.clear_session()
mmd_latent_with_fake_labels = sc.AnnData(X=mmd_latent_with_fake_labels)
mmd_latent_with_fake_labels.obs['condition'] = data.obs['condition'].values
mmd_latent_with_fake_labels.obs[cell_type_key] = data.obs[cell_type_key].values
color = ['condition', cell_type_key]
sc.pp.neighbors(train_data)
sc.tl.umap(train_data)
sc.pl.umap(train_data, color=color,
save=f'_{data_name}_{cell_type}_train_data',
show=False)
sc.pp.neighbors(latent_with_true_labels)
sc.tl.umap(latent_with_true_labels)
sc.pl.umap(latent_with_true_labels, color=color,
save=f"_{data_name}_{cell_type}_latent_with_true_labels",
show=False)
sc.pp.neighbors(latent_with_fake_labels)
sc.tl.umap(latent_with_fake_labels)
sc.pl.umap(latent_with_fake_labels, color=color,
save=f"_{data_name}_{cell_type}_latent_with_fake_labels",
show=False)
sc.pp.neighbors(mmd_latent_with_true_labels)
sc.tl.umap(mmd_latent_with_true_labels)
sc.pl.umap(mmd_latent_with_true_labels, color=color,
save=f"_{data_name}_{cell_type}_mmd_latent_with_true_labels",
show=False)
sc.pp.neighbors(mmd_latent_with_fake_labels)
train = sc.read("../data/chsal_train_7000.h5ad")
elif data_name == "species":
stim_key = "LPS6"
ctrl_key = "unst"
cell_type_key = "species"
train = sc.read("../data/train_all_lps6.h5ad")
conditions = {"ctrl": ctrl_key, "stim": stim_key}
os.chdir(f"./vae_results/{data_name}/")
sc.settings.figdir = os.getcwd()
recon_data = sc.read(f"./reconstructed.h5ad")
diff_genes = score(train, n_deg=500, n_genes=1000, cell_type_key=cell_type_key, conditions=conditions,
sortby=score_type)
diff_genes = diff_genes["genes"].tolist()
sc.pl.stacked_violin(recon_data,
var_names=diff_genes[:10],
groupby="condition",
save=f"_Top_{10}_{score_type}_genes_out_of_500_{data_name}",
swap_axes=True,
show=False)
os.chdir("../../")
wspace=0.15,
frameon=False)
sc.pp.neighbors(mmd_latent_with_true_labels)
sc.tl.umap(mmd_latent_with_true_labels)
sc.pl.umap(mmd_latent_with_true_labels, color=color,
save=f"_{data_name}_{cell_type}_mmd_latent_with_true_labels",
show=False,
wspace=0.15,
frameon=False)
if gene_list is not None:
for target_condition in target_keys:
pred_adata = pred_adatas[pred_adatas.obs[condition_key].str.endswith(target_condition)]
violin_adata = cell_type_adata.concatenate(pred_adata)
for gene in gene_list[:3]:
sc.pl.violin(violin_adata, keys=gene, groupby=condition_key,
save=f"_{data_name}_{cell_type}_{gene}_{target_condition}.pdf",
show=False,
wspace=0.2,
rotation=90,
frameon=False)
plt.close("all")
model_path=f"./")
# network.restore_model()
network.train(net_train_data, n_epochs=n_epochs, batch_size=batch_size, verbose=2)
print(f"network_{cell_type} has been trained!")
true_labels, _ = scgen.label_encoder(net_train_data)
fake_labels = np.ones(shape=(net_train_data.shape[0], 1))
latent_with_true_labels = network.to_latent(net_train_data.X, labels=true_labels)
latent_with_true_labels = sc.AnnData(X=latent_with_true_labels,
obs={condition_key: net_train_data.obs[condition_key].tolist(),
cell_type_key: net_train_data.obs[cell_type_key].tolist()})
sc.pp.neighbors(latent_with_true_labels)
sc.tl.umap(latent_with_true_labels)
sc.pl.umap(latent_with_true_labels, color=[condition_key, cell_type_key],
save=f"_latent_true_labels_{z_dim}",
show=False)
latent_with_fake_labels = network.to_latent(net_train_data.X, fake_labels)
latent_with_fake_labels = sc.AnnData(X=latent_with_fake_labels,
obs={condition_key: net_train_data.obs[condition_key].tolist(),
cell_type_key: net_train_data.obs[cell_type_key].tolist()})
sc.pp.neighbors(latent_with_fake_labels)
sc.tl.umap(latent_with_fake_labels)
sc.pl.umap(latent_with_fake_labels, color=[condition_key, cell_type_key],
save=f"_latent_fake_labels_{z_dim}",
show=False)
mmd_with_true_labels = network.to_mmd_layer(network.cvae_model, net_train_data.X,
encoder_labels=true_labels, feed_fake=False)
mmd_with_true_labels = sc.AnnData(X=mmd_with_true_labels,
mmd_latent_with_fake_labels = sc.AnnData(X=mmd_latent_with_fake_labels)
mmd_latent_with_fake_labels.obs['condition'] = data.obs['condition'].values
# mmd_latent_with_fake_labels.obs[cell_type_key] = data.obs[cell_type_key].values
color = ['condition']
sc.pp.neighbors(data)
sc.tl.umap(data)
sc.pl.umap(data, color=color,
save=f'_{data_name}_train_data',
show=False)
sc.pp.neighbors(latent_with_true_labels)
sc.tl.umap(latent_with_true_labels)
sc.pl.umap(latent_with_true_labels, color=color,
save=f"_{data_name}_latent_with_true_labels",
show=False)
sc.pp.neighbors(latent_with_fake_labels)
sc.tl.umap(latent_with_fake_labels)
sc.pl.umap(latent_with_fake_labels, color=color,
save=f"_{data_name}__latent_with_fake_labels",
show=False)
sc.pp.neighbors(mmd_latent_with_true_labels)
sc.tl.umap(mmd_latent_with_true_labels)
sc.pl.umap(mmd_latent_with_true_labels, color=color,
save=f"_{data_name}_mmd_latent_with_true_labels",
show=False)
sc.pp.neighbors(mmd_latent_with_fake_labels)
def run_embedding(adata):
if len(unique(adata.obs["louvain"].values)) < 10:
palette = "tab10"
else:
palette = "tab20"
if "umap" in embedding:
sc.tl.umap(adata)
if plotting:
sc.pl.umap(adata, color="louvain", palette=palette, save="_louvain")
if "tsne" in embedding:
sc.tl.tsne(adata)
if plotting:
sc.pl.tsne(adata, color="louvain", palette=palette, save="_louvain")
save="pred_all_genes",
show=False)
sc.pp.neighbors(all_adata_top_100_genes)
sc.tl.umap(all_adata_top_100_genes)
sc.pl.umap(all_adata_top_100_genes, color=condition_key,
save="pred_top_100_genes",
show=False)
sc.pp.neighbors(all_adata_top_50_genes)
sc.tl.umap(all_adata_top_50_genes)
sc.pl.umap(all_adata_top_50_genes, color=condition_key,
save="pred_top_50_genes",
show=False)
sc.pl.violin(all_adata, keys=diff_genes.tolist()[0], groupby=condition_key,
save=f"_{diff_genes.tolist()[0]}",
show=False)
plt.close("all")
save=None):
"""\
Scatter plot in tSNE basis.
Parameters
----------
{adata_color_etc}
{edges_arrows}
{scatter_bulk}
{show_save_ax}
Returns
-------
If `show==False` a :class:`~matplotlib.axes.Axes` or a list of it.
"""
sc.pl.tsne(adata, color=color, gene_symbols=feature_symbols, use_raw=use_raw,
layer=layer, sort_order=sort_order, groups=groups, components=components,
projection=projection, legend_loc=legend_loc, legend_fontsize=legend_fontsize,
legend_fontweight=legend_fontweight, legend_fontoutline=legend_fontoutline,
size=size, color_map=color_map, palette=palette, frameon=frameon, ncols=ncols,
wspace=wspace, hspace=hspace, title=title, return_fig=return_fig, show=show,
save=save)