Repository: nilearn/nilearn

Issue:
Marker size array is not well displayed in plot_glass_brain
<!--

Hi! 
If you have: 
 -1 Questions about how to use Nilearn or 
 -2 Need analysis suggestions & recommendations?

A bunch of fMRI researchers hang out at Neurostars (http://neurostars.org/). 
Post those questions there. 
Add the tag `nilearn` (we get an email from Neurostars if you do).

Posting them here makes life more complicated for the Nilearn developers.  
-->

<!--

For the Bug Report, 
Include this information:
-------------------------
What version of Nilearn are you using?
What were you trying to do?
What did you expect will happen?
What actually happened?

List the steps you performed that revealed the bug to you.
Include any code samples. Enclose them in triple back-ticks (```)
Like this:

```
<code>
``` 
-->
What version of Nilearn are you using? 
```python
In [36]: nilearn.__version__ 
Out[36]: '0.6.0a'
```
What were you trying to do?
Plot a connectome using ```plot_connectome``` function, giving the a numpy array as node_size.

Here's the snippet to replicate:
```coords``` and ```colors``` are obtained by some functions I've implemented in my libraries.

```python
from nilearn.plotting import plot_connectome
matrix = np.random.rand(90,90)
matrix = (matrix * matrix.T)/2
t_matrix = matrix * np.int_(matrix > 0.45) #Plot only some connections
f = plot_connectome(t_matrix, 
                        coords[:90], 
                        colors[:90], 
                        t_matrix.sum(0)*100, 
                        'magma_r',
                        display_mode='lyr',
                        edge_vmin=0.45,
                        edge_vmax=0.5,
                        figure=pl.figure(figsize=(25,15)),
                        )
```

What did you expect will happen?
![fixed](https://user-images.githubusercontent.com/695037/67026948-0d33a980-f109-11e9-83f5-24c2bf1c66e1.png)

What actually happened?
The node size of left and right images are different from those obtained in coronal view.
![buggy](https://user-images.githubusercontent.com/695037/67026961-13298a80-f109-11e9-909b-f8565edcd422.png)

I've found the problem in ```displays.py```and fixed it by adding at line 417
Maybe we also need some control on array/list length.
```python
            if isinstance(marker_size, np.ndarray) or \
                isinstance(marker_size, list):
                marker_size = np.array(marker_size)[relevant_coords]
```
I can do the PR, if you are ok.


Discussion:
Yes please!

On Thu, Oct 17, 2019, 18:18 robbisg <notifications@github.com> wrote:

> What version of Nilearn are you using?
>
> In [36]: nilearn.__version__
> Out[36]: '0.6.0a'
>
> What were you trying to do?
> Plot a connectome using plot_connectome function, giving the a numpy
> array as node_size.
>
> Here's the snippet to replicate:
> coords and colors are obtained by some functions I've implemented in my
> libraries.
>
> from nilearn.plotting import plot_connectome
> matrix = np.random.rand(90,90)
> matrix = (matrix * matrix.T)/2
> t_matrix = matrix * np.int_(matrix > 0.45) #Plot only some connections
> f = plot_connectome(t_matrix,
>                         coords[:90],
>                         colors[:90],
>                         t_matrix.sum(0)*100,
>                         'magma_r',
>                         display_mode='lyr',
>                         edge_vmin=0.45,
>                         edge_vmax=0.5,
>                         figure=pl.figure(figsize=(25,15)),
>                         )
>
> What did you expect will happen?
> [image: fixed]
> <https://user-images.githubusercontent.com/695037/67026948-0d33a980-f109-11e9-83f5-24c2bf1c66e1.png>
>
> What actually happened?
> The node size of left and right images are different from those obtained
> in coronal view.
> [image: buggy]
> <https://user-images.githubusercontent.com/695037/67026961-13298a80-f109-11e9-909b-f8565edcd422.png>
>
> I've found the problem in displays.pyand fixed it by adding at line 417
> Maybe we also need some control on array/list length.
>
>             if isinstance(marker_size, np.ndarray) or \
>                 isinstance(marker_size, list):
>                 marker_size = np.array(marker_size)[relevant_coords]
>
> I can do the PR, if you are ok.
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/nilearn/nilearn/issues/2185?email_source=notifications&email_token=AB6SXRAZVHXRL5LFDHWOGT3QPCF5FA5CNFSM4JB37HLKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HSQL4QA>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AB6SXRBA5TAH3YLIWECTRNLQPCF5FANCNFSM4JB37HLA>
> .
>


Resolving patch:
diff --git a/nilearn/plotting/displays.py b/nilearn/plotting/displays.py
--- a/nilearn/plotting/displays.py
+++ b/nilearn/plotting/displays.py
@@ -403,7 +403,7 @@ def _add_markers(self, marker_coords, marker_color, marker_size, **kwargs):
                 if self.direction == 'r' and xc >= 0:
                     relevant_coords.append(cidx)
                 elif self.direction == 'l' and xc <= 0:
-                        relevant_coords.append(cidx)
+                    relevant_coords.append(cidx)
             xdata = xdata[relevant_coords]
             ydata = ydata[relevant_coords]
             # if marker_color is string for example 'red' or 'blue', then
@@ -415,6 +415,9 @@ def _add_markers(self, marker_coords, marker_color, marker_size, **kwargs):
                     len(marker_color) != 1:
                 marker_color = marker_color[relevant_coords]
 
+            if not isinstance(marker_size, numbers.Number):
+                marker_size = np.asarray(marker_size)[relevant_coords]
+
         defaults = {'marker': 'o',
                     'zorder': 1000}
         for k, v in defaults.items():


Test patch: