Repository: nilearn/nilearn

Issue:
plot_stat_map does not display the bg anat when it contains NaNs
Presumably because thre sampling used in plotting does not handle Nans correctly.
This is suprising because I thought that this was not occurring in previous versions of Nilearn.
Maybe related to issue #1249 ?



Discussion:
> Maybe related to issue #1249 ?

It would be great if you can tell your problem is fixed now.

Maybe this should be closed ?
>Maybe this should be closed ?

I am waiting for response from @bthirion . If there are still some things which need to be fixed.
Cannot check now, but will do next Monday hopefully.
Hi @KamalakerDadi the problem is apparently not solved: whenever I try to upload the anatomical image in e.g. /storage/store/data/ibc/derivatives/sub-07/ses-00/anat/wsub-07_ses-00_T1w.nii.gz
I cannot use it as a bg_img in plot_stat_map: nothing appears, presumably doe to the NaN issue.
> I cannot use it as a bg_img in plot_stat_map: nothing appears, presumably doe
> to the NaN issue.

Did you check that it's because of NaNs?

Indeed, when I replace Nans in the anat by 0, the image displays correctly.

Resolving patch:
diff --git a/nilearn/plotting/img_plotting.py b/nilearn/plotting/img_plotting.py
--- a/nilearn/plotting/img_plotting.py
+++ b/nilearn/plotting/img_plotting.py
@@ -366,11 +366,14 @@ def _load_anat(anat_img=MNI152TEMPLATE, dim='auto', black_bg='auto'):
             black_bg = False
     else:
         anat_img = _utils.check_niimg_3d(anat_img)
+        # Clean anat_img for non-finite values to avoid computing unnecessary
+        # border data values.
+        data = _safe_get_data(anat_img, ensure_finite=True)
+        anat_img = new_img_like(anat_img, data, affine=_get_affine(anat_img))
         if dim or black_bg == 'auto':
             # We need to inspect the values of the image
-            data = anat_img.get_data()
-            vmin = data.min()
-            vmax = data.max()
+            vmin = np.nanmin(data)
+            vmax = np.nanmax(data)
         if black_bg == 'auto':
             # Guess if the background is rather black or light based on
             # the values of voxels near the border
@@ -470,6 +473,9 @@ def plot_anat(anat_img=MNI152TEMPLATE, cut_coords=None,
         -----
         Arrays should be passed in numpy convention: (x, y, z)
         ordered.
+
+        For visualization, non-finite values found in passed 'anat_img'
+        are set to zero.
     """
     anat_img, black_bg, anat_vmin, anat_vmax = _load_anat(
         anat_img,
@@ -563,7 +569,7 @@ def plot_epi(epi_img=None, cut_coords=None, output_file=None,
     return display
 
 
-def plot_roi(roi_img, bg_img=MNI152TEMPLATE, cut_coords=None, 
+def plot_roi(roi_img, bg_img=MNI152TEMPLATE, cut_coords=None,
              output_file=None, display_mode='ortho', figure=None, axes=None,
              title=None, annotate=True, draw_cross=True, black_bg='auto',
              threshold=0.5, alpha=0.7, cmap=plt.cm.gist_ncar, dim='auto',
@@ -639,6 +645,9 @@ def plot_roi(roi_img, bg_img=MNI152TEMPLATE, cut_coords=None,
         A small threshold is applied by default to eliminate numerical
         background noise.
 
+        For visualization, non-finite values found in passed 'roi_img' or
+        'bg_img' are set to zero.
+
         See Also
         --------
         nilearn.plotting.plot_prob_atlas : To simply plot probabilistic atlases
@@ -955,6 +964,9 @@ def plot_stat_map(stat_map_img, bg_img=MNI152TEMPLATE, cut_coords=None,
         Arrays should be passed in numpy convention: (x, y, z)
         ordered.
 
+        For visualization, non-finite values found in passed 'stat_map_img' or
+        'bg_img' are set to zero.
+
         See Also
         --------
 


Test patch:
diff --git a/nilearn/plotting/tests/test_img_plotting.py b/nilearn/plotting/tests/test_img_plotting.py
--- a/nilearn/plotting/tests/test_img_plotting.py
+++ b/nilearn/plotting/tests/test_img_plotting.py
@@ -916,3 +916,24 @@ def test_plotting_functions_with_cmaps():
         plot_stat_map(img, cmap='viridis', colorbar=True)
 
     plt.close()
+
+
+def test_plotting_functions_with_nans_in_bg_img():
+    bg_img = _generate_img()
+    bg_data = bg_img.get_data()
+
+    bg_data[6, 5, 1] = np.nan
+    bg_data[1, 5, 2] = np.nan
+    bg_data[1, 3, 2] = np.nan
+    bg_data[6, 5, 2] = np.inf
+
+    bg_img = nibabel.Nifti1Image(bg_data, mni_affine)
+    plot_anat(bg_img)
+    # test with plot_roi passing background image which contains nans values
+    # in it
+    roi_img = _generate_img()
+    plot_roi(roi_img=roi_img, bg_img=bg_img)
+    stat_map_img = _generate_img()
+    plot_stat_map(stat_map_img=stat_map_img, bg_img=bg_img)
+
+    plt.close()