Skip to content

MRG: defer use of ufunc / memmap test until testing #572

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 1 commit into from
Oct 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion nibabel/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
data_path = abspath(pjoin(dirname(__file__), '..', 'tests', 'data'))


from .np_features import VIRAL_MEMMAP
from .np_features import memmap_after_ufunc

def assert_dt_equal(a, b):
""" Assert two numpy dtype specifiers are equal
Expand Down
16 changes: 10 additions & 6 deletions nibabel/testing/np_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
import numpy as np


def _memmap_after_ufunc():
def memmap_after_ufunc():
""" Return True if ufuncs on memmap arrays always return memmap arrays

This should be True for numpy < 1.12, False otherwise.

Memoize after first call. We do this to avoid having to call this when
importing nibabel.testing, because we cannot depend on the source file
being present - see gh-571.
"""
if memmap_after_ufunc.result is not None:
return memmap_after_ufunc.result
with open(__file__, 'rb') as fobj:
mm_arr = np.memmap(fobj, mode='r', shape=(10,), dtype=np.uint8)
mm_preserved = isinstance(mm_arr + 1, np.memmap)
return mm_preserved

memmap_after_ufunc.result = isinstance(mm_arr + 1, np.memmap)
return memmap_after_ufunc.result

# True if ufunc on memmap always returns a memmap
VIRAL_MEMMAP = _memmap_after_ufunc()
memmap_after_ufunc.result = None
8 changes: 5 additions & 3 deletions nibabel/tests/test_arrayproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from numpy.testing import assert_array_equal, assert_array_almost_equal
from nose.tools import (assert_true, assert_false, assert_equal,
assert_not_equal, assert_raises)
from nibabel.testing import VIRAL_MEMMAP
from nibabel.testing import memmap_after_ufunc

from .test_fileslice import slicer_samples
from .test_openers import patch_indexed_gzip
Expand Down Expand Up @@ -298,6 +298,8 @@ def check_mmap(hdr, offset, proxy_class,
# Whether scaled array memory backed by memory map (regardless of what
# numpy says).
scaled_really_mmap = unscaled_really_mmap and not has_scaling
# Whether ufunc on memmap return memmap
viral_memmap = memmap_after_ufunc()
with InTemporaryDirectory():
with open(fname, 'wb') as fobj:
fobj.write(b' ' * offset)
Expand All @@ -324,9 +326,9 @@ def check_mmap(hdr, offset, proxy_class,
assert_false(back_is_mmap)
else:
assert_equal(unscaled_is_mmap,
VIRAL_MEMMAP or unscaled_really_mmap)
viral_memmap or unscaled_really_mmap)
assert_equal(back_is_mmap,
VIRAL_MEMMAP or scaled_really_mmap)
viral_memmap or scaled_really_mmap)
if scaled_really_mmap:
assert_equal(back_data.mode, expected_mode)
del prox, back_data
Expand Down
5 changes: 3 additions & 2 deletions nibabel/tests/test_spatialimages.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from .test_helpers import bytesio_round_trip
from ..testing import (clear_and_catch_warnings, suppress_warnings,
VIRAL_MEMMAP)
memmap_after_ufunc)
from ..tmpdirs import InTemporaryDirectory
from .. import load as top_load

Expand Down Expand Up @@ -464,6 +464,7 @@ def get_disk_image(self):
def test_load_mmap(self):
# Test memory mapping when loading images
img_klass = self.image_class
viral_memmap = memmap_after_ufunc()
with InTemporaryDirectory():
img, fname, has_scaling = self.get_disk_image()
file_map = img.file_map.copy()
Expand All @@ -485,7 +486,7 @@ def test_load_mmap(self):
# numpies returned a memmap object, even though the array
# has no mmap memory backing. See:
# https://github.com/numpy/numpy/pull/7406
if has_scaling and not VIRAL_MEMMAP:
if has_scaling and not viral_memmap:
expected_mode = None
kwargs = {}
if mmap is not None:
Expand Down