Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (11.1 KB)

1
# -*- coding: utf-8 -*-
2
import math
3

    
4
import networkx as nx
5
from nose import SkipTest
6
from nose.tools import assert_almost_equal, assert_equal, raises
7

    
8

    
9
class TestKatzCentrality(object):
10

    
11
    def test_K5(self):
12
        """Katz centrality: K5"""
13
        G = nx.complete_graph(5)
14
        alpha = 0.1
15
        b = nx.katz_centrality(G, alpha)
16
        v = math.sqrt(1 / 5.0)
17
        b_answer = dict.fromkeys(G, v)
18
        for n in sorted(G):
19
            assert_almost_equal(b[n], b_answer[n])
20
        nstart = dict([(n, 1) for n in G])
21
        b = nx.katz_centrality(G, alpha, nstart=nstart)
22
        for n in sorted(G):
23
            assert_almost_equal(b[n], b_answer[n])
24

    
25
    def test_P3(self):
26
        """Katz centrality: P3"""
27
        alpha = 0.1
28
        G = nx.path_graph(3)
29
        b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
30
                    2: 0.5598852584152162}
31
        b = nx.katz_centrality(G, alpha)
32
        for n in sorted(G):
33
            assert_almost_equal(b[n], b_answer[n], places=4)
34

    
35
    @raises(nx.PowerIterationFailedConvergence)
36
    def test_maxiter(self):
37
        alpha = 0.1
38
        G = nx.path_graph(3)
39
        max_iter = 0
40
        try:
41
            b = nx.katz_centrality(G, alpha, max_iter=max_iter)
42
        except nx.NetworkXError as e:
43
            assert str(max_iter) in e.args[0], "max_iter value not in error msg"
44
            raise  # So that the decorater sees the exception.
45

    
46
    def test_beta_as_scalar(self):
47
        alpha = 0.1
48
        beta = 0.1
49
        b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
50
                    2: 0.5598852584152162}
51
        G = nx.path_graph(3)
52
        b = nx.katz_centrality(G, alpha, beta)
53
        for n in sorted(G):
54
            assert_almost_equal(b[n], b_answer[n], places=4)
55

    
56
    def test_beta_as_dict(self):
57
        alpha = 0.1
58
        beta = {0: 1.0, 1: 1.0, 2: 1.0}
59
        b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
60
                    2: 0.5598852584152162}
61
        G = nx.path_graph(3)
62
        b = nx.katz_centrality(G, alpha, beta)
63
        for n in sorted(G):
64
            assert_almost_equal(b[n], b_answer[n], places=4)
65

    
66
    def test_multiple_alpha(self):
67
        alpha_list = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
68
        for alpha in alpha_list:
69
            b_answer = {0.1: {0: 0.5598852584152165, 1: 0.6107839182711449,
70
                              2: 0.5598852584152162},
71
                        0.2: {0: 0.5454545454545454, 1: 0.6363636363636365,
72
                              2: 0.5454545454545454},
73
                        0.3: {0: 0.5333964609104419, 1: 0.6564879518897746,
74
                              2: 0.5333964609104419},
75
                        0.4: {0: 0.5232045649263551, 1: 0.6726915834767423,
76
                              2: 0.5232045649263551},
77
                        0.5: {0: 0.5144957746691622, 1: 0.6859943117075809,
78
                              2: 0.5144957746691622},
79
                        0.6: {0: 0.5069794004195823, 1: 0.6970966755769258,
80
                              2: 0.5069794004195823}}
81
            G = nx.path_graph(3)
82
            b = nx.katz_centrality(G, alpha)
83
            for n in sorted(G):
84
                assert_almost_equal(b[n], b_answer[alpha][n], places=4)
85

    
86
    @raises(nx.NetworkXException)
87
    def test_multigraph(self):
88
        e = nx.katz_centrality(nx.MultiGraph(), 0.1)
89

    
90
    def test_empty(self):
91
        e = nx.katz_centrality(nx.Graph(), 0.1)
92
        assert_equal(e, {})
93

    
94
    @raises(nx.NetworkXException)
95
    def test_bad_beta(self):
96
        G = nx.Graph([(0, 1)])
97
        beta = {0: 77}
98
        e = nx.katz_centrality(G, 0.1, beta=beta)
99

    
100
    @raises(nx.NetworkXException)
101
    def test_bad_beta_numbe(self):
102
        G = nx.Graph([(0, 1)])
103
        e = nx.katz_centrality(G, 0.1, beta='foo')
