generated from openproblems-bio/task_template
-
Notifications
You must be signed in to change notification settings - Fork 9
Implementation of CellPLM #19
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
Open
HelloWorldLTY
wants to merge
17
commits into
openproblems-bio:main
Choose a base branch
from
HelloWorldLTY:main
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.
Open
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5170369
update cellplm and scfoundation
HelloWorldLTY 6156a77
update cellplm
HelloWorldLTY f479b85
update cellplm and scf
HelloWorldLTY 62bbea8
only cellplm
HelloWorldLTY 85a21a2
update cellplm all
HelloWorldLTY ff325ac
update doi
HelloWorldLTY c3c03a6
update all:
HelloWorldLTY 6a98ccb
update correct label
HelloWorldLTY 28f3ce0
Remove scgpt from cellplm config
lazappi f35db1d
Add model_name argument to cellplm
lazappi b96e12c
Warn if GPU is not available for cellplm
lazappi e6a15cd
Fix model temp dir cleanup
lazappi 4454035
Add cellplm to benchmark workflow
lazappi 3951a7d
Tidy and style cellplm script
lazappi 5c6b249
Merge remote-tracking branch 'origin/main' into HelloWorldLTY/main
lazappi 7b4f4a9
Adjust cellplm to use counts
lazappi 938d673
Add non-applicable exit code to CellPLM
lazappi 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 hidden or 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,52 @@ | ||
__merge__: ../../api/base_method.yaml | ||
|
||
|
||
name: cellplm | ||
|
||
label: CellPLM | ||
|
||
summary: "A foundation model pre-trained with cells as tokens." | ||
|
||
description: | | ||
CellPLM is a pre-trained language model specifically designed for single-cell analysis that leverages the principles of natural language processing (NLP) to understand and process single-cell gene expression data. | ||
references: | ||
doi: | ||
- 10.1101/2023.10.03.560734 | ||
links: | ||
|
||
documentation: https://github.com/OmicsML/CellPLM/tree/main/tutorials | ||
|
||
repository: https://github.com/OmicsML/CellPLM | ||
|
||
|
||
info: | ||
method_types: [embedding] | ||
preferred_normalization: counts | ||
|
||
arguments: | ||
- name: --model | ||
type: string | ||
description: String giving the CellPLM model to use | ||
choices: ["20231027_85M"] | ||
default: "20231027_85M" | ||
|
||
resources: | ||
- type: python_script | ||
path: script.py | ||
- path: /src/utils/read_anndata_partial.py | ||
|
||
engines: | ||
- type: docker | ||
image: openproblems/base_pytorch_nvidia:1.0.0 | ||
setup: | ||
- type: python | ||
pypi: | ||
- gdown | ||
- scgpt | ||
- cellplm | ||
|
||
runners: | ||
- type: executable | ||
- type: nextflow | ||
directives: | ||
label: [midtime, midmem, midcpu, gpu] |
This file contains hidden or 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,93 @@ | ||
import sys | ||
import tempfile | ||
import scanpy as sc | ||
import anndata as ad | ||
import gdown | ||
import torch | ||
|
||
import warnings | ||
warnings.filterwarnings("ignore") | ||
from CellPLM.utils import set_seed | ||
|
||
import numpy as np | ||
import anndata as ad | ||
from CellPLM.pipeline.cell_embedding import CellEmbeddingPipeline | ||
|
||
## VIASH START | ||
# Note: this section is auto-generated by viash at runtime. To edit it, make changes | ||
# in config.vsh.yaml and then run `viash config inject config.vsh.yaml`. | ||
|
||
par = { | ||
'input': 'resources_test/.../input.h5ad', | ||
'output': 'output.h5ad', | ||
"model": "20231027_85M", | ||
} | ||
meta = { | ||
'name': 'cellplm' | ||
} | ||
## VIASH END | ||
|
||
sys.path.append(meta["resources_dir"]) | ||
from read_anndata_partial import read_anndata | ||
|
||
set_seed(24) | ||
PRETRAIN_VERSION = par['model'] | ||
DEVICE = "cuda" if torch.cuda.is_available() else "cpu" | ||
|
||
print("\n>>> Reading input files...", flush=True) | ||
print(f"Input H5AD file: '{par['input']}'", flush=True) | ||
adata = read_anndata( | ||
par['input'], | ||
X='layers/normalized', | ||
obs='obs', | ||
var='var', | ||
uns='uns' | ||
) | ||
|
||
if adata.uns["dataset_organism"] != "homo_sapiens": | ||
raise ValueError( | ||
f"CellPLM can only be used with human data " | ||
f"(dataset_organism == \"{adata.uns['dataset_organism']}\")" | ||
) | ||
|
||
print(adata, flush=True) | ||
|
||
print('Train model', flush=True) | ||
lazappi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# ... train model ... | ||
|
||
drive_path = f"https://drive.google.com/drive/folders/1C2fVNEKX3plHnagaTwpuPW5tpwv1up9G?usp=sharing" | ||
model_dir = tempfile.TemporaryDirectory() | ||
print(f"Downloading from '{drive_path}'", flush=True) | ||
gdown.download_folder(drive_path, output=model_dir.name, quiet=True) | ||
print(f"Model directory: '{model_dir.name}'", flush=True) | ||
lazappi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
pipeline = CellEmbeddingPipeline(pretrain_prefix=PRETRAIN_VERSION, # Specify the pretrain checkpoint to load | ||
pretrain_directory=model_dir.name) | ||
|
||
# DEVICE ='cpu' | ||
embedding = pipeline.predict(adata, # An AnnData object | ||
device=DEVICE) # Specify a gpu or cpu for model inference | ||
|
||
embedding = embedding.cpu().numpy() | ||
|
||
print('Generate predictions', flush=True) | ||
lazappi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# ... generate predictions ... | ||
|
||
output = ad.AnnData( | ||
obs=adata.obs[[]], | ||
var=adata.var[[]], | ||
obsm={ | ||
"X_emb": embedding, | ||
}, | ||
uns={ | ||
"dataset_id": adata.uns["dataset_id"], | ||
"normalization_id": adata.uns["normalization_id"], | ||
"method_id": meta["name"], | ||
}, | ||
) | ||
print(output) | ||
|
||
output.write_h5ad(par['output'], compression='gzip') | ||
|
||
print("\n>>> Cleaning up temporary directories...", flush=True) | ||
model_dir.cleanup() |
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.
Uh oh!
There was an error while loading. Please reload this page.