Repository: nilearn/nilearn

Issue:
add_contours shrinks brain
Hi folks.
I have noticed a difference between `plot_anat` and `plot_roi` regarding the size in which the brain is displayed. With `plot_roi` it nearly fills the entire image, with `plot_anat` it is much smaller.
Is there a way to zoom in on the `plot_anat` images as well? Thanks.

`plot_roi`
![plot_roi](https://cloud.githubusercontent.com/assets/5772811/25886858/cc9d9bfe-355f-11e7-8775-a1dab44586d0.png)
`plot_anat`
![plot_anat](https://cloud.githubusercontent.com/assets/5772811/25886857/cc9c54b0-355f-11e7-9909-318c6a3927e8.png)



Discussion:
It could be because plot_roi has a tiny threshold by default and hence removes the background. Try adding a small threshold to your plot_anat 

⁣Sent from my phone, please excuse typos and briefness​

On May 10, 2017, 09:11, at 09:11, Franz Liem <notifications@github.com> wrote:
>Hi folks.
>I have noticed a difference between `plot_anat` and `plot_roi`
>regarding the size in which the brain is displayed. With `plot_roi` it
>nearly fills the entire image, with `plot_anat` it is much smaller.
>Is there a way to zoom in on the `plot_anat` images as well? Thanks.
>
>`plot_roi`
>![plot_roi](https://cloud.githubusercontent.com/assets/5772811/25886858/cc9d9bfe-355f-11e7-8775-a1dab44586d0.png)
>`plot_anat`
>![plot_anat](https://cloud.githubusercontent.com/assets/5772811/25886857/cc9c54b0-355f-11e7-9909-318c6a3927e8.png)
>
>
>-- 
>You are receiving this because you are subscribed to this thread.
>Reply to this email directly or view it on GitHub:
>https://github.com/nilearn/nilearn/issues/1450

That works. Thanks Gael.
Sorry to reopen.
While `plot_anat` displays the brain large if I add `threshold=1e-6`, adding a contour to the display shrinks it back again. Adding a threshold to the`.add_contours` call  doesn't help. Thanks!
```
display = plotting.plot_anat(brain, threshold=1e-6)
display.add_contours(contour, colors='r', threshold=1e-6)
```
![contours.pdf](https://github.com/nilearn/nilearn/files/989167/contours.pdf)

Yes, indeed, contours has this bug (can you change the title, please).

Basically, we need to use better the get_bounds mechanism in the
add_contours method of the objects in displays.py to set a tighter
bounding box, as we do in the _map_show method.

On Wed, May 10, 2017 at 01:30:34AM -0700, Franz Liem wrote:
> Sorry to reopen.
> While plot_anat displays the brain large if I add threshold=1e-6, adding a
> contour to the display shrinks it back again. Adding a threshold to
> the.add_contours call doesn't help. Thanks!

> display = plotting.plot_anat(brain, threshold=1e-6)
> display.add_contours(contour, colors='r', threshold=1e-6)

> contours.pdf

> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub, or mute the thread.*


-- 
    Gael Varoquaux
    Researcher, INRIA Parietal
    NeuroSpin/CEA Saclay , Bat 145, 91191 Gif-sur-Yvette France
    Phone:  ++ 33-1-69-08-79-68
    http://gael-varoquaux.info            http://twitter.com/GaelVaroquaux

@fliem It would be beneficial if you could provide image and script which reproduces this problem.

 >Basically, we need to use better the get_bounds mechanism in the add_contours method of the objects in displays.py to set a tighter bounding box, as we do in the _map_show method.

AFAIK, add_contours itself uses _map_show method. 

Resolving patch:
diff --git a/nilearn/plotting/displays.py b/nilearn/plotting/displays.py
--- a/nilearn/plotting/displays.py
+++ b/nilearn/plotting/displays.py
@@ -658,7 +658,17 @@ def _map_show(self, img, type='imshow',
         xmin_, xmax_, ymin_, ymax_, zmin_, zmax_ = \
             xmin, xmax, ymin, ymax, zmin, zmax
 
-        if hasattr(data, 'mask') and isinstance(data.mask, np.ndarray):
+        # Compute tight bounds
+        if type in ('contour', 'contourf'):
+            # Define a pseudo threshold to have a tight bounding box
+            if 'levels' in kwargs:
+                thr = 0.9 * np.min(np.abs(kwargs['levels']))
+            else:
+                thr = 1e-6
+            not_mask = np.logical_or(data > thr, data < -thr)
+            xmin_, xmax_, ymin_, ymax_, zmin_, zmax_ = \
+                get_mask_bounds(new_img_like(img, not_mask, affine))
+        elif hasattr(data, 'mask') and isinstance(data.mask, np.ndarray):
             not_mask = np.logical_not(data.mask)
             xmin_, xmax_, ymin_, ymax_, zmin_, zmax_ = \
                 get_mask_bounds(new_img_like(img, not_mask, affine))


Test patch: