Skip to content

Commit 0f09d69

Browse files
committed
Merge pull request #90 from matthew-brett/update-header-allclose
BF - only update header if affine not close A temporary fix for float32 / float64 problem discovered by MrBago.
2 parents dcee63a + 56b4adc commit 0f09d69

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
@@ -980,7 +980,7 @@ def update_header(self):
980980
# the header, update the heaader
981981
if self._affine is None:
982982
return
983-
if np.all(self._affine == hdr.get_best_affine()):
983+
if np.allclose(self._affine, hdr.get_best_affine()):
984984
return
985985
RZS = self._affine[:3, :3]
986986
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
@@ -1434,7 +1434,7 @@ def update_header(self):
14341434
# the header, update the heaader
14351435
if self._affine is None:
14361436
return
1437-
if np.all(self._affine == hdr.get_best_affine()):
1437+
if np.allclose(self._affine, hdr.get_best_affine()):
14381438
return
14391439
# Set affine into sform with default code
14401440
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
@@ -229,6 +229,18 @@ def test_qform_cycle(self):
229229
assert_array_equal(hdr_back.get_sform(), exp_aff)
230230
assert_array_equal(hdr_back.get_qform(), exp_aff)
231231

232+
def test_header_update_affine(self):
233+
# Test that updating occurs only if affine is not allclose
234+
img = self.image_class(np.zeros((2,3,4)), np.eye(4))
235+
hdr = img.get_header()
236+
aff = img.get_affine()
237+
aff[:] = np.diag([1.1, 1.1, 1.1, 1]) # inexact floats
238+
hdr.set_qform(aff, 2)
239+
hdr.set_sform(aff, 2)
240+
img.update_header()
241+
assert_equal(hdr['sform_code'], 2)
242+
assert_equal(hdr['qform_code'], 2)
243+
232244

233245
class TestNifti1Pair(TestNifti1Image):
234246
# Run analyze-flavor spatialimage tests

0 commit comments

Comments
 (0)