104

    
105

    
106
class TestKatzCentralityNumpy(object):
107
    numpy = 1  # nosetests attribute, use nosetests -a 'not numpy' to skip test
108

    
109
    @classmethod
110
    def setupClass(cls):
111
        global np
112
        try:
113
            import numpy as np
114
            import scipy
115
        except ImportError:
116
            raise SkipTest('SciPy not available.')
117

    
118
    def test_K5(self):
119
        """Katz centrality: K5"""
120
        G = nx.complete_graph(5)
121
        alpha = 0.1
122
        b = nx.katz_centrality(G, alpha)
123
        v = math.sqrt(1 / 5.0)
124
        b_answer = dict.fromkeys(G, v)
125
        for n in sorted(G):
126
            assert_almost_equal(b[n], b_answer[n])
127
        nstart = dict([(n, 1) for n in G])
128
        b = nx.eigenvector_centrality_numpy(G)
129
        for n in sorted(G):
130
            assert_almost_equal(b[n], b_answer[n], places=3)
131

    
132
    def test_P3(self):
133
        """Katz centrality: P3"""
134
        alpha = 0.1
135
        G = nx.path_graph(3)
136
        b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
137
                    2: 0.5598852584152162}
138
        b = nx.katz_centrality_numpy(G, alpha)
139
        for n in sorted(G):
140
            assert_almost_equal(b[n], b_answer[n], places=4)
141

    
142
    def test_beta_as_scalar(self):
143
        alpha = 0.1
144
        beta = 0.1
145
        b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
146
                    2: 0.5598852584152162}
147
        G = nx.path_graph(3)
148
        b = nx.katz_centrality_numpy(G, alpha, beta)
149
        for n in sorted(G):
150
            assert_almost_equal(b[n], b_answer[n], places=4)
151

    
152
    def test_beta_as_dict(self):
153
        alpha = 0.1
154
        beta = {0: 1.0, 1: 1.0, 2: 1.0}
155
        b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
156
                    2: 0.5598852584152162}
157
        G = nx.path_graph(3)
158
        b = nx.katz_centrality_numpy(G, alpha, beta)
159
        for n in sorted(G):
160
            assert_almost_equal(b[n], b_answer[n], places=4)
161

    
162
    def test_multiple_alpha(self):
163
        alpha_list = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
164
        for alpha in alpha_list:
165
            b_answer = {0.1: {0: 0.5598852584152165, 1: 0.6107839182711449,
166
                              2: 0.5598852584152162},
167
                        0.2: {0: 0.5454545454545454, 1: 0.6363636363636365,
168
                              2: 0.5454545454545454},
169
                        0.3: {0: 0.5333964609104419, 1: 0.6564879518897746,
170
                              2: 0.5333964609104419},
171
                        0.4: {0: 0.5232045649263551, 1: 0.6726915834767423,
172
                              2: 0.5232045649263551},
173
                        0.5: {0: 0.5144957746691622, 1: 0.6859943117075809,
174
                              2: 0.5144957746691622},
175
                        0.6: {0: 0.5069794004195823, 1: 0.6970966755769258,
176
                              2: 0.5069794004195823}}
177
            G = nx.path_graph(3)
178
            b = nx.katz_centrality_numpy(G, alpha)
179
            for n in sorted(G):
180
                assert_almost_equal(b[n], b_answer[alpha][n], places=4)
181

    
182
    @raises(nx.NetworkXException)
183
    def test_multigraph(self):
184
        e = nx.katz_centrality(nx.MultiGraph(), 0.1)
185

    
186
    def test_empty(self):
187
        e = nx.katz_centrality(nx.Graph(), 0.1)
188
        assert_equal(e, {})
189

    
190
    @raises(nx.NetworkXException)
191
    def test_bad_beta(self):
192
        G = nx.Graph([(0, 1)])
193
        beta = {0: 77}
194
        e = nx.katz_centrality_numpy(G, 0.1, beta=beta)
195

    
196
    @raises(nx.NetworkXException)
197
    def test_bad_beta_numbe(self):
198
        G = nx.Graph([(0, 1)])
199
        e = nx.katz_centrality_numpy(G, 0.1, beta='foo')
200

    
201
    def test_K5_unweighted(self):
202
        """Katz centrality: K5"""
203
        G = nx.complete_graph(5)
204
        alpha = 0.1
205
        b = nx.katz_centrality(G, alpha, weight=None)
206
        v = math.sqrt(1 / 5.0)
207
        b_answer = dict.fromkeys(G, v)
208
        for n in sorted(G):
209
            assert_almost_equal(b[n], b_answer[n])
