2020
2121import numpy as np
2222
23- from ..arrayproxy import (ArrayProxy , KEEP_FILE_OPEN_DEFAULT , is_proxy ,
24- reshape_dataobj )
23+ from ..arrayproxy import (ArrayProxy , is_proxy , reshape_dataobj )
2524from ..openers import ImageOpener
2625from ..nifti1 import Nifti1Header
2726
@@ -342,15 +341,20 @@ def check_mmap(hdr, offset, proxy_class,
342341# An image opener class which counts how many instances of itself have been
343342# created
344343class CountingImageOpener (ImageOpener ):
345-
346344 num_openers = 0
347-
348345 def __init__ (self , * args , ** kwargs ):
349-
350346 super (CountingImageOpener , self ).__init__ (* args , ** kwargs )
351347 CountingImageOpener .num_openers += 1
352348
353349
350+ def _count_ImageOpeners (proxy , data , voxels ):
351+ CountingImageOpener .num_openers = 0
352+ for i in range (voxels .shape [0 ]):
353+ x , y , z = [int (c ) for c in voxels [i , :]]
354+ assert proxy [x , y , z ] == x * 100 + y * 10 + z
355+ return CountingImageOpener .num_openers
356+
357+
354358def test_keep_file_open_true_false_invalid ():
355359 # Test the behaviour of the keep_file_open __init__ flag, when it is set to
356360 # True or False.
@@ -369,18 +373,11 @@ def test_keep_file_open_true_false_invalid():
369373 proxy_no_kfp = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
370374 keep_file_open = False )
371375 assert not proxy_no_kfp ._keep_file_open
372- for i in range (voxels .shape [0 ]):
373- x , y , z = [int (c ) for c in voxels [i , :]]
374- assert proxy_no_kfp [x , y , z ] == x * 100 + y * 10 + z
375- assert CountingImageOpener .num_openers == i + 1
376- CountingImageOpener .num_openers = 0
376+ assert _count_ImageOpeners (proxy_no_kfp , data , voxels ) == 10
377377 proxy_kfp = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
378378 keep_file_open = True )
379379 assert proxy_kfp ._keep_file_open
380- for i in range (voxels .shape [0 ]):
381- x , y , z = [int (c ) for c in voxels [i , :]]
382- assert proxy_kfp [x , y , z ] == x * 100 + y * 10 + z
383- assert CountingImageOpener .num_openers == 1
380+ assert _count_ImageOpeners (proxy_kfp , data , voxels ) == 1
384381 del proxy_kfp
385382 del proxy_no_kfp
386383 # Test that the keep_file_open flag has no effect if an open file
@@ -389,20 +386,15 @@ def test_keep_file_open_true_false_invalid():
389386 for kfo in (True , False , 'auto' ):
390387 proxy = ArrayProxy (fobj , ((10 , 10 , 10 ), dtype ),
391388 keep_file_open = kfo )
392- if kfo == 'auto' :
393- kfo = False
394- assert proxy ._keep_file_open is kfo
389+ assert proxy ._keep_file_open is False
395390 for i in range (voxels .shape [0 ]):
391+ x , y , z = [int (c ) for c in voxels [i , :]]
396392 assert proxy [x , y , z ] == x * 100 + y * 10 + z
397393 assert not fobj .closed
398394 del proxy
399395 assert not fobj .closed
400396 assert fobj .closed
401397 # Test invalid values of keep_file_open
402- with assert_raises (ValueError ):
403- ArrayProxy (fname , ((10 , 10 , 10 ), dtype ), keep_file_open = 0 )
404- with assert_raises (ValueError ):
405- ArrayProxy (fname , ((10 , 10 , 10 ), dtype ), keep_file_open = 1 )
406398 with assert_raises (ValueError ):
407399 ArrayProxy (fname , ((10 , 10 , 10 ), dtype ), keep_file_open = 55 )
408400 with assert_raises (ValueError ):
@@ -411,69 +403,80 @@ def test_keep_file_open_true_false_invalid():
411403 ArrayProxy (fname , ((10 , 10 , 10 ), dtype ), keep_file_open = 'cauto' )
412404
413405
414- @contextlib .contextmanager
415- def patch_keep_file_open_default (value ):
416- # Patch arrayproxy.KEEP_FILE_OPEN_DEFAULT with the given value
417- with mock .patch ('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT' , value ):
418- yield
419-
420-
421406def test_keep_file_open_auto ():
422407 # Test the behaviour of the keep_file_open __init__ flag, when it is set to
423- # 'auto'
408+ # 'auto'.
409+ # if indexed_gzip is present, the ArrayProxy should persist its ImageOpener.
410+ # Otherwise the ArrayProxy should drop openers.
424411 dtype = np .float32
425- data = np .arange (1000 , dtype = dtype ).reshape ((10 , 10 , 10 ))
412+ data = np .arange (1000 , dtype = dtype ).reshape ((10 , 10 , 10 ))
413+ voxels = np .random .randint (0 , 10 , (10 , 3 ))
426414 with InTemporaryDirectory ():
427415 fname = 'testdata.gz'
428416 with gzip .open (fname , 'wb' ) as fobj :
429417 fobj .write (data .tostring (order = 'F' ))
430- # If have_indexed_gzip, then keep_file_open should be True
431- with patch_indexed_gzip (True ):
418+ # If have_indexed_gzip, then the arrayproxy should create one
419+ # ImageOpener
420+ with patch_indexed_gzip (True ), \
421+ mock .patch ('nibabel.arrayproxy.ImageOpener' , CountingImageOpener ):
422+ CountingImageOpener .num_openers = 0
432423 proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
433424 keep_file_open = 'auto' )
434- assert proxy ._keep_file_open
425+ assert proxy ._keep_file_open == 'auto'
426+ assert _count_ImageOpeners (proxy , data , voxels ) == 1
435427 # If no have_indexed_gzip, then keep_file_open should be False
436- with patch_indexed_gzip (False ):
428+ with patch_indexed_gzip (False ), \
429+ mock .patch ('nibabel.arrayproxy.ImageOpener' , CountingImageOpener ):
430+ CountingImageOpener .num_openers = 0
437431 proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
438432 keep_file_open = 'auto' )
439- assert not proxy ._keep_file_open
433+ assert proxy ._keep_file_open is False
434+ assert _count_ImageOpeners (proxy , data , voxels ) == 10
435+
436+
437+ @contextlib .contextmanager
438+ def patch_keep_file_open_default (value ):
439+ # Patch arrayproxy.KEEP_FILE_OPEN_DEFAULT with the given value
440+ with mock .patch ('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT' , value ):
441+ yield
440442
441443
442444def test_keep_file_open_default ():
443445 # Test the behaviour of the keep_file_open __init__ flag, when the
444446 # arrayproxy.KEEP_FILE_OPEN_DEFAULT value is changed
445447 dtype = np .float32
446- data = np .arange (1000 , dtype = dtype ).reshape ((10 , 10 , 10 ))
448+ data = np .arange (1000 , dtype = dtype ).reshape ((10 , 10 , 10 ))
447449 with InTemporaryDirectory ():
448450 fname = 'testdata.gz'
449451 with gzip .open (fname , 'wb' ) as fobj :
450452 fobj .write (data .tostring (order = 'F' ))
451- # The default value of KEEP_FILE_OPEN_DEFAULT should cause
452- # keep_file_open to be False, regardless of whether or not indexed_gzip
453- # is present
454- assert KEEP_FILE_OPEN_DEFAULT is False
455- with patch_indexed_gzip (False ):
456- proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
457- assert not proxy ._keep_file_open
458- with patch_indexed_gzip (True ):
459- proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
460- assert not proxy ._keep_file_open
461- # KEEP_FILE_OPEN_DEFAULT=True should cause keep_file_open to be True,
462- # regardless of whether or not indexed_gzip is present
463- with patch_keep_file_open_default (True ), patch_indexed_gzip (True ):
464- proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
465- assert proxy ._keep_file_open
466- with patch_keep_file_open_default (True ), patch_indexed_gzip (False ):
467- proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
468- assert proxy ._keep_file_open
469- # KEEP_FILE_OPEN_DEFAULT=auto should cause keep_file_open to be True
470- # or False, depending on whether indeed_gzip is present,
471- with patch_keep_file_open_default ('auto' ), patch_indexed_gzip (True ):
472- proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
473- assert proxy ._keep_file_open
474- with patch_keep_file_open_default ('auto' ), patch_indexed_gzip (False ):
475- proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
476- assert not proxy ._keep_file_open
453+ # If KEEP_FILE_OPEN_DEFAULT is False, ArrayProxy instances should
454+ # interpret keep_file_open as False
455+ with patch_keep_file_open_default (False ):
456+ with patch_indexed_gzip (False ):
457+ proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
458+ assert proxy ._keep_file_open is False
459+ with patch_indexed_gzip (True ):
460+ proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
461+ assert proxy ._keep_file_open is False
462+ # If KEEP_FILE_OPEN_DEFAULT is True, ArrayProxy instances should
463+ # interpret keep_file_open as True
464+ with patch_keep_file_open_default (True ):
465+ with patch_indexed_gzip (False ):
466+ proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
467+ assert proxy ._keep_file_open is True
468+ with patch_indexed_gzip (True ):
469+ proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
470+ assert proxy ._keep_file_open is True
471+ # If KEEP_FILE_OPEN_DEFAULT is auto, ArrayProxy instances should
472+ # interpret it as auto if indexed_gzip is present, False otherwise.
473+ with patch_keep_file_open_default ('auto' ):
474+ with patch_indexed_gzip (False ):
475+ proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
476+ assert proxy ._keep_file_open is False
477+ with patch_indexed_gzip (True ):
478+ proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
479+ assert proxy ._keep_file_open == 'auto'
477480 # KEEP_FILE_OPEN_DEFAULT=any other value should cuse an error to be
478481 # raised
479482 with patch_keep_file_open_default ('badvalue' ):
0 commit comments