Skip to content

Commit 82e7c84

Browse files
committed
NF+RF: MGHImage from_file_map/from_filename methods passes keep_file_open flag
through to ArrayProxy. ArrayProxy handles keep_file_open flag a bit more explicitly/robustly.
1 parent 39dea17 commit 82e7c84

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

nibabel/arrayproxy.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,16 @@ def _should_keep_file_open(self, file_like, keep_file_open):
186186
The value of ``keep_file_open`` that will be used by this
187187
``ArrayProxy``.
188188
"""
189+
# if keep_file_open is True/False, we do what the user wants us to do
190+
if isinstance(keep_file_open, bool):
191+
return keep_file_open
192+
if keep_file_open != 'auto':
193+
raise ValueError(
194+
'keep_file_open should be one of {\'auto\', True, False }')
189195

190196
# file_like is a handle - keep_file_open is irrelevant
191197
if hasattr(file_like, 'read') and hasattr(file_like, 'seek'):
192198
return False
193-
# if keep_file_open is True/False, we do what the user wants us to do
194-
if keep_file_open != 'auto':
195-
return bool(keep_file_open)
196199
# Otherwise, if file_like is gzipped, and we have_indexed_gzip, we set
197200
# keep_file_open to True, else we set it to False
198201
return HAVE_INDEXED_GZIP and file_like.endswith('gz')

nibabel/freesurfer/mghformat.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)