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

view_img always masks bg_img with MNI mask #3120

Closed
dbrennan44 opened this issue Jan 7, 2022 · 8 comments · Fixed by #3312
Closed

view_img always masks bg_img with MNI mask #3120

dbrennan44 opened this issue Jan 7, 2022 · 8 comments · Fixed by #3312
Assignees
Labels
Bug for bug reports Plotting The issue is related to plotting functionalities.

Comments

@dbrennan44
Copy link

when plotting with view_img, it appears the bg image is being "masked" by a different brain (perhaps MNI?)
image

problem persists even when using same image as bg_image and plotted img

@dbrennan44 dbrennan44 added the Bug for bug reports label Jan 7, 2022
@bthirion
Copy link
Member

bthirion commented Jan 7, 2022

Thx for reporting. Can you provide a snippet of code (and possibly an image) to reproduce this ? Best,

@dbrennan44
Copy link
Author

malf_Labels.nii.gz

import nilearn as ni
from nilearn import image, plotting

malf=image.load_img('/Users/danielbrennan/Downloads/c001/malf_Labels.nii.gz')
plotting.view_img(malf,bg_img=malf,cmap="cold_hot",threshold="99%",black_bg=False)

@dbrennan44
Copy link
Author

from messing around, it seems to be some kind of center-of-mass-like calculation which offsets the viewpoint. removing the threshold="99%" fixes the issue. But if I threshold outside of the plotting function, I get the same result

@bthirion
Copy link
Member

bthirion commented Jan 8, 2022

Looking quickly at it, it seems to amount to the fact that bg_img is handled differently than stat_map_img: in particular it is not resampled. When these images are not in MNI space this indeed leads to spurious plots.
The point is that in most cases we have dealt with so far, the bg_img would be an MNI template, or at least in MNI space.

@dbrennan44
Copy link
Author

right, I'll see if I can change that behavior; I do most of my analysis in subject-space. Seems strange to allow supplying of bg_img but resample automatically into MNI.

Should also be emphasized again that this behavior goes away (i.e. plots correctly) when omitting the threshold argument

@NicolasGensollen
Copy link
Member

@dbrennan44 view_img resamples the stat map to the background image automatically as can be seen here:

stat_map_img, mask_img = _resample_stat_map(stat_map_img, bg_img, mask_img,
resampling_interpolation)

In addition, if you don't explicitely set bg_img=False in view_img, then the MNI152 template is used and the stat map is therefore resampled to the template.

%(bg_img)s
If nothing is specified, the MNI152 template will be used.
To turn off background image, just pass "bg_img=False".
Default='MNI152'.

By setting bg_img to False, or to the stat map like you did, view_img shouldn't resample the stat map.

I'm not sure to understand why the threshold would change this though. It is only used to select the cut slices here:

cut_slices = _get_cut_slices(stat_map_img, cut_coords, threshold)

@jeromedockes jeromedockes added the Plotting The issue is related to plotting functionalities. label Jan 10, 2022
@jeromedockes
Copy link
Member

  • the bug is here; when preparing the "brain sprite" for the background a mask is computed (not sure why the background needs to be masked, in fact I don't think it should). the problem is that this is done with compute_brain_mask which just thresholds the mni152 template after resampling it to its first argument's resolution -- this doesn't work if the image isn't in mni space
  • the problem also happens when there is no thresholding, but in that case we just don't see it because the background map is completely hidden behind the foreground map
  • it is not a resampling issue, if the passed bg_img and stat map (as is the case here) have the same affine and shape the stat map isn't modified
  • setting bg_img=None (or False) doesn't really solve the problem because (i) we do want the background image to be plotted and (ii) the masking of the background still happens so we see the outline of the mni template:

Screenshot from 2022-01-10 10-34-42

@jeromedockes jeromedockes changed the title View_img displaying images incorrectly (headers?) view_img always masks bg_img with MNI mask Feb 3, 2022
@jeromedockes
Copy link
Member

During core dev meeting yesterday it was decided that:

  • the default bg_img (MNI template) would continue to be masked, ie background is transparent outside of the brain
  • any other background image will not be masked (so black_bg parameter may not be very useful in that case)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug for bug reports Plotting The issue is related to plotting functionalities.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants