comparison env/lib/python3.9/site-packages/networkx/algorithms/community/tests/test_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 """Unit tests for the :mod:`networkx.algorithms.community.centrality`
2 module.
3
4 """
5 from operator import itemgetter
6
7
8 import networkx as nx
9 from networkx.algorithms.community import girvan_newman
10
11
12 def set_of_sets(iterable):
13 return set(map(frozenset, iterable))
14
15
16 def validate_communities(result, expected):
17 assert set_of_sets(result) == set_of_sets(expected)
18
19
20 def validate_possible_communities(result, *expected):
21 assert any(set_of_sets(result) == set_of_sets(p) for p in expected)
22
23
24 class TestGirvanNewman:
25 """Unit tests for the
26 :func:`networkx.algorithms.community.centrality.girvan_newman`
27 function.
28
29 """
30
31 def test_no_edges(self):
32 G = nx.empty_graph(3)
33 communities = list(girvan_newman(G))
34 assert len(communities) == 1
35 validate_communities(communities[0], [{0}, {1}, {2}])
36
37 def test_undirected(self):
38 # Start with the graph .-.-.-.
39 G = nx.path_graph(4)
40 communities = list(girvan_newman(G))
41 assert len(communities) == 3
42 # After one removal, we get the graph .-. .-.
43 validate_communities(communities[0], [{0, 1}, {2, 3}])
44 # After the next, we get the graph .-. . ., but there are two
45 # symmetric possible versions.
46 validate_possible_communities(
47 communities[1], [{0}, {1}, {2, 3}], [{0, 1}, {2}, {3}]
48 )
49 # After the last removal, we always get the empty graph.
50 validate_communities(communities[2], [{0}, {1}, {2}, {3}])
51
52 def test_directed(self):
53 G = nx.DiGraph(nx.path_graph(4))
54 communities = list(girvan_newman(G))
55 assert len(communities) == 3
56 validate_communities(communities[0], [{0, 1}, {2, 3}])
57 validate_possible_communities(
58 communities[1], [{0}, {1}, {2, 3}], [{0, 1}, {2}, {3}]
59 )
60 validate_communities(communities[2], [{0}, {1}, {2}, {3}])
61
62 def test_selfloops(self):
63 G = nx.path_graph(4)
64 G.add_edge(0, 0)
65 G.add_edge(2, 2)
66 communities = list(girvan_newman(G))
67 assert len(communities) == 3
68 validate_communities(communities[0], [{0, 1}, {2, 3}])
69 validate_possible_communities(
70 communities[1], [{0}, {1}, {2, 3}], [{0, 1}, {2}, {3}]
71 )
72 validate_communities(communities[2], [{0}, {1}, {2}, {3}])
73
74 def test_most_valuable_edge(self):
75 G = nx.Graph()
76 G.add_weighted_edges_from([(0, 1, 3), (1, 2, 2), (2, 3, 1)])
77 # Let the most valuable edge be the one with the highest weight.
78
79 def heaviest(G):
80 return max(G.edges(data="weight"), key=itemgetter(2))[:2]
81
82 communities = list(girvan_newman(G, heaviest))
83 assert len(communities) == 3
84 validate_communities(communities[0], [{0}, {1, 2, 3}])
85 validate_communities(communities[1], [{0}, {1}, {2, 3}])
86 validate_communities(communities[2], [{0}, {1}, {2}, {3}])