Skip to content

Commit

Permalink
Merge pull request #520 from carterbox/lib-warnings
Browse files Browse the repository at this point in the history
DOC: Make it clearer which shared libraries are optional
  • Loading branch information
carterbox committed Sep 18, 2020
2 parents db0dbea + aada2df commit b9b0f4f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
26 changes: 13 additions & 13 deletions source/tomopy/util/extern/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@
import ctypes
import os
import sys
import logging
import warnings

logger = logging.getLogger(__name__)


def c_shared_lib(lib_name, do_warn=True):
def c_shared_lib(lib_name, error=True):
"""Get the path and import the C-shared library."""
load_dll = ctypes.cdll.LoadLibrary
ext = '.so'
Expand All @@ -67,17 +65,19 @@ def c_shared_lib(lib_name, do_warn=True):
sharedlib = os.path.join(base_path, '%s%s' % (lib_name, ext))
if os.path.exists(sharedlib):
return load_dll(sharedlib)
# cannot find shared lib:
if do_warn is True:
logger.warning(
'OSError: ' +
'The following shared lib is missing!\n{}'.format(sharedlib))
if error:
raise ModuleNotFoundError(
f'The following shared library is missing:\n{sharedlib}')
warnings.warn(
'Some compiled functions are unavailable because an optional shared'
f' library is missing:\n{sharedlib}', ImportWarning)


def MissingLibrary(function):
print(f"The {function} algorithm is unavailable."
" Check CMake logs to determine if TomoPy was"
" built with dependencies required by this algorithm.")
def _missing_library(function):
raise ModuleNotFoundError(
f"The {function} algorithm is unavailable because its shared library"
" is missing. Check CMake logs to determine if TomoPy was"
" built with all dependencies required by this algorithm.")


from tomopy.util.extern.recon import *
Expand Down
8 changes: 4 additions & 4 deletions source/tomopy/util/extern/accel.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@

import tomopy.util.dtype as dtype
from . import c_shared_lib
from . import MissingLibrary
from . import _missing_library

__author__ = "Doga Gursoy"
__copyright__ = "Copyright (c) 2015, UChicago Argonne, LLC."
__docformat__ = 'restructuredtext en'
__all__ = ['c_accel_mlem',
'c_accel_sirt']

LIB_TOMOPY_ACCEL = c_shared_lib("libtomopy-accel")
LIB_TOMOPY_ACCEL = c_shared_lib("libtomopy-accel", error=False)


def c_accel_mlem(tomo, center, recon, theta, **kwargs):

if LIB_TOMOPY_ACCEL is None:
MissingLibrary("MLEM ACCEL")
_missing_library("MLEM ACCEL")

if len(tomo.shape) == 2:
# no y-axis (only one slice)
Expand Down Expand Up @@ -97,7 +97,7 @@ def c_accel_mlem(tomo, center, recon, theta, **kwargs):
def c_accel_sirt(tomo, center, recon, theta, **kwargs):

if LIB_TOMOPY_ACCEL is None:
return MissingLibrary("SIRT ACCEL")
_missing_library("SIRT ACCEL")

if len(tomo.shape) == 2:
# no y-axis (only one slice)
Expand Down
6 changes: 3 additions & 3 deletions source/tomopy/util/extern/gridrec.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@
"""
import tomopy.util.dtype as dtype
from . import c_shared_lib
from . import MissingLibrary
from . import _missing_library

__author__ = "Doga Gursoy"
__copyright__ = "Copyright (c) 2015, UChicago Argonne, LLC."
__docformat__ = 'restructuredtext en'
__all__ = ['c_gridrec']


LIB_TOMOPY_GRIDREC = c_shared_lib("libtomopy-gridrec")
LIB_TOMOPY_GRIDREC = c_shared_lib("libtomopy-gridrec", error=False)


def c_gridrec(tomo, center, recon, theta, **kwargs):

if LIB_TOMOPY_GRIDREC is None:
return MissingLibrary("gridrec")
_missing_library("gridrec")

if len(tomo.shape) == 2:
# no y-axis (only one slice)
Expand Down
1 change: 1 addition & 0 deletions test/test_tomopy/test_recon/test_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def test_write_center(self):
str('{0:.2f}'.format(cen[m]) + '.tiff'))), True)
shutil.rmtree(dpath)

@unittest.skipUnless(found_mkl, "Requires MKL")
def test_find_center(self):
sim = read_file('sinogram.npy')
ang = np.linspace(0, np.pi, sim.shape[0])
Expand Down

0 comments on commit b9b0f4f

Please sign in to comment.