Skip to content

Commit ae053f5

Browse files
committed
TEST: Test mmap parameter acceptance
1 parent 7ff785f commit ae053f5

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

nibabel/tests/test_image_api.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,34 @@ def validate_shape_deprecated(self, imaker, params):
416416
assert_equal(img.get_shape(), params['shape'])
417417
assert_equal(len(w), 1)
418418

419+
def validate_mmap_parameter(self, imaker, params):
420+
img = imaker()
421+
fname = img.get_filename()
422+
with InTemporaryDirectory():
423+
# Load test files with mmap parameters
424+
# or
425+
# Save a generated file so we can test it
426+
if fname is None:
427+
# Skip only formats we can't write
428+
if not img.rw or not img.valid_exts:
429+
return
430+
fname = 'image' + img.valid_exts[0]
431+
img.to_filename(fname)
432+
rt_img = img.__class__.from_filename(fname, mmap=True)
433+
assert_almost_equal(img.get_fdata(), rt_img.get_fdata())
434+
rt_img = img.__class__.from_filename(fname, mmap=False)
435+
assert_almost_equal(img.get_fdata(), rt_img.get_fdata())
436+
rt_img = img.__class__.from_filename(fname, mmap='c')
437+
assert_almost_equal(img.get_fdata(), rt_img.get_fdata())
438+
rt_img = img.__class__.from_filename(fname, mmap='r')
439+
assert_almost_equal(img.get_fdata(), rt_img.get_fdata())
440+
# r+ is specifically not valid for images
441+
assert_raises(ValueError,
442+
img.__class__.from_filename, fname, mmap='r+')
443+
assert_raises(ValueError,
444+
img.__class__.from_filename, fname, mmap='invalid')
445+
del rt_img # to allow windows to delete the directory
446+
419447

420448
class HeaderShapeMixin(object):
421449
""" Tests that header shape can be set and got

0 commit comments

Comments
 (0)