20
20
21
21
import numpy as np
22
22
23
- from ..arrayproxy import (ArrayProxy , KEEP_FILE_OPEN_DEFAULT , is_proxy ,
24
- reshape_dataobj )
23
+ from ..arrayproxy import (ArrayProxy , is_proxy , reshape_dataobj )
25
24
from ..openers import ImageOpener
26
25
from ..nifti1 import Nifti1Header
27
26
@@ -342,15 +341,20 @@ def check_mmap(hdr, offset, proxy_class,
342
341
# An image opener class which counts how many instances of itself have been
343
342
# created
344
343
class CountingImageOpener (ImageOpener ):
345
-
346
344
num_openers = 0
347
-
348
345
def __init__ (self , * args , ** kwargs ):
349
-
350
346
super (CountingImageOpener , self ).__init__ (* args , ** kwargs )
351
347
CountingImageOpener .num_openers += 1
352
348
353
349
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
+
354
358
def test_keep_file_open_true_false_invalid ():
355
359
# Test the behaviour of the keep_file_open __init__ flag, when it is set to
356
360
# True or False.
@@ -369,18 +373,11 @@ def test_keep_file_open_true_false_invalid():
369
373
proxy_no_kfp = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
370
374
keep_file_open = False )
371
375
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
377
377
proxy_kfp = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
378
378
keep_file_open = True )
379
379
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
384
381
del proxy_kfp
385
382
del proxy_no_kfp
386
383
# 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():
389
386
for kfo in (True , False , 'auto' ):
390
387
proxy = ArrayProxy (fobj , ((10 , 10 , 10 ), dtype ),
391
388
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
395
390
for i in range (voxels .shape [0 ]):
391
+ x , y , z = [int (c ) for c in voxels [i , :]]
396
392
assert proxy [x , y , z ] == x * 100 + y * 10 + z
397
393
assert not fobj .closed
398
394
del proxy
399
395
assert not fobj .closed
400
396
assert fobj .closed
401
397
# 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 )
406
398
with assert_raises (ValueError ):
407
399
ArrayProxy (fname , ((10 , 10 , 10 ), dtype ), keep_file_open = 55 )
408
400
with assert_raises (ValueError ):
@@ -411,69 +403,80 @@ def test_keep_file_open_true_false_invalid():
411
403
ArrayProxy (fname , ((10 , 10 , 10 ), dtype ), keep_file_open = 'cauto' )
412
404
413
405
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
-
421
406
def test_keep_file_open_auto ():
422
407
# 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.
424
411
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 ))
426
414
with InTemporaryDirectory ():
427
415
fname = 'testdata.gz'
428
416
with gzip .open (fname , 'wb' ) as fobj :
429
417
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
432
423
proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
433
424
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
435
427
# 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
437
431
proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
438
432
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
440
442
441
443
442
444
def test_keep_file_open_default ():
443
445
# Test the behaviour of the keep_file_open __init__ flag, when the
444
446
# arrayproxy.KEEP_FILE_OPEN_DEFAULT value is changed
445
447
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 ))
447
449
with InTemporaryDirectory ():
448
450
fname = 'testdata.gz'
449
451
with gzip .open (fname , 'wb' ) as fobj :
450
452
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'
477
480
# KEEP_FILE_OPEN_DEFAULT=any other value should cuse an error to be
478
481
# raised
479
482
with patch_keep_file_open_default ('badvalue' ):
0 commit comments