Skip to content

Commit 7e37aa2

Browse files
committed
RF - make default xform code for qform, sform be ALIGNED; set qform code to 0 when setting affine
1 parent a75a805 commit 7e37aa2

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

nibabel/nifti1.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ def set_qform(self, affine, code=None):
611611
String or integer giving meaning of transform in *affine*.
612612
The default is None. If code is None, then {if current
613613
qform code is not 0, leave code as it is in the header; else
614-
set to 1 ('scanner')}.
614+
set to 2 ('aligned')}.
615615
616616
Notes
617617
-----
@@ -633,8 +633,8 @@ def set_qform(self, affine, code=None):
633633
>>> hdr.set_qform(affine)
634634
>>> np.all(hdr.get_qform() == affine)
635635
True
636-
>>> int(hdr['qform_code']) # gives 1 - scanner
637-
1
636+
>>> int(hdr['qform_code']) # gives 2 - aligned
637+
2
638638
>>> hdr.set_qform(affine, code='talairach')
639639
>>> int(hdr['qform_code'])
640640
3
@@ -648,8 +648,8 @@ def set_qform(self, affine, code=None):
648648
hdr = self._header_data
649649
if code is None:
650650
code = hdr['qform_code']
651-
if code == 0:
652-
hdr['qform_code'] = 1
651+
if code == 0: # default is 'aligned'
652+
hdr['qform_code'] = 2
653653
else:
654654
code = self._field_recoders['qform_code'][code]
655655
hdr['qform_code'] = code
@@ -701,7 +701,7 @@ def set_sform(self, affine, code=None):
701701
String or integer giving meaning of transform in *affine*.
702702
The default is None. If code is None, then {if current
703703
sform code is not 0, leave code as it is in the header; else
704-
set to 1 ('scanner')}.
704+
set to 2 ('aligned')}.
705705
706706
Examples
707707
--------
@@ -714,8 +714,8 @@ def set_sform(self, affine, code=None):
714714
>>> hdr.set_sform(affine)
715715
>>> np.all(hdr.get_sform() == affine)
716716
True
717-
>>> int(hdr['sform_code']) # gives 1 - scanner
718-
1
717+
>>> int(hdr['sform_code']) # gives 2 - aligned
718+
2
719719
>>> hdr.set_sform(affine, code='talairach')
720720
>>> int(hdr['sform_code'])
721721
3
@@ -730,7 +730,7 @@ def set_sform(self, affine, code=None):
730730
if code is None:
731731
code = hdr['sform_code']
732732
if code == 0:
733-
hdr['sform_code'] = 1
733+
hdr['sform_code'] = 2 # aligned
734734
else:
735735
code = self._field_recoders['sform_code'][code]
736736
hdr['sform_code'] = code
@@ -1376,8 +1376,8 @@ def update_header(self):
13761376
hdr = self._header
13771377
hdr['magic'] = 'ni1'
13781378
if not self._affine is None:
1379-
hdr.set_sform(self._affine)
1380-
hdr.set_qform(self._affine)
1379+
hdr.set_sform(self._affine, code='aligned')
1380+
hdr['qform_code'] = 0
13811381

13821382

13831383
class Nifti1Image(Nifti1Pair):

nibabel/tests/test_nifti1.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,9 @@ def test_qform():
222222
yield assert_true, np.allclose(A, qA, atol=1e-5)
223223
yield assert_true, np.allclose(Z, ehdr['pixdim'][1:4])
224224
xfas = nifti1.xform_codes
225-
yield assert_true, ehdr['qform_code'] == xfas['scanner']
226-
ehdr.set_qform(A, 'aligned')
227225
yield assert_true, ehdr['qform_code'] == xfas['aligned']
226+
ehdr.set_qform(A, 'scanner')
227+
yield assert_true, ehdr['qform_code'] == xfas['scanner']
228228
ehdr.set_qform(A, xfas['aligned'])
229229
yield assert_true, ehdr['qform_code'] == xfas['aligned']
230230

@@ -236,9 +236,9 @@ def test_sform():
236236
sA = ehdr.get_sform()
237237
yield assert_true, np.allclose(A, sA, atol=1e-5)
238238
xfas = nifti1.xform_codes
239-
yield assert_true, ehdr['sform_code'] == xfas['scanner']
240-
ehdr.set_sform(A, 'aligned')
241239
yield assert_true, ehdr['sform_code'] == xfas['aligned']
240+
ehdr.set_sform(A, 'scanner')
241+
yield assert_true, ehdr['sform_code'] == xfas['scanner']
242242
ehdr.set_sform(A, xfas['aligned'])
243243
yield assert_true, ehdr['sform_code'] == xfas['aligned']
244244

@@ -615,3 +615,37 @@ def test_load_pixdims():
615615
assert_array_equal(rimg_hdr.get_qform(), qaff)
616616
assert_array_equal(rimg_hdr.get_sform(), saff)
617617
assert_array_equal(rimg_hdr.get_zooms(), [2,3,4])
618+
619+
620+
def test_affines_init():
621+
# Test we are doing vaguely spec-related qform things. The 'spec' here is
622+
# some thoughts by Mark Jenkinson:
623+
# http://nifti.nimh.nih.gov/nifti-1/documentation/nifti1fields/nifti1fields_pages/qsform_brief_usage
624+
arr = np.arange(24).reshape((2,3,4))
625+
aff = np.diag([2, 3, 4, 1])
626+
# Default is sform set, qform not set
627+
img = Nifti1Image(arr, aff)
628+
hdr = img.get_header()
629+
assert_equal(hdr['qform_code'], 0)
630+
assert_equal(hdr['sform_code'], 2)
631+
# This is also true for affines with header passed
632+
qaff = np.diag([3, 4, 5, 1])
633+
saff = np.diag([6, 7, 8, 1])
634+
hdr.set_qform(qaff, code='scanner')
635+
hdr.set_sform(saff, code='talairach')
636+
img = Nifti1Image(arr, aff, hdr)
637+
new_hdr = img.get_header()
638+
# Again affine is sort of anonymous space
639+
assert_equal(new_hdr['qform_code'], 0)
640+
assert_equal(new_hdr['sform_code'], 2)
641+
assert_array_equal(new_hdr.get_sform(), aff)
642+
# But if no affine passed, codes and matrices stay the same
643+
img = Nifti1Image(arr, None, hdr)
644+
new_hdr = img.get_header()
645+
assert_equal(new_hdr['qform_code'], 1) # scanner
646+
assert_array_equal(new_hdr.get_qform(), qaff)
647+
assert_equal(new_hdr['sform_code'], 3) # Still talairach
648+
assert_array_equal(new_hdr.get_sform(), saff)
649+
650+
651+

0 commit comments

Comments
 (0)