Divide by zero in layout.py when calling spring_layout.

521 views
Skip to first unread message

Nicholas Dronen

unread,
Sep 18, 2012, 4:07:13 PM9/18/12
to networkx...@googlegroups.com
Using, I believe, the latest networkx from github, I'm getting this when calling nx.spring_layout on a network of 500 nodes and 838 edges.  Any idea why?  (I can zip up the network and send it to whomever knows this code, if need be.)

layout.py:514: RuntimeWarning: divide by zero encountered in long_scalars
  pos[:,i]*=scale/lim

Regards,

Nick

Aric Hagberg

unread,
Sep 19, 2012, 3:57:10 PM9/19/12
to networkx...@googlegroups.com
In general that shouldn't happen. Does it happen everytime you call
spring_layout()?
If you can't figure it out you can send me the graph and I can take a look.
Aric

Nicholas Dronen

unread,
Sep 19, 2012, 5:15:35 PM9/19/12
to networkx...@googlegroups.com
It's partially deterministic.  Start an ipython session, then:

In [1]: G=nx.complete_graph(500)

In [2]: pos=nx.spring_layout(G)
/Users/ndronen/Source/networkx/networkx/drawing/layout.py:514: RuntimeWarning: divide by zero encountered in long_scalars
  pos[:,i]*=scale/lim

After that, though, it doesn't happen until I exit and start ipython again.

Regards,

Nick


Aric

--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To post to this group, send email to networkx...@googlegroups.com.
To unsubscribe from this group, send email to networkx-discu...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/networkx-discuss?hl=en.


Aric Hagberg

unread,
Sep 19, 2012, 5:23:33 PM9/19/12
to networkx...@googlegroups.com
On Wed, Sep 19, 2012 at 3:15 PM, Nicholas Dronen <ndr...@gmail.com> wrote:
> It's partially deterministic. Start an ipython session, then:
>
> In [1]: G=nx.complete_graph(500)
>
> In [2]: pos=nx.spring_layout(G)
> /Users/ndronen/Source/networkx/networkx/drawing/layout.py:514:
> RuntimeWarning: divide by zero encountered in long_scalars
> pos[:,i]*=scale/lim

Hmm. That doesn't produce an error for me.

> After that, though, it doesn't happen until I exit and start ipython again.

Maybe the warning is suppressed after the first one.
Aric

Nicholas Dronen

unread,
Sep 19, 2012, 5:30:04 PM9/19/12
to networkx...@googlegroups.com
Maybe it's my environment?

  $ ipython --version
  0.13
  $ python -V
  Python 2.7.3

The return value from spring_layout has all coordinates set to array([0,0]), so the resulting plot is just a red dot in the middle of the frame. :-)

I'll see if there's a new numpy in MacPorts.

Regards,

Nick

Aric

Aric Hagberg

unread,
Sep 19, 2012, 5:37:10 PM9/19/12
to networkx...@googlegroups.com
On Wed, Sep 19, 2012 at 3:30 PM, Nicholas Dronen <ndr...@gmail.com> wrote:
> Maybe it's my environment?
>
> $ ipython --version
> 0.13
> $ python -V
> Python 2.7.3
>
> The return value from spring_layout has all coordinates set to array([0,0]),
> so the resulting plot is just a red dot in the middle of the frame. :-)

That definitely isn't right (alll zeros). So there is some problem
with your setup. Possibly NumPy since the spring layout uses NumPy.
You might try running
>>> networkx.test()
to see if that reports any thing strange too.
Aric

Nicholas Dronen

unread,
Sep 19, 2012, 6:11:06 PM9/19/12
to networkx...@googlegroups.com
The only exception was a warning. 

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/sparse/linalg/eigen/arpack/arpack.py:63: UserWarning: Single-precision types in `eigs` and `eighs` are not supported currently. Double precision routines are used instead.

Doing a port update outdated and easy_install update now.   Thanks.

Regards,

Nick

Nicholas Dronen

unread,
Oct 3, 2012, 10:02:55 AM10/3/12
to networkx...@googlegroups.com
The updates didn't fix the problem.  The reason it was only happening with graphs with >= 500 nodes is because such graphs are handled by _sparse_fruchterman_reingold in layout.py.  In my versions of that file (gotten both from github and using macports), the call to to_scipy_sparse_matrix in fruchterman_reingold_layout doesn't set the dtype to float.  This differs from the call to to_scipy_sparse_matrix in spectral_layout.  To wit:

$ grep -n to_scipy_sparse_matrix layout.py
233:        A=nx.to_scipy_sparse_matrix(G,weight=weight)
428:        A=nx.to_scipy_sparse_matrix(G, weight=weight,dtype='f')
 
Adding dtype='f' to the to_scipy_sparse_matrix call in fruchterman_reingold_layout fixed the problem for me.

Regards,

Nick

diff --git a/networkx/drawing/layout.py b/networkx/drawing/layout.py
index 68a6594..22eebc8 100644
--- a/networkx/drawing/layout.py
+++ b/networkx/drawing/layout.py
@@ -230,7 +230,7 @@ def fruchterman_reingold_layout(G,dim=2,
         # Sparse matrix
         if len(G) < 500:  # sparse solver for large graphs
             raise ValueError
-        A=nx.to_scipy_sparse_matrix(G,weight=weight)
+        A=nx.to_scipy_sparse_matrix(G,weight=weight,dtype='f')
         pos=_sparse_fruchterman_reingold(A,dim,pos_arr,fixed,iterations)
     except:
         A=nx.to_numpy_matrix(G,weight=weight)


Nicholas Dronen

unread,
Oct 17, 2012, 3:22:33 AM10/17/12
to networkx...@googlegroups.com

Has anyone committed this change to the repository?  The problem is real and the fix is simple.

Regards, 

Nick

On Oct 16, 2012 6:12 PM, "Jacob Jensen" <2tim...@gmail.com> wrote:
Thank you, I had this problem and this fixed it!
--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/networkx-discuss/-/nUn8EtAj8qQJ.

Aric Hagberg

unread,
Oct 17, 2012, 9:38:17 AM10/17/12
to networkx...@googlegroups.com
On Wed, Oct 17, 2012 at 1:22 AM, Nicholas Dronen <ndr...@gmail.com> wrote:
> Has anyone committed this change to the repository? The problem is real and
> the fix is simple.
>

I opened a pull request with this change:
https://github.com/networkx/networkx/pull/777
So it will be included shortly.

Aric

Nicholas Dronen

unread,
Oct 17, 2012, 9:41:39 AM10/17/12
to networkx...@googlegroups.com
Cool.  Thank you.

--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
Reply all
Reply to author
Forward
0 new messages