Skip to content

Commit

Permalink
Fix kBET import issue (#350)
Browse files Browse the repository at this point in the history
* use r-lib actions

* move r-lib to top

* add R dependency install

* return error instead of np.nan when kBET is not installed

* install devtools via R

* use rp2y

* use remotes instead of devtools

* move r-lib actions to test workflow

* add GITHUB_PAT

* separate run for R functions

* use correct R version

* install test dependencies
  • Loading branch information
mumichae authored Nov 22, 2022
1 parent e028e04 commit ddf1c9e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
43 changes: 37 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: |
pre-commit run --color=always --show-diff-on-failure --all-files
test:
metrics:
runs-on: ${{ matrix.os }}
strategy:
matrix:
Expand Down Expand Up @@ -64,19 +64,50 @@ jobs:
--ignore=tests/integration/ \
--ignore=tests/metrics/rpy2 -vv
- name: Test R metrics
if: ${{ matrix.os != 'macos-latest'}}
- name: Upload coverage
if: ${{ matrix.os != 'macos-latest' }}
env:
CODECOV_NAME: ${{ matrix.os }}-${{ matrix.python }}
run: |
pip install '.[rpy2]'
python -m pytest --cov --cov-append --cov-report=term-missing -k rpy2 -vv
codecov --no-color --required --flags unittest
rpy2:
runs-on: ${{ matrix.os }}
strategy:
matrix:
r: [4.2]
python: [3.9]
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
- name: Set up R
uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.r }}

- name: Install dependencies
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
run: |
python -m pip install --upgrade pip
pip install '.[test,rpy2]'
Rscript -e "install.packages('remotes')"
Rscript -e "remotes::install_github('theislab/kBET')"
- name: Test with pytest
run: |
python -m pytest --cov --cov-append --cov-report=term-missing -k rpy2 -vv
- name: Upload coverage
if: ${{ matrix.os != 'macos-latest' }}
env:
CODECOV_NAME: ${{ matrix.os }}-${{ matrix.python }}
run: |
codecov --no-color --required --flags unittest
integration:
runs-on: ${{ matrix.os }}
strategy:
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ e.g. for installing `rpy2` and `bbknn` dependencies:
pip install 'scib[rpy2,bbknn]'
```

Optional dependencies outside of python need to be installed separately.
For instance, in order to run kBET, install it via the following command in R:

```R
remotes::install_github('theislab/kBET')
```

## Metrics

We implemented different metrics for evaluating batch correction and biological conservation in the `scib.metrics`
Expand Down
6 changes: 4 additions & 2 deletions scib/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class OptionalDependencyNotInstalled(ModuleNotFoundError):
def __init__(self, exception):
def __init__(self, exception, module_name=None):
if module_name is None:
module_name = exception.name
self.message = (
f"\n{exception.name} is an optional dependency and not installed by default. "
f"\n'{module_name}' is an optional dependency and not installed by default. "
f"Please make sure you install it manually."
)
super().__init__(self.message)
Expand Down
4 changes: 2 additions & 2 deletions scib/metrics/kbet.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def kBET(

try:
ro.r("library(kBET)")
except rpy2.rinterface_lib.embedded.RRuntimeError:
return np.nan
except rpy2.rinterface_lib.embedded.RRuntimeError as e:
raise OptionalDependencyNotInstalled(e, "kBET")

# compute connectivities for non-knn type data integrations
# and increase neighborhoods for knn type data integrations
Expand Down
6 changes: 2 additions & 4 deletions tests/metrics/rpy2/test_kbet.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import numpy as np

import scib
from tests.common import LOGGER
from tests.common import LOGGER, assert_near_exact


def test_kbet(adata_pca):
score = scib.me.kBET(
adata_pca, batch_key="batch", label_key="celltype", embed="X_pca"
)
LOGGER.info(f"score: {score}")
assert np.isnan(score)
assert_near_exact(score, 0.556108994805538, diff=1e-02)

0 comments on commit ddf1c9e

Please sign in to comment.