Conversation
brainprep_quasiraw() was passing brainmask()'s output straight to ANTs' N4BiasFieldCorrection as the -x mask. mri_synthstrip (FreeSurfer) and fslreorient2std (FSL) each write their own NIfTI header independently, so the mask can end up on a grid that's numerically equivalent to the reoriented image but not bit-for-bit identical — down to the qform and sform of a single file sometimes disagreeing with each other by a float32 ULP or two. N4's ITK reader enforces exact geometry equality between -i and -x and aborts otherwise with: "itk::ExceptionObject: Inputs do not occupy the same physical space". Observed on real data (OpenMind), this affected a meaningful share of T1w/T2w/FLAIR scans, heavily concentrated in a handful of source datasets whose particular header conventions land on the wrong side of that tolerance. Adds interfaces.regridmask(), inserted between brainmask() and biasfield(). It rewrites the mask onto the reference image's exact grid and transcribes the raw qform/sform NIfTI fields byte-for-byte from the reference image's own header — deliberately not recomputing either form from an affine matrix, since any fresh matrix -> quaternion decomposition (including nibabel's own set_qform()) can round differently than whatever decomposition originally produced the reference's qform. Verified on 30 real OpenMind scans that previously failed this way (across 3 source datasets, both FLAIR and T1w) plus 5 that already succeeded — 35/35 pass after this change. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This branch has not been deployed
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.
Problem
brainprep_quasiraw()passesbrainmask()'s output straight to ANTs'N4BiasFieldCorrectionas the-xmask.mri_synthstrip(FreeSurfer) andfslreorient2std(FSL) each write their own NIfTI header independently, so the mask can end up on a grid that's numerically equivalent to the reoriented image but not bit-for-bit identical — down to the qform and sform of a single file sometimes disagreeing with each other by a float32 ULP or two. N4's ITK reader enforces exact geometry equality between-iand-xand aborts otherwise with:Running the current
mainquasiraw workflow against real data (OpenMind, ~72k scans across T1w/T2w/FLAIR) hit this on a meaningful share of scans, heavily concentrated in a handful of source datasets whose particular header conventions land on the wrong side of that tolerance.Fix
Adds
interfaces.regridmask(), inserted betweenbrainmask()andbiasfield(). It:nibabel, same shape/data, just re-anchored).Step 2 matters on its own: I initially tried
flirt -applyxfmwith an identity transform, and separatelynibabel'sset_qform()/set_sform()with the reference's affine — both still failed on one case, because NIfTI's qform is quaternion-based, and any fresh matrix → quaternion decomposition (includingnibabel's own) can round differently than whatever decomposition originally produced the reference's qform (e.g. FSL's). The only way to get a genuinely identical header is to not recompute anything and copy the raw fields directly.Testing
Verified against real OpenMind scans on the actual container pipeline (bind-mounting this branch's source over the installed package, no rebuild needed):
🤖 Generated with Claude Code