Skip to content

Commit 6d54618

Browse files
authored
Merge pull request #575 from pauldmccarthy/make_qform_sform_codes_int
Force sform/qform codes to be ints, rather than numpy types.
2 parents b8545ef + 1c22df1 commit 6d54618

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

doc/source/nifti_images.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ You can get the affine and the code using the ``coded=True`` argument to
237237
(array([[ -2. , 0. , 0. , 117.86],
238238
[ -0. , 1.97, -0.36, -35.72],
239239
[ 0. , 0.32, 2.17, -7.25],
240-
[ 0. , 0. , 0. , 1. ]]), array(1, dtype=int16))
240+
[ 0. , 0. , 0. , 1. ]]), 1)
241241

242242
You can set the sform with the ``set_sform()`` method of the header and
243243
the image.
@@ -256,7 +256,7 @@ Set the affine and code using the ``code`` parameter to ``set_sform()``:
256256
(array([[ 3., 0., 0., 0.],
257257
[ 0., 4., 0., 0.],
258258
[ 0., 0., 5., 0.],
259-
[ 0., 0., 0., 1.]]), array(4, dtype=int16))
259+
[ 0., 0., 0., 1.]]), 4)
260260

261261
The qform affine
262262
================
@@ -276,7 +276,7 @@ the sform: ``get_qform()``, ``set_qform()``.
276276
(array([[ -2. , 0. , 0. , 117.86],
277277
[ -0. , 1.97, -0.36, -35.72],
278278
[ 0. , 0.32, 2.17, -7.25],
279-
[ 0. , 0. , 0. , 1. ]]), array(1, dtype=int16))
279+
[ 0. , 0. , 0. , 1. ]]), 1)
280280

281281
The qform also has a corresponding ``qform_code`` with the same interpretation
282282
as the `sform_code`.
@@ -332,7 +332,7 @@ respectively:
332332
(array([[ 2., 0., 0., 0.],
333333
[ 0., 2., 0., 0.],
334334
[ 0., 0., 2., 0.],
335-
[ 0., 0., 0., 1.]]), array(2, dtype=int16))
335+
[ 0., 0., 0., 1.]]), 2)
336336
>>> img.get_qform(coded=True)
337337
(None, 0)
338338

nibabel/freesurfer/mghformat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def get_zooms(self):
251251
-------
252252
z : tuple
253253
tuple of header zoom values
254-
254+
255255
.. _mghformat: https://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/MghFormat#line-82
256256
'''
257257
# Do not return time zoom (TR) if 3D image

nibabel/nifti1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ def get_qform(self, coded=False):
911911
Qform code. Only returned if `coded` is True.
912912
"""
913913
hdr = self._structarr
914-
code = hdr['qform_code']
914+
code = int(hdr['qform_code'])
915915
if code == 0 and coded:
916916
return None, 0
917917
quat = self.get_qform_quaternion()
@@ -1054,7 +1054,7 @@ def get_sform(self, coded=False):
10541054
Sform code. Only returned if `coded` is True.
10551055
"""
10561056
hdr = self._structarr
1057-
code = hdr['sform_code']
1057+
code = int(hdr['sform_code'])
10581058
if code == 0 and coded:
10591059
return None, 0
10601060
out = np.eye(4)

nibabel/tests/test_nifti1.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import warnings
1313
import struct
1414

15+
import six
16+
1517
import numpy as np
1618

1719
from nibabel import nifti1 as nifti1
@@ -595,7 +597,7 @@ def test_intents(self):
595597
assert_raises(HeaderDataError, ehdr.set_intent, 999, (1,),
596598
allow_unknown=True)
597599
assert_raises(HeaderDataError, ehdr.set_intent, 999, (1,2),
598-
allow_unknown=True)
600+
allow_unknown=True)
599601

600602
def test_set_slice_times(self):
601603
hdr = self.header_class()
@@ -900,6 +902,23 @@ def test_set_sform(self):
900902
img.set_sform(new_affine, 2)
901903
assert_array_almost_equal(img.affine, new_affine)
902904

905+
def test_sqform_code_type(self):
906+
# make sure get_s/qform returns codes as integers
907+
img = self.image_class(np.zeros((2, 3, 4)), None)
908+
assert isinstance(img.get_sform(coded=True)[1], six.integer_types)
909+
assert isinstance(img.get_qform(coded=True)[1], six.integer_types)
910+
img.set_sform(None, 3)
911+
img.set_qform(None, 3)
912+
assert isinstance(img.get_sform(coded=True)[1], six.integer_types)
913+
assert isinstance(img.get_qform(coded=True)[1], six.integer_types)
914+
img.set_sform(None, 2.0)
915+
img.set_qform(None, 4.0)
916+
assert isinstance(img.get_sform(coded=True)[1], six.integer_types)
917+
assert isinstance(img.get_qform(coded=True)[1], six.integer_types)
918+
img.set_sform(None, img.get_sform(coded=True)[1])
919+
img.set_qform(None, img.get_qform(coded=True)[1])
920+
921+
903922
def test_hdr_diff(self):
904923
# Check an offset beyond data does not raise an error
905924
img = self.image_class(np.zeros((2, 3, 4)), np.eye(4))

0 commit comments

Comments
 (0)