Skip to content

Commit e8935b0

Browse files
authored
Merge pull request #759 from effigies/fix/load_options
RF: Move mmap/keep_file_open parameters to DataobjImage.from_file*
2 parents 7ff785f + c1c427b commit e8935b0

File tree

10 files changed

+146
-160
lines changed

10 files changed

+146
-160
lines changed

nibabel/analyze.py

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -988,47 +988,6 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
988988
'file_map': copy_file_map(file_map)}
989989
return img
990990

991-
@classmethod
992-
@kw_only_meth(1)
993-
def from_filename(klass, filename, mmap=True, keep_file_open=None):
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.
999-
1000-
Parameters
1001-
----------
1002-
filename : str
1003-
Filename of image to load
1004-
mmap : {True, False, 'c', 'r'}, optional, keyword only
1005-
`mmap` controls the use of numpy memory mapping for reading image
1006-
array data. If False, do not try numpy ``memmap`` for data array.
1007-
If one of {'c', 'r'}, try numpy memmap with ``mode=mmap``. A
1008-
`mmap` value of True gives the same behavior as ``mmap='c'``. If
1009-
image data file cannot be memory-mapped, ignore `mmap` value and
1010-
read array from file.
1011-
keep_file_open : { None, True, False }, optional, keyword only
1012-
`keep_file_open` controls whether a new file handle is created
1013-
every time the image is accessed, or a single file handle is
1014-
created and used for the lifetime of this ``ArrayProxy``. If
1015-
``True``, a single file handle is created and used. If ``False``,
1016-
a new file handle is created every time the image is accessed.
1017-
The default value (``None``) will result in the value of
1018-
``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT`` being used.
1019-
1020-
Returns
1021-
-------
1022-
img : Analyze Image instance
1023-
'''
1024-
if mmap not in (True, False, 'c', 'r'):
1025-
raise ValueError("mmap should be one of {True, False, 'c', 'r'}")
1026-
file_map = klass.filespec_to_file_map(filename)
1027-
return klass.from_file_map(file_map, mmap=mmap,
1028-
keep_file_open=keep_file_open)
1029-
1030-
load = from_filename
1031-
1032991
@staticmethod
1033992
def _get_fileholders(file_map):
1034993
""" Return fileholder for header and image

nibabel/brikhead.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -541,40 +541,6 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
541541
return klass(data, hdr.get_affine(), header=hdr, extra=None,
542542
file_map=file_map)
543543

544-
@classmethod
545-
@kw_only_meth(1)
546-
def from_filename(klass, filename, mmap=True, keep_file_open=None):
547-
"""
548-
Creates an AFNIImage instance from `filename`
549-
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-
554-
Parameters
555-
----------
556-
filename : str
557-
Path to BRIK or HEAD file to be loaded
558-
mmap : {True, False, 'c', 'r'}, optional, keyword only
559-
`mmap` controls the use of numpy memory mapping for reading image
560-
array data. If False, do not try numpy ``memmap`` for data array.
561-
If one of {'c', 'r'}, try numpy memmap with ``mode=mmap``. A
562-
`mmap` value of True gives the same behavior as ``mmap='c'``. If
563-
image data file cannot be memory-mapped, ignore `mmap` value and
564-
read array from file.
565-
keep_file_open : {None, True, False}, optional, keyword only
566-
`keep_file_open` controls whether a new file handle is created
567-
every time the image is accessed, or a single file handle is
568-
created and used for the lifetime of this ``ArrayProxy``. If
569-
``True``, a single file handle is created and used. If ``False``,
570-
a new file handle is created every time the image is accessed.
571-
The default value (``None``) will result in the value of
572-
``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT` being used.
573-
"""
574-
file_map = klass.filespec_to_file_map(filename)
575-
return klass.from_file_map(file_map, mmap=mmap,
576-
keep_file_open=keep_file_open)
577-
578544
@classmethod
579545
def filespec_to_file_map(klass, filespec):
580546
"""
@@ -620,7 +586,5 @@ def filespec_to_file_map(klass, filespec):
620586
file_map[key].filename = fname
621587
return file_map
622588

623-
load = from_filename
624-
625589

626590
load = AFNIImage.load

nibabel/cifti2/cifti2.py

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from ..dataobj_images import DataobjImage
3030
from ..nifti2 import Nifti2Image, Nifti2Header
3131
from ..arrayproxy import reshape_dataobj
32+
from ..keywordonly import kw_only_meth
3233

3334

3435
def _float_01(val):
@@ -1355,7 +1356,8 @@ def nifti_header(self):
13551356
return self._nifti_header
13561357

13571358
@classmethod
1358-
def from_file_map(klass, file_map):
1359+
@kw_only_meth(1)
1360+
def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
13591361
""" Load a CIFTI-2 image from a file_map
13601362
13611363
Parameters
@@ -1368,7 +1370,8 @@ def from_file_map(klass, file_map):
13681370
Returns a Cifti2Image
13691371
"""
13701372
from .parse_cifti2 import _Cifti2AsNiftiImage, Cifti2Extension
1371-
nifti_img = _Cifti2AsNiftiImage.from_file_map(file_map)
1373+
nifti_img = _Cifti2AsNiftiImage.from_file_map(file_map, mmap=mmap,
1374+
keep_file_open=keep_file_open)
13721375

13731376
# Get cifti2 header
13741377
for item in nifti_img.header.extensions:
@@ -1380,7 +1383,7 @@ def from_file_map(klass, file_map):
13801383
'extension')
13811384

13821385
# Construct cifti image.
1383-
# User array proxy object where possible
1386+
# Use array proxy object where possible
13841387
dataobj = nifti_img.dataobj
13851388
return Cifti2Image(reshape_dataobj(dataobj, dataobj.shape[4:]),
13861389
header=cifti_header,
@@ -1455,33 +1458,5 @@ def set_data_dtype(self, dtype):
14551458
self._nifti_header.set_data_dtype(dtype)
14561459

14571460

1458-
def load(filename):
1459-
""" Load cifti2 from `filename`
1460-
1461-
Parameters
1462-
----------
1463-
filename : str
1464-
filename of image to be loaded
1465-
1466-
Returns
1467-
-------
1468-
img : Cifti2Image
1469-
cifti image instance
1470-
1471-
Raises
1472-
------
1473-
ImageFileError: if `filename` doesn't look like cifti
1474-
IOError : if `filename` does not exist
1475-
"""
1476-
return Cifti2Image.from_filename(filename)
1477-
1478-
1479-
def save(img, filename):
1480-
""" Save cifti to `filename`
1481-
1482-
Parameters
1483-
----------
1484-
filename : str
1485-
filename to which to save image
1486-
"""
1487-
Cifti2Image.instance_to_filename(img, filename)
1461+
load = Cifti2Image.from_filename
1462+
save = Cifti2Image.instance_to_filename

nibabel/dataobj_images.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import numpy as np
1212

1313
from .filebasedimages import FileBasedImage
14+
from .keywordonly import kw_only_meth
1415
from .deprecated import deprecate_with_version
1516

1617

@@ -404,3 +405,82 @@ def get_shape(self):
404405
""" Return shape for image
405406
"""
406407
return self.shape
408+
409+
@classmethod
410+
@kw_only_meth(1)
411+
def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
412+
''' Class method to create image from mapping in ``file_map``
413+
414+
.. deprecated:: 2.4.1
415+
``keep_file_open='auto'`` is redundant with `False` and has
416+
been deprecated. It will raise an error in nibabel 3.0.
417+
418+
Parameters
419+
----------
420+
file_map : dict
421+
Mapping with (kay, value) pairs of (``file_type``, FileHolder
422+
instance giving file-likes for each file needed for this image
423+
type.
424+
mmap : {True, False, 'c', 'r'}, optional, keyword only
425+
`mmap` controls the use of numpy memory mapping for reading image
426+
array data. If False, do not try numpy ``memmap`` for data array.
427+
If one of {'c', 'r'}, try numpy memmap with ``mode=mmap``. A
428+
`mmap` value of True gives the same behavior as ``mmap='c'``. If
429+
image data file cannot be memory-mapped, ignore `mmap` value and
430+
read array from file.
431+
keep_file_open : { None, True, False }, optional, keyword only
432+
`keep_file_open` controls whether a new file handle is created
433+
every time the image is accessed, or a single file handle is
434+
created and used for the lifetime of this ``ArrayProxy``. If
435+
``True``, a single file handle is created and used. If ``False``,
436+
a new file handle is created every time the image is accessed.
437+
If ``file_map`` refers to an open file handle, this setting has no
438+
effect. The default value (``None``) will result in the value of
439+
``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT`` being used.
440+
441+
Returns
442+
-------
443+
img : DataobjImage instance
444+
'''
445+
raise NotImplementedError
446+
447+
@classmethod
448+
@kw_only_meth(1)
449+
def from_filename(klass, filename, mmap=True, keep_file_open=None):
450+
'''Class method to create image from filename `filename`
451+
452+
.. deprecated:: 2.4.1
453+
``keep_file_open='auto'`` is redundant with `False` and has
454+
been deprecated. It will raise an error in nibabel 3.0.
455+
456+
Parameters
457+
----------
458+
filename : str
459+
Filename of image to load
460+
mmap : {True, False, 'c', 'r'}, optional, keyword only
461+
`mmap` controls the use of numpy memory mapping for reading image
462+
array data. If False, do not try numpy ``memmap`` for data array.
463+
If one of {'c', 'r'}, try numpy memmap with ``mode=mmap``. A
464+
`mmap` value of True gives the same behavior as ``mmap='c'``. If
465+
image data file cannot be memory-mapped, ignore `mmap` value and
466+
read array from file.
467+
keep_file_open : { None, True, False }, optional, keyword only
468+
`keep_file_open` controls whether a new file handle is created
469+
every time the image is accessed, or a single file handle is
470+
created and used for the lifetime of this ``ArrayProxy``. If
471+
``True``, a single file handle is created and used. If ``False``,
472+
a new file handle is created every time the image is accessed.
473+
The default value (``None``) will result in the value of
474+
``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT`` being used.
475+
476+
Returns
477+
-------
478+
img : DataobjImage instance
479+
'''
480+
if mmap not in (True, False, 'c', 'r'):
481+
raise ValueError("mmap should be one of {True, False, 'c', 'r'}")
482+
file_map = klass.filespec_to_file_map(filename)
483+
return klass.from_file_map(file_map, mmap=mmap,
484+
keep_file_open=keep_file_open)
485+
486+
load = from_filename

nibabel/ecat.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
from .arraywriters import make_array_writer
5555
from .wrapstruct import WrapStruct
5656
from .fileslice import canonical_slicers, predict_shape, slice2outax
57+
from .keywordonly import kw_only_meth
5758
from .deprecated import deprecate_with_version
5859

5960
BLOCK_SIZE = 512
@@ -873,7 +874,8 @@ def _get_fileholders(file_map):
873874
return file_map['header'], file_map['image']
874875

875876
@classmethod
876-
def from_file_map(klass, file_map):
877+
@kw_only_meth(1)
878+
def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
877879
"""class method to create image from mapping
878880
specified in file_map
879881
"""

nibabel/freesurfer/mghformat.py

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -536,17 +536,18 @@ def filespec_to_file_map(klass, filespec):
536536
@classmethod
537537
@kw_only_meth(1)
538538
def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
539-
'''Load image from ``file_map``
539+
''' Class method to create image from mapping in ``file_map``
540540
541541
.. deprecated:: 2.4.1
542542
``keep_file_open='auto'`` is redundant with `False` and has
543543
been deprecated. It will raise an error in nibabel 3.0.
544544
545545
Parameters
546546
----------
547-
file_map : None or mapping, optional
548-
files mapping. If None (default) use object's ``file_map``
549-
attribute instead
547+
file_map : dict
548+
Mapping with (kay, value) pairs of (``file_type``, FileHolder
549+
instance giving file-likes for each file needed for this image
550+
type.
550551
mmap : {True, False, 'c', 'r'}, optional, keyword only
551552
`mmap` controls the use of numpy memory mapping for reading image
552553
array data. If False, do not try numpy ``memmap`` for data array.
@@ -563,6 +564,10 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
563564
If ``file_map`` refers to an open file handle, this setting has no
564565
effect. The default value (``None``) will result in the value of
565566
``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT`` being used.
567+
568+
Returns
569+
-------
570+
img : MGHImage instance
566571
'''
567572
if mmap not in (True, False, 'c', 'r'):
568573
raise ValueError("mmap should be one of {True, False, 'c', 'r'}")
@@ -577,47 +582,6 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
577582
img = klass(data, affine, header, file_map=file_map)
578583
return img
579584

580-
@classmethod
581-
@kw_only_meth(1)
582-
def from_filename(klass, filename, mmap=True, keep_file_open=None):
583-
''' Class method to create image from filename ``filename``
584-
585-
.. deprecated:: 2.4.1
586-
``keep_file_open='auto'`` is redundant with `False` and has
587-
been deprecated. It will raise an error in nibabel 3.0.
588-
589-
Parameters
590-
----------
591-
filename : str
592-
Filename of image to load
593-
mmap : {True, False, 'c', 'r'}, optional, keyword only
594-
`mmap` controls the use of numpy memory mapping for reading image
595-
array data. If False, do not try numpy ``memmap`` for data array.
596-
If one of {'c', 'r'}, try numpy memmap with ``mode=mmap``. A
597-
`mmap` value of True gives the same behavior as ``mmap='c'``. If
598-
image data file cannot be memory-mapped, ignore `mmap` value and
599-
read array from file.
600-
keep_file_open : { None, True, False }, optional, keyword only
601-
`keep_file_open` controls whether a new file handle is created
602-
every time the image is accessed, or a single file handle is
603-
created and used for the lifetime of this ``ArrayProxy``. If
604-
``True``, a single file handle is created and used. If ``False``,
605-
a new file handle is created every time the image is accessed.
606-
The default value (``None``) will result in the value of
607-
``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT`` being used.
608-
609-
Returns
610-
-------
611-
img : MGHImage instance
612-
'''
613-
if mmap not in (True, False, 'c', 'r'):
614-
raise ValueError("mmap should be one of {True, False, 'c', 'r'}")
615-
file_map = klass.filespec_to_file_map(filename)
616-
return klass.from_file_map(file_map, mmap=mmap,
617-
keep_file_open=keep_file_open)
618-
619-
load = from_filename
620-
621585
def to_file_map(self, file_map=None):
622586
''' Write image to `file_map` or contained ``self.file_map``
623587

nibabel/minc1.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .spatialimages import SpatialHeader, SpatialImage
1818
from .fileslice import canonical_slicers
1919

20+
from .keywordonly import kw_only_meth
2021
from .deprecated import FutureWarningMixin
2122

2223
_dt_dict = {
@@ -310,7 +311,9 @@ class Minc1Image(SpatialImage):
310311
ImageArrayProxy = MincImageArrayProxy
311312

312313
@classmethod
313-
def from_file_map(klass, file_map):
314+
@kw_only_meth(1)
315+
def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
316+
# Note that mmap and keep_file_open are included for proper
314317
with file_map['image'].get_prepare_fileobj() as fobj:
315318
minc_file = Minc1File(netcdf_file(fobj))
316319
affine = minc_file.get_affine()

nibabel/minc2.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"""
2828
import numpy as np
2929

30+
from .keywordonly import kw_only_meth
3031
from .optpkg import optional_package
3132
h5py, have_h5py, setup_module = optional_package('h5py')
3233

@@ -158,7 +159,8 @@ class Minc2Image(Minc1Image):
158159
header_class = Minc2Header
159160

160161
@classmethod
161-
def from_file_map(klass, file_map):
162+
@kw_only_meth(1)
163+
def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
162164
holder = file_map['image']
163165
if holder.filename is None:
164166
raise MincError('MINC2 needs filename for load')

0 commit comments

Comments
 (0)