code: def normalize_xform(img):
      
      # Let nibabel convert from affine to quaternions, and recover xform
      tmp_header = img.header.copy()
      tmp_header.set_qform(img.affine)
      xform = tmp_header.get_qform()
      xform_code = 2
  
      # Check desired codes
      qform, qform_code = img.get_qform(coded=True)
      sform, sform_code = img.get_sform(coded=True)
      if all((qform is not None and np.allclose(qform, xform),
              sform is not None and np.allclose(sform, xform),
              int(qform_code) == xform_code, int(sform_code) == xform_code)):
          return img
  
      new_img = img.__class__(img.get_data(), xform, img.header)
      # Unconditionally set sform/qform
      new_img.set_sform(xform, xform_code)
      new_img.set_qform(xform, xform_code)
      return new_img
docs: Set identical, valid qform and sform matrices in an image
  
      Selects the best available affine (sform > qform > shape-based), and
      coerces it to be qform-compatible (no shears).
  
      The resulting image represents this same affine as both qform and sform,
      and is marked as NIFTI_XFORM_ALIGNED_ANAT, indicating that it is valid,
      not aligned to template, and not necessarily preserving the original
      coordinates.
  
      If header would be unchanged, returns input image.