Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure affine attribute in sync with header after init. #688

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions nibabel/nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -1810,11 +1810,10 @@ def update_header(self):

def _affine2header(self):
""" Unconditionally set affine into the header """
hdr = self._header
# Set affine into sform with default code
hdr.set_sform(self._affine, code='aligned')
self.set_sform(self._affine, code='aligned', update_affine=True)
# Make qform 'unknown'
hdr.set_qform(self._affine, code='unknown')
self.set_qform(self._affine, code='unknown', update_affine=True)

def get_qform(self, coded=False):
""" Return 4x4 affine matrix from qform parameters in header
Expand Down
22 changes: 22 additions & 0 deletions nibabel/tests/test_nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,28 @@ def test_write_scaling(self):
with np.errstate(invalid='ignore'):
self._check_write_scaling(slope, inter, e_slope, e_inter)

def test_same_affine_before_and_after_write(self):
# Ensure affine attribute is the same as the one will be written
# It may differ because of float64 to float32 casting
IC = self.image_class
data = np.ones((5, 5, 5))
affine = np.array(
[
[2.99892688e+00, 1.00872545e-02, 7.95947611e-02,
-1.19315027e+02],
[-5.04193734e-03, 2.99400663e+00, -1.89471290e-01,
-1.19174288e+02],
[-8.00727755e-02, 1.89269632e-01, 2.99295425e+00,
-2.83687850e+01],
[0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
1.00000000e+00]
]
)
img = IC(data, affine)
img_rt = bytesio_round_trip(img)
assert_array_equal(img.affine, img.header.get_best_affine())
assert_array_equal(img.affine, img_rt.affine)


class TestNifti1Image(TestNifti1Pair):
# Run analyze-flavor spatialimage tests
Expand Down