Skip to content

Commit 7cbbab2

Browse files
committed
TEST: Unit tests for ArrayProxy keep_file_open flag, and handling of internal
threading.Lock object.
1 parent 572ab00 commit 7cbbab2

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

nibabel/tests/test_arrayproxy.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@
1212

1313
import warnings
1414

15+
import pickle
1516
from io import BytesIO
1617
from ..tmpdirs import InTemporaryDirectory
1718

1819
import numpy as np
1920

2021
from ..arrayproxy import ArrayProxy, is_proxy, reshape_dataobj
22+
from ..openers import ImageOpener
2123
from ..nifti1 import Nifti1Header
2224

2325
from numpy.testing import assert_array_equal, assert_array_almost_equal
2426
from nose.tools import (assert_true, assert_false, assert_equal,
2527
assert_not_equal, assert_raises)
2628
from nibabel.testing import VIRAL_MEMMAP
29+
import mock
2730

2831
from .test_fileslice import slicer_samples
2932

@@ -327,3 +330,51 @@ def check_mmap(hdr, offset, proxy_class,
327330
# Check invalid values raise error
328331
assert_raises(ValueError, proxy_class, fname, hdr, mmap='rw')
329332
assert_raises(ValueError, proxy_class, fname, hdr, mmap='r+')
333+
334+
335+
def test_keep_file_open():
336+
# Test the behaviour of the keep_file_open __init__ flag.
337+
numopeners = [0]
338+
339+
class CountingImageOpener(ImageOpener):
340+
341+
def __init__(self, *args, **kwargs):
342+
343+
super(CountingImageOpener, self).__init__(*args, **kwargs)
344+
numopeners[0] += 1
345+
346+
fname = 'testdata'
347+
dtype = np.float32
348+
data = np.arange(1000, dtype=np.float32).reshape((10, 10, 10))
349+
with InTemporaryDirectory():
350+
with open(fname, 'wb') as fobj:
351+
fobj.write(data.tostring(order='F'))
352+
with mock.patch('nibabel.arrayproxy.ImageOpener', CountingImageOpener):
353+
proxy_no_kfp = ArrayProxy(fname, ((10, 10, 10), dtype))
354+
proxy_kfp = ArrayProxy(fname, ((10, 10, 10), dtype),
355+
keep_file_open=True)
356+
voxels = np.random.randint(0, 10, (10, 3), dtype=np.uint32)
357+
for i in range(voxels.shape[0]):
358+
x , y, z = [int(c) for c in voxels[i, :]]
359+
assert proxy_no_kfp[x, y, z] == x * 100 + y * 10 + z
360+
assert numopeners[0] == i + 1
361+
numopeners[0] = 0
362+
for i in range(voxels.shape[0]):
363+
x , y, z = [int(c) for c in voxels[i, :]]
364+
assert proxy_kfp[x, y, z] == x * 100 + y * 10 + z
365+
assert numopeners[0] == 1
366+
367+
368+
def test_pickle_lock():
369+
# Test that ArrayProxy can be pickled, and that thread lock is created
370+
371+
def islock(l):
372+
# isinstance doesn't work on threading.Lock?
373+
return hasattr(l, 'acquire') and hasattr(l, 'release')
374+
375+
proxy = ArrayProxy('dummyfile', ((10, 10, 10), np.float32))
376+
assert islock(proxy._lock)
377+
pickled = pickle.dumps(proxy)
378+
unpickled = pickle.loads(pickled)
379+
assert islock(unpickled._lock)
380+
assert proxy._lock is not unpickled._lock

0 commit comments

Comments
 (0)