Skip to content

Commit 855f5f7

Browse files
Merge pull request #205 from NeurodataWithoutBorders/lazy_dandi
[Dependencies] Make dandi a lazy import
2 parents 8038b58 + bd98b50 commit 855f5f7

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

.github/workflows/testing.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ jobs:
2222
pip install pytest
2323
pip install pytest-cov
2424
- name: Install package
25-
run: pip install -e .
25+
run: |
26+
pip install -e .
27+
pip install dandi
2628
- name: Uninstall h5py
2729
run: pip uninstall -y h5py
2830
- name: Install ROS3

nwbinspector/tools.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from concurrent.futures import ProcessPoolExecutor, as_completed
77

88
from pynwb import NWBFile
9-
from dandi.dandiapi import DandiAPIClient
9+
10+
from .utils import is_module_installed
1011

1112

1213
def make_minimal_nwbfile():
@@ -34,6 +35,9 @@ def get_s3_urls_and_dandi_paths(dandiset_id: str, version_id: Optional[str] = No
3435
3536
Returns dictionary that maps each S3 url to the displayed file path on the DANDI archive content page.
3637
"""
38+
assert is_module_installed(module_name="dandi"), "You must install DANDI to get S3 paths (pip install dandi)."
39+
from dandi.dandiapi import DandiAPIClient
40+
3741
assert re.fullmatch(
3842
pattern="^[0-9]{6}$", string=dandiset_id
3943
), "The specified 'path' is not a proper DANDISet ID. It should be a six-digit numeric identifier."
@@ -64,5 +68,9 @@ def get_s3_urls_and_dandi_paths(dandiset_id: str, version_id: Optional[str] = No
6468

6569

6670
def _get_content_url_and_path(asset, follow_redirects: int = 1, strip_query: bool = True) -> Dict[str, str]:
67-
"""Private helper function for parallelization in 'get_s3_urls_and_dandi_paths'."""
71+
"""
72+
Private helper function for parallelization in 'get_s3_urls_and_dandi_paths'.
73+
74+
Must be globally defined (not as a part of get_s3_urls..) in order to be pickled.
75+
"""
6876
return {asset.get_content_url(follow_redirects=1, strip_query=True): asset.path}

nwbinspector/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import numpy as np
55
from typing import TypeVar, Optional, List
66
from pathlib import Path
7+
from importlib import import_module
78

89
PathType = TypeVar("PathType", str, Path) # For types that can be either files or folders
910
FilePathType = TypeVar("FilePathType", str, Path)
@@ -73,3 +74,16 @@ def is_string_json_loadable(string: str):
7374
return True
7475
except json.JSONDecodeError:
7576
return False
77+
78+
79+
def is_module_installed(module_name: str):
80+
"""
81+
Check if the given module is installed on the system.
82+
83+
Used for lazy imports.
84+
"""
85+
try:
86+
import_module(name=module_name)
87+
return True
88+
except ModuleNotFoundError:
89+
return False

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
packages=find_packages(),
1515
include_package_data=True,
1616
url="https://github.com/NeurodataWithoutBorders/nwbinspector",
17-
install_requires=["pynwb", "natsort", "click", "PyYAML", "jsonschema", "dandi>=0.39.2", "tqdm"],
17+
install_requires=["pynwb", "natsort", "click", "PyYAML", "jsonschema", "tqdm"],
18+
extras_require=dict(dandi=["dandi>=0.39.2"]),
1819
entry_points={"console_scripts": ["nwbinspector=nwbinspector.nwbinspector:inspect_all_cli"]},
1920
license="BSD-3-Clause",
2021
)

0 commit comments

Comments
 (0)