Skip to content

fix: pass mask image to N4 so intensity rescaling takes effect - #1065

Open
cpruijsen wants to merge 1 commit into
nipreps:masterfrom
cpruijsen:fix/issue-1054-5df38bf9
Open

cpruijsen wants to merge 1 commit into
nipreps:masterfrom
cpruijsen:fix/issue-1054-5df38bf9

Conversation

@cpruijsen

Copy link
Copy Markdown

Title: FIX: Pass a mask image to N4 so --rescale-intensities takes effect

Connects a brain mask to `mask_image` at every N4 node that sets `rescale_intensities=True`, so the rescaling takes effect. Fixes #1054.

`N4BiasFieldCorrection` applies `--rescale-intensities` (`-r`) only when a mask image (`-x`) is given. Without one, ANTs sets `doRescale = false` and ignores the flag. `weight_image` (`-w`) does not count as a mask for this check.

Each node now uses a mask the workflow already computes:

| Workflow | N4 node | `mask_image` source |
|---|---|---|
| `init_brain_extraction_wf` (no Atropos refinement) | `inu_n4_final` | `thr_brainmask`, the warped and thresholded template brain mask (also `out_mask`) |
| `init_atropos_wf` | `inu_n4_final` | `msk_conform`, the refined brain mask (also `out_mask`) |
| `init_n4_only_wf` | `inu_n4` | `binarize`, the binarized first input (also `out_mask`) |
| `init_enhance_and_skullstrip_bold_wf` | `n4_correct` | the tentative mask that already feeds `weight_image` (both `pre_mask` branches) |

This matches the N4 step in ANTs' `antsAtroposN4.sh`, which passes the brain mask with `-x` and the weights with `-w`.

In `afni_wf` (`niworkflows/anat/skullstrip.py`), N4 runs before skull-stripping, so no mask exists. The flag did nothing there, so I removed it.

`-x` also limits the region where N4 fits the bias field, so the `bias_corrected` outputs of these workflows change numerically. `init_n4_only_wf` changes most, because N4 used the whole image before. The no-change alternative is to drop `rescale_intensities=True` from all five nodes, which abandons the intent of 406b4e5. I chose the mask because rescaling is what that setting was for. If you prefer the no-change option, or a different mask at any node, I can update the PR.

New construction-time tests in `niworkflows/anat/tests/test_ants.py`, `niworkflows/anat/tests/test_skullstrip.py` and `niworkflows/func/tests/test_util.py` check the flag and the `mask_image` source for each node. All 7 fail on `master` and pass with this change. I did not run ANTs on real data; the issue shows the runtime effect (output range 0 to 9999 with `-x`, 0 to 14617 without).

N4BiasFieldCorrection only honors --rescale-intensities when a mask
image (-x) is provided; without one the flag is silently ignored.

Every caller that set rescale_intensities now also wires an existing
upstream brain mask into mask_image so the rescale is applied, as
antsAtroposN4.sh does for its N4 step (-x mask, -w weights):

- init_brain_extraction_wf: the warped, thresholded template brain mask
- init_atropos_wf: the refined brain mask (msk_conform output)
- init_n4_only_wf: the binarized input mask
- init_enhance_and_skullstrip_bold_wf: the tentative brain mask that
  already feeds weight_image

afni_wf runs N4 before skull-stripping, so no mask exists there; the
dead option is removed rather than left misleadingly set.

Fixes nipreps#1054.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant