Skip to content

Commit bbeb989

Browse files
committed
BF+TST: bug in nifti convert for 'unknown' space
'unknown' space should work if the header matches, but if there is no header, the zooms weren't being set, and the check was failing. Fix and test.
1 parent 95f1539 commit bbeb989

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

nipy/io/nifti_ref.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,15 @@ def nipy2nifti(img, data_dtype=None, strict=None, fix0=True):
336336
elif not out_space in ncrs.unknown_space: # no space we recognize
337337
raise NiftiError('Image world not a NIFTI world')
338338
else: # unknown space requires affine that matches
339+
# Set guessed shape to set zooms correctly
340+
hdr.set_data_shape(img.shape)
341+
# Use qform set to set the zooms, but with 'unknown' code
342+
hdr.set_qform(xyz_affine, 'unknown')
343+
hdr.set_sform(None)
339344
if not np.allclose(xyz_affine, hdr.get_base_affine()):
340345
raise NiftiError("Image world is 'unknown' but affine not "
341346
"compatible; please reset image world or "
342347
"affine")
343-
hdr.set_qform(None)
344-
hdr.set_sform(None)
345348
# Use list() to get .index method for python < 2.6
346349
input_names = list(coordmap.function_domain.coord_names)
347350
spatial_names = input_names[:3]

nipy/io/tests/test_nifti_ref.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ def test_unknown():
119119
assert_true(np.allclose(bare_affine, displaced_img.coordmap.affine))
120120
nimg = nipy2nifti(displaced_img)
121121
assert_array_equal(nimg.get_affine(), bare_affine)
122+
# Get and check coordinate map
123+
inimg = nifti2nipy(nimg)
124+
assert_true(inimg.coordmap.function_range in unknown_space)
125+
# This also so if there is no header
126+
displaced_img.metadata.pop('header')
127+
nimg = nipy2nifti(displaced_img)
128+
assert_array_equal(nimg.get_affine(), bare_affine)
129+
# Get and check coordinate map
122130
inimg = nifti2nipy(nimg)
123131
assert_true(inimg.coordmap.function_range in unknown_space)
124132

0 commit comments

Comments
 (0)