comparison env/lib/python3.9/site-packages/networkx/algorithms/centrality/tests/test_reaching.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """Unit tests for the :mod:`networkx.algorithms.centrality.reaching` module."""
2 import pytest
3
4 from networkx import nx
5 from networkx.testing import almost_equal
6
7
8 class TestGlobalReachingCentrality:
9 """Unit tests for the global reaching centrality function."""
10
11 def test_non_positive_weights(self):
12 with pytest.raises(nx.NetworkXError):
13 G = nx.DiGraph()
14 nx.global_reaching_centrality(G, weight="weight")
15
16 def test_negatively_weighted(self):
17 with pytest.raises(nx.NetworkXError):
18 G = nx.Graph()
19 G.add_weighted_edges_from([(0, 1, -2), (1, 2, +1)])
20 nx.global_reaching_centrality(G, weight="weight")
21
22 def test_directed_star(self):
23 G = nx.DiGraph()
24 G.add_weighted_edges_from([(1, 2, 0.5), (1, 3, 0.5)])
25 grc = nx.global_reaching_centrality
26 assert grc(G, normalized=False, weight="weight") == 0.5
27 assert grc(G) == 1
28
29 def test_undirected_unweighted_star(self):
30 G = nx.star_graph(2)
31 grc = nx.global_reaching_centrality
32 assert grc(G, normalized=False, weight=None) == 0.25
33
34 def test_undirected_weighted_star(self):
35 G = nx.Graph()
36 G.add_weighted_edges_from([(1, 2, 1), (1, 3, 2)])
37 grc = nx.global_reaching_centrality
38 assert grc(G, normalized=False, weight="weight") == 0.375
39
40 def test_cycle_directed_unweighted(self):
41 G = nx.DiGraph()
42 G.add_edge(1, 2)
43 G.add_edge(2, 1)
44 assert nx.global_reaching_centrality(G, weight=None) == 0
45
46 def test_cycle_undirected_unweighted(self):
47 G = nx.Graph()
48 G.add_edge(1, 2)
49 assert nx.global_reaching_centrality(G, weight=None) == 0
50
51 def test_cycle_directed_weighted(self):
52 G = nx.DiGraph()
53 G.add_weighted_edges_from([(1, 2, 1), (2, 1, 1)])
54 assert nx.global_reaching_centrality(G) == 0
55
56 def test_cycle_undirected_weighted(self):
57 G = nx.Graph()
58 G.add_edge(1, 2, weight=1)
59 grc = nx.global_reaching_centrality
60 assert grc(G, normalized=False) == 0
61
62 def test_directed_weighted(self):
63 G = nx.DiGraph()
64 G.add_edge("A", "B", weight=5)
65 G.add_edge("B", "C", weight=1)
66 G.add_edge("B", "D", weight=0.25)
67 G.add_edge("D", "E", weight=1)
68
69 denom = len(G) - 1
70 A_local = sum([5, 3, 2.625, 2.0833333333333]) / denom
71 B_local = sum([1, 0.25, 0.625]) / denom
72 C_local = 0
73 D_local = sum([1]) / denom
74 E_local = 0
75
76 local_reach_ctrs = [A_local, C_local, B_local, D_local, E_local]
77 max_local = max(local_reach_ctrs)
78 expected = sum(max_local - lrc for lrc in local_reach_ctrs) / denom
79 grc = nx.global_reaching_centrality
80 actual = grc(G, normalized=False, weight="weight")
81 assert almost_equal(expected, actual, places=7)
82
83
84 class TestLocalReachingCentrality:
85 """Unit tests for the local reaching centrality function."""
86
87 def test_non_positive_weights(self):
88 with pytest.raises(nx.NetworkXError):
89 G = nx.DiGraph()
90 G.add_weighted_edges_from([(0, 1, 0)])
91 nx.local_reaching_centrality(G, 0, weight="weight")
92
93 def test_negatively_weighted(self):
94 with pytest.raises(nx.NetworkXError):
95 G = nx.Graph()
96 G.add_weighted_edges_from([(0, 1, -2), (1, 2, +1)])
97 nx.local_reaching_centrality(G, 0, weight="weight")
98
99 def test_undirected_unweighted_star(self):
100 G = nx.star_graph(2)
101 grc = nx.local_reaching_centrality
102 assert grc(G, 1, weight=None, normalized=False) == 0.75
103
104 def test_undirected_weighted_star(self):
105 G = nx.Graph()
106 G.add_weighted_edges_from([(1, 2, 1), (1, 3, 2)])
107 centrality = nx.local_reaching_centrality(
108 G, 1, normalized=False, weight="weight"
109 )
110 assert centrality == 1.5