/usr/lib/python3/dist-packages/networkx/algorithms/centrality/tests/test_katz_centrality.py is in python3-networkx 1.8.1-0ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | # -*- coding: utf-8 -*-
import math
from nose import SkipTest
from nose.tools import *
import networkx
class TestKatzCentrality(object):
def test_K5(self):
"""Katz centrality: K5"""
G = networkx.complete_graph(5)
alpha = 0.1
b = networkx.katz_centrality(G, alpha)
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.katz_centrality(G, alpha, nstart=nstart)
for n in sorted(G):
assert_almost_equal(b[n], b_answer[n])
def test_P3(self):
"""Katz centrality: P3"""
alpha = 0.1
G = networkx.path_graph(3)
b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
2: 0.5598852584152162}
b = networkx.katz_centrality(G, alpha)
for n in sorted(G):
assert_almost_equal(b[n], b_answer[n], places=4)
@raises(networkx.NetworkXError)
def test_maxiter(self):
alpha = 0.1
G = networkx.path_graph(3)
b = networkx.katz_centrality(G, alpha, max_iter=0)
def test_beta_as_scalar(self):
alpha = 0.1
beta = 0.1
b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
2: 0.5598852584152162}
G = networkx.path_graph(3)
b = networkx.katz_centrality(G, alpha, beta)
for n in sorted(G):
assert_almost_equal(b[n], b_answer[n], places=4)
def test_beta_as_dict(self):
alpha = 0.1
beta = {0: 1.0, 1: 1.0, 2: 1.0}
b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
2: 0.5598852584152162}
G = networkx.path_graph(3)
b = networkx.katz_centrality(G, alpha, beta)
for n in sorted(G):
assert_almost_equal(b[n], b_answer[n], places=4)
def test_multiple_alpha(self):
alpha_list = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
for alpha in alpha_list:
b_answer = {0.1: {0: 0.5598852584152165, 1: 0.6107839182711449,
2: 0.5598852584152162},
0.2: {0: 0.5454545454545454, 1: 0.6363636363636365,
2: 0.5454545454545454},
0.3: {0: 0.5333964609104419, 1: 0.6564879518897746,
2: 0.5333964609104419},
0.4: {0: 0.5232045649263551, 1: 0.6726915834767423,
2: 0.5232045649263551},
0.5: {0: 0.5144957746691622, 1: 0.6859943117075809,
2: 0.5144957746691622},
0.6: {0: 0.5069794004195823, 1: 0.6970966755769258,
2: 0.5069794004195823}}
G = networkx.path_graph(3)
b = networkx.katz_centrality(G, alpha)
for n in sorted(G):
assert_almost_equal(b[n], b_answer[alpha][n], places=4)
@raises(networkx.NetworkXException)
def test_multigraph(self):
e = networkx.katz_centrality(networkx.MultiGraph(), 0.1)
def test_empty(self):
e = networkx.katz_centrality(networkx.Graph(), 0.1)
assert_equal(e, {})
@raises(networkx.NetworkXException)
def test_bad_beta(self):
G = networkx.Graph([(0,1)])
beta = {0:77}
e = networkx.katz_centrality(G, 0.1,beta=beta)
@raises(networkx.NetworkXException)
def test_bad_beta_numbe(self):
G = networkx.Graph([(0,1)])
e = networkx.katz_centrality(G, 0.1,beta='foo')
class TestKatzCentralityNumpy(object):
numpy = 1 # nosetests attribute, use nosetests -a 'not numpy' to skip test
@classmethod
def setupClass(cls):
global np
try:
import numpy as np
except ImportError:
raise SkipTest('NumPy not available.')
def test_K5(self):
"""Katz centrality: K5"""
G = networkx.complete_graph(5)
alpha = 0.1
b = networkx.katz_centrality(G, alpha)
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_numpy(G)
for n in sorted(G):
assert_almost_equal(b[n], b_answer[n], places=3)
def test_P3(self):
"""Katz centrality: P3"""
alpha = 0.1
G = networkx.path_graph(3)
b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
2: 0.5598852584152162}
b = networkx.katz_centrality_numpy(G, alpha)
for n in sorted(G):
assert_almost_equal(b[n], b_answer[n], places=4)
def test_beta_as_scalar(self):
alpha = 0.1
beta = 0.1
b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
2: 0.5598852584152162}
G = networkx.path_graph(3)
b = networkx.katz_centrality_numpy(G, alpha, beta)
for n in sorted(G):
assert_almost_equal(b[n], b_answer[n], places=4)
def test_beta_as_dict(self):
alpha = 0.1
beta = {0: 1.0, 1: 1.0, 2: 1.0}
b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
2: 0.5598852584152162}
G = networkx.path_graph(3)
b = networkx.katz_centrality_numpy(G, alpha, beta)
for n in sorted(G):
assert_almost_equal(b[n], b_answer[n], places=4)
def test_multiple_alpha(self):
alpha_list = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
for alpha in alpha_list:
b_answer = {0.1: {0: 0.5598852584152165, 1: 0.6107839182711449,
2: 0.5598852584152162},
0.2: {0: 0.5454545454545454, 1: 0.6363636363636365,
2: 0.5454545454545454},
0.3: {0: 0.5333964609104419, 1: 0.6564879518897746,
2: 0.5333964609104419},
0.4: {0: 0.5232045649263551, 1: 0.6726915834767423,
2: 0.5232045649263551},
0.5: {0: 0.5144957746691622, 1: 0.6859943117075809,
2: 0.5144957746691622},
0.6: {0: 0.5069794004195823, 1: 0.6970966755769258,
2: 0.5069794004195823}}
G = networkx.path_graph(3)
b = networkx.katz_centrality_numpy(G, alpha)
for n in sorted(G):
assert_almost_equal(b[n], b_answer[alpha][n], places=4)
@raises(networkx.NetworkXException)
def test_multigraph(self):
e = networkx.katz_centrality(networkx.MultiGraph(), 0.1)
def test_empty(self):
e = networkx.katz_centrality(networkx.Graph(), 0.1)
assert_equal(e, {})
@raises(networkx.NetworkXException)
def test_bad_beta(self):
G = networkx.Graph([(0,1)])
beta = {0:77}
e = networkx.katz_centrality_numpy(G, 0.1,beta=beta)
@raises(networkx.NetworkXException)
def test_bad_beta_numbe(self):
G = networkx.Graph([(0,1)])
e = networkx.katz_centrality_numpy(G, 0.1,beta='foo')
class TestKatzCentralityDirected(object):
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
self.G.alpha = 0.1
self.G.evc = [
0.3289589783189635,
0.2832077296243516,
0.3425906003685471,
0.3970420865198392,
0.41074871061646284,
0.272257430756461,
0.4201989685435462,
0.34229059218038554,
]
H = networkx.DiGraph(edges)
self.H = G
self.H.alpha = 0.1
self.H.evc = [
0.3289589783189635,
0.2832077296243516,
0.3425906003685471,
0.3970420865198392,
0.41074871061646284,
0.272257430756461,
0.4201989685435462,
0.34229059218038554,
]
def test_eigenvector_centrality_weighted(self):
G = self.G
alpha = self.G.alpha
p = networkx.katz_centrality(G, alpha)
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
alpha = self.H.alpha
p = networkx.katz_centrality(G, alpha)
for (a, b) in zip(list(p.values()), self.G.evc):
assert_almost_equal(a, b)
class TestKatzCentralityDirectedNumpy(TestKatzCentralityDirected):
numpy = 1 # nosetests attribute, use nosetests -a 'not numpy' to skip test
@classmethod
def setupClass(cls):
global np
try:
import numpy as np
except ImportError:
raise SkipTest('NumPy not available.')
def test_eigenvector_centrality_weighted(self):
G = self.G
alpha = self.G.alpha
p = networkx.katz_centrality_numpy(G, alpha)
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
alpha = self.H.alpha
p = networkx.katz_centrality_numpy(G, alpha)
for (a, b) in zip(list(p.values()), self.G.evc):
assert_almost_equal(a, b)
class TestKatzEigenvectorVKatz(object):
numpy = 1 # nosetests attribute, use nosetests -a 'not numpy' to skip test
@classmethod
def setupClass(cls):
global np
global eigvals
try:
import numpy as np
from numpy.linalg import eigvals
except ImportError:
raise SkipTest('NumPy not available.')
def test_eigenvector_v_katz_random(self):
G = networkx.gnp_random_graph(10,0.5)
l = float(max(eigvals(networkx.adjacency_matrix(G))))
e = networkx.eigenvector_centrality_numpy(G)
k = networkx.katz_centrality_numpy(G, 1.0/l)
for n in G:
assert_almost_equal(e[n], k[n])
|