@@ -476,7 +476,7 @@ def filespec_to_file_map(klass, filespec):
476
476
477
477
@classmethod
478
478
@kw_only_meth (1 )
479
- def from_file_map (klass , file_map , mmap = True ):
479
+ def from_file_map (klass , file_map , mmap = True , keep_file_open = 'auto' ):
480
480
'''Load image from `file_map`
481
481
482
482
Parameters
@@ -491,6 +491,17 @@ def from_file_map(klass, file_map, mmap=True):
491
491
`mmap` value of True gives the same behavior as ``mmap='c'``. If
492
492
image data file cannot be memory-mapped, ignore `mmap` value and
493
493
read array from file.
494
+ keep_file_open : { 'auto', True, False }, optional, keyword only
495
+ `keep_file_open` controls whether a new file handle is created
496
+ every time the image is accessed, or a single file handle is
497
+ created and used for the lifetime of this ``ArrayProxy``. If
498
+ ``True``, a single file handle is created and used. If ``False``,
499
+ a new file handle is created every time the image is accessed. If
500
+ ``'auto'`` (the default), and the optional ``indexed_gzip``
501
+ dependency is present, a single file handle is created and
502
+ persisted. If ``indexed_gzip`` is not available, behaviour is the
503
+ same as if ``keep_file_open is False``. If ``file_map`` refers to
504
+ an open file handle, this setting has no effect.
494
505
'''
495
506
if mmap not in (True , False , 'c' , 'r' ):
496
507
raise ValueError ("mmap should be one of {True, False, 'c', 'r'}" )
@@ -500,7 +511,8 @@ def from_file_map(klass, file_map, mmap=True):
500
511
affine = header .get_affine ()
501
512
hdr_copy = header .copy ()
502
513
# Pass original image fileobj / filename to array proxy
503
- data = klass .ImageArrayProxy (img_fh .file_like , hdr_copy , mmap = mmap )
514
+ data = klass .ImageArrayProxy (img_fh .file_like , hdr_copy , mmap = mmap ,
515
+ keep_file_open = keep_file_open )
504
516
img = klass (data , affine , header , file_map = file_map )
505
517
img ._load_cache = {'header' : hdr_copy ,
506
518
'affine' : affine .copy (),
@@ -509,7 +521,7 @@ def from_file_map(klass, file_map, mmap=True):
509
521
510
522
@classmethod
511
523
@kw_only_meth (1 )
512
- def from_filename (klass , filename , mmap = True ):
524
+ def from_filename (klass , filename , mmap = True , keep_file_open = 'auto' ):
513
525
''' class method to create image from filename `filename`
514
526
515
527
Parameters
@@ -523,6 +535,16 @@ def from_filename(klass, filename, mmap=True):
523
535
`mmap` value of True gives the same behavior as ``mmap='c'``. If
524
536
image data file cannot be memory-mapped, ignore `mmap` value and
525
537
read array from file.
538
+ keep_file_open : { 'auto', True, False }, optional, keyword only
539
+ `keep_file_open` controls whether a new file handle is created
540
+ every time the image is accessed, or a single file handle is
541
+ created and used for the lifetime of this ``ArrayProxy``. If
542
+ ``True``, a single file handle is created and used. If ``False``,
543
+ a new file handle is created every time the image is accessed. If
544
+ ``'auto'`` (the default), and the optional ``indexed_gzip``
545
+ dependency is present, a single file handle is created and
546
+ persisted. If ``indexed_gzip`` is not available, behaviour is the
547
+ same as if ``keep_file_open is False``.
526
548
527
549
Returns
528
550
-------
@@ -531,7 +553,8 @@ def from_filename(klass, filename, mmap=True):
531
553
if mmap not in (True , False , 'c' , 'r' ):
532
554
raise ValueError ("mmap should be one of {True, False, 'c', 'r'}" )
533
555
file_map = klass .filespec_to_file_map (filename )
534
- return klass .from_file_map (file_map , mmap = mmap )
556
+ return klass .from_file_map (file_map , mmap = mmap ,
557
+ keep_file_open = keep_file_open )
535
558
536
559
load = from_filename
537
560
0 commit comments