Skip to content

Commit ac879aa

Browse files
committed
BF - make nifti1 load and save allow single or pair-type filenames
1 parent 0c5fba6 commit ac879aa

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

nibabel/nifti1.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import numpy.linalg as npl
1313

1414
from .volumeutils import Recoder, make_dt_codes, endian_codes
15-
from .spatialimages import HeaderDataError
15+
from .spatialimages import HeaderDataError, ImageFileError
1616
from .batteryrunners import Report
1717
from .quaternions import fillpositive, quat2mat, mat2quat
1818
from . import analyze # module import
@@ -1404,5 +1404,41 @@ def update_header(self):
14041404
hdr['vox_offset'] = min_vox_offset
14051405

14061406

1407-
load = Nifti1Image.load
1408-
save = Nifti1Image.instance_to_filename
1407+
def load(filename):
1408+
""" Load nifti1 single or pair from `filename`
1409+
1410+
Parameters
1411+
----------
1412+
filename : str
1413+
filename of image to be loaded
1414+
1415+
Returns
1416+
-------
1417+
img : Nifti1Image or Nifti1Pair
1418+
nifti1 single or pair image instance
1419+
1420+
Raises
1421+
------
1422+
ImageFileError: if `filename` doesn't look like nifti1
1423+
IOError : if `filename` does not exist
1424+
"""
1425+
try:
1426+
img = Nifti1Image.load(filename)
1427+
except ImageFileError:
1428+
return Nifti1Pair.load(filename)
1429+
return img
1430+
1431+
1432+
def save(img, filename):
1433+
""" Save nifti1 single or pair to `filename`
1434+
1435+
Parameters
1436+
----------
1437+
filename : str
1438+
filename to which to save image
1439+
"""
1440+
try:
1441+
Nifti1Image.instance_to_filename(img, filename)
1442+
except ImageFileError:
1443+
Nifti1Pair.instance_to_filename(img, filename)
1444+

nibabel/tests/test_nifti1.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
assert_raises)
2727
from nose import SkipTest
2828

29+
from ..tmpdirs import InTemporaryDirectory
2930
from ..testing import parametric, data_path
3031

3132
from . import test_analyze as tana
@@ -531,3 +532,18 @@ def test_recoded_fields():
531532
yield assert_equal(hdr.get_value_label('slice_code'),
532533
'alternating decreasing')
533534

535+
536+
def test_load():
537+
# test module level load. We try to load a nii and an .img and a .hdr and
538+
# expect to get a nifti back of single or pair type
539+
arr = np.arange(24).reshape((2,3,4))
540+
aff = np.diag([2, 3, 4, 1])
541+
simg = Nifti1Image(arr, aff)
542+
pimg = Nifti1Pair(arr, aff)
543+
with InTemporaryDirectory():
544+
nifti1.save(simg, 'test.nii')
545+
assert_array_equal(arr, nifti1.load('test.nii').get_data())
546+
nifti1.save(simg, 'test.img')
547+
assert_array_equal(arr, nifti1.load('test.img').get_data())
548+
nifti1.save(simg, 'test.hdr')
549+
assert_array_equal(arr, nifti1.load('test.hdr').get_data())

0 commit comments

Comments
 (0)