1919
2020import numpy as np
2121
22- from ..arrayproxy import ArrayProxy , is_proxy , reshape_dataobj
22+ from ..arrayproxy import (ArrayProxy , KEEP_FILE_OPEN_DEFAULT , is_proxy ,
23+ reshape_dataobj )
2324from ..openers import ImageOpener
2425from ..nifti1 import Nifti1Header
2526
@@ -409,9 +410,9 @@ def test_keep_file_open_true_false_invalid():
409410 ArrayProxy (fname , ((10 , 10 , 10 ), dtype ), keep_file_open = 'cauto' )
410411
411412
412- def test_keep_file_open_default ():
413+ def test_keep_file_open_auto ():
413414 # Test the behaviour of the keep_file_open __init__ flag, when it is set to
414- # its default value
415+ # 'auto'
415416 dtype = np .float32
416417 data = np .arange (1000 , dtype = dtype ).reshape ((10 , 10 , 10 ))
417418 with InTemporaryDirectory ():
@@ -423,18 +424,75 @@ def test_keep_file_open_default():
423424 mock .patch ('nibabel.arrayproxy.HAVE_INDEXED_GZIP' , True ), \
424425 mock .patch ('nibabel.openers.SafeIndexedGzipFile' , gzip .GzipFile ,
425426 create = True ):
426- proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
427- assert proxy ._keep_file_open
428427 proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ), keep_file_open = 'auto' )
429428 assert proxy ._keep_file_open
430429 # If no have_indexed_gzip, then keep_file_open should be False
430+ with mock .patch ('nibabel.openers.HAVE_INDEXED_GZIP' , False ), \
431+ mock .patch ('nibabel.arrayproxy.HAVE_INDEXED_GZIP' , False ), \
432+ mock .patch ('nibabel.openers.SafeIndexedGzipFile' , None , create = True ):
433+ proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ), keep_file_open = 'auto' )
434+ assert not proxy ._keep_file_open
435+
436+
437+ def test_keep_file_open_default ():
438+ # Test the behaviour of the keep_file_open __init__ flag, when the
439+ # arrayproxy.KEEP_FILE_OPEN_DEFAULT value is changed
440+ dtype = np .float32
441+ data = np .arange (1000 , dtype = dtype ).reshape ((10 , 10 , 10 ))
442+ with InTemporaryDirectory ():
443+ fname = 'testdata.gz'
444+ with gzip .open (fname , 'wb' ) as fobj :
445+ fobj .write (data .tostring (order = 'F' ))
446+ # The default value of KEEP_FILE_OPEN_DEFAULT should cause keep_file_open
447+ # to be False, regardless of whether or not indexed_gzip is present
448+ assert KEEP_FILE_OPEN_DEFAULT is False
431449 with mock .patch ('nibabel.openers.HAVE_INDEXED_GZIP' , False ), \
432450 mock .patch ('nibabel.arrayproxy.HAVE_INDEXED_GZIP' , False ), \
433451 mock .patch ('nibabel.openers.SafeIndexedGzipFile' , None , create = True ):
434452 proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
435453 assert not proxy ._keep_file_open
436- proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ), keep_file_open = 'auto' )
454+ with mock .patch ('nibabel.openers.HAVE_INDEXED_GZIP' , True ), \
455+ mock .patch ('nibabel.arrayproxy.HAVE_INDEXED_GZIP' , True ), \
456+ mock .patch ('nibabel.openers.SafeIndexedGzipFile' , gzip .GzipFile ,
457+ create = True ):
458+ proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
459+ assert not proxy ._keep_file_open
460+ # KEEP_FILE_OPEN_DEFAULT=True should cause keep_file_open to be True,
461+ # regardless of whether or not indexed_gzip is present
462+ with mock .patch ('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT' , True ), \
463+ mock .patch ('nibabel.openers.HAVE_INDEXED_GZIP' , True ), \
464+ mock .patch ('nibabel.arrayproxy.HAVE_INDEXED_GZIP' , True ), \
465+ mock .patch ('nibabel.openers.SafeIndexedGzipFile' , gzip .GzipFile ,
466+ create = True ):
467+ proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
468+ assert proxy ._keep_file_open
469+ with mock .patch ('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT' , True ), \
470+ mock .patch ('nibabel.openers.HAVE_INDEXED_GZIP' , False ), \
471+ mock .patch ('nibabel.arrayproxy.HAVE_INDEXED_GZIP' , False ), \
472+ mock .patch ('nibabel.openers.SafeIndexedGzipFile' , None , create = True ):
473+ proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
474+ assert proxy ._keep_file_open
475+ # KEEP_FILE_OPEN_DEFAULT=auto should cause keep_file_open to be True
476+ # or False, depending on whether indeed_gzip is present,
477+ with mock .patch ('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT' , 'auto' ), \
478+ mock .patch ('nibabel.openers.HAVE_INDEXED_GZIP' , True ), \
479+ mock .patch ('nibabel.arrayproxy.HAVE_INDEXED_GZIP' , True ), \
480+ mock .patch ('nibabel.openers.SafeIndexedGzipFile' , gzip .GzipFile ,
481+ create = True ):
482+ proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
483+ assert proxy ._keep_file_open
484+ with mock .patch ('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT' , 'auto' ), \
485+ mock .patch ('nibabel.openers.HAVE_INDEXED_GZIP' , False ), \
486+ mock .patch ('nibabel.arrayproxy.HAVE_INDEXED_GZIP' , False ), \
487+ mock .patch ('nibabel.openers.SafeIndexedGzipFile' , None , create = True ):
488+ proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
437489 assert not proxy ._keep_file_open
490+ # KEEP_FILE_OPEN_DEFAULT=any other value should cuse an error to be raised
491+ with mock .patch ('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT' , 'badvalue' ):
492+ assert_raises (ValueError , ArrayProxy , fname , ((10 , 10 , 10 ), dtype ))
493+ with mock .patch ('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT' , None ):
494+ assert_raises (ValueError , ArrayProxy , fname , ((10 , 10 , 10 ), dtype ))
495+
438496
439497def test_pickle_lock ():
440498 # Test that ArrayProxy can be pickled, and that thread lock is created
0 commit comments