Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / link_analysis / tests / test_hits.py @ 5cef0f13

History | View | Annotate | Download (2.73 KB)

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(sorted(G), [0.000000, 0.000000, 0.366025,
27
                                        0.133975, 0.500000, 0.000000]))
28
        self.G.h = dict(zip(sorted(G), [0.366025, 0.000000, 0.211325,
29
                                        0.000000, 0.211325, 0.211325]))
30

    
31
    def test_hits(self):
32
        G = self.G
33
        h, a = networkx.hits(G, tol=1.e-08)
34
        for n in G:
35
            assert_almost_equal(h[n], G.h[n], places=4)
36
        for n in G:
37
            assert_almost_equal(a[n], G.a[n], places=4)
38

    
39
    def test_hits_nstart(self):
40
        G = self.G
41
        nstart = dict([(i, 1. / 2) for i in G])
42
        h, a = networkx.hits(G, nstart=nstart)
43

    
44
    @attr('numpy')
45
    def test_hits_numpy(self):
46
        try:
47
            import numpy as np
48
        except ImportError:
49
            raise SkipTest('NumPy not available.')
50

    
51
        G = self.G
52
        h, a = networkx.hits_numpy(G)
53
        for n in G:
54
            assert_almost_equal(h[n], G.h[n], places=4)
55
        for n in G:
56
            assert_almost_equal(a[n], G.a[n], places=4)
57

    
58
    def test_hits_scipy(self):
59
        try:
60
            import scipy as sp
61
        except ImportError:
62
            raise SkipTest('SciPy not available.')
63

    
64
        G = self.G
65
        h, a = networkx.hits_scipy(G, tol=1.e-08)
66
        for n in G:
67
            assert_almost_equal(h[n], G.h[n], places=4)
68
        for n in G:
69
            assert_almost_equal(a[n], G.a[n], places=4)
70

    
71
    @attr('numpy')
72
    def test_empty(self):
73
        try:
74
            import numpy
75
        except ImportError:
76
            raise SkipTest('numpy not available.')
77
        G = networkx.Graph()
78
        assert_equal(networkx.hits(G), ({}, {}))
79
        assert_equal(networkx.hits_numpy(G), ({}, {}))
80
        assert_equal(networkx.authority_matrix(G).shape, (0, 0))
81
        assert_equal(networkx.hub_matrix(G).shape, (0, 0))
82

    
83
    def test_empty_scipy(self):
84
        try:
85
            import scipy
86
        except ImportError:
87
            raise SkipTest('scipy not available.')
88
        G = networkx.Graph()
89
        assert_equal(networkx.hits_scipy(G), ({}, {}))
90

    
91
    @raises(networkx.PowerIterationFailedConvergence)
92
    def test_hits_not_convergent(self):
93
        G = self.G
94
        networkx.hits(G, max_iter=0)