Skip to content

Commit 21d08ee

Browse files
authored
Add a mechanism for user to know the availability of cucim.CuImage (#107)
- Add `cucim.is_available()` method Users can query if a specific module is available or not. For example, if cucim.clara module which requires C shared library is not available: ``` >>> import cucim >>> cucim.is_available() False >>> cucim.is_available("skimage") True >>> cucim.is_available("core") True >>> cucim.is_available("clara") False ``` Resolves #104 Supports Project-MONAI/MONAI#2987 Authors: - Gigon Bae (https://github.com/gigony) Approvers: - Gregory R. Lee (https://github.com/grlee77) - https://github.com/jakirkham URL: #107
1 parent 68f6042 commit 21d08ee

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

python/cucim/src/cucim/__init__.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,49 @@
3030
Functions from scikit-image.
3131
3232
"""
33+
_is_cupy_available = False
34+
_is_clara_available = False
3335

3436
# Try to import cupy first.
3537
# If cucim.clara package is imported first, you may see the following error when running on CUDA 10.x (#44)
3638
# python3: Relink `/usr/lib/x86_64-linux-gnu/libnccl.so.2.8.3' with `/lib/x86_64-linux-gnu/librt.so.1' for IFUNC symbol `clock_gettime'
3739
# Segmentation fault
3840
try:
3941
import cupy
42+
_is_cupy_available = True
4043
except ImportError:
4144
pass
4245

4346
try:
4447
from .clara import CuImage, __version__, cli
48+
_is_clara_available = True
4549
except ImportError:
4650
from ._version import get_versions
4751
__version__ = get_versions()['version']
4852
del get_versions
4953
del _version
54+
55+
56+
def is_available(module_name: str = "") -> bool:
57+
"""Check if a specific module is available.
58+
59+
If module_name is not specified, returns True if all of the modules are
60+
available.
61+
62+
Parameters
63+
----------
64+
module_name : str
65+
Name of the module to check. (e.g. "skimage", "core", and "clara")
66+
67+
Returns
68+
-------
69+
bool
70+
True if the module is available, False otherwise.
71+
72+
"""
73+
if module_name in ("skimage", "core"):
74+
return _is_cupy_available
75+
elif module_name == 'clara':
76+
return _is_clara_available
77+
else:
78+
return _is_cupy_available and _is_clara_available
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#
2+
# Copyright (c) 2021, NVIDIA CORPORATION.
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
from unittest.mock import patch
16+
17+
18+
def test_is_available():
19+
with patch('cucim._is_cupy_available', False):
20+
with patch('cucim._is_clara_available', False):
21+
import cucim
22+
assert cucim.is_available() is False
23+
assert cucim.is_available("skimage") is False
24+
assert cucim.is_available("clara") is False
25+
assert cucim.is_available("unknown") is False
26+
with patch('cucim._is_clara_available', True):
27+
import cucim
28+
assert cucim.is_available() is False
29+
assert cucim.is_available("skimage") is False
30+
assert cucim.is_available("clara") is True
31+
assert cucim.is_available("unknown") is False
32+
33+
with patch('cucim._is_cupy_available', True):
34+
with patch('cucim._is_clara_available', False):
35+
import cucim
36+
assert cucim.is_available() is False
37+
assert cucim.is_available("skimage") is True
38+
assert cucim.is_available("clara") is False
39+
assert cucim.is_available("unknown") is False
40+
with patch('cucim._is_clara_available', True):
41+
import cucim
42+
assert cucim.is_available() is True
43+
assert cucim.is_available("skimage") is True
44+
assert cucim.is_available("clara") is True
45+
assert cucim.is_available("unknown") is True

run

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ test_python() {
721721

722722
local testsuite=""
723723
local testsuite_unit_skimage="src"
724-
local testsuite_unit_clara="tests/unit/clara"
724+
local testsuite_unit_clara="tests/unit"
725725
local testsuite_performance="tests/performance"
726726

727727
install_python_test_deps_

0 commit comments

Comments
 (0)