Repository: nilearn/nilearn

Issue:
connectome_plotter fails with nan's in adjacency matrix 
returns ''ValueError: 'adjacency_matrix' should be symmetric''



Discussion:
Off the top of my head, the symmetricity check is just checking (adjacency_matrix == adjacency_matrix.T).all().

I guess we need it a bit more clever than that.

Make the comparison with `nan_to_num`? E.g.
`tmp=np.nan_to_num(adjacency_matrix)`
 and `all_close(tmp, tmp.T)` (I don't think there is a function
`nan_equals`)? Or is this too ugly?

On Tue, Mar 3, 2015 at 3:54 PM, Loïc Estève notifications@github.com
wrote:

> Off the top of my head, the symmetricity check is just checking
> (adjacency_matrix == adjacency_matrix.T).all().
> 
> I guess we need it a bit more clever than that.
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/nilearn/nilearn/issues/479#issuecomment-76959697.

I thought of that, but there is the edge case of having a NaN in the element (i, j) and a zero in the (j, i) element. The nan_to_num solution won't catch that. In any case it should be fairly simple to fix.

> I thought of that, but there is the edge case of having a NaN in the
> element (i, j) and a zero in the (j, i) element. The nan_to_num
> solution won't catch that. In any case it should be fairly simple to
> fix.

I think that casting nans to zero is a safe bet.

> I think that casting nans to zero is a safe bet.

:+1:


Resolving patch:
diff --git a/nilearn/plotting/displays.py b/nilearn/plotting/displays.py
--- a/nilearn/plotting/displays.py
+++ b/nilearn/plotting/displays.py
@@ -1068,6 +1068,13 @@ def add_graph(self, adjacency_matrix, node_coords,
 
         node_coords = np.asarray(node_coords)
 
+        # decompress input matrix if sparse
+        if sparse.issparse(adjacency_matrix):
+            adjacency_matrix = adjacency_matrix.toarray()
+
+        # make the lines below well-behaved
+        adjacency_matrix = np.nan_to_num(adjacency_matrix)
+
         # safety checks
         if 's' in node_kwargs:
             raise ValueError("Please use 'node_size' and not 'node_kwargs' "
@@ -1101,9 +1108,6 @@ def add_graph(self, adjacency_matrix, node_coords,
                 "'adjacency_matrix' shape is {0}, 'node_coords' shape is {1}"
                 .format(adjacency_matrix_shape, node_coords_shape))
 
-        if sparse.issparse(adjacency_matrix):
-            adjacency_matrix = adjacency_matrix.toarray()
-
         if not np.allclose(adjacency_matrix, adjacency_matrix.T, rtol=1e-3):
             raise ValueError("'adjacency_matrix' should be symmetric")
 


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
@@ -316,6 +316,13 @@ def test_plot_connectome():
     plot_connectome(sparse_adjacency_matrix, node_coords,
                     **kwargs)
 
+    # NaN matrix support
+    nan_adjacency_matrix = np.array([[1., np.nan, np.nan],
+                                     [np.nan, 1., 2.],
+                                     [np.nan, 2., 1.]])
+    nan_node_coords = np.arange(3 * 3).reshape(3, 3)
+    plot_connectome(nan_adjacency_matrix, nan_node_coords, **kwargs)
+
 
 def test_plot_connectome_exceptions():
     import pylab as pl