Skip to content

Commit fc5d677

Browse files
committed
MAINT: Deprecate keep_file_open == "auto" (scheduled for 2.4.0)
1 parent bb4dcd9 commit fc5d677

File tree

5 files changed

+98
-100
lines changed

5 files changed

+98
-100
lines changed

nibabel/analyze.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,11 @@ def set_data_dtype(self, dtype):
935935
@classmethod
936936
@kw_only_meth(1)
937937
def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
938-
'''class method to create image from mapping in `file_map ``
938+
''' Class method to create image from mapping in ``file_map``
939+
940+
.. deprecated:: 2.4.1
941+
``keep_file_open='auto'`` is redundant with `False` and has
942+
been deprecated. It will raise an error in nibabel 3.0.
939943
940944
Parameters
941945
----------
@@ -950,18 +954,14 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
950954
`mmap` value of True gives the same behavior as ``mmap='c'``. If
951955
image data file cannot be memory-mapped, ignore `mmap` value and
952956
read array from file.
953-
keep_file_open : { None, 'auto', True, False }, optional, keyword only
957+
keep_file_open : { None, True, False }, optional, keyword only
954958
`keep_file_open` controls whether a new file handle is created
955959
every time the image is accessed, or a single file handle is
956960
created and used for the lifetime of this ``ArrayProxy``. If
957961
``True``, a single file handle is created and used. If ``False``,
958-
a new file handle is created every time the image is accessed. If
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
964-
(``None``) will result in the value of
962+
a new file handle is created every time the image is accessed.
963+
If ``file_map`` refers to an open file handle, this setting has no
964+
effect. The default value (``None``) will result in the value of
965965
``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT`` being used.
966966
967967
Returns
@@ -991,7 +991,11 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
991991
@classmethod
992992
@kw_only_meth(1)
993993
def from_filename(klass, filename, mmap=True, keep_file_open=None):
994-
'''class method to create image from filename `filename`
994+
'''Class method to create image from filename `filename`
995+
996+
.. deprecated:: 2.4.1
997+
``keep_file_open='auto'`` is redundant with `False` and has
998+
been deprecated. It will raise an error in nibabel 3.0.
995999
9961000
Parameters
9971001
----------
@@ -1004,17 +1008,13 @@ def from_filename(klass, filename, mmap=True, keep_file_open=None):
10041008
`mmap` value of True gives the same behavior as ``mmap='c'``. If
10051009
image data file cannot be memory-mapped, ignore `mmap` value and
10061010
read array from file.
1007-
keep_file_open : { None, 'auto', True, False }, optional, keyword only
1011+
keep_file_open : { None, True, False }, optional, keyword only
10081012
`keep_file_open` controls whether a new file handle is created
10091013
every time the image is accessed, or a single file handle is
10101014
created and used for the lifetime of this ``ArrayProxy``. If
10111015
``True``, a single file handle is created and used. If ``False``,
1012-
a new file handle is created every time the image is accessed. If
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 (``None``) will
1017-
result in the value of
1016+
a new file handle is created every time the image is accessed.
1017+
The default value (``None``) will result in the value of
10181018
``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT`` being used.
10191019
10201020
Returns

