Skip to content
Snippets Groups Projects
Commit b5ab4482 authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

Assertions can be disabled via a "disabled" context manager function.

parent f4cda5e1
No related branches found
No related tags found
No related merge requests found
...@@ -6,22 +6,72 @@ ...@@ -6,22 +6,72 @@
# Paul McCarthy <pauldmccarthy@gmail.com> # Paul McCarthy <pauldmccarthy@gmail.com>
# #
"""This module contains a handful of miscellaneous assertion routines. """This module contains a handful of miscellaneous assertion routines.
.. autosummary::
:nosignatures:
assertFileExists
assertIsNifti3D
assertIsNifti4D
assertIsNifti
assertNiftiShape
assertIsSurfGifti
assertIsFuncGifti
assertIsMelodicDir
The :func:`disabled` context manager can be used to temporarily disable
assertion checks.
""" """
import os.path as op import os.path as op
import contextlib
import nibabel as nib import nibabel as nib
import fsl.utils.ensure as ensure import fsl.utils.ensure as ensure
import fsl.data.melodicanalysis as fslma import fsl.data.melodicanalysis as fslma
_DISABLE_ASSERTIONS = False
"""
"""
@contextlib.contextmanager
def disabled():
"""Context manager which allows assertion checks to be temporarily
disabled.
"""
global _DISABLE_ASSERTIONS
oldval = _DISABLE_ASSERTIONS
_DISABLE_ASSERTIONS = True
try:
yield
finally:
_DISABLE_ASSERTIONS = oldval
def _canDisable(func):
"""Decorator used on assertion functions, allowing them to be disabled
via the :func:`disabled` context manager.
"""
def wrapper(*args, **kwargs):
if not _DISABLE_ASSERTIONS:
return func(*args, **kwargs)
return wrapper
@_canDisable
def assertFileExists(*args): def assertFileExists(*args):
"""Raise an exception if the specified file/folder/s do not exist.""" """Raise an exception if the specified file/folder/s do not exist."""
for f in args: for f in args:
assert op.exists(f), 'file/folder does not exist: {}'.format(f) assert op.exists(f), 'file/folder does not exist: {}'.format(f)
@_canDisable
def assertIsNifti3D(*args): def assertIsNifti3D(*args):
"""Raise an exception if the specified file/s are not 3D nifti.""" """Raise an exception if the specified file/s are not 3D nifti."""
for f in args: for f in args:
...@@ -31,6 +81,7 @@ def assertIsNifti3D(*args): ...@@ -31,6 +81,7 @@ def assertIsNifti3D(*args):
'incorrect shape for 3D nifti: {}:{}'.format(d.shape, f) 'incorrect shape for 3D nifti: {}:{}'.format(d.shape, f)
@_canDisable
def assertIsNifti4D(*args): def assertIsNifti4D(*args):
"""Raise an exception if the specified file/s are not 4D nifti.""" """Raise an exception if the specified file/s are not 4D nifti."""
for f in args: for f in args:
...@@ -40,6 +91,7 @@ def assertIsNifti4D(*args): ...@@ -40,6 +91,7 @@ def assertIsNifti4D(*args):
'incorrect shape for 4D nifti: {}:{}'.format(d.shape, f) 'incorrect shape for 4D nifti: {}:{}'.format(d.shape, f)
@_canDisable
def assertIsNifti(*args): def assertIsNifti(*args):
"""Raise an exception if the specified file/s are not nifti.""" """Raise an exception if the specified file/s are not nifti."""
for f in args: for f in args:
...@@ -51,6 +103,7 @@ def assertIsNifti(*args): ...@@ -51,6 +103,7 @@ def assertIsNifti(*args):
'file must be a nifti (.nii or .nii.gz): {}'.format(f) 'file must be a nifti (.nii or .nii.gz): {}'.format(f)
@_canDisable
def assertNiftiShape(shape, *args): def assertNiftiShape(shape, *args):
"""Raise an exception if the specified nifti/s are not specified shape.""" """Raise an exception if the specified nifti/s are not specified shape."""
for fname in args: for fname in args:
...@@ -60,6 +113,7 @@ def assertNiftiShape(shape, *args): ...@@ -60,6 +113,7 @@ def assertNiftiShape(shape, *args):
shape, d.shape, fname) shape, d.shape, fname)
@_canDisable
def assertIsSurfGifti(*args): def assertIsSurfGifti(*args):
"""Raise an exception if the specified file/s are not surface gifti.""" """Raise an exception if the specified file/s are not surface gifti."""
for fname in args: for fname in args:
...@@ -67,6 +121,7 @@ def assertIsSurfGifti(*args): ...@@ -67,6 +121,7 @@ def assertIsSurfGifti(*args):
'file must be a surface gifti (surf.gii): {}'.format(fname) 'file must be a surface gifti (surf.gii): {}'.format(fname)
@_canDisable
def assertIsFuncGifti(*args): def assertIsFuncGifti(*args):
"""Raise an exception if the specified file/s are not functional gifti.""" """Raise an exception if the specified file/s are not functional gifti."""
for fname in args: for fname in args:
...@@ -74,6 +129,7 @@ def assertIsFuncGifti(*args): ...@@ -74,6 +129,7 @@ def assertIsFuncGifti(*args):
'file must be a functional gifti (func.gii): {}'.format(fname) 'file must be a functional gifti (func.gii): {}'.format(fname)
@_canDisable
def assertIsMelodicDir(path): def assertIsMelodicDir(path):
"""Raise an exception if the specified path is not a melodic directory. """Raise an exception if the specified path is not a melodic directory.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment