Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/generators/tests/test_internet_as_graphs.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 networkx import is_connected, neighbors | |
2 from networkx.generators.internet_as_graphs import random_internet_as_graph | |
3 from networkx.testing import almost_equal | |
4 | |
5 | |
6 class TestInternetASTopology: | |
7 @classmethod | |
8 def setup_class(cls): | |
9 cls.n = 1000 | |
10 cls.seed = 42 | |
11 cls.G = random_internet_as_graph(cls.n, cls.seed) | |
12 cls.T = [] | |
13 cls.M = [] | |
14 cls.C = [] | |
15 cls.CP = [] | |
16 cls.customers = {} | |
17 cls.providers = {} | |
18 | |
19 for i in cls.G.nodes(): | |
20 if cls.G.nodes[i]["type"] == "T": | |
21 cls.T.append(i) | |
22 elif cls.G.nodes[i]["type"] == "M": | |
23 cls.M.append(i) | |
24 elif cls.G.nodes[i]["type"] == "C": | |
25 cls.C.append(i) | |
26 elif cls.G.nodes[i]["type"] == "CP": | |
27 cls.CP.append(i) | |
28 else: | |
29 raise ValueError( | |
30 "Inconsistent data in the graph\ | |
31 node attributes" | |
32 ) | |
33 cls.set_customers(i) | |
34 cls.set_providers(i) | |
35 | |
36 @classmethod | |
37 def set_customers(cls, i): | |
38 if i not in cls.customers: | |
39 cls.customers[i] = set() | |
40 for j in neighbors(cls.G, i): | |
41 e = cls.G.edges[(i, j)] | |
42 if e["type"] == "transit": | |
43 customer = int(e["customer"]) | |
44 if j == customer: | |
45 cls.set_customers(j) | |
46 cls.customers[i] = cls.customers[i].union(cls.customers[j]) | |
47 cls.customers[i].add(j) | |
48 elif i != customer: | |
49 raise ValueError( | |
50 "Inconsistent data in the graph\ | |
51 edge attributes" | |
52 ) | |
53 | |
54 @classmethod | |
55 def set_providers(cls, i): | |
56 if i not in cls.providers: | |
57 cls.providers[i] = set() | |
58 for j in neighbors(cls.G, i): | |
59 e = cls.G.edges[(i, j)] | |
60 if e["type"] == "transit": | |
61 customer = int(e["customer"]) | |
62 if i == customer: | |
63 cls.set_providers(j) | |
64 cls.providers[i] = cls.providers[i].union(cls.providers[j]) | |
65 cls.providers[i].add(j) | |
66 elif j != customer: | |
67 raise ValueError( | |
68 "Inconsistent data in the graph\ | |
69 edge attributes" | |
70 ) | |
71 | |
72 def test_wrong_input(self): | |
73 G = random_internet_as_graph(0) | |
74 assert len(G.nodes()) == 0 | |
75 | |
76 G = random_internet_as_graph(-1) | |
77 assert len(G.nodes()) == 0 | |
78 | |
79 G = random_internet_as_graph(1) | |
80 assert len(G.nodes()) == 1 | |
81 | |
82 def test_node_numbers(self): | |
83 assert len(self.G.nodes()) == self.n | |
84 assert len(self.T) < 7 | |
85 assert len(self.M) == int(round(self.n * 0.15)) | |
86 assert len(self.CP) == int(round(self.n * 0.05)) | |
87 numb = self.n - len(self.T) - len(self.M) - len(self.CP) | |
88 assert len(self.C) == numb | |
89 | |
90 def test_connectivity(self): | |
91 assert is_connected(self.G) | |
92 | |
93 def test_relationships(self): | |
94 # T nodes are not customers of anyone | |
95 for i in self.T: | |
96 assert len(self.providers[i]) == 0 | |
97 | |
98 # C nodes are not providers of anyone | |
99 for i in self.C: | |
100 assert len(self.customers[i]) == 0 | |
101 | |
102 # CP nodes are not providers of anyone | |
103 for i in self.CP: | |
104 assert len(self.customers[i]) == 0 | |
105 | |
106 # test whether there is a customer-provider loop | |
107 for i in self.G.nodes(): | |
108 assert len(self.customers[i].intersection(self.providers[i])) == 0 | |
109 | |
110 # test whether there is a peering with a customer or provider | |
111 for i, j in self.G.edges(): | |
112 if self.G.edges[(i, j)]["type"] == "peer": | |
113 assert j not in self.customers[i] | |
114 assert i not in self.customers[j] | |
115 assert j not in self.providers[i] | |
116 assert i not in self.providers[j] | |
117 | |
118 def test_degree_values(self): | |
119 d_m = 0 # multihoming degree for M nodes | |
120 d_cp = 0 # multihoming degree for CP nodes | |
121 d_c = 0 # multihoming degree for C nodes | |
122 p_m_m = 0 # avg number of peering edges between M and M | |
123 p_cp_m = 0 # avg number of peering edges between CP and M | |
124 p_cp_cp = 0 # avg number of peering edges between CP and CP | |
125 t_m = 0 # probability M's provider is T | |
126 t_cp = 0 # probability CP's provider is T | |
127 t_c = 0 # probability C's provider is T | |
128 | |
129 for i, j in self.G.edges(): | |
130 e = self.G.edges[(i, j)] | |
131 if e["type"] == "transit": | |
132 cust = int(e["customer"]) | |
133 if i == cust: | |
134 prov = j | |
135 elif j == cust: | |
136 prov = i | |
137 else: | |
138 raise ValueError( | |
139 "Inconsistent data in the graph edge\ | |
140 attributes" | |
141 ) | |
142 if cust in self.M: | |
143 d_m += 1 | |
144 if self.G.nodes[prov]["type"] == "T": | |
145 t_m += 1 | |
146 elif cust in self.C: | |
147 d_c += 1 | |
148 if self.G.nodes[prov]["type"] == "T": | |
149 t_c += 1 | |
150 elif cust in self.CP: | |
151 d_cp += 1 | |
152 if self.G.nodes[prov]["type"] == "T": | |
153 t_cp += 1 | |
154 else: | |
155 raise ValueError( | |
156 "Inconsistent data in the graph edge\ | |
157 attributes" | |
158 ) | |
159 elif e["type"] == "peer": | |
160 if self.G.nodes[i]["type"] == "M" and self.G.nodes[j]["type"] == "M": | |
161 p_m_m += 1 | |
162 if self.G.nodes[i]["type"] == "CP" and self.G.nodes[j]["type"] == "CP": | |
163 p_cp_cp += 1 | |
164 if ( | |
165 self.G.nodes[i]["type"] == "M" | |
166 and self.G.nodes[j]["type"] == "CP" | |
167 or self.G.nodes[i]["type"] == "CP" | |
168 and self.G.nodes[j]["type"] == "M" | |
169 ): | |
170 p_cp_m += 1 | |
171 else: | |
172 raise ValueError( | |
173 "Unexpected data in the graph edge\ | |
174 attributes" | |
175 ) | |
176 | |
177 assert almost_equal(d_m / len(self.M), 2 + (2.5 * self.n) / 10000, places=0) | |
178 assert almost_equal(d_cp / len(self.CP), 2 + (1.5 * self.n) / 10000, places=0) | |
179 assert almost_equal(d_c / len(self.C), 1 + (5 * self.n) / 100000, places=0) | |
180 | |
181 assert almost_equal(p_m_m / len(self.M), 1 + (2 * self.n) / 10000, places=0) | |
182 assert almost_equal(p_cp_m / len(self.CP), 0.2 + (2 * self.n) / 10000, places=0) | |
183 assert almost_equal( | |
184 p_cp_cp / len(self.CP), 0.05 + (2 * self.n) / 100000, places=0 | |
185 ) | |
186 | |
187 assert almost_equal(t_m / d_m, 0.375, places=1) | |
188 assert almost_equal(t_cp / d_cp, 0.375, places=1) | |
189 assert almost_equal(t_c / d_c, 0.125, places=1) |