Skip to content

Resample lesion mask into in_file's voxel grid#1050

Draft
tsalo wants to merge 2 commits into
nipreps:masterfrom
tsalo:lps-create-cfm
Draft

Resample lesion mask into in_file's voxel grid#1050
tsalo wants to merge 2 commits into
nipreps:masterfrom
tsalo:lps-create-cfm

Conversation

@tsalo

@tsalo tsalo commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Closes #1021.

Summary

create_cfm() builds the cost-function mask used to constrain registration by subtracting a lesion mask from a global/brain mask. It loaded the lesion via nb.as_closest_canonical(), reorienting it to RAS, while leaving in_file in its stored orientation. When in_file is not RAS, the two voxel arrays are in different orders, so the lesion gets subtracted from the wrong voxels — the lesion has no effect at its true location and spurious differences appear elsewhere.

This reorients/resamples the lesion mask into in_file's exact voxel grid with nibabel.processing.resample_from_to(..., order=0) before subtracting. Nearest-neighbor keeps the mask binary, and resampling by world coordinates makes the result correct for any orientation or grid — not just RAS.

Reported downstream in PennLINC/qsiprep#1023, where the normalization runs in LPS and every lesion ROI hit this bug.

Changes

  • niworkflows/interfaces/norm.py: replace as_closest_canonical lesion handling in create_cfm with resample_from_to(..., order=0); update the now-inaccurate docstring note that claimed the inputs must share a grid.
  • niworkflows/interfaces/tests/test_norm.py: add test_create_cfm_lesion_orientation, which builds an LPS in_file and an RAS single-voxel lesion at a known world coordinate and asserts the cost-function mask is excluded at exactly that world location. Fails on the old code (excluded voxel lands at the mirrored location), passes with the fix.

Testing

pytest niworkflows/interfaces/tests/test_norm.py — passes (incl. the new regression test); no other tests affected.

🤖 Generated with Claude Code

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.

Orientation error in norm.py create_cfm (Create a mask to constrain registration.).

1 participant