Skip to content

Commit

Permalink
add harmony (#317)
Browse files Browse the repository at this point in the history
* add harmony

* fix formatting

* update exception

* add test for harmony

* install harmony

Co-authored-by: Michaela Mueller <[email protected]>
Co-authored-by: Michaela Mueller <[email protected]>
  • Loading branch information
3 people authored Nov 22, 2022
1 parent 77ab015 commit e028e04
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install '.[test,scanorama,scvi]'
pip install '.[test,scanorama,scvi,harmony]'
- name: Test with pytest
run: |
Expand Down
23 changes: 23 additions & 0 deletions scib/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@
from .exceptions import OptionalDependencyNotInstalled


def harmony(adata, batch, hvg=None, **kwargs):
"""Harmony wrapper function
Based on `harmony-pytorch <https://github.com/lilab-bcb/harmony-pytorch>`_ version 0.1.7
:param adata: preprocessed ``anndata`` object
:param batch: batch key in ``adata.obs``
:param hvg: list of highly variables to subset to. If ``None``, the full dataset will be used
:return: ``anndata`` object containing the corrected feature matrix as well as an embedding representation of the
corrected data
"""
try:
from harmony import harmonize
except ModuleNotFoundError as e:
raise OptionalDependencyNotInstalled(e)

utils.check_sanity(adata, batch, hvg)
sc.tl.pca(adata)
adata.obsm["X_emb"] = harmonize(adata.obsm["X_pca"], adata.obs, batch_key=batch)

return adata


def scanorama(adata, batch, hvg=None, **kwargs):
"""Scanorama wrapper function
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ scvi = scvi-tools >=0.16.1
trvae = trvae ==1.1.2
trvaep = trvaep ==0.1.0
desc = desc ==2.0.3
harmony = harmony-pytorch

[tool.isort]
include_trailing_comma = true
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/test_harmony.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import scib
from tests.common import LOGGER, assert_near_exact


def test_harmony(adata_paul15_template):
adata = scib.ig.harmony(adata_paul15_template, batch="batch")

scib.pp.reduce_data(
adata, n_top_genes=200, neighbors=True, use_rep="X_emb", pca=True, umap=False
)

# check NMI after clustering
res_max, score_max, _ = scib.cl.opt_louvain(
adata,
label_key="celltype",
cluster_key="cluster",
plot=False,
inplace=True,
)
LOGGER.info(f"max resolution: {res_max}, max score: {score_max}")

assert_near_exact(score_max, 0.4954028837739288, 1e-2)

0 comments on commit e028e04

Please sign in to comment.