File: test_pagerank.py

package info (click to toggle)
python-networkx 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,780 kB
  • ctags: 1,910
  • sloc: python: 29,050; makefile: 126
file content (71 lines) | stat: -rw-r--r-- 1,887 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
from nose.tools import *
from nose import SkipTest
import networkx

# Example from
# A. Langville and C. Meyer, "A survey of eigenvector methods of web
# information retrieval."  http://citeseer.ist.psu.edu/713792.html


class TestPageRank:

    def setUp(self):
        
        G=networkx.DiGraph()
        edges=[(1,2),(1,3),\
           (3,1),(3,2),(3,5),\
           (4,5),(4,6),\
           (5,4),(5,6),\
           (6,4)]
           
        G.add_edges_from(edges)
        self.G=G
        self.G.pagerank=dict(zip(G,
                                 [0.03721197,0.05395735,0.04150565,
                                  0.37508082,0.20599833, 0.28624589]))

    def test_pagerank(self):
        G=self.G
        p=networkx.pagerank(G,alpha=0.9,tol=1.e-08)
        for n in G:
            assert_almost_equal(p[n],G.pagerank[n],places=4)

    def test_numpy_pagerank(self):
        try:
            import numpy
        except ImportError:
            raise SkipTest('numpy not available.')
        G=self.G
        p=networkx.pagerank_numpy(G,alpha=0.9)
        for n in G:
            assert_almost_equal(p[n],G.pagerank[n],places=4)    


    def test_google_matrix(self):
        try:
            import numpy.linalg
        except ImportError:
            raise SkipTest('numpy not available.')
        G=self.G
        M=networkx.google_matrix(G,alpha=0.9)
        e,ev=numpy.linalg.eig(M.T)
        p=numpy.array(ev[:,0]/ev[:,0].sum())[:,0]
        for (a,b) in zip(p,self.G.pagerank.values()):
            assert_almost_equal(a,b)


    def test_scipy_pagerank(self):
        G=self.G
        try:
            import scipy
        except ImportError:
            raise SkipTest('scipy not available.')
        p=networkx.pagerank_scipy(G,alpha=0.9,tol=1.e-08)
        for n in G:
            assert_almost_equal(p[n],G.pagerank[n],places=4)