12
12
13
13
import warnings
14
14
import gzip
15
+ import contextlib
15
16
16
17
import pickle
17
18
from io import BytesIO
24
25
from ..openers import ImageOpener
25
26
from ..nifti1 import Nifti1Header
26
27
28
+ import mock
29
+
27
30
from numpy .testing import assert_array_equal , assert_array_almost_equal
28
31
from nose .tools import (assert_true , assert_false , assert_equal ,
29
32
assert_not_equal , assert_raises )
30
33
from nibabel .testing import VIRAL_MEMMAP
31
- import mock
32
34
33
35
from .test_fileslice import slicer_samples
34
36
@@ -338,18 +340,18 @@ def check_mmap(hdr, offset, proxy_class,
338
340
# created
339
341
class CountingImageOpener (ImageOpener ):
340
342
341
- numOpeners = 0
343
+ num_openers = 0
342
344
343
345
def __init__ (self , * args , ** kwargs ):
344
346
345
347
super (CountingImageOpener , self ).__init__ (* args , ** kwargs )
346
- CountingImageOpener .numOpeners += 1
348
+ CountingImageOpener .num_openers += 1
347
349
348
350
349
351
def test_keep_file_open_true_false_invalid ():
350
352
# Test the behaviour of the keep_file_open __init__ flag, when it is set to
351
353
# True or False.
352
- CountingImageOpener .numOpeners = 0
354
+ CountingImageOpener .num_openers = 0
353
355
fname = 'testdata'
354
356
dtype = np .float32
355
357
data = np .arange (1000 , dtype = dtype ).reshape ((10 , 10 , 10 ))
@@ -367,15 +369,15 @@ def test_keep_file_open_true_false_invalid():
367
369
for i in range (voxels .shape [0 ]):
368
370
x , y , z = [int (c ) for c in voxels [i , :]]
369
371
assert proxy_no_kfp [x , y , z ] == x * 100 + y * 10 + z
370
- assert CountingImageOpener .numOpeners == i + 1
371
- CountingImageOpener .numOpeners = 0
372
+ assert CountingImageOpener .num_openers == i + 1
373
+ CountingImageOpener .num_openers = 0
372
374
proxy_kfp = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
373
375
keep_file_open = True )
374
376
assert proxy_kfp ._keep_file_open
375
377
for i in range (voxels .shape [0 ]):
376
378
x , y , z = [int (c ) for c in voxels [i , :]]
377
379
assert proxy_kfp [x , y , z ] == x * 100 + y * 10 + z
378
- assert CountingImageOpener .numOpeners == 1
380
+ assert CountingImageOpener .num_openers == 1
379
381
# Test that the keep_file_open flag has no effect if an open file
380
382
# handle is passed in
381
383
with open (fname , 'rb' ) as fobj :
@@ -410,6 +412,28 @@ def test_keep_file_open_true_false_invalid():
410
412
ArrayProxy (fname , ((10 , 10 , 10 ), dtype ), keep_file_open = 'cauto' )
411
413
412
414
415
+ @contextlib .contextmanager
416
+ def patch_indexed_gzip (state ):
417
+ # Make it look like we do (state==True) or do not (state==False) have
418
+ # the indexed gzip module.
419
+ if state :
420
+ values = (True , True , gzip .GzipFile )
421
+ else :
422
+ values = (False , False , None )
423
+ with mock .patch ('nibabel.openers.HAVE_INDEXED_GZIP' , values [0 ]), \
424
+ mock .patch ('nibabel.arrayproxy.HAVE_INDEXED_GZIP' , values [1 ]), \
425
+ mock .patch ('nibabel.openers.SafeIndexedGzipFile' , values [2 ],
426
+ create = True ):
427
+ yield
428
+
429
+
430
+ @contextlib .contextmanager
431
+ def patch_keep_file_open_default (value ):
432
+ # Patch arrayproxy.KEEP_FILE_OPEN_DEFAULT with the given value
433
+ with mock .patch ('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT' , value ):
434
+ yield
435
+
436
+
413
437
def test_keep_file_open_auto ():
414
438
# Test the behaviour of the keep_file_open __init__ flag, when it is set to
415
439
# 'auto'
@@ -420,18 +444,12 @@ def test_keep_file_open_auto():
420
444
with gzip .open (fname , 'wb' ) as fobj :
421
445
fobj .write (data .tostring (order = 'F' ))
422
446
# If have_indexed_gzip, then keep_file_open should be True
423
- with mock .patch ('nibabel.openers.HAVE_INDEXED_GZIP' , True ), \
424
- mock .patch ('nibabel.arrayproxy.HAVE_INDEXED_GZIP' , True ), \
425
- mock .patch ('nibabel.openers.SafeIndexedGzipFile' , gzip .GzipFile ,
426
- create = True ):
447
+ with patch_indexed_gzip (True ):
427
448
proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
428
449
keep_file_open = 'auto' )
429
450
assert proxy ._keep_file_open
430
451
# If no have_indexed_gzip, then keep_file_open should be False
431
- with mock .patch ('nibabel.openers.HAVE_INDEXED_GZIP' , False ), \
432
- mock .patch ('nibabel.arrayproxy.HAVE_INDEXED_GZIP' , False ), \
433
- mock .patch ('nibabel.openers.SafeIndexedGzipFile' , None ,
434
- create = True ):
452
+ with patch_indexed_gzip (False ):
435
453
proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
436
454
keep_file_open = 'auto' )
437
455
assert not proxy ._keep_file_open
@@ -450,56 +468,34 @@ def test_keep_file_open_default():
450
468
# keep_file_open to be False, regardless of whether or not indexed_gzip
451
469
# is present
452
470
assert KEEP_FILE_OPEN_DEFAULT is False
453
- with mock .patch ('nibabel.openers.HAVE_INDEXED_GZIP' , False ), \
454
- mock .patch ('nibabel.arrayproxy.HAVE_INDEXED_GZIP' , False ), \
455
- mock .patch ('nibabel.openers.SafeIndexedGzipFile' , None ,
456
- create = True ):
471
+ with patch_indexed_gzip (False ):
457
472
proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
458
473
assert not proxy ._keep_file_open
459
- with mock .patch ('nibabel.openers.HAVE_INDEXED_GZIP' , True ), \
460
- mock .patch ('nibabel.arrayproxy.HAVE_INDEXED_GZIP' , True ), \
461
- mock .patch ('nibabel.openers.SafeIndexedGzipFile' , gzip .GzipFile ,
462
- create = True ):
474
+ with patch_indexed_gzip (True ):
463
475
proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
464
476
assert not proxy ._keep_file_open
465
477
# KEEP_FILE_OPEN_DEFAULT=True should cause keep_file_open to be True,
466
478
# regardless of whether or not indexed_gzip is present
467
- with mock .patch ('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT' , True ), \
468
- mock .patch ('nibabel.openers.HAVE_INDEXED_GZIP' , True ), \
469
- mock .patch ('nibabel.arrayproxy.HAVE_INDEXED_GZIP' , True ), \
470
- mock .patch ('nibabel.openers.SafeIndexedGzipFile' , gzip .GzipFile ,
471
- create = True ):
479
+ with patch_keep_file_open_default (True ), patch_indexed_gzip (True ):
472
480
proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
473
481
assert proxy ._keep_file_open
474
- with mock .patch ('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT' , True ), \
475
- mock .patch ('nibabel.openers.HAVE_INDEXED_GZIP' , False ), \
476
- mock .patch ('nibabel.arrayproxy.HAVE_INDEXED_GZIP' , False ), \
477
- mock .patch ('nibabel.openers.SafeIndexedGzipFile' , None ,
478
- create = True ):
482
+ with patch_keep_file_open_default (True ), patch_indexed_gzip (False ):
479
483
proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
480
484
assert proxy ._keep_file_open
481
485
# KEEP_FILE_OPEN_DEFAULT=auto should cause keep_file_open to be True
482
486
# or False, depending on whether indeed_gzip is present,
483
- with mock .patch ('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT' , 'auto' ), \
484
- mock .patch ('nibabel.openers.HAVE_INDEXED_GZIP' , True ), \
485
- mock .patch ('nibabel.arrayproxy.HAVE_INDEXED_GZIP' , True ), \
486
- mock .patch ('nibabel.openers.SafeIndexedGzipFile' , gzip .GzipFile ,
487
- create = True ):
487
+ with patch_keep_file_open_default ('auto' ), patch_indexed_gzip (True ):
488
488
proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
489
489
assert proxy ._keep_file_open
490
- with mock .patch ('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT' , 'auto' ), \
491
- mock .patch ('nibabel.openers.HAVE_INDEXED_GZIP' , False ), \
492
- mock .patch ('nibabel.arrayproxy.HAVE_INDEXED_GZIP' , False ), \
493
- mock .patch ('nibabel.openers.SafeIndexedGzipFile' , None ,
494
- create = True ):
490
+ with patch_keep_file_open_default ('auto' ), patch_indexed_gzip (False ):
495
491
proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
496
492
assert not proxy ._keep_file_open
497
493
# KEEP_FILE_OPEN_DEFAULT=any other value should cuse an error to be
498
494
# raised
499
- with mock . patch ( 'nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT' , 'badval ' ):
495
+ with patch_keep_file_open_default ( 'badvalue ' ):
500
496
assert_raises (ValueError , ArrayProxy , fname , ((10 , 10 , 10 ),
501
497
dtype ))
502
- with mock . patch ( 'nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT' , None ):
498
+ with patch_keep_file_open_default ( None ):
503
499
assert_raises (ValueError , ArrayProxy , fname , ((10 , 10 , 10 ),
504
500
dtype ))
505
501
0 commit comments