@@ -476,7 +476,7 @@ def filespec_to_file_map(klass, filespec):
476476
477477 @classmethod
478478 @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' ):
480480 '''Load image from `file_map`
481481
482482 Parameters
@@ -491,6 +491,17 @@ def from_file_map(klass, file_map, mmap=True):
491491 `mmap` value of True gives the same behavior as ``mmap='c'``. If
492492 image data file cannot be memory-mapped, ignore `mmap` value and
493493 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.
494505 '''
495506 if mmap not in (True , False , 'c' , 'r' ):
496507 raise ValueError ("mmap should be one of {True, False, 'c', 'r'}" )
@@ -500,7 +511,8 @@ def from_file_map(klass, file_map, mmap=True):
500511 affine = header .get_affine ()
501512 hdr_copy = header .copy ()
502513 # 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 )
504516 img = klass (data , affine , header , file_map = file_map )
505517 img ._load_cache = {'header' : hdr_copy ,
506518 'affine' : affine .copy (),
@@ -509,7 +521,7 @@ def from_file_map(klass, file_map, mmap=True):
509521
510522 @classmethod
511523 @kw_only_meth (1 )
512- def from_filename (klass , filename , mmap = True ):
524+ def from_filename (klass , filename , mmap = True , keep_file_open = 'auto' ):
513525 ''' class method to create image from filename `filename`
514526
515527 Parameters
@@ -523,6 +535,16 @@ def from_filename(klass, filename, mmap=True):
523535 `mmap` value of True gives the same behavior as ``mmap='c'``. If
524536 image data file cannot be memory-mapped, ignore `mmap` value and
525537 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``.
526548
527549 Returns
528550 -------
@@ -531,7 +553,8 @@ def from_filename(klass, filename, mmap=True):
531553 if mmap not in (True , False , 'c' , 'r' ):
532554 raise ValueError ("mmap should be one of {True, False, 'c', 'r'}" )
533555 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 )
535558
536559 load = from_filename
537560
0 commit comments