Skip to content

Commit 56b4adc

Browse files
committed
BF - only update header if affine not close
Discussion at #55. Because affine in nifti1 stored as float32, using == to check if affine has changed would give false positives, comparing to float64 input. Brendan Moloney kindly suggested allclose fix.
1 parent 6e83d54 commit 56b4adc

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

nibabel/analyze.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ def update_header(self):
10551055
# the header, update the heaader
10561056
if self._affine is None:
10571057
return
1058-
if np.all(self._affine == hdr.get_best_affine()):
1058+
if np.allclose(self._affine, hdr.get_best_affine()):
10591059
return
10601060
RZS = self._affine[:3, :3]
10611061
vox = np.sqrt(np.sum(RZS * RZS, axis=0))

nibabel/nifti1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ def update_header(self):
14321432
# the header, update the heaader
14331433
if self._affine is None:
14341434
return
1435-
if np.all(self._affine == hdr.get_best_affine()):
1435+
if np.allclose(self._affine, hdr.get_best_affine()):
14361436
return
14371437
# Set affine into sform with default code
14381438
hdr.set_sform(self._affine, code='aligned')

nibabel/tests/test_nifti1.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,18 @@ def test_qform_cycle(self):
211211
assert_array_equal(hdr_back.get_sform(), exp_aff)
212212
assert_array_equal(hdr_back.get_qform(), exp_aff)
213213

214+
def test_header_update_affine(self):
215+
# Test that updating occurs only if affine is not allclose
216+
img = self.image_class(np.zeros((2,3,4)), np.eye(4))
217+
hdr = img.get_header()
218+
aff = img.get_affine()
219+
aff[:] = np.diag([1.1, 1.1, 1.1, 1]) # inexact floats
220+
hdr.set_qform(aff, 2)
221+
hdr.set_sform(aff, 2)
222+
img.update_header()
223+
assert_equal(hdr['sform_code'], 2)
224+
assert_equal(hdr['qform_code'], 2)
225+
214226

215227
class TestNifti1Pair(TestNifti1Image):
216228
# Run analyze-flavor spatialimage tests

0 commit comments

Comments
 (0)