nibabel/arrayproxy.py

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"""
2828
from contextlib import contextmanager
2929
from threading import RLock
30+
import warnings
3031

3132
import numpy as np
3233

@@ -40,23 +41,22 @@
4041
"""This flag controls whether a new file handle is created every time an image
4142
is accessed through an ``ArrayProxy``, or a single file handle is created and
4243
used for the lifetime of the ``ArrayProxy``. It should be set to one of
43-
``True``, ``False``, or ``'auto'``.
44+
``True`` or ``False``.
4445
4546
Management of file handles will be performed either by ``ArrayProxy`` objects,
4647
or by the ``indexed_gzip`` package if it is used.
4748
4849
If this flag is set to ``True``, a single file handle is created and used. If
49-
``False``, a new file handle is created every time the image is accessed. For
50-
gzip files, if ``'auto'``, and the optional ``indexed_gzip`` dependency is
51-
present, a single file handle is created and persisted. If ``indexed_gzip`` is
52-
not available, behaviour is the same as if ``keep_file_open is False``.
50+
``False``, a new file handle is created every time the image is accessed.
51+
If this flag is set to ``'auto'``, a ``DeprecationWarning`` will be raised, which
52+
will become a ``ValueError`` in nibabel 3.0.0.
5353
5454
If this is set to any other value, attempts to create an ``ArrayProxy`` without
5555
specifying the ``keep_file_open`` flag will result in a ``ValueError`` being
5656
raised.
5757
58-
.. warning:: Setting this flag to a value of ``'auto'`` will become deprecated
59-
behaviour in version 2.4.0. Support for ``'auto'`` will be removed
58+
.. warning:: Setting this flag to a value of ``'auto'`` became deprecated
59+
behaviour in version 2.4.1. Support for ``'auto'`` will be removed
6060
in version 3.0.0.
6161
"""
6262
KEEP_FILE_OPEN_DEFAULT = False
@@ -100,6 +100,10 @@ class ArrayProxy(object):
100100
def __init__(self, file_like, spec, mmap=True, keep_file_open=None):
101101
"""Initialize array proxy instance
102102
103+
.. deprecated:: 2.4.1
104+
``keep_file_open='auto'`` is redundant with `False` and has
105+
been deprecated. It will raise an error in nibabel 3.0.
106+
103107
Parameters
104108
----------
105109
file_like : object
@@ -127,18 +131,15 @@ def __init__(self, file_like, spec, mmap=True, keep_file_open=None):
127131
True gives the same behavior as ``mmap='c'``. If `file_like`
128132
cannot be memory-mapped, ignore `mmap` value and read array from
129133
file.
130-
keep_file_open : { None, 'auto', True, False }, optional, keyword only
134+
keep_file_open : { None, True, False }, optional, keyword only
131135
`keep_file_open` controls whether a new file handle is created
132136
every time the image is accessed, or a single file handle is
133137
created and used for the lifetime of this ``ArrayProxy``. If
134138
``True``, a single file handle is created and used. If ``False``,
135-
a new file handle is created every time the image is accessed. If
136-
``'auto'``, and the optional ``indexed_gzip`` dependency is
137-
present, a single file handle is created and persisted. If
138-
``indexed_gzip`` is not available, behaviour is the same as if
139-
``keep_file_open is False``. If ``file_like`` is an open file
140-
handle, this setting has no effect. The default value (``None``)
141-
will result in the value of ``KEEP_FILE_OPEN_DEFAULT`` being used.
139+
a new file handle is created every time the image is accessed.
140+
If ``file_like`` is an open file handle, this setting has no
141+
effect. The default value (``None``) will result in the value of
142+
``KEEP_FILE_OPEN_DEFAULT`` being used.
142143
"""
143144
if mmap not in (True, False, 'c', 'r'):
144145
raise ValueError("mmap should be one of {True, False, 'c', 'r'}")
@@ -236,17 +237,9 @@ def _should_keep_file_open(self, file_like, keep_file_open):
236237
In this case, file handle management is delegated to the
237238
``indexed_gzip`` library.
238239
239-
5. If ``keep_file_open`` is ``'auto'``, ``file_like`` is a path to a
240-
``.gz`` file, and ``indexed_gzip`` is present, both internal flags
241-
are set to ``True``.
242-
243-
6. If ``keep_file_open`` is ``'auto'``, and ``file_like`` is not a
244-
path to a ``.gz`` file, or ``indexed_gzip`` is not present, both
245-
internal flags are set to ``False``.
246-
247-
Note that a value of ``'auto'`` for ``keep_file_open`` will become
248-
deprecated behaviour in version 2.4.0, and support for ``'auto'`` will
249-
be removed in version 3.0.0.
240+
.. deprecated:: 2.4.1
241+
``keep_file_open='auto'`` is redundant with `False` and has
242+
been deprecated. It will be removed in nibabel 3.0.
250243
251244
Parameters
252245
----------
@@ -266,20 +259,26 @@ def _should_keep_file_open(self, file_like, keep_file_open):
266259
"""
267260
if keep_file_open is None:
268261
keep_file_open = KEEP_FILE_OPEN_DEFAULT
269-
if keep_file_open not in ('auto', True, False):
270-
raise ValueError('keep_file_open should be one of {None, '
271-
'\'auto\', True, False}')
262+
if keep_file_open == 'auto':
263+
warnings.warn("Setting nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT to 'auto' is "
264+
"deprecated and will become an error in v3.0.", DeprecationWarning)
265+
if keep_file_open == 'auto':
266+
warnings.warn("A value of 'auto' for keep_file_open is deprecated and will become an "
267+
"error in v3.0. You probably want False.", DeprecationWarning)
268+
elif keep_file_open not in (True, False):
269+
raise ValueError('keep_file_open should be one of {None, True, False}')
270+
272271
# file_like is a handle - keep_file_open is irrelevant
273272
if hasattr(file_like, 'read') and hasattr(file_like, 'seek'):
274273
return False, False
275274
# if the file is a gzip file, and we have_indexed_gzip,
276275
have_igzip = openers.HAVE_INDEXED_GZIP and file_like.endswith('.gz')
276+
# XXX Remove in v3.0
277277
if keep_file_open == 'auto':
278278
return have_igzip, have_igzip
279-
elif keep_file_open:
280-
return True, True
281-
else:
282-
return False, have_igzip
279+
280+
persist_opener = keep_file_open or have_igzip
281+
return keep_file_open, persist_opener
283282

284283
@property
285284
@deprecate_with_version('ArrayProxy.header deprecated', '2.2', '3.0')

