Skip to content

ENH: Make MRI defacing function public (cf issue: #1529) #1544

Draft
nordme wants to merge 5 commits intomne-tools:mainfrom
nordme:deface
Draft

ENH: Make MRI defacing function public (cf issue: #1529) #1544
nordme wants to merge 5 commits intomne-tools:mainfrom
nordme:deface

Conversation

@nordme
Copy link
Copy Markdown

@nordme nordme commented Mar 25, 2026

PR Description

This PR exposes a previously private function for defacing MRI's to the public API. It addresses Issue #1529

Additionally, tests are added for testing the defacing function, and fixtures necessary for testing are added as well.

Merge checklist

Maintainer, please confirm the following before merging.
If applicable:

  • All comments are resolved
  • This is not your own PR
  • All CIs are happy
  • PR title starts with [MRG]
  • whats_new.rst is updated
  • New contributors have been added to CITATION.cff
  • PR description includes phrase "closes <#issue-number>"

Copy link
Copy Markdown
Member

@larsoner larsoner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good start!

from nibabel.spatialimages import SpatialImage

defaced_mri = deface_mri(t1_image, mri_landmarks, None)
assert isinstance(defaced_mri, SpatialImage)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would go a little farther than this... for example if you get_fdata() on the original image and the defaced image, ravel the results, and np.corrcoef(x.ravel(), y.ravel())[0, 1], I'd expect the result to be high but not 1. So maybe between 0.8 and 0.9 or something? (Not sure how many voxels get zeroed, but adjust upper and lower as needed. If you have to adjust upper to 1 it's a problem, if you have to adjust lower to 0 it's a problem). Or maybe better, np.isclose(x, y, atol=...).mean() (for a suitable atol given the voxel values) gives you the proportion of voxels that remained the same, which should be quite high.

Comment thread mne_bids/write.py
Comment on lines +1297 to +1303
deface : None | dict
Dict should have two keys with positive numeric values. "Inset"
controls how close the blackout plane sits to the nasion point in
mm (a value of 5 will set the blackout plane 5 mm behind the
nasion); "theta" controls the angle of the blackout plane with
respect to vertical. Inset and theta values default to 5 and 15,
respectively.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a weird public API, let's make it better if we can. When making this public I'd expect something more like

def deface_mri(image, *, landmarks, inset=5, theta=15):

then adjust internal uses accordingly

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.

2 participants