Restore qform_code on NIFTI_NORDIC output NIfTIs#28
Open
jessefontaine wants to merge 1 commit into
Open
Conversation
MATLAB's niftiwrite drops qform_code on output (sets it to 0), even when the input has a valid qform. Downstream AFNI tools prefer qform over sform and fall back to identity-voxel-space when qform_code=0, silently misaligning oblique data. Add a small helper that patches the qform_code and qform parameters at the documented NIfTI-1 header byte offsets after each niftiwrite call. Voxel data is not touched. No new dependencies (base MATLAB only). Fixes SteenMoeller#27
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #27
Summary
niftiwriteignoresinfo.raw.qform_code/quatern_*/qoffset_*on the write path, so every NIFTI_NORDIC output ends up withqform_code = 0. AFNI tools (which prefer qform over sform) then read the output as identity-voxel-space, silently misaligning oblique data with paired fieldmaps and crashing fMRIPrep's PEPOLAR SDC workflow on strongly-oblique acquisitions. See # for the full bug analysis and reproducer.What this PR changes
restore_qform_from_source(target_file, source_info, is_compressed)at the end ofNIFTI_NORDIC.m.niftiwriteinvocations (gfactor write, complex-mode magn write, complex-mode phase write, main output write).The helper writes
qform_code,quatern_b/c/d,qoffset_x/y/z, andpixdim[0](qfac) at their documented NIfTI-1 header byte offsets. The voxel data block is never opened..nii.gzoutputs are transparently gunzipped, patched, and re-gzipped.No new dependencies — uses only
fopen/fwrite/gzip/gunzipfrom base MATLAB.Verification
On a synthetic oblique input (
qform_code = 1, 45° rotation), before this PR:mag.nii.gzNORDIC_mag.nii.gzAfter this PR, the output's qform matches the input's bit-for-bit.
Full reproducer in the linked issue.
Notes
NIFTI_NORDIC.m.NORDIC.mmay have similarniftiwritecall sites; happy to extend the patch if you confirm.save_untouch_niifrom Jimmy Shen's NIfTI Tools, which would preserve qform natively — but that adds an external dependency, which I assumed you'd want to avoid).