From d5e90dce0792c95af234da8ebbcf91f27ac0640f Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Tue, 30 Jul 2019 15:46:54 -0400 Subject: [PATCH 1/3] TEST: Use package-wide setup and teardown to adjust numpy print options --- nibabel/__init__.py | 30 +++++++++++++++++++++--------- nibabel/affines.py | 1 - nibabel/casting.py | 1 - nibabel/nicom/dwiparams.py | 1 - nibabel/nifti1.py | 1 - nibabel/quaternions.py | 1 - 6 files changed, 21 insertions(+), 14 deletions(-) diff --git a/nibabel/__init__.py b/nibabel/__init__.py index 20f1aafefc..5f571ad12a 100644 --- a/nibabel/__init__.py +++ b/nibabel/__init__.py @@ -35,16 +35,28 @@ For more detailed information see the :ref:`manual`. """ - -def setup_test(): - """ Set numpy print options to "legacy" for new versions of numpy - - If imported into a file, nosetest will run this before any doctests. - """ - import numpy +# Package-wide test setup and teardown +# Numpy changed print options in 1.14; we can update docstrings and remove +# these when our minimum for building docs exceeds that +_save_printopts = None + +def setup_package(): + """ Set numpy print style to legacy="1.13" for newer versions of numpy """ + import nibabel as nb + import numpy as np from distutils.version import LooseVersion - if LooseVersion(numpy.__version__) >= LooseVersion('1.14'): - numpy.set_printoptions(legacy="1.13") + if nb._save_printopts is None: + nb._save_printopts = np.get_printoptions().get('legacy') + if LooseVersion(np.__version__) >= LooseVersion('1.14'): + np.set_printoptions(legacy="1.13") + +def teardown_package(): + """ Reset print options when tests finish """ + import nibabel as nb + import numpy as np + if nb._save_printopts is not None: + np.set_printoptions(legacy=nb._save_printopts) + nb._save_printopts = None # module imports diff --git a/nibabel/affines.py b/nibabel/affines.py index 057233e454..07154089a1 100644 --- a/nibabel/affines.py +++ b/nibabel/affines.py @@ -6,7 +6,6 @@ import numpy as np from six.moves import reduce -from . import setup_test # noqa class AffineError(ValueError): diff --git a/nibabel/casting.py b/nibabel/casting.py index 3709ee1dea..89be788da5 100644 --- a/nibabel/casting.py +++ b/nibabel/casting.py @@ -8,7 +8,6 @@ from platform import processor, machine import numpy as np -from . import setup_test # noqa class CastingError(Exception): diff --git a/nibabel/nicom/dwiparams.py b/nibabel/nicom/dwiparams.py index 1fda89b0da..e9d05c0d57 100644 --- a/nibabel/nicom/dwiparams.py +++ b/nibabel/nicom/dwiparams.py @@ -21,7 +21,6 @@ ''' import numpy as np import numpy.linalg as npl -from .. import setup_test as setup_module # noqa def B2q(B, tol=None): diff --git a/nibabel/nifti1.py b/nibabel/nifti1.py index a050195234..c2d409e81a 100644 --- a/nibabel/nifti1.py +++ b/nibabel/nifti1.py @@ -28,7 +28,6 @@ from .spm99analyze import SpmAnalyzeHeader from .casting import have_binary128 from .pydicom_compat import have_dicom, pydicom as pdcm -from . import setup_test # noqa # nifti1 flat header definition for Analyze-like first 348 bytes # first number in comments indicates offset in file header in bytes diff --git a/nibabel/quaternions.py b/nibabel/quaternions.py index f9318a93f2..adc2367238 100644 --- a/nibabel/quaternions.py +++ b/nibabel/quaternions.py @@ -27,7 +27,6 @@ import math import numpy as np -from . import setup_test # noqa MAX_FLOAT = np.maximum_sctype(np.float) FLOAT_EPS = np.finfo(np.float).eps From 4d7dd36854b6b519fd28e363d704ad5a1acb17d7 Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Tue, 30 Jul 2019 19:42:13 -0400 Subject: [PATCH 2/3] MAINT: Use a more general _test_state structure for setup/teardown --- nibabel/__init__.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/nibabel/__init__.py b/nibabel/__init__.py index 5f571ad12a..f407b9ed39 100644 --- a/nibabel/__init__.py +++ b/nibabel/__init__.py @@ -36,27 +36,27 @@ """ # Package-wide test setup and teardown -# Numpy changed print options in 1.14; we can update docstrings and remove -# these when our minimum for building docs exceeds that -_save_printopts = None +_test_states = { + # Numpy changed print options in 1.14; we can update docstrings and remove + # these when our minimum for building docs exceeds that + 'legacy_printopt': None, + } def setup_package(): """ Set numpy print style to legacy="1.13" for newer versions of numpy """ - import nibabel as nb import numpy as np from distutils.version import LooseVersion - if nb._save_printopts is None: - nb._save_printopts = np.get_printoptions().get('legacy') if LooseVersion(np.__version__) >= LooseVersion('1.14'): + if _test_states.get('legacy_printopt') is None: + _test_states['legacy_printopt'] = np.get_printoptions().get('legacy') np.set_printoptions(legacy="1.13") def teardown_package(): """ Reset print options when tests finish """ - import nibabel as nb import numpy as np - if nb._save_printopts is not None: - np.set_printoptions(legacy=nb._save_printopts) - nb._save_printopts = None + if _test_states.get('legacy_printopt') is not None: + np.set_printoptions(legacy=_test_states['legacy_printopt']) + _test_states['legacy_printopt'] = None # module imports From 4cccd86cd936436110c539a0946513ce6fa33fa0 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Wed, 31 Jul 2019 10:42:45 -0400 Subject: [PATCH 3/3] Apply suggestions from code review Co-Authored-By: Yaroslav Halchenko --- nibabel/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nibabel/__init__.py b/nibabel/__init__.py index f407b9ed39..20fdad3469 100644 --- a/nibabel/__init__.py +++ b/nibabel/__init__.py @@ -55,8 +55,7 @@ def teardown_package(): """ Reset print options when tests finish """ import numpy as np if _test_states.get('legacy_printopt') is not None: - np.set_printoptions(legacy=_test_states['legacy_printopt']) - _test_states['legacy_printopt'] = None + np.set_printoptions(legacy=_test_states.pop('legacy_printopt')) # module imports