Skip to content

Commit dc07879

Browse files
committed
DOC+RF: Adjust docs for ArrayProxy keep_file_open flag, and for fileslice
thread lock. Variable rename in fileslice to avoid collision with built-in.
1 parent 0b41c49 commit dc07879

File tree

5 files changed

+40
-28
lines changed

5 files changed

+40
-28
lines changed

nibabel/analyze.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
950950
`mmap` value of True gives the same behavior as ``mmap='c'``. If
951951
image data file cannot be memory-mapped, ignore `mmap` value and
952952
read array from file.
953-
keep_file_open : { 'auto', True, False }, optional, keyword only
953+
keep_file_open : { None, 'auto', True, False }, optional, keyword only
954954
`keep_file_open` controls whether a new file handle is created
955955
every time the image is accessed, or a single file handle is
956956
created and used for the lifetime of this ``ArrayProxy``. If
@@ -960,8 +960,9 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
960960
present, a single file handle is created and persisted. If
961961
``indexed_gzip`` is not available, behaviour is the same as if
962962
``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``.
963+
file handle, this setting has no effect. The default value
964+
(``None``) will result in the value of
965+
``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT`` being used.
965966
966967
Returns
967968
-------
@@ -1003,8 +1004,7 @@ def from_filename(klass, filename, mmap=True, keep_file_open=None):
10031004
`mmap` value of True gives the same behavior as ``mmap='c'``. If
10041005
image data file cannot be memory-mapped, ignore `mmap` value and
10051006
read array from file.
1006-
1007-
keep_file_open : { 'auto', True, False }, optional, keyword only
1007+
keep_file_open : { None, 'auto', True, False }, optional, keyword only
10081008
`keep_file_open` controls whether a new file handle is created
10091009
every time the image is accessed, or a single file handle is
10101010
created and used for the lifetime of this ``ArrayProxy``. If
@@ -1013,8 +1013,9 @@ def from_filename(klass, filename, mmap=True, keep_file_open=None):
10131013
``'auto'``, and the optional ``indexed_gzip`` dependency is
10141014
present, a single file handle is created and persisted. If
10151015
``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``.
1016+
``keep_file_open is False``. The default value (``None``) will
1017+
result in the value of
1018+
``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT`` being used.
10181019
10191020
Returns
10201021
-------