210
        nstart = dict([(n, 1) for n in G])
211
        b = nx.eigenvector_centrality_numpy(G, weight=None)
212
        for n in sorted(G):
213
            assert_almost_equal(b[n], b_answer[n], places=3)
214

    
215
    def test_P3_unweighted(self):
216
        """Katz centrality: P3"""
217
        alpha = 0.1
218
        G = nx.path_graph(3)
219
        b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
220
                    2: 0.5598852584152162}
221
        b = nx.katz_centrality_numpy(G, alpha, weight=None)
222
        for n in sorted(G):
223
            assert_almost_equal(b[n], b_answer[n], places=4)
224

    
225

    
226
class TestKatzCentralityDirected(object):
227
    def setUp(self):
228
        G = nx.DiGraph()
229
        edges = [(1, 2), (1, 3), (2, 4), (3, 2), (3, 5), (4, 2), (4, 5),
230
                 (4, 6), (5, 6), (5, 7), (5, 8), (6, 8), (7, 1), (7, 5),
231
                 (7, 8), (8, 6), (8, 7)]
232
        G.add_edges_from(edges, weight=2.0)
233
        self.G = G.reverse()
234
        self.G.alpha = 0.1
235
        self.G.evc = [
236
            0.3289589783189635,
237
            0.2832077296243516,
238
            0.3425906003685471,
239
            0.3970420865198392,
240
            0.41074871061646284,
241
            0.272257430756461,
242
            0.4201989685435462,
243
            0.34229059218038554,
244
        ]
245

    
246
        H = nx.DiGraph(edges)
247
        self.H = G.reverse()
248
        self.H.alpha = 0.1
249
        self.H.evc = [
250
            0.3289589783189635,
251
            0.2832077296243516,
252
            0.3425906003685471,
253
            0.3970420865198392,
254
            0.41074871061646284,
255
            0.272257430756461,
256
            0.4201989685435462,
257
            0.34229059218038554,
258
        ]
259

    
260
    def test_katz_centrality_weighted(self):
261
        G = self.G
262
        alpha = self.G.alpha
263
        p = nx.katz_centrality(G, alpha, weight='weight')
264
        for (a, b) in zip(list(p.values()), self.G.evc):
265
            assert_almost_equal(a, b)
266

    
267
    def test_katz_centrality_unweighted(self):
268
        H = self.H
269
        alpha = self.H.alpha
270
        p = nx.katz_centrality(H, alpha, weight='weight')
271
        for (a, b) in zip(list(p.values()), self.H.evc):
272
            assert_almost_equal(a, b)
273

    
274

    
275
class TestKatzCentralityDirectedNumpy(TestKatzCentralityDirected):
276
    numpy = 1  # nosetests attribute, use nosetests -a 'not numpy' to skip test
277

    
278
    @classmethod
279
    def setupClass(cls):
280
        global np
281
        try:
282
            import numpy as np
283
            import scipy
284
        except ImportError:
285
            raise SkipTest('SciPy not available.')
286

    
287
    def test_katz_centrality_weighted(self):
288
        G = self.G
289
        alpha = self.G.alpha
290
        p = nx.katz_centrality_numpy(G, alpha, weight='weight')
291
        for (a, b) in zip(list(p.values()), self.G.evc):
292
            assert_almost_equal(a, b)
293

    
294
    def test_katz_centrality_unweighted(self):
295
        H = self.H
296
        alpha = self.H.alpha
297
        p = nx.katz_centrality_numpy(H, alpha, weight='weight')
298
        for (a, b) in zip(list(p.values()), self.H.evc):
299
            assert_almost_equal(a, b)
300

    
301

    
302
class TestKatzEigenvectorVKatz(object):
303
    numpy = 1  # nosetests attribute, use nosetests -a 'not numpy' to skip test
304

    
305
    @classmethod
306
    def setupClass(cls):
307
        global np
308
        global eigvals
309
        try:
310
            import numpy as np
311
            import scipy
312
            from numpy.linalg import eigvals
313
        except ImportError:
314
            raise SkipTest('SciPy not available.')
315

    
316
    def test_eigenvector_v_katz_random(self):
317
        G = nx.gnp_random_graph(10, 0.5, seed=1234)
318
        l = float(max(eigvals(nx.adjacency_matrix(G).todense())))
319
        e = nx.eigenvector_centrality_numpy(G)
320
        k = nx.katz_centrality_numpy(G, 1.0 / l)
321
        for n in G:
322
            assert_almost_equal(e[n], k[n])