Repository: nilearn/nilearn

Issue:
Cater for almost 3D images in view_stat_map
view_stat_map fails when given a 4D image with shape[-1] = 1:

```
>>> plotting.view_stat_map(decoder.coef_img_, bg_img=haxby_dataset.anat[0])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-30-dde0b99b81f1> in <module>()
----> 1 plotting.view_stat_map(decoder.coef_img_, bg_img=haxby_dataset.anat[0])

/home/varoquau/dev/nilearn/nilearn/plotting/html_stat_map.py in view_stat_map(stat_map_img, threshold, bg_img, vmax)
     90     stat_map_img = image.resample_to_img(stat_map_img, bg_img)
     91     stat_map_img = image.new_img_like(
---> 92         stat_map_img, stat_map_img.get_data() * bg_mask.get_data())
     93     if threshold is None:
     94         abs_threshold = 'null'

ValueError: operands could not be broadcast together with shapes (124,256,256,1) (124,256,256) 
```


Discussion:

Resolving patch:
diff --git a/nilearn/plotting/html_stat_map.py b/nilearn/plotting/html_stat_map.py
--- a/nilearn/plotting/html_stat_map.py
+++ b/nilearn/plotting/html_stat_map.py
@@ -8,6 +8,7 @@
 import numpy as np
 
 from .. import image, datasets
+from .._utils import check_niimg_3d
 from .._utils.extmath import fast_abs_percentile
 from .._utils.param_validation import check_threshold
 from .js_plotting_utils import get_html_template, HTMLDocument
@@ -56,7 +57,8 @@ def view_stat_map(stat_map_img, threshold=None, bg_img=None, vmax=None):
     ----------
     stat_map_img : Niimg-like object
         See http://nilearn.github.io/manipulating_images/input_output.html
-        The statistical map image
+        The statistical map image. Should be 3D or
+        4D with exactly one time point (i.e. stat_map_img.shape[-1] = 1)
 
     threshold : str, number or None, optional (default=None)
         If None, no thresholding.
@@ -81,6 +83,7 @@ def view_stat_map(stat_map_img, threshold=None, bg_img=None, vmax=None):
         Jupyter notebook.
 
     """
+    stat_map_img = check_niimg_3d(stat_map_img, dtype='auto')
     if bg_img is None:
         bg_img = datasets.load_mni152_template()
         bg_mask = datasets.load_mni152_brain_mask()


Test patch:
diff --git a/nilearn/plotting/tests/test_html_stat_map.py b/nilearn/plotting/tests/test_html_stat_map.py
--- a/nilearn/plotting/tests/test_html_stat_map.py
+++ b/nilearn/plotting/tests/test_html_stat_map.py
@@ -39,3 +39,7 @@ def test_view_stat_map():
     _check_html(html)
     html = html_stat_map.view_stat_map(img, threshold=2., vmax=4.)
     _check_html(html)
+    img_4d = image.new_img_like(img, img.get_data()[:, :, :, np.newaxis])
+    assert len(img_4d.shape) == 4
+    html = html_stat_map.view_stat_map(img_4d, threshold=2., vmax=4.)
+    _check_html(html)
diff --git a/nilearn/plotting/tests/test_html_surface.py b/nilearn/plotting/tests/test_html_surface.py
--- a/nilearn/plotting/tests/test_html_surface.py
+++ b/nilearn/plotting/tests/test_html_surface.py
@@ -3,7 +3,7 @@
 import numpy as np
 from numpy.testing import assert_raises
 
-from nilearn import datasets, surface
+from nilearn import datasets, surface, image
 from nilearn.plotting import html_surface
 from nilearn.plotting.js_plotting_utils import decode
 from nilearn.datasets import fetch_surf_fsaverage
@@ -136,3 +136,7 @@ def test_view_img_on_surf():
     html = html_surface.view_img_on_surf(img, surf_mesh='fsaverage')
     check_html(html)
     assert_raises(DimensionError, html_surface.view_img_on_surf, [img, img])
+    img_4d = image.new_img_like(img, img.get_data()[:, :, :, np.newaxis])
+    assert len(img_4d.shape) == 4
+    html = html_surface.view_img_on_surf(img, threshold='92.3%')
+    check_html(html)
diff --git a/nilearn/plotting/tests/test_js_plotting_utils.py b/nilearn/plotting/tests/test_js_plotting_utils.py
--- a/nilearn/plotting/tests/test_js_plotting_utils.py
+++ b/nilearn/plotting/tests/test_js_plotting_utils.py
@@ -270,13 +270,13 @@ def test_temp_file_removing():
     wb_open = webbrowser.open
     webbrowser.open = _open_mock
     try:
-        html.open_in_browser(temp_file_lifetime=1.5)
+        html.open_in_browser(temp_file_lifetime=.5)
         assert os.path.isfile(html._temp_file)
-        time.sleep(1.6)
+        time.sleep(1.5)
         assert not os.path.isfile(html._temp_file)
         html.open_in_browser(temp_file_lifetime=None)
         assert os.path.isfile(html._temp_file)
-        time.sleep(1.6)
+        time.sleep(1.5)
         assert os.path.isfile(html._temp_file)
     finally:
         webbrowser.open = wb_open