Skip to content

Commit

Permalink
Fix docs rendering for classes using lazy import (#651)
Browse files Browse the repository at this point in the history
* Fix lazy import for docs

* Pre-commit

* Fixed error naming

* Fixed SCGEN usage display

* Run only two CI tests

* Fix cinemaot tests
  • Loading branch information
Lilly-May committed Sep 1, 2024
1 parent ba09f89 commit 5874fd1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 26 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ jobs:
- os: ubuntu-latest
python: "3.12"
run_mode: "slow"
- os: ubuntu-latest
python: "3.12"
run_mode: "fast"
- os: ubuntu-latest
python: "3.12"
run_mode: slow
pip-flags: "--pre"
# - os: ubuntu-latest
# python: "3.12"
# run_mode: "fast"
# - os: ubuntu-latest
# python: "3.12"
# run_mode: slow
# pip-flags: "--pre"

env:
OS: ${{ matrix.os }}
Expand Down
4 changes: 2 additions & 2 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ Follow these steps to install pertpy on an Apple Silicon machine (tested on a Ma
brew install --cask mambaforge
```

3. Create a new environment using mamba (here with python 3.10) and activate it
3. Create a new environment using mamba and activate it

```console
mamba create -n pertpy-env python=3.11
mamba create -n pertpy-env
mamba activate pertpy-env
```

Expand Down
2 changes: 1 addition & 1 deletion docs/usage/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ See [scGen predicts single-cell perturbation responses](https://www.nature.com/a
.. autosummary::
:toctree: tools
tools.SCGEN
tools.Scgen
```

Example implementation:
Expand Down
27 changes: 12 additions & 15 deletions pertpy/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
from functools import wraps
from importlib import import_module


def lazy_import(module_path, class_name, extras):
def _import():
try:
for extra in extras:
import_module(extra)
except ImportError as e:
raise ImportError(
f"Extra dependencies required: {', '.join(extras)}. "
f"Please install with: pip install {' '.join(extras)}"
) from e
try:
for extra in extras:
import_module(extra)
module = import_module(module_path)
return getattr(module, class_name)
except ImportError:

@wraps(_import)
def wrapper(*args, **kwargs):
return _import()(*args, **kwargs)
class Placeholder:
def __init__(self, *args, **kwargs):
raise ImportError(
f"Extra dependencies required: {', '.join(extras)}. "
f"Please install with: pip install {' '.join(extras)}"
)

return wrapper
return Placeholder


from pertpy.tools._augur import Augur
Expand Down Expand Up @@ -49,7 +46,7 @@ def wrapper(*args, **kwargs):
Tasccoda = lazy_import("pertpy.tools._coda._tasccoda", "Tasccoda", CODA_EXTRAS)

DE_EXTRAS = ["formulaic", "pydeseq2"]
EdgeR = lazy_import("pertpy.tools._differential_gene_expression", "EdgeR", DE_EXTRAS + ["edger"])
EdgeR = lazy_import("pertpy.tools._differential_gene_expression", "EdgeR", DE_EXTRAS) # edgeR will be imported via rpy2
PyDESeq2 = lazy_import("pertpy.tools._differential_gene_expression", "PyDESeq2", DE_EXTRAS)
Statsmodels = lazy_import("pertpy.tools._differential_gene_expression", "Statsmodels", DE_EXTRAS + ["statsmodels"])
TTest = lazy_import("pertpy.tools._differential_gene_expression", "TTest", DE_EXTRAS)
Expand Down
2 changes: 1 addition & 1 deletion pertpy/tools/_cinemaot.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def causaleffect(
TE.obsm["X_embedding"] = embedding

if return_matching:
TE.obsm["ot"] = ot_sink.matrix.T
TE.obsm["ot"] = np.asarray(ot_sink.matrix.T)
return TE
else:
return TE
Expand Down

0 comments on commit 5874fd1

Please sign in to comment.