Skip to content

Commit 961f882

Browse files
committed
TEST: New test which checks default behaviour of the ArrayProxy keep_file_open
flag. Other minor refactorings
1 parent 0a111ca commit 961f882

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

nibabel/tests/test_arrayproxy.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from __future__ import division, print_function, absolute_import
1212

1313
import warnings
14+
import gzip
1415

1516
import pickle
1617
from io import BytesIO
@@ -332,16 +333,22 @@ def check_mmap(hdr, offset, proxy_class,
332333
assert_raises(ValueError, proxy_class, fname, hdr, mmap='r+')
333334

334335

335-
def test_keep_file_open():
336-
# Test the behaviour of the keep_file_open __init__ flag.
337-
numopeners = [0]
338-
class CountingImageOpener(ImageOpener):
336+
# An image opener class which counts how many instances of itself have been
337+
# created
338+
class CountingImageOpener(ImageOpener):
339339

340-
def __init__(self, *args, **kwargs):
340+
numOpeners = 0
341341

342-
super(CountingImageOpener, self).__init__(*args, **kwargs)
343-
numopeners[0] += 1
342+
def __init__(self, *args, **kwargs):
344343

344+
super(CountingImageOpener, self).__init__(*args, **kwargs)
345+
CountingImageOpener.numOpeners += 1
346+
347+
348+
def test_keep_file_open_true_false():
349+
# Test the behaviour of the keep_file_open __init__ flag, when it is set to
350+
# True or False.
351+
CountingImageOpener.numOpeners = 0
345352
fname = 'testdata'
346353
dtype = np.float32
347354
data = np.arange(1000, dtype=dtype).reshape((10, 10, 10))
@@ -354,22 +361,25 @@ def __init__(self, *args, **kwargs):
354361
# handle on every data access.
355362
with mock.patch('nibabel.arrayproxy.ImageOpener', CountingImageOpener):
356363
proxy_no_kfp = ArrayProxy(fname, ((10, 10, 10), dtype))
364+
assert not proxy_no_kfp._keep_file_open
357365
for i in range(voxels.shape[0]):
358366
x , y, z = [int(c) for c in voxels[i, :]]
359367
assert proxy_no_kfp[x, y, z] == x * 100 + y * 10 + z
360-
assert numopeners[0] == i + 1
361-
numopeners[0] = 0
368+
assert CountingImageOpener.numOpeners == i + 1
369+
CountingImageOpener.numOpeners = 0
362370
proxy_kfp = ArrayProxy(fname, ((10, 10, 10), dtype),
363371
keep_file_open=True)
372+
assert proxy_kfp._keep_file_open
364373
for i in range(voxels.shape[0]):
365374
x , y, z = [int(c) for c in voxels[i, :]]
366375
assert proxy_kfp[x, y, z] == x * 100 + y * 10 + z
367-
assert numopeners[0] == 1
376+
assert CountingImageOpener.numOpeners == 1
368377
# Test that the keep_file_open flag has no effect if an open file
369378
# handle is passed in
370379
with open(fname, 'rb') as fobj:
371380
proxy_no_kfp = ArrayProxy(fobj, ((10, 10, 10), dtype),
372381
keep_file_open=False)
382+
assert not proxy_no_kfp._keep_file_open
373383
for i in range(voxels.shape[0]):
374384
assert proxy_no_kfp[x, y, z] == x * 100 + y * 10 + z
375385
assert not fobj.closed
@@ -378,6 +388,7 @@ def __init__(self, *args, **kwargs):
378388
assert not fobj.closed
379389
proxy_kfp = ArrayProxy(fobj, ((10, 10, 10), dtype),
380390
keep_file_open=True)
391+
assert not proxy_kfp._keep_file_open
381392
for i in range(voxels.shape[0]):
382393
assert proxy_kfp[x, y, z] == x * 100 + y * 10 + z
383394
assert not fobj.closed
@@ -386,6 +397,27 @@ def __init__(self, *args, **kwargs):
386397
assert not fobj.closed
387398

388399

400+
def test_keep_file_open_default():
401+
# Test the behaviour of the keep_file_open __init__ flag, when it is set to
402+
# its default value
403+
dtype = np.float32
404+
data = np.arange(1000, dtype=dtype).reshape((10, 10, 10))
405+
voxels = np.random.randint(0, 10, (10, 3))
406+
mockmod = mock.MagicMock()
407+
with InTemporaryDirectory():
408+
fname = 'testdata.gz'
409+
with gzip.open(fname, 'wb') as fobj:
410+
fobj.write(data.tostring(order='F'))
411+
# If have_indexed_gzip, then keep_file_open should be True
412+
with mock.patch.dict('sys.modules', {'indexed_gzip' : mockmod}), \
413+
mock.patch('indexed_gzip.SafeIndexedGzipFile', gzip.GzipFile):
414+
proxy = ArrayProxy(fname, ((10, 10, 10), dtype))
415+
assert proxy._keep_file_open
416+
# If no have_indexed_gzip, then keep_file_open should be False
417+
with mock.patch.dict('sys.modules', {'indexed_gzip' : None}):
418+
proxy = ArrayProxy(fname, ((10, 10, 10), dtype))
419+
assert not proxy._keep_file_open
420+
389421
def test_pickle_lock():
390422
# Test that ArrayProxy can be pickled, and that thread lock is created
391423

nibabel/tests/test_openers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def test_Opener_gzip_type():
103103
fname = 'test.gz'
104104
mockmod = mock.MagicMock()
105105

106-
class MockClass(object):
106+
class MockIGZFile(object):
107107
def __init__(self, *args, **kwargs):
108108
pass
109109

@@ -120,8 +120,8 @@ def __init__(self, *args, **kwargs):
120120

121121
# test with indexd_gzip present
122122
with mock.patch.dict('sys.modules', {'indexed_gzip' : mockmod}), \
123-
mock.patch('indexed_gzip.SafeIndexedGzipFile', MockClass):
124-
assert isinstance(Opener(fname, mode='rb').fobj, MockClass)
123+
mock.patch('indexed_gzip.SafeIndexedGzipFile', MockIGZFile):
124+
assert isinstance(Opener(fname, mode='rb').fobj, MockIGZFile)
125125
assert isinstance(Opener(fname, mode='wb').fobj, GzipFile)
126126

127127

0 commit comments

Comments
 (0)