From aeb352e44e5d91eec1a752d5f7d5f07bfb7f8c97 Mon Sep 17 00:00:00 2001 From: willGraham01 <1willgraham@gmail.com> Date: Wed, 21 Jun 2023 09:54:21 +0100 Subject: [PATCH 1/6] Make morphapi and cellfinder optional, adjust tests --- brainglobe/__init__.py | 16 +++++++++++++++- pyproject.toml | 10 +++++++--- tests/test_unit/test_tool_exposure.py | 27 ++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/brainglobe/__init__.py b/brainglobe/__init__.py index 4857f87..ea6872f 100644 --- a/brainglobe/__init__.py +++ b/brainglobe/__init__.py @@ -12,4 +12,18 @@ import brainreg import brainreg_segment import cellfinder_core -import morphapi + +# Determine if optional dependencies were installed, +# and expose if necessary. +# morphapi +_MORPHAPI_INSTALLED = True +try: + import morphapi +except ImportError: + _MORPHAPI_INSTALLED = False +# cellfinder - not exposed, but still check +_CELLFINDER_INSTALLED = True +try: + import cellfinder +except ImportError: + _CELLFINDER_INSTALLED = False diff --git a/pyproject.toml b/pyproject.toml index e6724a7..3fbacc2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,12 +30,10 @@ dependencies = [ # "bg-atlasgen", [WIP] # "brainglobe-napari", [WIP] "brainglobe-napari-io", - "morphapi>=0.1.3.0", "cellfinder-napari", "brainreg-segment>=0.0.2", "brainreg", - "brainreg-napari", - "cellfinder" + "brainreg-napari" ] [project.optional-dependencies] @@ -50,6 +48,12 @@ dev = [ "ruff", "setuptools_scm", ] +morphapi = [ + "morphapi>=0.1.3.0" +] +cellfinder = [ + "cellfinder" +] [build-system] requires = [ diff --git a/tests/test_unit/test_tool_exposure.py b/tests/test_unit/test_tool_exposure.py index d1d1895..fa132f2 100644 --- a/tests/test_unit/test_tool_exposure.py +++ b/tests/test_unit/test_tool_exposure.py @@ -9,8 +9,8 @@ "brainreg", "brainreg_segment", "cellfinder_core", - "morphapi", ] +OPTIONAL_TOOLS = ["morphapi", "cellfinder"] def test_tool_exposure() -> None: @@ -23,3 +23,28 @@ def test_tool_exposure() -> None: assert inspect.ismodule( getattr(bg, exposed_tool) ), f"brainglobe.{exposed_tool} is not a submodule" + + # Now check the optional dependencies + # morphapi + bg_has_morphapi = hasattr(bg, "morphapi") + if bg._MORPHAPI_INSTALLED: + assert ( + bg_has_morphapi + ), "brainglobe has morphapi, but it is flagged as installed" + assert inspect.ismodule( + getattr(bg, "morphapi") + ), "brainglobe.morphapi is not a submodule" + else: + assert ( + not bg_has_morphapi + ), "brainglobe has morphapi, but it is flagged as not installed" + # cellfinder + bg_has_cellfinder = hasattr(bg, "cellfinder") + if bg._CELLFINDER_INSTALLED: + assert ( + bg_has_cellfinder + ), "brainglobe has cellfinder, but it is flagged as installed" + else: + assert ( + not bg_has_cellfinder + ), "brainglobe has cellfinder, but it is flagged as not installed" From 57a317b0cd554a2cdf08e8c21269b45f151d299a Mon Sep 17 00:00:00 2001 From: willGraham01 <1willgraham@gmail.com> Date: Wed, 21 Jun 2023 09:56:36 +0100 Subject: [PATCH 2/6] Add optional dependencies to readme --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dabd93b..37f855c 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,17 @@ The BrainGlobe project is only possible due to grant funding and the generous su ## Installation The `brainglobe` package can be installed from [PyPI](https://pypi.org/project/brainglobe/) into a Python environment by running -```python +```sh pip install brainglobe ``` + +If you want to install the additional packages `morphapi` and `cellfinder`, you can specify them as optional dependencies: +```sh +pip install brainglobe[morphapi] # Include morphapi +pip install brainglobe[cellfinder] # Include cellfinder +pip install brainglobe[morphapi,cellfinder] # Include both morphapi and cellfinder +``` + Alternatively, you can download the source from [PyPI here](https://pypi.org/project/brainglobe/#files). ## Contributing From 43a95ddd58e2d3cf443ee7283b0ea3f08bda1040 Mon Sep 17 00:00:00 2001 From: willGraham01 <1willgraham@gmail.com> Date: Wed, 21 Jun 2023 10:39:55 +0100 Subject: [PATCH 3/6] Tidy up submdule presence tests --- brainglobe/__init__.py | 6 +++--- tests/test_unit/test_tool_exposure.py | 31 +++++++++++---------------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/brainglobe/__init__.py b/brainglobe/__init__.py index ea6872f..134f07c 100644 --- a/brainglobe/__init__.py +++ b/brainglobe/__init__.py @@ -21,9 +21,9 @@ import morphapi except ImportError: _MORPHAPI_INSTALLED = False -# cellfinder - not exposed, but still check +# cellfinder - not exposed, but still check presence _CELLFINDER_INSTALLED = True try: - import cellfinder -except ImportError: + version("cellfinder") +except PackageNotFoundError: _CELLFINDER_INSTALLED = False diff --git a/tests/test_unit/test_tool_exposure.py b/tests/test_unit/test_tool_exposure.py index fa132f2..85b46d8 100644 --- a/tests/test_unit/test_tool_exposure.py +++ b/tests/test_unit/test_tool_exposure.py @@ -1,5 +1,7 @@ import inspect +import pytest + import brainglobe as bg # Tools that will be exposed in the brainglobe module/namespace @@ -25,26 +27,19 @@ def test_tool_exposure() -> None: ), f"brainglobe.{exposed_tool} is not a submodule" # Now check the optional dependencies - # morphapi - bg_has_morphapi = hasattr(bg, "morphapi") - if bg._MORPHAPI_INSTALLED: - assert ( - bg_has_morphapi + # morphapi - if not installed, should not be exposed + if not bg._MORPHAPI_INSTALLED: + with pytest.raises(ImportError): + import bg.morphapi + else: + assert hasattr( + bg, "morphapi" ), "brainglobe has morphapi, but it is flagged as installed" assert inspect.ismodule( getattr(bg, "morphapi") ), "brainglobe.morphapi is not a submodule" - else: - assert ( - not bg_has_morphapi - ), "brainglobe has morphapi, but it is flagged as not installed" - # cellfinder - bg_has_cellfinder = hasattr(bg, "cellfinder") + # cellfinder - if installed, should not be exposed if bg._CELLFINDER_INSTALLED: - assert ( - bg_has_cellfinder - ), "brainglobe has cellfinder, but it is flagged as installed" - else: - assert ( - not bg_has_cellfinder - ), "brainglobe has cellfinder, but it is flagged as not installed" + assert not hasattr( + bg, "cellfinder" + ), "brainglobe has exposed cellfinder" From 1e00ab35cd074bfabe40f0bf8bbb98c90bb1ccbe Mon Sep 17 00:00:00 2001 From: willGraham01 <1willgraham@gmail.com> Date: Wed, 21 Jun 2023 10:56:17 +0100 Subject: [PATCH 4/6] Revert "Tidy up submdule presence tests" This reverts commit 43a95ddd58e2d3cf443ee7283b0ea3f08bda1040. --- brainglobe/__init__.py | 6 +++--- tests/test_unit/test_tool_exposure.py | 31 ++++++++++++++++----------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/brainglobe/__init__.py b/brainglobe/__init__.py index 134f07c..ea6872f 100644 --- a/brainglobe/__init__.py +++ b/brainglobe/__init__.py @@ -21,9 +21,9 @@ import morphapi except ImportError: _MORPHAPI_INSTALLED = False -# cellfinder - not exposed, but still check presence +# cellfinder - not exposed, but still check _CELLFINDER_INSTALLED = True try: - version("cellfinder") -except PackageNotFoundError: + import cellfinder +except ImportError: _CELLFINDER_INSTALLED = False diff --git a/tests/test_unit/test_tool_exposure.py b/tests/test_unit/test_tool_exposure.py index 85b46d8..fa132f2 100644 --- a/tests/test_unit/test_tool_exposure.py +++ b/tests/test_unit/test_tool_exposure.py @@ -1,7 +1,5 @@ import inspect -import pytest - import brainglobe as bg # Tools that will be exposed in the brainglobe module/namespace @@ -27,19 +25,26 @@ def test_tool_exposure() -> None: ), f"brainglobe.{exposed_tool} is not a submodule" # Now check the optional dependencies - # morphapi - if not installed, should not be exposed - if not bg._MORPHAPI_INSTALLED: - with pytest.raises(ImportError): - import bg.morphapi - else: - assert hasattr( - bg, "morphapi" + # morphapi + bg_has_morphapi = hasattr(bg, "morphapi") + if bg._MORPHAPI_INSTALLED: + assert ( + bg_has_morphapi ), "brainglobe has morphapi, but it is flagged as installed" assert inspect.ismodule( getattr(bg, "morphapi") ), "brainglobe.morphapi is not a submodule" - # cellfinder - if installed, should not be exposed + else: + assert ( + not bg_has_morphapi + ), "brainglobe has morphapi, but it is flagged as not installed" + # cellfinder + bg_has_cellfinder = hasattr(bg, "cellfinder") if bg._CELLFINDER_INSTALLED: - assert not hasattr( - bg, "cellfinder" - ), "brainglobe has exposed cellfinder" + assert ( + bg_has_cellfinder + ), "brainglobe has cellfinder, but it is flagged as installed" + else: + assert ( + not bg_has_cellfinder + ), "brainglobe has cellfinder, but it is flagged as not installed" From a6104d54f86e6b79320b4dcbc03377a5dc853795 Mon Sep 17 00:00:00 2001 From: willGraham01 <1willgraham@gmail.com> Date: Wed, 21 Jun 2023 11:08:00 +0100 Subject: [PATCH 5/6] Adjust tests to make mypy happy --- tests/test_unit/test_tool_exposure.py | 40 +++++++++++++-------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/tests/test_unit/test_tool_exposure.py b/tests/test_unit/test_tool_exposure.py index fa132f2..d47f64f 100644 --- a/tests/test_unit/test_tool_exposure.py +++ b/tests/test_unit/test_tool_exposure.py @@ -1,5 +1,7 @@ import inspect +import pytest + import brainglobe as bg # Tools that will be exposed in the brainglobe module/namespace @@ -24,27 +26,25 @@ def test_tool_exposure() -> None: getattr(bg, exposed_tool) ), f"brainglobe.{exposed_tool} is not a submodule" - # Now check the optional dependencies - # morphapi - bg_has_morphapi = hasattr(bg, "morphapi") + # Determine if optional dependencies were installed, + # and exposed if necessary + + # morphapi - should be exposed if installed if bg._MORPHAPI_INSTALLED: - assert ( - bg_has_morphapi - ), "brainglobe has morphapi, but it is flagged as installed" + assert hasattr( + bg, "morphapi" + ), "morphapi is installed but not exposed." assert inspect.ismodule( - getattr(bg, "morphapi") - ), "brainglobe.morphapi is not a submodule" + bg.morphapi + ), "brainglobe.morphapi is not a module" else: - assert ( - not bg_has_morphapi - ), "brainglobe has morphapi, but it is flagged as not installed" - # cellfinder - bg_has_cellfinder = hasattr(bg, "cellfinder") - if bg._CELLFINDER_INSTALLED: - assert ( - bg_has_cellfinder - ), "brainglobe has cellfinder, but it is flagged as installed" + assert not hasattr(bg, "morphapi") + + # cellfinder - should not be exposed if installed + if not bg._CELLFINDER_INSTALLED: + with pytest.raises(ImportError): + pass else: - assert ( - not bg_has_cellfinder - ), "brainglobe has cellfinder, but it is flagged as not installed" + assert not hasattr( + bg, "cellfinder" + ), "brainglobe.cellfinder is exposed" From 2b6a2eb504e3e218e0dfde5fa82a3753bce10d62 Mon Sep 17 00:00:00 2001 From: willGraham01 <1willgraham@gmail.com> Date: Wed, 21 Jun 2023 11:17:09 +0100 Subject: [PATCH 6/6] pass is not a test, but import module is --- tests/test_unit/test_tool_exposure.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/test_unit/test_tool_exposure.py b/tests/test_unit/test_tool_exposure.py index d47f64f..3d70ccb 100644 --- a/tests/test_unit/test_tool_exposure.py +++ b/tests/test_unit/test_tool_exposure.py @@ -1,7 +1,5 @@ import inspect -import pytest - import brainglobe as bg # Tools that will be exposed in the brainglobe module/namespace @@ -41,10 +39,7 @@ def test_tool_exposure() -> None: assert not hasattr(bg, "morphapi") # cellfinder - should not be exposed if installed - if not bg._CELLFINDER_INSTALLED: - with pytest.raises(ImportError): - pass - else: + if bg._CELLFINDER_INSTALLED: assert not hasattr( bg, "cellfinder" ), "brainglobe.cellfinder is exposed"