Skip to content

Commit 2139ce0

Browse files
authored
Merge pull request #572 from matthew-brett/defer_viral_memmap
MRG: defer use of ufunc / memmap test until testing
2 parents b1bf5e7 + 30f8ddd commit 2139ce0

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

Diff for: nibabel/testing/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
data_path = abspath(pjoin(dirname(__file__), '..', 'tests', 'data'))
3333

3434

35-
from .np_features import VIRAL_MEMMAP
35+
from .np_features import memmap_after_ufunc
3636

3737
def assert_dt_equal(a, b):
3838
""" Assert two numpy dtype specifiers are equal

Diff for: nibabel/testing/np_features.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@
44
import numpy as np
55

66

7-
def _memmap_after_ufunc():
7+
def memmap_after_ufunc():
88
""" Return True if ufuncs on memmap arrays always return memmap arrays
99
1010
This should be True for numpy < 1.12, False otherwise.
11+
12+
Memoize after first call. We do this to avoid having to call this when
13+
importing nibabel.testing, because we cannot depend on the source file
14+
being present - see gh-571.
1115
"""
16+
if memmap_after_ufunc.result is not None:
17+
return memmap_after_ufunc.result
1218
with open(__file__, 'rb') as fobj:
1319
mm_arr = np.memmap(fobj, mode='r', shape=(10,), dtype=np.uint8)
14-
mm_preserved = isinstance(mm_arr + 1, np.memmap)
15-
return mm_preserved
16-
20+
memmap_after_ufunc.result = isinstance(mm_arr + 1, np.memmap)
21+
return memmap_after_ufunc.result
1722

18-
# True if ufunc on memmap always returns a memmap
19-
VIRAL_MEMMAP = _memmap_after_ufunc()
23+
memmap_after_ufunc.result = None

Diff for: nibabel/tests/test_arrayproxy.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from numpy.testing import assert_array_equal, assert_array_almost_equal
3131
from nose.tools import (assert_true, assert_false, assert_equal,
3232
assert_not_equal, assert_raises)
33-
from nibabel.testing import VIRAL_MEMMAP
33+
from nibabel.testing import memmap_after_ufunc
3434

3535
from .test_fileslice import slicer_samples
3636
from .test_openers import patch_indexed_gzip
@@ -298,6 +298,8 @@ def check_mmap(hdr, offset, proxy_class,
298298
# Whether scaled array memory backed by memory map (regardless of what
299299
# numpy says).
300300
scaled_really_mmap = unscaled_really_mmap and not has_scaling
301+
# Whether ufunc on memmap return memmap
302+
viral_memmap = memmap_after_ufunc()
301303
with InTemporaryDirectory():
302304
with open(fname, 'wb') as fobj:
303305
fobj.write(b' ' * offset)
@@ -324,9 +326,9 @@ def check_mmap(hdr, offset, proxy_class,
324326
assert_false(back_is_mmap)
325327
else:
326328
assert_equal(unscaled_is_mmap,
327-
VIRAL_MEMMAP or unscaled_really_mmap)
329+
viral_memmap or unscaled_really_mmap)
328330
assert_equal(back_is_mmap,
329-
VIRAL_MEMMAP or scaled_really_mmap)
331+
viral_memmap or scaled_really_mmap)
330332
if scaled_really_mmap:
331333
assert_equal(back_data.mode, expected_mode)
332334
del prox, back_data

Diff for: nibabel/tests/test_spatialimages.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from .test_helpers import bytesio_round_trip
2727
from ..testing import (clear_and_catch_warnings, suppress_warnings,
28-
VIRAL_MEMMAP)
28+
memmap_after_ufunc)
2929
from ..tmpdirs import InTemporaryDirectory
3030
from .. import load as top_load
3131

@@ -464,6 +464,7 @@ def get_disk_image(self):
464464
def test_load_mmap(self):
465465
# Test memory mapping when loading images
466466
img_klass = self.image_class
467+
viral_memmap = memmap_after_ufunc()
467468
with InTemporaryDirectory():
468469
img, fname, has_scaling = self.get_disk_image()
469470
file_map = img.file_map.copy()
@@ -485,7 +486,7 @@ def test_load_mmap(self):
485486
# numpies returned a memmap object, even though the array
486487
# has no mmap memory backing. See:
487488
# https://github.com/numpy/numpy/pull/7406
488-
if has_scaling and not VIRAL_MEMMAP:
489+
if has_scaling and not viral_memmap:
489490
expected_mode = None
490491
kwargs = {}
491492
if mmap is not None:

0 commit comments

Comments
 (0)