Skip to content

TEST: Use package-wide setup and teardown to adjust numpy print options #785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions nibabel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so it is not "printopts" really but a "legacyprintopt", right?

FWIW -- ATM it is just a single variable here. In DataLad we setup/teardown a few, so I just made a _test_states dict variable: https://github.com/datalad/datalad/blob/master/datalad/__init__.py#L92 to store all those in without breeding them at the top module level.

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
Expand Down
1 change: 0 additions & 1 deletion nibabel/affines.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import numpy as np

from six.moves import reduce
from . import setup_test # noqa


class AffineError(ValueError):
Expand Down
1 change: 0 additions & 1 deletion nibabel/casting.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from platform import processor, machine

import numpy as np
from . import setup_test # noqa


class CastingError(Exception):
Expand Down
1 change: 0 additions & 1 deletion nibabel/nicom/dwiparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 0 additions & 1 deletion nibabel/nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion nibabel/quaternions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down