Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / centrality / tests / test_eigenvector_centrality.py @ 5cef0f13

History | View | Annotate | Download (4.72 KB)

1
#!/usr/bin/env python
2
import math
3
from nose import SkipTest
4
from nose.tools import *
5
import networkx as nx
6

    
7

    
8
class TestEigenvectorCentrality(object):
9
    numpy = 1  # nosetests attribute, use nosetests -a 'not numpy' to skip test
10

    
11
    @classmethod
12
    def setupClass(cls):
13
        global np
14
        try:
15
            import numpy as np
16
            import scipy
17
        except ImportError:
18
            raise SkipTest('SciPy not available.')
19

    
20
    def test_K5(self):
21
        """Eigenvector centrality: K5"""
22
        G = nx.complete_graph(5)
23
        b = nx.eigenvector_centrality(G)
24
        v = math.sqrt(1 / 5.0)
25
        b_answer = dict.fromkeys(G, v)
26
        for n in sorted(G):
27
            assert_almost_equal(b[n], b_answer[n])
28
        nstart = dict([(n, 1) for n in G])
29
        b = nx.eigenvector_centrality(G, nstart=nstart)
30
        for n in sorted(G):
31
            assert_almost_equal(b[n], b_answer[n])
32

    
33
        b = nx.eigenvector_centrality_numpy(G)
34
        for n in sorted(G):
35
            assert_almost_equal(b[n], b_answer[n], places=3)
36

    
37
    def test_P3(self):
38
        """Eigenvector centrality: P3"""
39
        G = nx.path_graph(3)
40
        b_answer = {0: 0.5, 1: 0.7071, 2: 0.5}
41
        b = nx.eigenvector_centrality_numpy(G)
42
        for n in sorted(G):
43
            assert_almost_equal(b[n], b_answer[n], places=4)
44
        b = nx.eigenvector_centrality(G)
45
        for n in sorted(G):
46
            assert_almost_equal(b[n], b_answer[n], places=4)
47

    
48
    def test_P3_unweighted(self):
49
        """Eigenvector centrality: P3"""
50
        G = nx.path_graph(3)
51
        b_answer = {0: 0.5, 1: 0.7071, 2: 0.5}
52
        b = nx.eigenvector_centrality_numpy(G, weight=None)
53
        for n in sorted(G):
54
            assert_almost_equal(b[n], b_answer[n], places=4)
55

    
56
    @raises(nx.PowerIterationFailedConvergence)
57
    def test_maxiter(self):
58
        G = nx.path_graph(3)
59
        b = nx.eigenvector_centrality(G, max_iter=0)
60

    
61

    
62
class TestEigenvectorCentralityDirected(object):
63
    numpy = 1  # nosetests attribute, use nosetests -a 'not numpy' to skip test
64

    
65
    @classmethod
66
    def setupClass(cls):
67
        global np
68
        try:
69
            import numpy as np
70
            import scipy
71
        except ImportError:
72
            raise SkipTest('SciPy not available.')
73

    
74
    def setUp(self):
75

    
76
        G = nx.DiGraph()
77

    
78
        edges = [(1, 2), (1, 3), (2, 4), (3, 2), (3, 5), (4, 2), (4, 5), (4, 6),
79
                 (5, 6), (5, 7), (5, 8), (6, 8), (7, 1), (7, 5),
80
                 (7, 8), (8, 6), (8, 7)]
81

    
82
        G.add_edges_from(edges, weight=2.0)
83
        self.G = G.reverse()
84
        self.G.evc = [0.25368793,  0.19576478,  0.32817092,  0.40430835,
85
                      0.48199885, 0.15724483,  0.51346196,  0.32475403]
86

    
87
        H = nx.DiGraph()
88

    
89
        edges = [(1, 2), (1, 3), (2, 4), (3, 2), (3, 5), (4, 2), (4, 5), (4, 6),
90
                 (5, 6), (5, 7), (5, 8), (6, 8), (7, 1), (7, 5),
91
                 (7, 8), (8, 6), (8, 7)]
92

    
93
        G.add_edges_from(edges)
94
        self.H = G.reverse()
95
        self.H.evc = [0.25368793,  0.19576478,  0.32817092,  0.40430835,
96
                      0.48199885, 0.15724483,  0.51346196,  0.32475403]
97

    
98
    def test_eigenvector_centrality_weighted(self):
99
        G = self.G
100
        p = nx.eigenvector_centrality(G)
101
        for (a, b) in zip(list(p.values()), self.G.evc):
102
            assert_almost_equal(a, b, places=4)
103

    
104
    def test_eigenvector_centrality_weighted_numpy(self):
105
        G = self.G
106
        p = nx.eigenvector_centrality_numpy(G)
107
        for (a, b) in zip(list(p.values()), self.G.evc):
108
            assert_almost_equal(a, b)
109

    
110
    def test_eigenvector_centrality_unweighted(self):
111
        G = self.H
112
        p = nx.eigenvector_centrality(G)
113
        for (a, b) in zip(list(p.values()), self.G.evc):
114
            assert_almost_equal(a, b, places=4)
115

    
116
    def test_eigenvector_centrality_unweighted_numpy(self):
117
        G = self.H
118
        p = nx.eigenvector_centrality_numpy(G)
119
        for (a, b) in zip(list(p.values()), self.G.evc):
120
            assert_almost_equal(a, b)
121

    
122

    
123
class TestEigenvectorCentralityExceptions(object):
124
    numpy = 1  # nosetests attribute, use nosetests -a 'not numpy' to skip test
125

    
126
    @classmethod
127
    def setupClass(cls):
128
        global np
129
        try:
130
            import numpy as np
131
            import scipy
132
        except ImportError:
133
            raise SkipTest('SciPy not available.')
134

    
135
    @raises(nx.NetworkXException)
136
    def test_multigraph(self):
137
        e = nx.eigenvector_centrality(nx.MultiGraph())
138

    
139
    @raises(nx.NetworkXException)
140
    def test_multigraph_numpy(self):
141
        e = nx.eigenvector_centrality_numpy(nx.MultiGraph())
142

    
143
    @raises(nx.NetworkXException)
144
    def test_empty(self):
145
        e = nx.eigenvector_centrality(nx.Graph())
146

    
147
    @raises(nx.NetworkXException)
148
    def test_empty_numpy(self):
149
        e = nx.eigenvector_centrality_numpy(nx.Graph())