Skip to content

Should as_closest_canonical drop scanner anat q/sform_code? #1427

@dangom

Description

@dangom

The current reordering of as_closest_canonical causes sform and qform codes to change.

When converting a dicom to nifti with dcm2niix, we'll get sform and qform codes 1 for NIFTI_XFORM_SCANNER_ANAT.

git clone https://github.com/effigies/nitest-dicom.git
cd nitest-dicom.git
dcm2niix -f test -o . 4d_multiframe_with_derived.dcm
python
import nibabel as nib
nii = nib.load("test.nii")
niiacc = nib.as_closest_canonical(nii)
print(nii.header['sform_code'])
print(nii.header['qform_code'])
print(niiacc.header['sform_code'])
print(niiacc.header['qform_code'])
1
1
2
0

This is counter-intuitive to me because if we do the dcm2niix conversion flipping the rows (dcm2niix -y n -f test -o . 4d_multiframe_with_derived.dcm), for example, we'd still get scanner anat. Should I thus not expect that as_closest_canonical would maintain those as well?


Ok, it seems that the thing is that as_closest_canonical is a wrapper to as_reoriented, which does return self.__class__(t_arr, new_aff, self.header). Since the new_aff is checked against the header, and those will obviously differ, upon object instantiation the sforms and qforms are changed.

I take it'd be safe to assume that, if is_closest_canonical is called on an image with qform and sform set to 1, that it should keep it that way?

So instead of:

    # Get the image class to transform the data for us
    img = img.as_reoriented(io_orientation(img.affine))

    # however, the affine may not be diagonal
    if enforce_diag and not _aff_is_diag(img.affine):
        raise OrientationError('Transformed affine is not diagonal')

    return img

one would do:

    orig_sform_code = img.get_sform(coded=True)[1]
    orig_qform_code = img.get_qform(coded=True)[1]

    # Get the image class to transform the data for us
    img = img.as_reoriented(io_orientation(img.affine))

    # however, the affine may not be diagonal
    if enforce_diag and not _aff_is_diag(img.affine):
        raise OrientationError('Transformed affine is not diagonal')

    # if sform and qform were 1 before:
    if (orig_sform_code == 1) and (orig_qform_code ==1):
        img.set_sform(img.get_sform(), orig_sform_code)
        img.set_qform(img.get_qform(), orig_qform_code)

    return img

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions