Home | History | Annotate | Download | only in tests
      1 #!/usr/bin/env python
      2 from nose.tools import *
      3 from nose import SkipTest
      4 from nose.plugins.attrib import attr
      5 import networkx
      6 
      7 # Example from
      8 # A. Langville and C. Meyer, "A survey of eigenvector methods of web
      9 # information retrieval."  http://citeseer.ist.psu.edu/713792.html
     10 
     11 
     12 class TestHITS:
     13 
     14     def setUp(self):
     15         
     16         G=networkx.DiGraph()
     17 
     18         edges=[(1,3),(1,5),\
     19            (2,1),\
     20            (3,5),\
     21            (5,4),(5,3),\
     22            (6,5)]
     23            
     24         G.add_edges_from(edges,weight=1)
     25         self.G=G
     26         self.G.a=dict(zip(G,[0.000000, 0.000000, 0.366025,
     27                              0.133975, 0.500000, 0.000000]))
     28         self.G.h=dict(zip(G,[ 0.366025, 0.000000, 0.211325, 
     29                               0.000000, 0.211325, 0.211325]))
     30 
     31 
     32     def test_hits(self):
     33         G=self.G
     34         h,a=networkx.hits(G,tol=1.e-08)
     35         for n in G:
     36             assert_almost_equal(h[n],G.h[n],places=4)
     37         for n in G:
     38             assert_almost_equal(a[n],G.a[n],places=4)
     39 
     40     def test_hits_nstart(self):
     41         G = self.G
     42         nstart = dict([(i, 1./2) for i in G])
     43         h, a = networkx.hits(G, nstart = nstart)
     44 
     45     @attr('numpy')
     46     def test_hits_numpy(self):
     47         try:
     48             import numpy as np
     49         except ImportError:
     50             raise SkipTest('NumPy not available.')
     51 
     52 
     53         G=self.G
     54         h,a=networkx.hits_numpy(G)
     55         for n in G:
     56             assert_almost_equal(h[n],G.h[n],places=4)
     57         for n in G:
     58             assert_almost_equal(a[n],G.a[n],places=4)
     59 
     60 
     61     def test_hits_scipy(self):
     62         try:
     63             import scipy as sp
     64         except ImportError:
     65             raise SkipTest('SciPy not available.')
     66 
     67         G=self.G
     68         h,a=networkx.hits_scipy(G,tol=1.e-08)
     69         for n in G:
     70             assert_almost_equal(h[n],G.h[n],places=4)
     71         for n in G:
     72             assert_almost_equal(a[n],G.a[n],places=4)
     73 
     74 
     75     @attr('numpy')
     76     def test_empty(self):
     77         try:
     78             import numpy
     79         except ImportError:
     80             raise SkipTest('numpy not available.')
     81         G=networkx.Graph()
     82         assert_equal(networkx.hits(G),({},{}))
     83         assert_equal(networkx.hits_numpy(G),({},{}))
     84         assert_equal(networkx.authority_matrix(G).shape,(0,0))
     85         assert_equal(networkx.hub_matrix(G).shape,(0,0))
     86 
     87     def test_empty_scipy(self):
     88         try:
     89             import scipy
     90         except ImportError:
     91             raise SkipTest('scipy not available.')
     92         G=networkx.Graph()
     93         assert_equal(networkx.hits_scipy(G),({},{}))
     94