-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] mrVI training pipeline #982
Draft
ebezzi
wants to merge
5
commits into
main
Choose a base branch
from
ebezzi/mrvi
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
census: | ||
organism: | ||
"homo_sapiens" | ||
obs_query: # Use if you want to train on a subset of the model | ||
null | ||
obs_query_model: # Required when loading data for model training. Do not change. | ||
'is_primary_data == True and nnz >= 300' | ||
hvg: | ||
top_n_hvg: | ||
5000 | ||
hvg_batch: | ||
[suspension_type, assay] | ||
anndata: | ||
batch_key: | ||
[dataset_id, assay, suspension_type, donor_id] | ||
model_filename: | ||
anndata_model.h5ad | ||
model: | ||
filename: "mrvi.model" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import scvi_v2 | ||
|
||
print(scvi_v2.__file__) | ||
|
||
|
||
import torch | ||
|
||
torch.manual_seed(0) | ||
|
||
import anndata as ad | ||
import numpy as np | ||
import yaml | ||
|
||
file = "mrvi-config.yaml" | ||
|
||
if __name__ == "__main__": | ||
with open(file) as f: | ||
config = yaml.safe_load(f) | ||
|
||
adata_config = config["anndata"] | ||
filename = adata_config.get("model_filename") | ||
|
||
census_config = config["census"] | ||
experiment_name = census_config.get("organism") | ||
|
||
scvi_dataset = ad.read_h5ad(filename) | ||
|
||
scvi_dataset.obs["nuisance"] = ( | ||
# scvi_dataset.obs['dataset_id'].astype(str) + '_' + | ||
scvi_dataset.obs["assay"].astype(str) | ||
+ "_" | ||
+ scvi_dataset.obs["suspension_type"].astype(str) | ||
) | ||
scvi_dataset.obs["sample"] = ( | ||
scvi_dataset.obs["dataset_id"].astype(str) + "_" + scvi_dataset.obs["donor_id"].astype(str) | ||
) | ||
|
||
# hv = pd.read_pickle("hv_genes.pkl") | ||
# hv_idx = hv[hv].index | ||
|
||
# census = cellxgene_census.open_soma(census_version="2023-12-15") | ||
|
||
# obs_query = None # not for now | ||
|
||
# query = census["census_data"][experiment_name].axis_query( | ||
# measurement_name="RNA", | ||
# obs_query=obs_query, | ||
# var_query=soma.AxisQuery(coords=(list(hv_idx),)), | ||
# ) | ||
|
||
# idx = query.obs(column_names=["soma_joinid"]).concat().to_pandas().index.to_numpy() | ||
|
||
# adata = query.to_anndata(X_name="raw") | ||
|
||
model_config = config.get("model") | ||
model_filename = model_config.get("filename") | ||
|
||
# May or may not be necessary | ||
# scvi_v2.MrVI.setup_anndata(scvi_dataset, sample_key="sample", batch_key="nuisance", labels_key="cell_type") | ||
|
||
mrvi_model = scvi_v2.MrVI.load("mrvi.model", adata=scvi_dataset) | ||
|
||
latent = mrvi_model.get_latent_representation(give_z=False) | ||
|
||
# with open("mrvi-latent-idx.npy", "wb") as f: | ||
# np.save(f, idx) | ||
|
||
with open("mrvi-latent.npy", "wb") as f: | ||
np.save(f, latent) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import scvi_v2 | ||
|
||
print(scvi_v2.__file__) | ||
|
||
|
||
import torch | ||
|
||
torch.manual_seed(0) | ||
|
||
import anndata as ad | ||
import flax.linen as nn | ||
import yaml | ||
from lightning.pytorch.loggers import TensorBoardLogger | ||
|
||
file = "mrvi-config.yaml" | ||
|
||
if __name__ == "__main__": | ||
with open(file) as f: | ||
config = yaml.safe_load(f) | ||
|
||
adata_config = config["anndata"] | ||
filename = adata_config.get("model_filename") | ||
|
||
scvi_dataset = ad.read_h5ad(filename) | ||
|
||
train_kwargs = { | ||
"early_stopping": True, | ||
} | ||
|
||
plan_kwargs = {"lr": 1e-3, "n_epochs_kl_warmup": 20} | ||
|
||
model_kwargs = { | ||
"n_latent": 100, | ||
"n_latent_u": 20, | ||
"qz_nn_flavor": "attention", | ||
"px_nn_flavor": "attention", | ||
"qz_kwargs": {"use_map": False, "stop_gradients": False, "stop_gradients_mlp": True, "dropout_rate": 0.03}, | ||
"px_kwargs": { | ||
"stop_gradients": False, | ||
"stop_gradients_mlp": True, | ||
"h_activation": nn.softmax, | ||
"dropout_rate": 0.03, | ||
"low_dim_batch": True, | ||
}, | ||
"learn_z_u_prior_scale": False, | ||
"z_u_prior": False, | ||
"u_prior_mixture": True, | ||
"u_prior_mixture_k": 100, | ||
} | ||
|
||
scvi_dataset.obs["nuisance"] = ( | ||
# scvi_dataset.obs['dataset_id'].astype(str) + '_' + | ||
scvi_dataset.obs["assay"].astype(str) | ||
+ "_" | ||
+ scvi_dataset.obs["suspension_type"].astype(str) | ||
) | ||
scvi_dataset.obs["sample"] = ( | ||
scvi_dataset.obs["dataset_id"].astype(str) + "_" + scvi_dataset.obs["donor_id"].astype(str) | ||
) | ||
|
||
model_config = config.get("model") | ||
n_hidden = model_config.get("n_hidden") | ||
n_latent = model_config.get("n_latent") | ||
n_layers = model_config.get("n_layers") | ||
dropout_rate = model_config.get("dropout_rate") | ||
output_filename = model_config.get("filename") | ||
|
||
scvi_v2.MrVI.setup_anndata(scvi_dataset, sample_key="sample", batch_key="nuisance", labels_key="cell_type") | ||
mrvi_model = scvi_v2.MrVI(scvi_dataset, **model_kwargs) | ||
|
||
logger = TensorBoardLogger("mrvi_tb_logs", name="mrvi_50_epochs") | ||
|
||
mrvi_model.train(max_epochs=50, batch_size=4096, plan_kwargs=plan_kwargs, **train_kwargs) | ||
|
||
mrvi_model.save(output_filename) | ||
|
||
# # Get z representation | ||
# adata.obsm["X_mrvi_z"] = mrvi_model.get_latent_representation(give_z=True) | ||
# # Get u representation | ||
# adata.obsm["X_mrvi_u"] = mrvi_model.get_latent_representation(give_z=False) | ||
# sc.pp.neighbors(adata, use_rep="X_mrvi_u", key_added="neighbors_mrvi", method='rapids', n_neighbors=30) | ||
# sc.tl.umap(adata, neighbors_key="neighbors_mrvi", method='rapids') | ||
# sc.pl.umap(adata, color=['dataset_id', 'cell_subclass', 'suspension_type', 'sex'], ncols=1, frameon=False, wspace=0.4, title='mrVI (Census)') |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently unused.