Skip to content
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

Update concatenation function during batch removal #77

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8, 3.9, 3.10]
python-version: ['3.8', '3.9', '3.10']

steps:
- uses: actions/checkout@v2
Expand Down
1,791 changes: 897 additions & 894 deletions docs/tutorials/scgen_batch_removal.ipynb

Large diffs are not rendered by default.

60 changes: 36 additions & 24 deletions docs/tutorials/scgen_perturbation_prediction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"outputs": [],
"source": [
"import sys\n",
"#if branch is stable, will install via pypi, else will install from source\n",
"\n",
"# if branch is stable, will install via pypi, else will install from source\n",
"branch = \"stable\"\n",
"IN_COLAB = \"google.colab\" in sys.modules\n",
"\n",
Expand Down Expand Up @@ -78,8 +79,10 @@
}
],
"source": [
"train = sc.read(\"./tests/data/train_kang.h5ad\",\n",
" backup_url='https://drive.google.com/uc?id=1r87vhoLLq6PXAYdmyyd89zG90eJOFYLk')"
"train = sc.read(\n",
" \"./tests/data/train_kang.h5ad\",\n",
" backup_url=\"https://drive.google.com/uc?id=1r87vhoLLq6PXAYdmyyd89zG90eJOFYLk\",\n",
")"
]
},
{
Expand All @@ -95,8 +98,9 @@
"metadata": {},
"outputs": [],
"source": [
"train_new = train[~((train.obs[\"cell_type\"] == \"CD4T\") &\n",
" (train.obs[\"condition\"] == \"stimulated\"))]"
"train_new = train[\n",
" ~((train.obs[\"cell_type\"] == \"CD4T\") & (train.obs[\"condition\"] == \"stimulated\"))\n",
"]"
]
},
{
Expand Down Expand Up @@ -198,10 +202,7 @@
],
"source": [
"model.train(\n",
" max_epochs=100,\n",
" batch_size=32,\n",
" early_stopping=True,\n",
" early_stopping_patience=25\n",
" max_epochs=100, batch_size=32, early_stopping=True, early_stopping_patience=25\n",
")"
]
},
Expand Down Expand Up @@ -258,8 +259,13 @@
"source": [
"sc.pp.neighbors(latent_adata)\n",
"sc.tl.umap(latent_adata)\n",
"sc.pl.umap(latent_adata, color=['condition', 'cell_type'], wspace=0.4, frameon=False,\n",
" save='latentspace_batch32_klw000005_z100__100e.pdf')"
"sc.pl.umap(\n",
" latent_adata,\n",
" color=[\"condition\", \"cell_type\"],\n",
" wspace=0.4,\n",
" frameon=False,\n",
" save=\"latentspace_batch32_klw000005_z100__100e.pdf\",\n",
")"
]
},
{
Expand Down Expand Up @@ -339,11 +345,9 @@
],
"source": [
"pred, delta = model.predict(\n",
" ctrl_key='control',\n",
" stim_key='stimulated',\n",
" celltype_to_predict='CD4T'\n",
" ctrl_key=\"control\", stim_key=\"stimulated\", celltype_to_predict=\"CD4T\"\n",
")\n",
"pred.obs['condition'] = 'pred'"
"pred.obs[\"condition\"] = \"pred\""
]
},
{
Expand Down Expand Up @@ -380,8 +384,12 @@
"metadata": {},
"outputs": [],
"source": [
"ctrl_adata = train[((train.obs['cell_type'] == 'CD4T') & (train.obs['condition'] == 'control'))]\n",
"stim_adata = train[((train.obs['cell_type'] == 'CD4T') & (train.obs['condition'] == 'stimulated'))]"
"ctrl_adata = train[\n",
" ((train.obs[\"cell_type\"] == \"CD4T\") & (train.obs[\"condition\"] == \"control\"))\n",
"]\n",
"stim_adata = train[\n",
" ((train.obs[\"cell_type\"] == \"CD4T\") & (train.obs[\"condition\"] == \"stimulated\"))\n",
"]"
]
},
{
Expand Down Expand Up @@ -433,8 +441,12 @@
],
"source": [
"sc.tl.pca(eval_adata)\n",
"sc.pl.pca(eval_adata, color=\"condition\", frameon=False,\n",
" save='pred_stim_b32_klw000005_z100__100e.pdf')"
"sc.pl.pca(\n",
" eval_adata,\n",
" color=\"condition\",\n",
" frameon=False,\n",
" save=\"pred_stim_b32_klw000005_z100__100e.pdf\",\n",
")"
]
},
{
Expand Down Expand Up @@ -466,7 +478,7 @@
}
],
"source": [
"CD4T = train[train.obs[\"cell_type\"] ==\"CD4T\"]"
"CD4T = train[train.obs[\"cell_type\"] == \"CD4T\"]"
]
},
{
Expand Down Expand Up @@ -527,7 +539,7 @@
" labels={\"x\": \"predicted\", \"y\": \"ground truth\"},\n",
" path_to_save=\"./reg_mean1.pdf\",\n",
" show=True,\n",
" legend=False\n",
" legend=False,\n",
")"
]
},
Expand Down Expand Up @@ -567,11 +579,11 @@
" eval_adata,\n",
" axis_keys={\"x\": \"pred\", \"y\": \"stimulated\"},\n",
" gene_list=diff_genes[:10],\n",
" top_100_genes= diff_genes,\n",
" labels={\"x\": \"predicted\",\"y\": \"ground truth\"},\n",
" top_100_genes=diff_genes,\n",
" labels={\"x\": \"predicted\", \"y\": \"ground truth\"},\n",
" path_to_save=\"./reg_mean1.pdf\",\n",
" show=True,\n",
" legend=False\n",
" legend=False,\n",
")"
]
},
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ python-igraph = {version = "*", optional = true}
scanpy = ">=1.6"
scanpydoc = {version = ">=0.5", optional = true}
scikit-misc = {version = ">=0.1.3", optional = true}
scvi-tools = ">=0.20.0"
scvi-tools = "<0.20.0"
seaborn = ">=0.11"
sphinx = {version = ">=4.1,<4.4", optional = true}
sphinx-autodoc-typehints = {version = "*", optional = true}
Expand Down
2 changes: 1 addition & 1 deletion readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build:
sphinx:
configuration: docs/conf.py
python:
version: 3.10
version: 3.8
install:
- method: pip
path: .
Expand Down
6 changes: 2 additions & 4 deletions scgen/_scgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import scanpy as sc
import torch
from adjustText import adjust_text
from anndata import AnnData
from anndata import AnnData, concat
from matplotlib import pyplot
from scipy import stats
from scvi import REGISTRY_KEYS
Expand Down Expand Up @@ -265,9 +265,7 @@ def batch_removal(self, adata: Optional[AnnData] = None) -> AnnData:
batch_list[study].X = delta + batch_list[study].X
temp_cell[batch_ind[study]].X = batch_list[study].X
shared_ct.append(temp_cell)
all_shared_ann = AnnData.concatenate(
*shared_ct, batch_key="concat_batch", index_unique=None
)
all_shared_ann = concat(shared_ct, label="concat_batch", index_unique=None)
if "concat_batch" in all_shared_ann.obs.columns:
del all_shared_ann.obs["concat_batch"]
if len(not_shared_ct) < 1:
Expand Down
6 changes: 4 additions & 2 deletions scgen/_scgenvae.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ def loss(
inference_outputs,
generative_outputs,
):

x = tensors[REGISTRY_KEYS.X_KEY]
qz_m = inference_outputs["qz_m"]
qz_v = inference_outputs["qz_v"]
Expand Down Expand Up @@ -156,7 +155,10 @@ def sample(
tensor with shape (n_cells, n_genes, n_samples)
"""
inference_kwargs = dict(n_samples=n_samples)
inference_outputs, generative_outputs, = self.forward(
(
inference_outputs,
generative_outputs,
) = self.forward(
tensors,
inference_kwargs=inference_kwargs,
compute_loss=False,
Expand Down
1 change: 0 additions & 1 deletion scgen/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def balancer(
adata,
cell_type_key,
):

"""
Makes cell type population equal.

Expand Down
4 changes: 2 additions & 2 deletions tests/test_scgen.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import scvi
import scanpy as sc
import scvi

from scgen import SCGEN


def test_scgen():

adata = scvi.data.synthetic_iid()
SCGEN.setup_anndata(
adata,
Expand Down