poldracklab / pydeface

defacing utility for MRI images
MIT License
110 stars 42 forks source link

Mark images as defaced #36

Open effigies opened 3 years ago

effigies commented 3 years ago

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 commented 3 years ago

+1

On Wed, Sep 23, 2020 at 7:41 AM Chris Markiewicz notifications@github.com wrote:

Question came up on Neurostars about detecting already deface 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(...)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/poldracklab/pydeface/issues/36, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGUVEAXKKBF2PZFL6URMRLSHICKPANCNFSM4RXEBFFQ .

-- Russell A. Poldrack Albert Ray Lang Professor of Psychology and Professor (by courtesy) of Computer Science Director, DSI Center for Open and Reproducible Science Building 420 Stanford University Stanford, CA 94305

poldrack@stanford.edu http://www.poldracklab.org/

vsoch commented 3 years ago

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

ofgulban commented 3 years ago

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 commented 3 years ago

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

effigies commented 3 years ago

I proposed that field in BIDS and got some pushback, so that's stalled: https://github.com/bids-standard/bids-specification/pull/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 commented 3 years ago

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 commented 3 years ago

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