Skip to content

Commit 4ab086b

Browse files
committed
enh: move cifti intent check into to_file_map
1 parent fc619c0 commit 4ab086b

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

nibabel/cifti2/cifti2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,8 @@ def to_file_map(self, file_map=None):
13841384
header = self._nifti_header
13851385
extension = Cifti2Extension(content=self.header.to_xml())
13861386
header.extensions.append(extension)
1387+
if header.get_intent()[0] == 'none':
1388+
raise ValueError("CIFTI image has an invalid intent code.")
13871389
data = reshape_dataobj(self.dataobj,
13881390
(1, 1, 1, 1) + self.dataobj.shape)
13891391
# If qform not set, reset pixdim values so Nifti2 does not complain
@@ -1442,6 +1444,4 @@ def save(img, filename):
14421444
filename : str
14431445
filename to which to save image
14441446
"""
1445-
if img.nifti_header.get_intent()[0] == 'none':
1446-
raise ValueError("CIFTI image has an invalid intent code.")
14471447
Cifti2Image.instance_to_filename(img, filename)

nibabel/cifti2/tests/test_new_cifti2.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from nibabel import cifti2 as ci
1313
from nibabel.tmpdirs import InTemporaryDirectory
1414

15-
from nose.tools import assert_true, assert_equal
15+
from nose.tools import assert_true, assert_equal, assert_raises
1616

1717
affine = [[-1.5, 0, 0, 90],
1818
[0, 1.5, 0, -85],
@@ -213,6 +213,8 @@ def test_dtseries():
213213
hdr = ci.Cifti2Header(matrix)
214214
data = np.random.randn(13, 9)
215215
img = ci.Cifti2Image(data, hdr)
216+
with assert_raises(ValueError):
217+
img.to_filename('test.dtseries.nii')
216218
img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_DENSE_SERIES')
217219

218220
with InTemporaryDirectory():
@@ -234,6 +236,8 @@ def test_dscalar():
234236
hdr = ci.Cifti2Header(matrix)
235237
data = np.random.randn(2, 9)
236238
img = ci.Cifti2Image(data, hdr)
239+
with assert_raises(ValueError):
240+
nib.save(img, 'test.dscalar.nii')
237241
img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_DENSE_SCALARS')
238242

239243
with InTemporaryDirectory():
@@ -255,6 +259,8 @@ def test_dlabel():
255259
hdr = ci.Cifti2Header(matrix)
256260
data = np.random.randn(2, 9)
257261
img = ci.Cifti2Image(data, hdr)
262+
with assert_raises(ValueError):
263+
ci.save(img, 'test.dlabel.nii')
258264
img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_DENSE_LABELS')
259265

260266
with InTemporaryDirectory():
@@ -379,7 +385,7 @@ def test_plabel():
379385
matrix.append(parcel_map)
380386
hdr = ci.Cifti2Header(matrix)
381387
data = np.random.randn(2, 3)
382-
img = ci.Cifti2Image(data, hdr
388+
img = ci.Cifti2Image(data, hdr)
383389
img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_UNKNOWN')
384390

385391
with InTemporaryDirectory():

0 commit comments

Comments
 (0)