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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
#!/usr/bin/env python
import math
from nose import SkipTest
from nose.tools import *
import networkx
class TestEigenvectorCentrality(object):
numpy=1 # nosetests attribute, use nosetests -a 'not numpy' to skip test
@classmethod
def setupClass(cls):
global np
try:
import numpy as np
import scipy
except ImportError:
raise SkipTest('SciPy not available.')
def test_K5(self):
"""Eigenvector centrality: K5"""
G=networkx.complete_graph(5)
b=networkx.eigenvector_centrality(G)
v=math.sqrt(1/5.0)
b_answer=dict.fromkeys(G,v)
for n in sorted(G):
assert_almost_equal(b[n],b_answer[n])
nstart = dict([(n,1) for n in G])
b=networkx.eigenvector_centrality(G,nstart=nstart)
for n in sorted(G):
assert_almost_equal(b[n],b_answer[n])
b=networkx.eigenvector_centrality_numpy(G)
for n in sorted(G):
assert_almost_equal(b[n],b_answer[n],places=3)
def test_P3(self):
"""Eigenvector centrality: P3"""
G=networkx.path_graph(3)
b_answer={0: 0.5, 1: 0.7071, 2: 0.5}
b=networkx.eigenvector_centrality_numpy(G)
for n in sorted(G):
assert_almost_equal(b[n],b_answer[n],places=4)
def test_P3_unweighted(self):
"""Eigenvector centrality: P3"""
G=networkx.path_graph(3)
b_answer={0: 0.5, 1: 0.7071, 2: 0.5}
b=networkx.eigenvector_centrality_numpy(G, weight=None)
for n in sorted(G):
assert_almost_equal(b[n],b_answer[n],places=4)
@raises(networkx.NetworkXError)
def test_maxiter(self):
G=networkx.path_graph(3)
b=networkx.eigenvector_centrality(G,max_iter=0)
class TestEigenvectorCentralityDirected(object):
numpy=1 # nosetests attribute, use nosetests -a 'not numpy' to skip test
@classmethod
def setupClass(cls):
global np
try:
import numpy as np
import scipy
except ImportError:
raise SkipTest('SciPy not available.')
def setUp(self):
G=networkx.DiGraph()
edges=[(1,2),(1,3),(2,4),(3,2),(3,5),(4,2),(4,5),(4,6),\
(5,6),(5,7),(5,8),(6,8),(7,1),(7,5),\
(7,8),(8,6),(8,7)]
G.add_edges_from(edges,weight=2.0)
self.G=G.reverse()
self.G.evc=[0.25368793, 0.19576478, 0.32817092, 0.40430835,
0.48199885, 0.15724483, 0.51346196, 0.32475403]
H=networkx.DiGraph()
edges=[(1,2),(1,3),(2,4),(3,2),(3,5),(4,2),(4,5),(4,6),\
(5,6),(5,7),(5,8),(6,8),(7,1),(7,5),\
(7,8),(8,6),(8,7)]
G.add_edges_from(edges)
self.H=G.reverse()
self.H.evc=[0.25368793, 0.19576478, 0.32817092, 0.40430835,
0.48199885, 0.15724483, 0.51346196, 0.32475403]
def test_eigenvector_centrality_weighted(self):
G=self.G
p=networkx.eigenvector_centrality(G)
for (a,b) in zip(list(p.values()),self.G.evc):
assert_almost_equal(a,b,places=4)
def test_eigenvector_centrality_weighted_numpy(self):
G=self.G
p=networkx.eigenvector_centrality_numpy(G)
for (a,b) in zip(list(p.values()),self.G.evc):
assert_almost_equal(a,b)
def test_eigenvector_centrality_unweighted(self):
G=self.H
p=networkx.eigenvector_centrality(G)
for (a,b) in zip(list(p.values()),self.G.evc):
assert_almost_equal(a,b,places=4)
def test_eigenvector_centrality_unweighted_numpy(self):
G=self.H
p=networkx.eigenvector_centrality_numpy(G)
for (a,b) in zip(list(p.values()),self.G.evc):
assert_almost_equal(a,b)
class TestEigenvectorCentralityExceptions(object):
numpy=1 # nosetests attribute, use nosetests -a 'not numpy' to skip test
@classmethod
def setupClass(cls):
global np
try:
import numpy as np
import scipy
except ImportError:
raise SkipTest('SciPy not available.')
numpy=1 # nosetests attribute, use nosetests -a 'not numpy' to skip test
@raises(networkx.NetworkXException)
def test_multigraph(self):
e = networkx.eigenvector_centrality(networkx.MultiGraph())
@raises(networkx.NetworkXException)
def test_multigraph_numpy(self):
e = networkx.eigenvector_centrality_numpy(networkx.MultiGraph())
@raises(networkx.NetworkXException)
def test_empty(self):
e = networkx.eigenvector_centrality(networkx.Graph())
@raises(networkx.NetworkXException)
def test_empty_numpy(self):
e = networkx.eigenvector_centrality_numpy(networkx.Graph())
|