Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark images as defaced #36

Open
effigies opened this issue Sep 23, 2020 · 7 comments
Open

Mark images as defaced #36

effigies opened this issue Sep 23, 2020 · 7 comments

Comments

@effigies
Copy link
Contributor

effigies commented Sep 23, 2020

Question came up on Neurostars about detecting already defaced images: https://neurostars.org/t/is-it-an-issue-to-run-pydeface-twice/16866

The general problem is difficult, but we could make people's lives easier by marking images as defaced in the descrip header field (at least for NIfTI images):

defaced = nb.load(..., mmap=False)
descrip = f"pydeface {__version__}".encode()
if defaced.header["descrip"]:
    descrip += b"; " + defaced.header["descrip"]
defaced.header[descrip] = descrip[:80]
defaced.to_filename(...)
@poldrack
Copy link
Contributor

poldrack commented Sep 23, 2020 via email

@vsoch
Copy link
Contributor

vsoch commented Sep 23, 2020

For Dicom files there is a known header for this purpose https://dicom.innolitics.com/ciods/cr-image/patient/00120062

@ofgulban
Copy link
Collaborator

ofgulban commented Sep 24, 2020

I like @effigies suggestion. One danger might be writing defaced into header when the defacing was sub-optimal. I think it is hard to avoid object recognition completely to make sure something is defaced at the end.

On a related thought, I came up with a score that relates to the extent of an image being masked. This score yields values close to 0 for "untouched" images, and close to 1 for masked images. It works by taking the ratio of zero voxel counts in the source image and its gradient magnitude derivative. The idea is that if a part of an image is masked (voxel assigned to zeros) the local neighborhood of those voxels will often be exactly zero too. Such a rigid spatial structure does not exist in "untouched" MRI data due to thermal noise. I have tried this on around 10 different MR images of various contrasts and coverages that are defaced or not. I got above 0.9 for defaced images, and below 0.5 for not-defaced images. Putting my script here in case if anyone finds this interesting or useful: https://gist.github.com/ofgulban/5ce784d808187b02c5c7714ef5538579 .

Might be useful to generate a warning in pydeface to say the input might have been masked already etc.

@marcelzwiers
Copy link

Why not add a {"Defaced": True} or something like that in the json sidecar file?

@effigies
Copy link
Contributor Author

I proposed that field in BIDS and got some pushback, so that's stalled: bids-standard/bids-specification#666

Also, I don't think pydeface has any concept of a sidecar file (though it's been some time since I looked into it closely).

@marcelzwiers
Copy link

marcelzwiers commented Apr 27, 2021

I thought that BIDS allowed the user to add their own private key-value pairs to the sidecar files? Anyhow, that's what I did in the (drmaa capable) BIDS wrapper around pydeface that I made:

https://bidscoin.readthedocs.io/en/latest/finalizing.html#defacing
https://github.com/Donders-Institute/bidscoin/blob/master/bidscoin/deface.py#L117

@effigies
Copy link
Contributor Author

Yes, totally fine to add your own metadata. Just in the absence of a standard, it might not be respected by tools.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants