Repository: nilearn/nilearn

Issue:
plotting with vmax=nan crashes with a useless error
We should handle this gracefully: warn, but still run as if no vmax was passed.



Discussion:
I can take care of that.

> I can take care of that.

Thanks!

Well, you had what you deserved :P

What is the usecase? Your tried to compute vmax yourself and your image had nans?

> What is the usecase? Your tried to compute vmax yourself and your image had
> nans?

Not me, but the student with whom I am working did that.

OK. Do you think that the error message should advise the user to use `np.nan_max` rather than `np.max`?

> OK. Do you think that the error message should advise the user to use
> np.nan_max rather than np.max?

Yes, but I'd like to not have crasher, because sometimes there is a nan
in the data, and everything crashes which is a problem when you are
trying to analyse data.


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
@@ -11,6 +11,7 @@
 # Standard library imports
 import functools
 import numbers
+import warnings
 
 # Standard scientific libraries imports (more specific imports are
 # delayed, so that the part module can be used without them).
@@ -62,6 +63,20 @@ def _plot_img_with_bg(img, bg_img=None, cut_coords=None,
         display_factory: function
             takes a display_mode argument and return a display class
     """
+    show_nan_msg = False
+    if ('vmax' in kwargs and kwargs['vmax'] is not None and
+        np.isnan(kwargs['vmax'])):
+        kwargs.pop('vmax')
+        show_nan_msg = True
+    if ('vmin' in kwargs and kwargs['vmin'] is not None and
+        np.isnan(kwargs['vmin'])):
+        kwargs.pop('vmin')
+        show_nan_msg = True
+    if show_nan_msg:
+        nan_msg = ('NaN is not permitted for the vmax and vmin arguments.\n'
+                   'Tip: Use np.nan_max() instead of np.max().')
+        warnings.warn(nan_msg)
+
     if img is not False and img is not None:
         img = _utils.check_niimg(img, ensure_3d=True)
         data = img.get_data()


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
@@ -77,11 +77,11 @@ def test_plot_anat():
     import pylab as pl
     pl.switch_backend('template')
     img = _generate_img()
+
     # Test saving with empty plot
     z_slicer = plot_anat(anat_img=False, display_mode='z')
     with tempfile.TemporaryFile(suffix='.png') as fp:
         z_slicer.savefig(fp.name)
-
     z_slicer = plot_anat(display_mode='z')
     with tempfile.TemporaryFile(suffix='.png') as fp:
         z_slicer.savefig(fp.name)
@@ -103,6 +103,15 @@ def test_plot_functions():
         with tempfile.TemporaryFile(suffix='.png') as fp:
             plot_func(img, output_file=fp.name)
 
+    # test for bad input arguments (cf. #510)
+    ax = pl.subplot(111, rasterized=True)
+    with tempfile.TemporaryFile(suffix='.png') as fp:
+        plot_stat_map(
+            img, symmetric_cbar=True,
+            output_file=fp.name,
+            axes=ax, vmax=np.nan)
+    pl.close()
+
 
 def test_plot_glass_brain():
     mp.use('template', warn=False)