Repository: nilearn/nilearn

Issue:
add_markers on plot_glass_brain results in TypeError
Hey there!

I tried to do the following:

```python
fig = plotting.plot_glass_brain(None)
coords = [(-34, -39, -9)]
fig.add_markers(coords)
```

Unfortunately, it fails with the following error:

```python
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-8bfe69e50628> in <module>()
      4 # and third argument `marker_size` denotes size of the sphere
      5 coords = [(-34, -39, -9)]
----> 6 fig.add_markers(coords)

C:\Anaconda2\lib\site-packages\nilearn\plotting\displays.pyc in add_markers(self, marker_coords, marker_color, marker_size, **kwargs)
    833             # current slice.
    834             # XXX: should we keep this heuristic?
--> 835             mask = np.abs(third_d - coord) <= 2.
    836             xdata, ydata = marker_coords_2d.T
    837             display_ax.ax.scatter(xdata[mask], ydata[mask],

TypeError: unsupported operand type(s) for -: 'int' and 'NoneType'
```

I am using the latest nilearn from pypi.

Thanks a lot for looking into this issue!


Discussion:

Resolving patch:
diff --git a/nilearn/plotting/displays.py b/nilearn/plotting/displays.py
--- a/nilearn/plotting/displays.py
+++ b/nilearn/plotting/displays.py
@@ -829,13 +829,19 @@ def add_markers(self, marker_coords, marker_color='r', marker_size=30,
             coord = display_ax.coord
             marker_coords_2d, third_d = _coords_3d_to_2d(
                 marker_coords, direction, return_direction=True)
-            # Heuristic that plots only markers that are 2mm away from the
-            # current slice.
-            # XXX: should we keep this heuristic?
-            mask = np.abs(third_d - coord) <= 2.
             xdata, ydata = marker_coords_2d.T
-            display_ax.ax.scatter(xdata[mask], ydata[mask],
-                                  s=marker_size,
+            # Check if coord has integer represents a cut in direction
+            # to follow the heuristic. If no foreground image is given
+            # coordinate is empty or None. This case is valid for plotting
+            # markers on glass brain without any foreground image.
+            if isinstance(coord, numbers.Number):
+                # Heuristic that plots only markers that are 2mm away
+                # from the current slice.
+                # XXX: should we keep this heuristic?
+                mask = np.abs(third_d - coord) <= 2.
+                xdata = xdata[mask]
+                ydata = ydata[mask]
+            display_ax.ax.scatter(xdata, ydata, s=marker_size,
                                   c=marker_color, **kwargs)
 
     def annotate(self, left_right=True, positions=True, size=12, **kwargs):


Test patch:
diff --git a/nilearn/plotting/tests/test_displays.py b/nilearn/plotting/tests/test_displays.py
--- a/nilearn/plotting/tests/test_displays.py
+++ b/nilearn/plotting/tests/test_displays.py
@@ -91,3 +91,11 @@ def test_data_complete_mask():
     oslicer = OrthoSlicer(cut_coords=(0, 0, 0))
     oslicer.add_overlay(img)
     oslicer.close()
+
+
+def test_add_markers_cut_coords_is_none():
+    # A special case test for add_markers when cut_coords are None. This
+    # case is used when coords are placed on glass brain
+    orthoslicer = OrthoSlicer(cut_coords=(None, None, None))
+    orthoslicer.add_markers([(0, 0, 2)])
+    orthoslicer.close()
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
@@ -944,3 +944,10 @@ def test_plotting_functions_with_dim_invalid_input():
     # Test whether error raises with bad error to input
     img = _generate_img()
     assert_raises(ValueError, plot_stat_map, img, dim='-10')
+
+
+def test_add_markers_using_plot_glass_brain():
+    fig = plot_glass_brain(None)
+    coords = [(-34, -39, -9)]
+    fig.add_markers(coords)
+    fig.close()