nibabel/arrayproxy.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def __init__(self, file_like, spec, mmap=True, keep_file_open=None):
120120
True gives the same behavior as ``mmap='c'``. If `file_like`
121121
cannot be memory-mapped, ignore `mmap` value and read array from
122122
file.
123-
keep_file_open : { 'auto', True, False }, optional, keyword only
123+
keep_file_open : { None, 'auto', True, False }, optional, keyword only
124124
`keep_file_open` controls whether a new file handle is created
125125
every time the image is accessed, or a single file handle is
126126
created and used for the lifetime of this ``ArrayProxy``. If
@@ -130,8 +130,8 @@ def __init__(self, file_like, spec, mmap=True, keep_file_open=None):
130130
present, a single file handle is created and persisted. If
131131
``indexed_gzip`` is not available, behaviour is the same as if
132132
``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``.
133+
handle, this setting has no effect. The default value (``None``)
134+
will result in the value of ``KEEP_FILE_OPEN_DEFAULT`` being used.
135135
"""
136136
if mmap not in (True, False, 'c', 'r'):
137137
raise ValueError("mmap should be one of {True, False, 'c', 'r'}")
@@ -211,8 +211,8 @@ def _should_keep_file_open(self, file_like, keep_file_open):
211211
if isinstance(keep_file_open, bool):
212212
return keep_file_open
213213
if keep_file_open != 'auto':
214-
raise ValueError(
215-
'keep_file_open should be one of {\'auto\', True, False }')
214+
raise ValueError('keep_file_open should be one of {None, '
215+
'\'auto\', True, False}')
216216

217217
# file_like is a handle - keep_file_open is irrelevant
218218
if hasattr(file_like, 'read') and hasattr(file_like, 'seek'):
@@ -257,7 +257,6 @@ def _get_fileobj(self):
257257
The specific behaviour depends on the value of the ``keep_file_open``
258258
flag that was passed to ``__init__``.
259259
260-
261260
Yields
262261
------
263262
ImageOpener

nibabel/fileslice.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919

2020
class _NullLock(object):
21-
"""The ``_NullLock`` is an object which can be used in place of a
21+
"""Can be used as no-function dummy object in place of ``threading.lock``.
22+
23+
The ``_NullLock`` is an object which can be used in place of a
2224
``threading.Lock`` object, but doesn't actually do anything.
2325
2426
It is used by the ``read_segments`` function in the event that a
@@ -648,12 +650,14 @@ def read_segments(fileobj, segments, n_bytes, lock=None):
648650
absolute file offset in bytes and number of bytes to read
649651
n_bytes : int
650652
total number of bytes that will be read
651-
lock : threading.Lock
653+
lock : {None, threading.Lock, lock-like} optional
652654
If provided, used to ensure that paired calls to ``seek`` and ``read``
653655
cannot be interrupted by another thread accessing the same ``fileobj``.
654656
Each thread which accesses the same file via ``read_segments`` must
655657
share a lock in order to ensure that the file access is thread-safe.
656-
A lock does not need to be provided for single-threaded access.
658+
A lock does not need to be provided for single-threaded access. The
659+
default value (``None``) results in a lock-like object (a
660+
``_NullLock``) which does not do anything.
657661
658662
Returns
659663
-------
@@ -763,9 +767,14 @@ def fileslice(fileobj, sliceobj, shape, dtype, offset=0, order='C',
763767
returning one of 'full', 'contiguous', None. See
764768
:func:`optimize_slicer` and see :func:`threshold_heuristic` for an
765769
example.
766-
lock: threading.Lock, optional
770+
lock : {None, threading.Lock, lock-like} optional
767771
If provided, used to ensure that paired calls to ``seek`` and ``read``
768772
cannot be interrupted by another thread accessing the same ``fileobj``.
773+
Each thread which accesses the same file via ``read_segments`` must
774+
share a lock in order to ensure that the file access is thread-safe.
775+
A lock does not need to be provided for single-threaded access. The
776+
default value (``None``) results in a lock-like object (a
777+
``_NullLock``) which does not do anything.
769778
770779
Returns
771780
-------
@@ -779,8 +788,8 @@ def fileslice(fileobj, sliceobj, shape, dtype, offset=0, order='C',
779788
segments, sliced_shape, post_slicers = calc_slicedefs(
780789
sliceobj, shape, itemsize, offset, order)
781790
n_bytes = reduce(operator.mul, sliced_shape, 1) * itemsize
782-
bytes = read_segments(fileobj, segments, n_bytes, lock)
783-
sliced = np.ndarray(sliced_shape, dtype, buffer=bytes, order=order)
791+
arr_data = read_segments(fileobj, segments, n_bytes, lock)
792+
sliced = np.ndarray(sliced_shape, dtype, buffer=arr_data, order=order)
784793
return sliced[post_slicers]
785794

786795

nibabel/freesurfer/mghformat.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
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
494+
keep_file_open : { None, 'auto', True, False }, optional, keyword only
495495
`keep_file_open` controls whether a new file handle is created
496496
every time the image is accessed, or a single file handle is
497497
created and used for the lifetime of this ``ArrayProxy``. If
@@ -501,8 +501,9 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
501501
present, a single file handle is created and persisted. If
502502
``indexed_gzip`` is not available, behaviour is the same as if
503503
``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``.
504+
file handle, this setting has no effect. The default value
505+
(``None``) will result in the value of
506+
``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT`` being used.
506507
'''
507508
if mmap not in (True, False, 'c', 'r'):
508509
raise ValueError("mmap should be one of {True, False, 'c', 'r'}")
@@ -536,7 +537,7 @@ def from_filename(klass, filename, mmap=True, keep_file_open=None):
536537
`mmap` value of True gives the same behavior as ``mmap='c'``. If
537538
image data file cannot be memory-mapped, ignore `mmap` value and
538539
read array from file.
539-
keep_file_open : { 'auto', True, False }, optional, keyword only
540+
keep_file_open : { None, 'auto', True, False }, optional, keyword only
540541
`keep_file_open` controls whether a new file handle is created
541542
every time the image is accessed, or a single file handle is
542543
created and used for the lifetime of this ``ArrayProxy``. If
@@ -545,8 +546,9 @@ def from_filename(klass, filename, mmap=True, keep_file_open=None):
545546
``'auto'``, and the optional ``indexed_gzip`` dependency is
546547
present, a single file handle is created and persisted. If
547548
``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``.
549+
``keep_file_open is False``. The default value (``None``) will
550+
result in the value of
551+
``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT`` being used.
550552
551553
Returns
552554
-------

nibabel/spm99analyze.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
261261
`mmap` value of True gives the same behavior as ``mmap='c'``. If
262262
image data file cannot be memory-mapped, ignore `mmap` value and
263263
read array from file.
264-
keep_file_open : { 'auto', True, False }, optional, keyword only
264+
keep_file_open : { None, 'auto', True, False }, optional, keyword only
265265
`keep_file_open` controls whether a new file handle is created
266266
every time the image is accessed, or a single file handle is
267267
created and used for the lifetime of this ``ArrayProxy``. If
@@ -271,8 +271,9 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
271271
present, a single file handle is created and persisted. If
272272
``indexed_gzip`` is not available, behaviour is the same as if
273273
``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``.
274+
file handle, this setting has no effect. The default value
275+
(``None``) will result in the value of
276+
``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT`` being used.
276277
277278
Returns
278279
-------

0 commit comments

Comments
 (0)