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

[ENH]: Rework masking logic #395

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

[ENH]: Rework masking logic #395

wants to merge 2 commits into from

Conversation

synchon
Copy link
Member

@synchon synchon commented Nov 15, 2024

          This will fix the error, but I think we have a logic issue/bug.

Right after "computing the brain mask", we resample the brain mask to the image and apply the threshold:

target_img = target_data["data"]
resampled_template = resample_to_img(
source_img=template, target_img=target_img
)
# Threshold and get mask
mask = (get_data(resampled_template) >= threshold).astype("int8")

The order made sense when we implemented: we resample and then threshold, as these are probability maps.

When we added the multi-MNI space and "warping" to masks, we now need to do the warping BEFORE thresholding. What we actually want to warp is the probability map and then apply the threshold.

In short, compute_brain_mask should already give you the mask in the required space, even if this is "native" space.

We should also be able to combine compute_brain_mask and compute_epi_mask in native space, why not?

I think the whole get function is flawed in this sense as we started adding features like combining, interescting, computing and using pre-defined masks.

The logic should be:

  1. Get all the mask images (non-computed) into whatever space we need.
  2. Get all the computed masks in the needed space
  3. Do the merge.

While this might be inefficient at some point (warping many images from the same MNI to native separately), it is a rare use case in which one might want to "merge" two or more masks that are in standard space to be used in native space. Usually (except for HCP), one has the subject-specific probseg files and can use the compute_brain_mask without warping.

A possible optimization would be: in the case that all masks are non-computed and the target space is native, warp to the intermediate required standard space and delay warping to native space at the end, after intersection/union/etc. Will not be numerically equal but would be conceptually the same in case of union/intersection (threshold 0 or 1)

Originally posted by @fraimondo in #394 (comment)

@synchon synchon added the mask Issues or pull requests related to masks label Nov 13, 2024
Copy link

PR Preview Action v1.4.8
🚀 Deployed preview to https://juaml.github.io/junifer/pr-preview/pr-395/
on branch gh-pages at 2024-11-15 16:16 UTC

@synchon synchon added this to the 0.0.6 (alpha 5) milestone Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mask Issues or pull requests related to masks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant