Skip to content

Commit aae3417

Browse files
committed
RF: Default keep_file_open value is now False, and can be controlled via a
module-level arrayproxy.KEEP_FILE_OPEN_DEFAULT flag
1 parent 16f191e commit aae3417

File tree

4 files changed

+64
-37
lines changed

4 files changed

+64
-37
lines changed

nibabel/analyze.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -934,8 +934,8 @@ def set_data_dtype(self, dtype):
934934

935935
@classmethod
936936
@kw_only_meth(1)
937-
def from_file_map(klass, file_map, mmap=True, keep_file_open='auto'):
938-
''' class method to create image from mapping in `file_map ``
937+
def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
938+
'''class method to create image from mapping in `file_map ``
939939
940940
Parameters
941941
----------
@@ -956,11 +956,12 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open='auto'):
956956
created and used for the lifetime of this ``ArrayProxy``. If
957957
``True``, a single file handle is created and used. If ``False``,
958958
a new file handle is created every time the image is accessed. If
959-
``'auto'`` (the default), and the optional ``indexed_gzip``
960-
dependency is present, a single file handle is created and
961-
persisted. If ``indexed_gzip`` is not available, behaviour is the
962-
same as if ``keep_file_open is False``. If ``file_map`` refers
963-
to an open file handle, this setting has no effect.
959+
``'auto'``, and the optional ``indexed_gzip`` dependency is
960+
present, a single file handle is created and persisted. If
961+
``indexed_gzip`` is not available, behaviour is the same as if
962+
``keep_file_open is False``. If ``file_map`` refers to an open
963+
file handle, this setting has no effect. The default value is
964+
set to the value of ``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT``.
964965
965966
Returns
966967
-------
@@ -988,8 +989,8 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open='auto'):
988989

989990
@classmethod
990991
@kw_only_meth(1)
991-
def from_filename(klass, filename, mmap=True, keep_file_open='auto'):
992-
''' class method to create image from filename `filename`
992+
def from_filename(klass, filename, mmap=True, keep_file_open=None):
993+
'''class method to create image from filename `filename`
993994
994995
Parameters
995996
----------
@@ -1002,16 +1003,18 @@ def from_filename(klass, filename, mmap=True, keep_file_open='auto'):
10021003
`mmap` value of True gives the same behavior as ``mmap='c'``. If
10031004
image data file cannot be memory-mapped, ignore `mmap` value and
10041005
read array from file.
1006+
10051007
keep_file_open : { 'auto', True, False }, optional, keyword only
10061008
`keep_file_open` controls whether a new file handle is created
10071009
every time the image is accessed, or a single file handle is
10081010
created and used for the lifetime of this ``ArrayProxy``. If
10091011
``True``, a single file handle is created and used. If ``False``,
10101012
a new file handle is created every time the image is accessed. If
1011-
``'auto'`` (the default), and the optional ``indexed_gzip``
1012-
dependency is present, a single file handle is created and
1013-
persisted. If ``indexed_gzip`` is not available, behaviour is the
1014-
same as if ``keep_file_open is False``.
1013+
``'auto'``, and the optional ``indexed_gzip`` dependency is
1014+
present, a single file handle is created and persisted. If
1015+
``indexed_gzip`` is not available, behaviour is the same as if
1016+
``keep_file_open is False``. The default value is set to the value
1017+
of ``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT``.
10151018
10161019
Returns
10171020
-------

nibabel/arrayproxy.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@
3737
from .openers import ImageOpener, HAVE_INDEXED_GZIP
3838

3939

40+
KEEP_FILE_OPEN_DEFAULT = False
41+
"""This flag controls whether a new file handle is created every time an image
42+
is accessed through an ``ArrayProxy``, or a single file handle is created and
43+
used for the lifetime of the ``ArrayProxy``. It should be set to one of
44+
``True``, ``False``, or ``'auto'``.
45+
46+
If ``True``, a single file handle is created and used. If ``False``, a new
47+
file handle is created every time the image is accessed. If ``'auto'``, and
48+
the optional ``indexed_gzip`` dependency is present, a single file handle is
49+
created and persisted. If ``indexed_gzip`` is not available, behaviour is the
50+
same as if ``keep_file_open is False``.
51+
52+
If this is set to any other value, attempts to create an ``ArrayProxy`` without
53+
specifying the ``keep_file_open`` flag will result in a ``ValueError`` being
54+
raised.
55+
"""
56+
57+
4058
class ArrayProxy(object):
4159
""" Class to act as proxy for the array that can be read from a file
4260
@@ -72,7 +90,7 @@ class ArrayProxy(object):
7290
_header = None
7391

7492
@kw_only_meth(2)
75-
def __init__(self, file_like, spec, mmap=True, keep_file_open='auto'):
93+
def __init__(self, file_like, spec, mmap=True, keep_file_open=None):
7694
"""Initialize array proxy instance
7795
7896
Parameters
@@ -108,11 +126,12 @@ def __init__(self, file_like, spec, mmap=True, keep_file_open='auto'):
108126
created and used for the lifetime of this ``ArrayProxy``. If
109127
``True``, a single file handle is created and used. If ``False``,
110128
a new file handle is created every time the image is accessed. If
111-
``'auto'`` (the default), and the optional ``indexed_gzip``
112-
dependency is present, a single file handle is created and
113-
persisted. If ``indexed_gzip`` is not available, behaviour is the
114-
same as if ``keep_file_open is False``. If ``file_like`` is an
115-
open file handle, this setting has no effect.
129+
``'auto'``, and the optional ``indexed_gzip`` dependency is
130+
present, a single file handle is created and persisted. If
131+
``indexed_gzip`` is not available, behaviour is the same as if
132+
``keep_file_open is False``. If ``file_like`` is an open file
133+
handle, this setting has no effect. The default value is set to
134+
the value of ``KEEP_FILE_OPEN_DEFAULT``.
116135
"""
117136
if mmap not in (True, False, 'c', 'r'):
118137
raise ValueError("mmap should be one of {True, False, 'c', 'r'}")
@@ -186,6 +205,8 @@ def _should_keep_file_open(self, file_like, keep_file_open):
186205
The value of ``keep_file_open`` that will be used by this
187206
``ArrayProxy``.
188207
"""
208+
if keep_file_open is None:
209+
keep_file_open = KEEP_FILE_OPEN_DEFAULT
189210
# if keep_file_open is True/False, we do what the user wants us to do
190211
if isinstance(keep_file_open, bool):
191212
return keep_file_open

nibabel/freesurfer/mghformat.py

Lines changed: 14 additions & 12 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, keep_file_open='auto'):
479+
def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
480480
'''Load image from `file_map`
481481
482482
Parameters
@@ -497,11 +497,12 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open='auto'):
497497
created and used for the lifetime of this ``ArrayProxy``. If
498498
``True``, a single file handle is created and used. If ``False``,
499499
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.
500+
``'auto'``, and the optional ``indexed_gzip`` dependency is
501+
present, a single file handle is created and persisted. If
502+
``indexed_gzip`` is not available, behaviour is the same as if
503+
``keep_file_open is False``. If ``file_map`` refers to an open
504+
file handle, this setting has no effect. The default value is
505+
set to the value of ``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT``.
505506
'''
506507
if mmap not in (True, False, 'c', 'r'):
507508
raise ValueError("mmap should be one of {True, False, 'c', 'r'}")
@@ -521,8 +522,8 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open='auto'):
521522

522523
@classmethod
523524
@kw_only_meth(1)
524-
def from_filename(klass, filename, mmap=True, keep_file_open='auto'):
525-
''' class method to create image from filename `filename`
525+
def from_filename(klass, filename, mmap=True, keep_file_open=None):
526+
'''class method to create image from filename `filename`
526527
527528
Parameters
528529
----------
@@ -541,10 +542,11 @@ def from_filename(klass, filename, mmap=True, keep_file_open='auto'):
541542
created and used for the lifetime of this ``ArrayProxy``. If
542543
``True``, a single file handle is created and used. If ``False``,
543544
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``.
545+
``'auto'``, and the optional ``indexed_gzip`` dependency is
546+
present, a single file handle is created and persisted. If
547+
``indexed_gzip`` is not available, behaviour is the same as if
548+
``keep_file_open is False``. The default value is set to the value
549+
of ``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT``.
548550
549551
Returns
550552
-------

nibabel/spm99analyze.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class Spm99AnalyzeImage(analyze.AnalyzeImage):
245245

246246
@classmethod
247247
@kw_only_meth(1)
248-
def from_file_map(klass, file_map, mmap=True, keep_file_open='auto'):
248+
def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
249249
'''class method to create image from mapping in `file_map ``
250250
251251
Parameters
@@ -267,11 +267,12 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open='auto'):
267267
created and used for the lifetime of this ``ArrayProxy``. If
268268
``True``, a single file handle is created and used. If ``False``,
269269
a new file handle is created every time the image is accessed. If
270-
``'auto'`` (the default), and the optional ``indexed_gzip``
271-
dependency is present, a single file handle is created and
272-
persisted. If ``indexed_gzip`` is not available, behaviour is the
273-
same as if ``keep_file_open is False``. If ``file_map`` refers to
274-
an open file handle, this setting has no effect.
270+
``'auto'``, and the optional ``indexed_gzip`` dependency is
271+
present, a single file handle is created and persisted. If
272+
``indexed_gzip`` is not available, behaviour is the same as if
273+
``keep_file_open is False``. If ``file_map`` refers to an open
274+
file handle, this setting has no effect. The default value is
275+
set to the value of ``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT``.
275276
276277
Returns
277278
-------

0 commit comments

Comments
 (0)