nibabel/brikhead.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ def __init__(self, file_like, header, mmap=True, keep_file_open=None):
227227
"""
228228
Initialize AFNI array proxy
229229
230+
.. deprecated:: 2.4.1
231+
``keep_file_open='auto'`` is redundant with `False` and has
232+
been deprecated. It will raise an error in nibabel 3.0.
233+
230234
Parameters
231235
----------
232236
file_like : file-like object
@@ -240,18 +244,14 @@ def __init__(self, file_like, header, mmap=True, keep_file_open=None):
240244
True gives the same behavior as ``mmap='c'``. If `file_like`
241245
cannot be memory-mapped, ignore `mmap` value and read array from
242246
file.
243-
keep_file_open : { None, 'auto', True, False }, optional, keyword only
247+
keep_file_open : { None, True, False }, optional, keyword only
244248
`keep_file_open` controls whether a new file handle is created
245249
every time the image is accessed, or a single file handle is
246250
created and used for the lifetime of this ``ArrayProxy``. If
247251
``True``, a single file handle is created and used. If ``False``,
248-
a new file handle is created every time the image is accessed. If
249-
``'auto'``, and the optional ``indexed_gzip`` dependency is
250-
present, a single file handle is created and persisted. If
251-
``indexed_gzip`` is not available, behavior is the same as if
252-
``keep_file_open is False``. If ``file_like`` refers to an open
253-
file handle, this setting has no effect. The default value
254-
(``None``) will result in the value of
252+
a new file handle is created every time the image is accessed.
253+
If ``file_like`` refers to an open file handle, this setting has no
254+
effect. The default value (``None``) will result in the value of
255255
``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT` being used.
256256
"""
257257
super(AFNIArrayProxy, self).__init__(file_like,
@@ -506,6 +506,10 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
506506
"""
507507
Creates an AFNIImage instance from `file_map`
508508
509+
.. deprecated:: 2.4.1
510+
``keep_file_open='auto'`` is redundant with `False` and has
511+
been deprecated. It will raise an error in nibabel 3.0.
512+
509513
Parameters
510514
----------
511515
file_map : dict
@@ -518,18 +522,14 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
518522
`mmap` value of True gives the same behavior as ``mmap='c'``. If
519523
image data file cannot be memory-mapped, ignore `mmap` value and
520524
read array from file.
521-
keep_file_open : {None, 'auto', True, False}, optional, keyword only
525+
keep_file_open : {None, True, False}, optional, keyword only
522526
`keep_file_open` controls whether a new file handle is created
523527
every time the image is accessed, or a single file handle is
524528
created and used for the lifetime of this ``ArrayProxy``. If
525529
``True``, a single file handle is created and used. If ``False``,
526-
a new file handle is created every time the image is accessed. If
527-
``'auto'``, and the optional ``indexed_gzip`` dependency is
528-
present, a single file handle is created and persisted. If
529-
``indexed_gzip`` is not available, behavior is the same as if
530-
``keep_file_open is False``. If ``file_like`` refers to an open
531-
file handle, this setting has no effect. The default value
532-
(``None``) will result in the value of
530+
a new file handle is created every time the image is accessed.
531+
If ``file_like`` refers to an open file handle, this setting has no
532+
effect. The default value (``None``) will result in the value of
533533
``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT` being used.
534534
"""
535535
with file_map['header'].get_prepare_fileobj('rt') as hdr_fobj:
@@ -547,6 +547,10 @@ def from_filename(klass, filename, mmap=True, keep_file_open=None):
547547
"""
548548
Creates an AFNIImage instance from `filename`
549549
550+
.. deprecated:: 2.4.1
551+
``keep_file_open='auto'`` is redundant with `False` and has
552+
been deprecated. It will raise an error in nibabel 3.0.
553+
550554
Parameters
551555
----------
552556
filename : str
@@ -558,18 +562,13 @@ def from_filename(klass, filename, mmap=True, keep_file_open=None):
558562
`mmap` value of True gives the same behavior as ``mmap='c'``. If
559563
image data file cannot be memory-mapped, ignore `mmap` value and
560564
read array from file.
561-
keep_file_open : {None, 'auto', True, False}, optional, keyword only
565+
keep_file_open : {None, True, False}, optional, keyword only
562566
`keep_file_open` controls whether a new file handle is created
563567
every time the image is accessed, or a single file handle is
564568
created and used for the lifetime of this ``ArrayProxy``. If
565569
``True``, a single file handle is created and used. If ``False``,
566-
a new file handle is created every time the image is accessed. If
567-
``'auto'``, and the optional ``indexed_gzip`` dependency is
568-
present, a single file handle is created and persisted. If
569-
``indexed_gzip`` is not available, behavior is the same as if
570-
``keep_file_open is False``. If ``file_like`` refers to an open
571-
file handle, this setting has no effect. The default value
572-
(``None``) will result in the value of
570+
a new file handle is created every time the image is accessed.
571+
The default value (``None``) will result in the value of
573572
``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT` being used.
574573
"""
575574
file_map = klass.filespec_to_file_map(filename)

0 commit comments

Comments
 (0)