comparison env/lib/python3.9/site-packages/networkx/algorithms/assortativity/tests/test_connectivity.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 from itertools import permutations
2
3 import pytest
4
5 import networkx as nx
6 from networkx.testing import almost_equal
7
8
9 class TestNeighborConnectivity:
10 def test_degree_p4(self):
11 G = nx.path_graph(4)
12 answer = {1: 2.0, 2: 1.5}
13 nd = nx.average_degree_connectivity(G)
14 assert nd == answer
15
16 D = G.to_directed()
17 answer = {2: 2.0, 4: 1.5}
18 nd = nx.average_degree_connectivity(D)
19 assert nd == answer
20
21 answer = {1: 2.0, 2: 1.5}
22 D = G.to_directed()
23 nd = nx.average_degree_connectivity(D, source="in", target="in")
24 assert nd == answer
25
26 D = G.to_directed()
27 nd = nx.average_degree_connectivity(D, source="in", target="in")
28 assert nd == answer
29
30 def test_degree_p4_weighted(self):
31 G = nx.path_graph(4)
32 G[1][2]["weight"] = 4
33 answer = {1: 2.0, 2: 1.8}
34 nd = nx.average_degree_connectivity(G, weight="weight")
35 assert nd == answer
36 answer = {1: 2.0, 2: 1.5}
37 nd = nx.average_degree_connectivity(G)
38 assert nd == answer
39
40 D = G.to_directed()
41 answer = {2: 2.0, 4: 1.8}
42 nd = nx.average_degree_connectivity(D, weight="weight")
43 assert nd == answer
44
45 answer = {1: 2.0, 2: 1.8}
46 D = G.to_directed()
47 nd = nx.average_degree_connectivity(
48 D, weight="weight", source="in", target="in"
49 )
50 assert nd == answer
51
52 D = G.to_directed()
53 nd = nx.average_degree_connectivity(
54 D, source="in", target="out", weight="weight"
55 )
56 assert nd == answer
57
58 def test_weight_keyword(self):
59 G = nx.path_graph(4)
60 G[1][2]["other"] = 4
61 answer = {1: 2.0, 2: 1.8}
62 nd = nx.average_degree_connectivity(G, weight="other")
63 assert nd == answer
64 answer = {1: 2.0, 2: 1.5}
65 nd = nx.average_degree_connectivity(G, weight=None)
66 assert nd == answer
67
68 D = G.to_directed()
69 answer = {2: 2.0, 4: 1.8}
70 nd = nx.average_degree_connectivity(D, weight="other")
71 assert nd == answer
72
73 answer = {1: 2.0, 2: 1.8}
74 D = G.to_directed()
75 nd = nx.average_degree_connectivity(D, weight="other", source="in", target="in")
76 assert nd == answer
77
78 D = G.to_directed()
79 nd = nx.average_degree_connectivity(D, weight="other", source="in", target="in")
80 assert nd == answer
81
82 def test_degree_barrat(self):
83 G = nx.star_graph(5)
84 G.add_edges_from([(5, 6), (5, 7), (5, 8), (5, 9)])
85 G[0][5]["weight"] = 5
86 nd = nx.average_degree_connectivity(G)[5]
87 assert nd == 1.8
88 nd = nx.average_degree_connectivity(G, weight="weight")[5]
89 assert almost_equal(nd, 3.222222, places=5)
90 nd = nx.k_nearest_neighbors(G, weight="weight")[5]
91 assert almost_equal(nd, 3.222222, places=5)
92
93 def test_zero_deg(self):
94 G = nx.DiGraph()
95 G.add_edge(1, 2)
96 G.add_edge(1, 3)
97 G.add_edge(1, 4)
98 c = nx.average_degree_connectivity(G)
99 assert c == {1: 0, 3: 1}
100 c = nx.average_degree_connectivity(G, source="in", target="in")
101 assert c == {0: 0, 1: 0}
102 c = nx.average_degree_connectivity(G, source="in", target="out")
103 assert c == {0: 0, 1: 3}
104 c = nx.average_degree_connectivity(G, source="in", target="in+out")
105 assert c == {0: 0, 1: 3}
106 c = nx.average_degree_connectivity(G, source="out", target="out")
107 assert c == {0: 0, 3: 0}
108 c = nx.average_degree_connectivity(G, source="out", target="in")
109 assert c == {0: 0, 3: 1}
110 c = nx.average_degree_connectivity(G, source="out", target="in+out")
111 assert c == {0: 0, 3: 1}
112
113 def test_in_out_weight(self):
114 G = nx.DiGraph()
115 G.add_edge(1, 2, weight=1)
116 G.add_edge(1, 3, weight=1)
117 G.add_edge(3, 1, weight=1)
118 for s, t in permutations(["in", "out", "in+out"], 2):
119 c = nx.average_degree_connectivity(G, source=s, target=t)
120 cw = nx.average_degree_connectivity(G, source=s, target=t, weight="weight")
121 assert c == cw
122
123 def test_invalid_source(self):
124 with pytest.raises(ValueError):
125 G = nx.DiGraph()
126 nx.average_degree_connectivity(G, source="bogus")
127
128 def test_invalid_target(self):
129 with pytest.raises(ValueError):
130 G = nx.DiGraph()
131 nx.average_degree_connectivity(G, target="bogus")
132
133 def test_single_node(self):
134 # TODO Is this really the intended behavior for providing a
135 # single node as the argument `nodes`? Shouldn't the function
136 # just return the connectivity value itself?
137 G = nx.trivial_graph()
138 conn = nx.average_degree_connectivity(G, nodes=0)
139 assert conn == {0: 0}