topolib 0.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of topolib might be problematic. Click here for more details.

tests/test_topology.py ADDED
@@ -0,0 +1,225 @@
1
+ """
2
+ Unit tests for the Topology class.
3
+ Covers all public methods, success and error cases, and adjacency matrix validation (numpy), including NetworkX integration.
4
+ """
5
+
6
+ import pytest
7
+ import numpy as np
8
+ from topolib.topology import Topology
9
+ from topolib.elements.node import Node
10
+ from topolib.elements.link import Link
11
+
12
+
13
+ def test_add_and_get_nodes():
14
+ topo = Topology()
15
+ n1 = Node(1, "A", 0.0, 0.0)
16
+ n2 = Node(2, "B", 1.0, 1.0)
17
+ topo.add_node(n1)
18
+ topo.add_node(n2)
19
+ assert n1 in topo.nodes and n2 in topo.nodes
20
+ assert len(topo.nodes) == 2
21
+ # Check that nodes are present in the internal networkx graph
22
+ assert n1.id in topo._graph.nodes # type: ignore
23
+ assert n2.id in topo._graph.nodes # type: ignore
24
+
25
+
26
+ def test_add_duplicate_node_allowed():
27
+ # The current design allows duplicate nodes (no check in Topology)
28
+ topo = Topology()
29
+ n1 = Node(1, "A", 0.0, 0.0)
30
+ topo.add_node(n1)
31
+ topo.add_node(n1)
32
+ assert topo.nodes.count(n1) == 2
33
+
34
+
35
+ def test_remove_node():
36
+ topo = Topology()
37
+ n1 = Node(1, "A", 0.0, 0.0)
38
+ n2 = Node(2, "B", 1.0, 1.0)
39
+ topo.add_node(n1)
40
+ topo.add_node(n2)
41
+ topo.remove_node(n1.id)
42
+ assert n1 not in topo.nodes
43
+ assert n2 in topo.nodes
44
+ # Check that the node was removed from the internal graph
45
+ assert n1.id not in topo._graph.nodes # type: ignore
46
+
47
+
48
+ def test_remove_nonexistent_node():
49
+ import networkx as nx
50
+
51
+ topo = Topology()
52
+ n1 = Node(1, "A", 0.0, 0.0)
53
+ # Should raise NetworkXError when trying to remove a node not in the graph
54
+ with pytest.raises(nx.NetworkXError):
55
+ topo.remove_node(n1.id)
56
+ assert n1 not in topo.nodes
57
+
58
+
59
+ def test_add_and_get_links():
60
+ topo = Topology()
61
+ n1 = Node(1, "A", 0.0, 0.0)
62
+ n2 = Node(2, "B", 1.0, 1.0)
63
+ topo.add_node(n1)
64
+ topo.add_node(n2)
65
+ l1 = Link(1, n1, n2, 10.0)
66
+ topo.add_link(l1)
67
+ assert l1 in topo.links
68
+ assert len(topo.links) == 1
69
+ # Check that the edge is present in the internal networkx graph
70
+ assert topo._graph.has_edge(n1.id, n2.id) # type: ignore
71
+
72
+
73
+ def test_add_link_with_missing_nodes():
74
+ # The current design allows adding links even if nodes are not in the topology
75
+ topo = Topology()
76
+ n1 = Node(1, "A", 0.0, 0.0)
77
+ n2 = Node(2, "B", 1.0, 1.0)
78
+ l1 = Link(1, n1, n2, 10.0)
79
+ topo.add_link(l1)
80
+ assert l1 in topo.links
81
+
82
+
83
+ def test_remove_link():
84
+ topo = Topology()
85
+ n1 = Node(1, "A", 0.0, 0.0)
86
+ n2 = Node(2, "B", 1.0, 1.0)
87
+ topo.add_node(n1)
88
+ topo.add_node(n2)
89
+ l1 = Link(1, n1, n2, 10.0)
90
+ topo.add_link(l1)
91
+ topo.remove_link(l1.id)
92
+ assert l1 not in topo.links
93
+ # Check that the edge was removed from the internal graph
94
+ assert not topo._graph.has_edge(n1.id, n2.id) # type: ignore
95
+
96
+
97
+ def test_remove_nonexistent_link():
98
+ topo = Topology()
99
+ n1 = Node(1, "A", 0.0, 0.0)
100
+ n2 = Node(2, "B", 1.0, 1.0)
101
+ topo.add_node(n1)
102
+ topo.add_node(n2)
103
+ # Should not raise, just does nothing
104
+ topo.remove_link(999)
105
+ assert len(topo.links) == 0
106
+
107
+
108
+ def test_adjacency_matrix():
109
+ topo = Topology()
110
+ n1 = Node(1, "A", 0.0, 0.0)
111
+ n2 = Node(2, "B", 1.0, 1.0)
112
+ n3 = Node(3, "C", 2.0, 2.0)
113
+ topo.add_node(n1)
114
+ topo.add_node(n2)
115
+ topo.add_node(n3)
116
+ l1 = Link(1, n1, n2, 10.0)
117
+ l2 = Link(2, n2, n3, 20.0)
118
+ topo.add_link(l1)
119
+ topo.add_link(l2)
120
+ matrix = topo.adjacency_matrix()
121
+ assert isinstance(matrix, np.ndarray)
122
+ assert matrix.shape == (3, 3)
123
+ # Check connections
124
+ idx = {n.id: i for i, n in enumerate(topo.nodes)}
125
+ assert matrix[idx[n1.id], idx[n2.id]] == 1
126
+ assert matrix[idx[n2.id], idx[n3.id]] == 1
127
+ assert matrix[idx[n1.id], idx[n3.id]] == 0
128
+ # Validate with networkx adjacency
129
+ adj_nx = np.asarray(
130
+ np.array(
131
+ [
132
+ [1 if topo._graph.has_edge(n1.id, n2.id) else 0 for n2 in topo.nodes] # type: ignore
133
+ for n1 in topo.nodes
134
+ ]
135
+ )
136
+ )
137
+ assert np.array_equal(matrix, adj_nx)
138
+
139
+
140
+ def test_adjacency_matrix_empty():
141
+ topo = Topology()
142
+ matrix = topo.adjacency_matrix()
143
+ assert isinstance(matrix, np.ndarray)
144
+ assert matrix.shape == (0, 0)
145
+
146
+
147
+ def test_topology_from_json():
148
+ import os
149
+ import tempfile
150
+ import json
151
+ from topolib.topology import Topology
152
+
153
+ # Usar un ejemplo mínimo de topología
154
+ data = {
155
+ "name": "TestNet",
156
+ "nodes": [
157
+ {
158
+ "id": 1,
159
+ "name": "A",
160
+ "latitude": 0.0,
161
+ "longitude": 0.0,
162
+ "weight": 1.5,
163
+ "pop": 1000,
164
+ },
165
+ {
166
+ "id": 2,
167
+ "name": "B",
168
+ "latitude": 1.0,
169
+ "longitude": 1.0,
170
+ "weight": 2.5,
171
+ "pop": 2000,
172
+ },
173
+ ],
174
+ "links": [{"id": 10, "src": 1, "dst": 2, "length": 42.0}],
175
+ }
176
+ with tempfile.NamedTemporaryFile("w+", suffix=".json", delete=False) as f:
177
+ json.dump(data, f)
178
+ f.flush()
179
+ path = f.name
180
+ topo = Topology.from_json(path)
181
+ os.remove(path)
182
+ assert len(topo.nodes) == 2
183
+ assert len(topo.links) == 1
184
+ n1, n2 = topo.nodes
185
+ assert n1.name == "A"
186
+ assert n2.pop == 2000
187
+ l = topo.links[0]
188
+ assert l.length == 42.0
189
+ assert l.source.id == 1 and l.target.id == 2
190
+
191
+
192
+ def test_topology_from_json_reads_dc_ixp(tmp_path):
193
+ import json
194
+ from topolib.topology import Topology
195
+
196
+ # Crear un archivo JSON temporal con los nuevos campos
197
+ data = {
198
+ "name": "TestNet",
199
+ "nodes": [
200
+ {
201
+ "id": 1,
202
+ "name": "A",
203
+ "latitude": 0.0,
204
+ "longitude": 0.0,
205
+ "dc": 10,
206
+ "ixp": 2,
207
+ },
208
+ {
209
+ "id": 2,
210
+ "name": "B",
211
+ "latitude": 1.0,
212
+ "longitude": 1.0,
213
+ "dc": 20,
214
+ "ixp": 5,
215
+ },
216
+ ],
217
+ "links": [],
218
+ }
219
+ json_file = tmp_path / "testnet.json"
220
+ json_file.write_text(json.dumps(data))
221
+ topo = Topology.from_json(str(json_file))
222
+ assert topo.nodes[0].dc == 10
223
+ assert topo.nodes[0].ixp == 2
224
+ assert topo.nodes[1].dc == 20
225
+ assert topo.nodes[1].ixp == 5
tests/test_version.py ADDED
@@ -0,0 +1,6 @@
1
+ import importlib
2
+
3
+ def test_package_exports_version():
4
+ pkg = importlib.import_module("topolib")
5
+ assert hasattr(pkg, "__version__"), "topolib should expose __version__"
6
+ assert isinstance(pkg.__version__, str), "__version__ should be a string"
topolib/__init__.py ADDED
@@ -0,0 +1,4 @@
1
+ try:
2
+ from ._version import __version__
3
+ except ImportError:
4
+ __version__ = "unknown"
@@ -0,0 +1,3 @@
1
+ from .metrics import Metrics
2
+
3
+ __all__ = ["Metrics"]
@@ -0,0 +1,80 @@
1
+ """
2
+ Metrics module for network topology analysis.
3
+ """
4
+
5
+ from typing import List, Any, Dict, Optional
6
+
7
+
8
+ from topolib.topology import Topology
9
+
10
+
11
+ class Metrics:
12
+ """
13
+ Provides static methods for computing metrics on network topologies.
14
+
15
+ Todos los métodos reciben una instancia de Topology.
16
+
17
+ Métodos
18
+ -------
19
+ node_degree(topology)
20
+ Calcula el grado de cada nodo.
21
+ link_length_stats(topology)
22
+ Calcula estadísticas (min, max, avg) de las longitudes de los enlaces.
23
+ connection_matrix(topology)
24
+ Construye la matriz de adyacencia.
25
+ """
26
+
27
+ @staticmethod
28
+ def node_degree(topology: "Topology") -> Dict[int, int]:
29
+ """
30
+ Calculates the degree of each node in the topology.
31
+
32
+ :param topology: Instance of topolib.topology.topology.Topology.
33
+ :type topology: topolib.topology.topology.Topology
34
+ :return: Dictionary {node_id: degree}
35
+ :rtype: dict[int, int]
36
+ """
37
+ degree = {n.id: 0 for n in topology.nodes}
38
+ for link in topology.links:
39
+ degree[link.source.id] += 1
40
+ degree[link.target.id] += 1
41
+ return degree
42
+
43
+ @staticmethod
44
+ def link_length_stats(topology: "Topology") -> Dict[str, Optional[float]]:
45
+ """
46
+ Calculates the minimum, maximum, and average link lengths.
47
+
48
+ :param topology: Instance of topolib.topology.topology.Topology.
49
+ :type topology: topolib.topology.topology.Topology
50
+ :return: Dictionary with keys 'min', 'max', 'avg'.
51
+ :rtype: dict[str, float | None]
52
+ """
53
+ lengths = [l.length for l in topology.links]
54
+ if not lengths:
55
+ return {"min": None, "max": None, "avg": None}
56
+ return {
57
+ "min": min(lengths),
58
+ "max": max(lengths),
59
+ "avg": sum(lengths) / len(lengths),
60
+ }
61
+
62
+ @staticmethod
63
+ def connection_matrix(topology: "Topology") -> List[List[int]]:
64
+ """
65
+ Builds the adjacency matrix of the topology.
66
+
67
+ :param topology: Instance of topolib.topology.topology.Topology.
68
+ :type topology: topolib.topology.topology.Topology
69
+ :return: Adjacency matrix (1 if connected, 0 otherwise).
70
+ :rtype: list[list[int]]
71
+ """
72
+ id_to_idx = {n.id: i for i, n in enumerate(topology.nodes)}
73
+ size = len(topology.nodes)
74
+ matrix = [[0] * size for _ in range(size)]
75
+ for link in topology.links:
76
+ i = id_to_idx[link.source.id]
77
+ j = id_to_idx[link.target.id]
78
+ matrix[i][j] = 1
79
+ matrix[j][i] = 1
80
+ return matrix
@@ -0,0 +1,285 @@
1
+ {
2
+ "name": "Abilene",
3
+ "nodes": [
4
+ {
5
+ "id": 0,
6
+ "name": "Seattle",
7
+ "weight": 0,
8
+ "longitude": -122.3328481,
9
+ "latitude": 47.6061389,
10
+ "pop": 780995,
11
+ "DC": 58,
12
+ "IXP": 5
13
+ },
14
+ {
15
+ "id": 1,
16
+ "name": "Sunnyvale",
17
+ "weight": 0,
18
+ "longitude": -122.0363496,
19
+ "latitude": 37.36883,
20
+ "pop": 155805,
21
+ "DC": 0,
22
+ "IXP": 0
23
+ },
24
+ {
25
+ "id": 2,
26
+ "name": "Los Angeles",
27
+ "weight": 0,
28
+ "longitude": -118.242643,
29
+ "latitude": 34.0549076,
30
+ "pop": 3820914,
31
+ "DC": 71,
32
+ "IXP": 9
33
+ },
34
+ {
35
+ "id": 3,
36
+ "name": "Denver",
37
+ "weight": 0,
38
+ "longitude": -104.990251,
39
+ "latitude": 39.7392358,
40
+ "pop": 729019,
41
+ "DC": 49,
42
+ "IXP": 6
43
+ },
44
+ {
45
+ "id": 4,
46
+ "name": "Kansas City",
47
+ "weight": 0,
48
+ "longitude": -94.5785667,
49
+ "latitude": 39.0997265,
50
+ "pop": 475378,
51
+ "DC": 25,
52
+ "IXP": 1
53
+ },
54
+ {
55
+ "id": 5,
56
+ "name": "Houston",
57
+ "weight": 0,
58
+ "longitude": -95.3701108,
59
+ "latitude": 29.7600771,
60
+ "pop": 2314157,
61
+ "DC": 55,
62
+ "IXP": 6
63
+ },
64
+ {
65
+ "id": 6,
66
+ "name": "Atlanta",
67
+ "weight": 0,
68
+ "longitude": -84.3885209,
69
+ "latitude": 33.7501275,
70
+ "pop": 510823,
71
+ "DC": 145,
72
+ "IXP": 7
73
+ },
74
+ {
75
+ "id": 7,
76
+ "name": "Indianapolis",
77
+ "weight": 0,
78
+ "longitude": -86.158018,
79
+ "latitude": 39.76909,
80
+ "pop": 887642,
81
+ "DC": 30,
82
+ "IXP": 2
83
+ },
84
+ {
85
+ "id": 8,
86
+ "name": "Chicago",
87
+ "weight": 0,
88
+ "longitude": -87.6323879,
89
+ "latitude": 41.88325,
90
+ "pop": 2664452,
91
+ "DC": 165,
92
+ "IXP": 10
93
+ },
94
+ {
95
+ "id": 9,
96
+ "name": "Washington D.C.",
97
+ "weight": 0,
98
+ "longitude": -77.0368707,
99
+ "latitude": 38.9071923,
100
+ "pop": 689545,
101
+ "DC": 7,
102
+ "IXP": 3
103
+ },
104
+ {
105
+ "id": 10,
106
+ "name": "New York",
107
+ "weight": 0,
108
+ "longitude": -74.0059728,
109
+ "latitude": 40.7127753,
110
+ "pop": 8804190,
111
+ "DC": 70,
112
+ "IXP": 11
113
+ }
114
+ ],
115
+ "links": [
116
+ {
117
+ "id": 0,
118
+ "src": 0,
119
+ "dst": 1,
120
+ "length": 1482.26
121
+ },
122
+ {
123
+ "id": 1,
124
+ "src": 2,
125
+ "dst": 1,
126
+ "length": 649.31
127
+ },
128
+ {
129
+ "id": 2,
130
+ "src": 5,
131
+ "dst": 2,
132
+ "length": 2870.2
133
+ },
134
+ {
135
+ "id": 3,
136
+ "src": 4,
137
+ "dst": 5,
138
+ "length": 1360.7
139
+ },
140
+ {
141
+ "id": 4,
142
+ "src": 3,
143
+ "dst": 0,
144
+ "length": 2123.16
145
+ },
146
+ {
147
+ "id": 5,
148
+ "src": 3,
149
+ "dst": 1,
150
+ "length": 1949.85
151
+ },
152
+ {
153
+ "id": 6,
154
+ "src": 4,
155
+ "dst": 3,
156
+ "length": 1153.26
157
+ },
158
+ {
159
+ "id": 7,
160
+ "src": 8,
161
+ "dst": 7,
162
+ "length": 345.36
163
+ },
164
+ {
165
+ "id": 8,
166
+ "src": 4,
167
+ "dst": 7,
168
+ "length": 933.65
169
+ },
170
+ {
171
+ "id": 9,
172
+ "src": 5,
173
+ "dst": 6,
174
+ "length": 1471.24
175
+ },
176
+ {
177
+ "id": 10,
178
+ "src": 6,
179
+ "dst": 7,
180
+ "length": 912.61
181
+ },
182
+ {
183
+ "id": 11,
184
+ "src": 10,
185
+ "dst": 8,
186
+ "length": 1489.76
187
+ },
188
+ {
189
+ "id": 12,
190
+ "src": 6,
191
+ "dst": 9,
192
+ "length": 1126.74
193
+ },
194
+ {
195
+ "id": 13,
196
+ "src": 9,
197
+ "dst": 10,
198
+ "length": 416.12
199
+ },
200
+ {
201
+ "id": 14,
202
+ "src": 1,
203
+ "dst": 0,
204
+ "length": 1482.26
205
+ },
206
+ {
207
+ "id": 15,
208
+ "src": 1,
209
+ "dst": 2,
210
+ "length": 649.31
211
+ },
212
+ {
213
+ "id": 16,
214
+ "src": 2,
215
+ "dst": 5,
216
+ "length": 2870.2
217
+ },
218
+ {
219
+ "id": 17,
220
+ "src": 5,
221
+ "dst": 4,
222
+ "length": 1360.7
223
+ },
224
+ {
225
+ "id": 18,
226
+ "src": 0,
227
+ "dst": 3,
228
+ "length": 2123.16
229
+ },
230
+ {
231
+ "id": 19,
232
+ "src": 1,
233
+ "dst": 3,
234
+ "length": 1949.85
235
+ },
236
+ {
237
+ "id": 20,
238
+ "src": 3,
239
+ "dst": 4,
240
+ "length": 1153.26
241
+ },
242
+ {
243
+ "id": 21,
244
+ "src": 7,
245
+ "dst": 8,
246
+ "length": 345.36
247
+ },
248
+ {
249
+ "id": 22,
250
+ "src": 7,
251
+ "dst": 4,
252
+ "length": 933.65
253
+ },
254
+ {
255
+ "id": 23,
256
+ "src": 6,
257
+ "dst": 5,
258
+ "length": 1471.24
259
+ },
260
+ {
261
+ "id": 24,
262
+ "src": 7,
263
+ "dst": 6,
264
+ "length": 912.61
265
+ },
266
+ {
267
+ "id": 25,
268
+ "src": 8,
269
+ "dst": 10,
270
+ "length": 1489.76
271
+ },
272
+ {
273
+ "id": 26,
274
+ "src": 9,
275
+ "dst": 6,
276
+ "length": 1126.74
277
+ },
278
+ {
279
+ "id": 27,
280
+ "src": 10,
281
+ "dst": 9,
282
+ "length": 416.12
283
+ }
284
+ ]
285
+ }