Skip to content

Nifti1Image should ensure pixdim matches affine in update_header #668

Open
@effigies

Description

@effigies

Nifti1Header.set_zooms() updates the pixdim field, but this doesn't translate to an updated affine. get_qform() will reflect the change, but get_sform() will not, and hence in almost all (perhaps all?) cases we'll get out of sync.

As img.affine is considered correct, update_header() should override pixdim settings with zooms derived from the affine. This may be best dealt with in some superclass of Nifti1Image, but we should not be creating invalid files.

>>> import numpy as np
>>> import nibabel as nib

>>> img = nib.Nifti1Image(np.random.random((2,3,4)), np.eye(4))
>>> img.header.set_zooms((2,2,2))
>>> img.to_filename('test.nii')
>>> reload = nib.load('test.nii')
>>> reload.affine
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])
>>> reload.header.get_zooms()
(2.0, 2.0, 2.0)

Sub-issue identified in #619.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions