Skip to content

Commit

Permalink
[FIX] do not mask view_img background with MNI brain mask (#3312)
Browse files Browse the repository at this point in the history
fix issue #3120
  • Loading branch information
jeromedockes committed Aug 1, 2022
1 parent 456da45 commit 3e5f42e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
1 change: 1 addition & 0 deletions doc/changes/latest.rst
Expand Up @@ -27,6 +27,7 @@ Fixes
- Fix bug introduced due to a fix in the pre-release version of scipy (``1.9.0rc1``) which now enforces that elements of a band-pass filter must meet condition ``Wn[0] < Wn[1]``.
Now if band-pass elements are equal :func:`~nilearn.signal.butterworth` returns an unfiltered signal with a warning (:gh:`3293` by `Yasmin Mzayek`_).
- The parameter ``alpha`` is now correctly passed to :func:`~plotting.plot_glass_brain` in :func:`~plotting.plot_connectome` (:gh:`3306` by `Koen Helwegen`_).
- Fix plotting of background image in :func:`~nilearn.plotting.view_img` when the background is not the MNI template (:gh:`3312` by `Jerome Dockes`_).

Enhancements
------------
Expand Down
34 changes: 17 additions & 17 deletions nilearn/plotting/html_stat_map.py
Expand Up @@ -14,7 +14,7 @@

from nibabel.affines import apply_affine

from ..image import resample_to_img, new_img_like, reorder_img
from ..image import resample_to_img, new_img_like, reorder_img, get_data
from .js_plotting_utils import get_html_template, colorscale
from ..plotting import cm
from ..plotting.find_cuts import find_xyz_cut_coords
Expand All @@ -26,7 +26,6 @@
from .._utils.extmath import fast_abs_percentile
from .._utils.niimg import _safe_get_data
from ..datasets import load_mni152_template
from ..masking import compute_brain_mask


def _data_to_sprite(data):
Expand Down Expand Up @@ -209,19 +208,23 @@ def _load_bg_img(stat_map_img, bg_img='MNI152', black_bg='auto', dim='auto'):
Returns: bg_img, bg_min, bg_max, black_bg
"""
if (bg_img is None or bg_img is False) and black_bg == 'auto':
black_bg = False

if bg_img is not None and bg_img is not False:
if (bg_img is None or bg_img is False):
if black_bg == 'auto':
black_bg = False
bg_img = new_img_like(
stat_map_img, np.ma.masked_all(stat_map_img.shape))
bg_min, bg_max = 0, 0
else:
if isinstance(bg_img, str) and bg_img == "MNI152":
bg_img = load_mni152_template()
bg_img, black_bg, bg_min, bg_max = _load_anat(bg_img, dim=dim,
black_bg=black_bg)
else:
bg_img = new_img_like(stat_map_img, np.zeros(stat_map_img.shape),
stat_map_img.affine)
bg_min = 0
bg_max = 0
else:
bg_img = check_niimg_3d(bg_img)
masked_data = np.ma.masked_inside(
_safe_get_data(bg_img, ensure_finite=True),
-1e-6, 1e-6, copy=False)
bg_img = new_img_like(bg_img, masked_data)
bg_img, black_bg, bg_min, bg_max = _load_anat(
bg_img, dim=dim, black_bg=black_bg)
bg_img = reorder_img(bg_img, resample='nearest')
return bg_img, bg_min, bg_max, black_bg

Expand Down Expand Up @@ -313,10 +316,7 @@ def _json_view_size(params):

def _get_bg_mask_and_cmap(bg_img, black_bg):
"""Helper function of _json_view_data."""
bg_mask = _safe_get_data(compute_brain_mask(bg_img),
ensure_finite=True)
bg_mask = np.logical_not(bg_mask).astype(float)
bg_mask[bg_mask == 1] = np.nan
bg_mask = np.ma.getmaskarray(get_data(bg_img))
bg_cmap = copy.copy(matplotlib.cm.get_cmap('gray'))
if black_bg:
bg_cmap.set_bad('black')
Expand Down
8 changes: 8 additions & 0 deletions nilearn/plotting/tests/test_html_stat_map.py
Expand Up @@ -187,6 +187,14 @@ def test_load_bg_img():
_check_affine(bg_img.affine)


def test_get_bg_mask_and_cmap():
# non-regression test for issue #3120 (bg image was masked with mni
# template mask)
img, _ = _simulate_img()
mask, cmap = html_stat_map._get_bg_mask_and_cmap(img, False)
assert (mask == np.zeros(img.shape, dtype=bool)).all()


def test_resample_stat_map():

# Start with simple simulated data
Expand Down

0 comments on commit 3e5f42e

Please sign in to comment.