Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/algorithms/centrality/tests/test_second_order_centrality.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 """ | |
| 2 Tests for second order centrality. | |
| 3 """ | |
| 4 | |
| 5 import pytest | |
| 6 | |
| 7 np = pytest.importorskip("numpy") | |
| 8 scipy = pytest.importorskip("scipy") | |
| 9 | |
| 10 import networkx as nx | |
| 11 from networkx.testing import almost_equal | |
| 12 | |
| 13 | |
| 14 class TestSecondOrderCentrality: | |
| 15 def test_empty(self): | |
| 16 with pytest.raises(nx.NetworkXException): | |
| 17 G = nx.empty_graph() | |
| 18 nx.second_order_centrality(G) | |
| 19 | |
| 20 def test_non_connected(self): | |
| 21 with pytest.raises(nx.NetworkXException): | |
| 22 G = nx.Graph() | |
| 23 G.add_node(0) | |
| 24 G.add_node(1) | |
| 25 nx.second_order_centrality(G) | |
| 26 | |
| 27 def test_non_negative_edge_weights(self): | |
| 28 with pytest.raises(nx.NetworkXException): | |
| 29 G = nx.path_graph(2) | |
| 30 G.add_edge(0, 1, weight=-1) | |
| 31 nx.second_order_centrality(G) | |
| 32 | |
| 33 def test_one_node_graph(self): | |
| 34 """Second order centrality: single node""" | |
| 35 G = nx.Graph() | |
| 36 G.add_node(0) | |
| 37 G.add_edge(0, 0) | |
| 38 assert nx.second_order_centrality(G)[0] == 0 | |
| 39 | |
| 40 def test_P3(self): | |
| 41 """Second order centrality: line graph, as defined in paper""" | |
| 42 G = nx.path_graph(3) | |
| 43 b_answer = {0: 3.741, 1: 1.414, 2: 3.741} | |
| 44 | |
| 45 b = nx.second_order_centrality(G) | |
| 46 | |
| 47 for n in sorted(G): | |
| 48 assert almost_equal(b[n], b_answer[n], places=2) | |
| 49 | |
| 50 def test_K3(self): | |
| 51 """Second order centrality: complete graph, as defined in paper""" | |
| 52 G = nx.complete_graph(3) | |
| 53 b_answer = {0: 1.414, 1: 1.414, 2: 1.414} | |
| 54 | |
| 55 b = nx.second_order_centrality(G) | |
| 56 | |
| 57 for n in sorted(G): | |
| 58 assert almost_equal(b[n], b_answer[n], places=2) | |
| 59 | |
| 60 def test_ring_graph(self): | |
| 61 """Second order centrality: ring graph, as defined in paper""" | |
| 62 G = nx.cycle_graph(5) | |
| 63 b_answer = {0: 4.472, 1: 4.472, 2: 4.472, 3: 4.472, 4: 4.472} | |
| 64 | |
| 65 b = nx.second_order_centrality(G) | |
| 66 | |
| 67 for n in sorted(G): | |
| 68 assert almost_equal(b[n], b_answer[n], places=2) |
