topolib 0.3.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.
- analysis/__init__.py +3 -0
- analysis/metrics.py +80 -0
- assets/DT14.json +386 -0
- assets/DT17.json +423 -0
- assets/FRANCE-43.json +1104 -0
- assets/JPN-12.json +282 -0
- assets/NSFNet.json +357 -0
- assets/PLN-12.json +308 -0
- assets/UKNet.json +617 -0
- elements/__init__.py +8 -0
- elements/link.py +145 -0
- elements/node.py +119 -0
- topolib-0.3.0.dist-info/METADATA +127 -0
- topolib-0.3.0.dist-info/RECORD +20 -0
- topolib-0.3.0.dist-info/WHEEL +5 -0
- topolib-0.3.0.dist-info/licenses/LICENSE +22 -0
- topolib-0.3.0.dist-info/top_level.txt +4 -0
- topology/__init__.py +4 -0
- topology/path.py +84 -0
- topology/topology.py +126 -0
elements/link.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
if TYPE_CHECKING:
|
|
6
|
+
from topolib.elements.node import Node
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Link:
|
|
10
|
+
|
|
11
|
+
"""
|
|
12
|
+
.. :noindex:
|
|
13
|
+
|
|
14
|
+
Represents a link between two nodes.
|
|
15
|
+
|
|
16
|
+
Parameters
|
|
17
|
+
----------
|
|
18
|
+
id : int
|
|
19
|
+
Unique identifier for the link.
|
|
20
|
+
source : :class:`topolib.elements.node.Node`
|
|
21
|
+
Source node-like object (must have id, name, latitude, longitude).
|
|
22
|
+
target : :class:`topolib.elements.node.Node`
|
|
23
|
+
Target node-like object (must have id, name, latitude, longitude).
|
|
24
|
+
length : float
|
|
25
|
+
Length of the link (must be non-negative).
|
|
26
|
+
|
|
27
|
+
Attributes
|
|
28
|
+
----------
|
|
29
|
+
id : int
|
|
30
|
+
Unique identifier for the link.
|
|
31
|
+
source : :class:`topolib.elements.node.Node`
|
|
32
|
+
Source node of the link.
|
|
33
|
+
target : :class:`topolib.elements.node.Node`
|
|
34
|
+
Target node of the link.
|
|
35
|
+
length : float
|
|
36
|
+
Length of the link.
|
|
37
|
+
|
|
38
|
+
Examples
|
|
39
|
+
--------
|
|
40
|
+
>>> link = Link(1, nodeA, nodeB, 10.5)
|
|
41
|
+
>>> link.length
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
def __init__(self, id: int, source: "Node", target: "Node", length: float):
|
|
45
|
+
self._id = id
|
|
46
|
+
self.source = source
|
|
47
|
+
self.target = target
|
|
48
|
+
self.length = length
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def id(self) -> int:
|
|
52
|
+
"""
|
|
53
|
+
.. :noindex:
|
|
54
|
+
|
|
55
|
+
int: Unique identifier for the link.
|
|
56
|
+
"""
|
|
57
|
+
return self._id
|
|
58
|
+
|
|
59
|
+
@id.setter
|
|
60
|
+
def id(self, value: int) -> None:
|
|
61
|
+
"""
|
|
62
|
+
Set the link's unique identifier.
|
|
63
|
+
"""
|
|
64
|
+
self._id = value
|
|
65
|
+
|
|
66
|
+
@property
|
|
67
|
+
def source(self) -> "Node":
|
|
68
|
+
"""
|
|
69
|
+
.. :noindex:
|
|
70
|
+
|
|
71
|
+
:class:`topolib.elements.node.Node`: Source node of the link.
|
|
72
|
+
"""
|
|
73
|
+
return self._source
|
|
74
|
+
|
|
75
|
+
@source.setter
|
|
76
|
+
def source(self, value: "Node") -> None:
|
|
77
|
+
"""
|
|
78
|
+
Set the source node. Must have id, name, latitude, longitude.
|
|
79
|
+
"""
|
|
80
|
+
required_attrs = ("id", "name", "latitude", "longitude")
|
|
81
|
+
for attr in required_attrs:
|
|
82
|
+
if not hasattr(value, attr):
|
|
83
|
+
raise TypeError(
|
|
84
|
+
f"source must behave like a Node (missing {attr})")
|
|
85
|
+
self._source = value
|
|
86
|
+
|
|
87
|
+
@property
|
|
88
|
+
def target(self) -> "Node":
|
|
89
|
+
"""
|
|
90
|
+
.. :noindex:
|
|
91
|
+
|
|
92
|
+
:class:`topolib.elements.node.Node`: Target node of the link.
|
|
93
|
+
"""
|
|
94
|
+
return self._target
|
|
95
|
+
|
|
96
|
+
@target.setter
|
|
97
|
+
def target(self, value: "Node") -> None:
|
|
98
|
+
"""
|
|
99
|
+
Set the target node. Must have id, name, latitude, longitude.
|
|
100
|
+
"""
|
|
101
|
+
required_attrs = ("id", "name", "latitude", "longitude")
|
|
102
|
+
for attr in required_attrs:
|
|
103
|
+
if not hasattr(value, attr):
|
|
104
|
+
raise TypeError(
|
|
105
|
+
f"target must behave like a Node (missing {attr})")
|
|
106
|
+
self._target = value
|
|
107
|
+
|
|
108
|
+
@property
|
|
109
|
+
def length(self) -> float:
|
|
110
|
+
"""
|
|
111
|
+
.. :noindex:
|
|
112
|
+
|
|
113
|
+
float: Length of the link (non-negative).
|
|
114
|
+
"""
|
|
115
|
+
return self._length
|
|
116
|
+
|
|
117
|
+
@length.setter
|
|
118
|
+
def length(self, value: float) -> None:
|
|
119
|
+
"""
|
|
120
|
+
Set the length of the link. Must be a non-negative float.
|
|
121
|
+
"""
|
|
122
|
+
try:
|
|
123
|
+
numeric = float(value)
|
|
124
|
+
except Exception:
|
|
125
|
+
raise TypeError("length must be a numeric value")
|
|
126
|
+
if numeric < 0:
|
|
127
|
+
raise ValueError("length must be non-negative")
|
|
128
|
+
self._length = numeric
|
|
129
|
+
|
|
130
|
+
def endpoints(self):
|
|
131
|
+
"""
|
|
132
|
+
Return the (source, target) nodes as a tuple.
|
|
133
|
+
|
|
134
|
+
Returns
|
|
135
|
+
-------
|
|
136
|
+
tuple
|
|
137
|
+
(source_node, target_node)
|
|
138
|
+
"""
|
|
139
|
+
return self._source, self._target
|
|
140
|
+
|
|
141
|
+
def __repr__(self) -> str:
|
|
142
|
+
"""
|
|
143
|
+
Return a string representation of the Link.
|
|
144
|
+
"""
|
|
145
|
+
return f"Link(id={self._id}, source={self._source.id}, target={self._target.id}, length={self._length})"
|
elements/node.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Node class for optical network topologies.
|
|
3
|
+
|
|
4
|
+
This module defines the Node class, representing a network node with geographic coordinates.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Tuple
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Node:
|
|
11
|
+
"""
|
|
12
|
+
.. :noindex:
|
|
13
|
+
|
|
14
|
+
Represents a node in an optical network topology.
|
|
15
|
+
|
|
16
|
+
:param id: Unique identifier for the node.
|
|
17
|
+
:type id: int
|
|
18
|
+
:param name: Name of the node.
|
|
19
|
+
:type name: str
|
|
20
|
+
:param latitude: Latitude coordinate of the node.
|
|
21
|
+
:type latitude: float
|
|
22
|
+
:param longitude: Longitude coordinate of the node.
|
|
23
|
+
:type longitude: float
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(self, id: int, name: str, latitude: float, longitude: float):
|
|
27
|
+
self._id = id
|
|
28
|
+
self._name = name
|
|
29
|
+
self._latitude = latitude
|
|
30
|
+
self._longitude = longitude
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def id(self) -> int:
|
|
34
|
+
"""
|
|
35
|
+
Get the unique identifier of the node.
|
|
36
|
+
|
|
37
|
+
:return: Node ID.
|
|
38
|
+
:rtype: int
|
|
39
|
+
"""
|
|
40
|
+
return self._id
|
|
41
|
+
|
|
42
|
+
@id.setter
|
|
43
|
+
def id(self, value: int) -> None:
|
|
44
|
+
"""
|
|
45
|
+
Set the unique identifier of the node.
|
|
46
|
+
|
|
47
|
+
:param value: Node ID.
|
|
48
|
+
:type value: int
|
|
49
|
+
"""
|
|
50
|
+
self._id = value
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def name(self) -> str:
|
|
54
|
+
"""
|
|
55
|
+
Get the name of the node.
|
|
56
|
+
|
|
57
|
+
:return: Node name.
|
|
58
|
+
:rtype: str
|
|
59
|
+
"""
|
|
60
|
+
return self._name
|
|
61
|
+
|
|
62
|
+
@name.setter
|
|
63
|
+
def name(self, value: str) -> None:
|
|
64
|
+
"""
|
|
65
|
+
Set the name of the node.
|
|
66
|
+
|
|
67
|
+
:param value: Node name.
|
|
68
|
+
:type value: str
|
|
69
|
+
"""
|
|
70
|
+
self._name = value
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def latitude(self) -> float:
|
|
74
|
+
"""
|
|
75
|
+
Get the latitude coordinate of the node.
|
|
76
|
+
|
|
77
|
+
:return: Latitude value.
|
|
78
|
+
:rtype: float
|
|
79
|
+
"""
|
|
80
|
+
return self._latitude
|
|
81
|
+
|
|
82
|
+
@latitude.setter
|
|
83
|
+
def latitude(self, value: float) -> None:
|
|
84
|
+
"""
|
|
85
|
+
Set the latitude coordinate of the node.
|
|
86
|
+
|
|
87
|
+
:param value: Latitude value.
|
|
88
|
+
:type value: float
|
|
89
|
+
"""
|
|
90
|
+
self._latitude = value
|
|
91
|
+
|
|
92
|
+
@property
|
|
93
|
+
def longitude(self) -> float:
|
|
94
|
+
"""
|
|
95
|
+
Get the longitude coordinate of the node.
|
|
96
|
+
|
|
97
|
+
:return: Longitude value.
|
|
98
|
+
:rtype: float
|
|
99
|
+
"""
|
|
100
|
+
return self._longitude
|
|
101
|
+
|
|
102
|
+
@longitude.setter
|
|
103
|
+
def longitude(self, value: float) -> None:
|
|
104
|
+
"""
|
|
105
|
+
Set the longitude coordinate of the node.
|
|
106
|
+
|
|
107
|
+
:param value: Longitude value.
|
|
108
|
+
:type value: float
|
|
109
|
+
"""
|
|
110
|
+
self._longitude = value
|
|
111
|
+
|
|
112
|
+
def coordinates(self) -> Tuple[float, float]:
|
|
113
|
+
"""
|
|
114
|
+
Returns the (latitude, longitude) coordinates of the node.
|
|
115
|
+
|
|
116
|
+
:return: Tuple containing latitude and longitude.
|
|
117
|
+
:rtype: Tuple[float, float]
|
|
118
|
+
"""
|
|
119
|
+
return self._latitude, self._longitude
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: topolib
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: A compact Python library for modeling, analyzing, and visualizing optical network topologies.
|
|
5
|
+
Author-email: Danilo Bórquez-Paredes <danilo.borquez.p@uai.cl>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://gitlab.com/DaniloBorquez/topolib
|
|
8
|
+
Project-URL: Documentation, https://topolib.readthedocs.io/
|
|
9
|
+
Project-URL: Source, https://gitlab.com/DaniloBorquez/topolib
|
|
10
|
+
Project-URL: Issues, https://gitlab.com/DaniloBorquez/topolib/-/issues
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Requires-Dist: numpy>=1.21
|
|
20
|
+
Requires-Dist: networkx>=2.6
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
|
|
23
|
+
# Topolib 🚀
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
[](https://www.python.org/)
|
|
27
|
+
[](LICENSE)
|
|
28
|
+
[](https://gitlab.com/DaniloBorquez/topolib/-/issues)
|
|
29
|
+
[](https://gitlab.com/DaniloBorquez/topolib/-/pipelines?ref=develop)
|
|
30
|
+
[](https://gitlab.com/DaniloBorquez/topolib/-/pipelines?ref=release)
|
|
31
|
+
[](https://topolib.readthedocs.io/en/latest/?badge=latest)
|
|
32
|
+
|
|
33
|
+
A compact Python library for working with optical network topologies: nodes, links, metrics and visualization tools. 🌐
|
|
34
|
+
|
|
35
|
+
## Overview
|
|
36
|
+
|
|
37
|
+
Topolib models network topologies with three main modules:
|
|
38
|
+
|
|
39
|
+
- `topolib.elements` — Definitions of elementary building blocks
|
|
40
|
+
- `Node` — represents a network node with id, name and geographic coordinates
|
|
41
|
+
- `Link` — connects two `Node` objects and stores link length and id
|
|
42
|
+
|
|
43
|
+
- `topolib.topology` — High-level topology model
|
|
44
|
+
- `Topology` — holds nodes and links, provides methods to add/remove nodes and links, compute metrics, export JSON, and compute shortest/disjoint paths
|
|
45
|
+
- `Path` — represents a path through the topology
|
|
46
|
+
|
|
47
|
+
- `topolib.analysis` — Metrics and analysis helpers
|
|
48
|
+
- `Metrics` — functions to compute node degree, link length statistics, connection matrices, etc.
|
|
49
|
+
|
|
50
|
+
- `topolib.visualization` — Visualization helpers
|
|
51
|
+
- `MapView` — functions to display topology with OSM or paper-style maps
|
|
52
|
+
|
|
53
|
+
(These components are derived from the project's class diagram in `diagrams/class_diagram.puml`.)
|
|
54
|
+
|
|
55
|
+
## Features
|
|
56
|
+
- Modular design: elements, topology, analysis, and visualization
|
|
57
|
+
- Easy-to-use classes for nodes, links, and paths
|
|
58
|
+
- Built-in metrics and analysis helpers
|
|
59
|
+
- JSON import/export and interoperability
|
|
60
|
+
- Ready for Sphinx, Read the Docs, and PyPI
|
|
61
|
+
|
|
62
|
+
## Quickstart ⚡
|
|
63
|
+
|
|
64
|
+
Create and activate a virtual environment, install dev dependencies and run tests:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
python -m venv .venv
|
|
68
|
+
source .venv/bin/activate
|
|
69
|
+
python -m pip install -U pip
|
|
70
|
+
python -m pip install -r dev-requirements.txt
|
|
71
|
+
python -m pytest -q
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Installation
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
pip install topolib
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Or for development:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
git clone https://gitlab.com/DaniloBorquez/topolib.git
|
|
84
|
+
cd topolib
|
|
85
|
+
python -m venv .venv
|
|
86
|
+
source .venv/bin/activate
|
|
87
|
+
pip install -e .
|
|
88
|
+
pip install -r dev-requirements.txt
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Documentation
|
|
92
|
+
|
|
93
|
+
Full documentation: [https://topolib.readthedocs.io/](https://topolib.readthedocs.io/)
|
|
94
|
+
|
|
95
|
+
## Basic usage example
|
|
96
|
+
|
|
97
|
+
```py
|
|
98
|
+
from topolib.elements.node import Node
|
|
99
|
+
from topolib.topology.topology import Topology
|
|
100
|
+
|
|
101
|
+
# Create nodes
|
|
102
|
+
n1 = Node(1, 'A', 10.0, 20.0)
|
|
103
|
+
n2 = Node(2, 'B', 11.0, 21.0)
|
|
104
|
+
|
|
105
|
+
# Build topology
|
|
106
|
+
topo = Topology()
|
|
107
|
+
topo.add_node(n1)
|
|
108
|
+
topo.add_node(n2)
|
|
109
|
+
# add links, compute metrics, visualize
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
## Development 🛠️
|
|
114
|
+
|
|
115
|
+
See `CONTRIBUTING.md` for development guidelines, commit message rules and pre-commit setup.
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
## Class diagram
|
|
120
|
+
|
|
121
|
+

|
|
122
|
+
|
|
123
|
+
(If you prefer a rendered image of the UML, render the PlantUML file locally or in your CI pipeline.)
|
|
124
|
+
|
|
125
|
+
## License
|
|
126
|
+
|
|
127
|
+
See `LICENSE` in the project root.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
analysis/__init__.py,sha256=qvUC9wV_jQNQIlwJXk7LJ-dkTXhH1Ttn0lKWIdwBbrc,52
|
|
2
|
+
analysis/metrics.py,sha256=Di4HGHlz9c2UTCYdfnmjtPjo7w3wwYuyDPeXI7bR0zA,2592
|
|
3
|
+
assets/DT14.json,sha256=eG7Yz7kIMSov3bWAlSBsA9dXpq7MZRN8IZF20OVg2Wc,7543
|
|
4
|
+
assets/DT17.json,sha256=oC-5yxJpONqomYVMxLIeEf39UpPTAxUc3yDH9sVjot4,8505
|
|
5
|
+
assets/FRANCE-43.json,sha256=NCb2NJnQv4dq7PAbjJQfdunE1JJRn3G2xFKEJzkHaMo,21918
|
|
6
|
+
assets/JPN-12.json,sha256=IYGerYCjUsaYie2vSOpDwXR-hwT40fJJ4axeKAhCtBY,5377
|
|
7
|
+
assets/NSFNet.json,sha256=_uiuG3_6RnjafsIZbg0-QoZFMtMmrSLIGQUTI5WaGzY,6888
|
|
8
|
+
assets/PLN-12.json,sha256=zxmWnSONK6cR2SFB_cG92Mgts7lhXQWvx6V_KQkrXsI,5995
|
|
9
|
+
assets/UKNet.json,sha256=TurrVnx6pa3IgpZYJziOUJlXIIPS5RalmMI5DgRX7dQ,11911
|
|
10
|
+
elements/__init__.py,sha256=ZWSd5uwVtppJtMnZ30zrAdzXhUAYIK62RrUaH9rsWu0,180
|
|
11
|
+
elements/link.py,sha256=BwjdCR8peh7dDpKunZodlIsjKcfvwjKxyQ3tmp1obb8,3778
|
|
12
|
+
elements/node.py,sha256=zIzKm1_dlEqOuNQbNc-R3NOWHytk0Tn6jMLWIoV7ENk,2704
|
|
13
|
+
topolib-0.3.0.dist-info/licenses/LICENSE,sha256=kbnIP0XU6f2ualiTjEawdlU81IGPBbwc-_GF3N-1e9E,1081
|
|
14
|
+
topology/__init__.py,sha256=2VRhVm4ZvKBiTDB2T8BDmLZBpwGCVFRF3fvtxxC_d28,86
|
|
15
|
+
topology/path.py,sha256=oUNwmpBcS6LMMAJIxokROm3MVqr7vRR44M3Fh5ADq_w,2057
|
|
16
|
+
topology/topology.py,sha256=5aeIahfPEGAK01chEka21slkhg4D_hb7apMZpRn-VOg,4168
|
|
17
|
+
topolib-0.3.0.dist-info/METADATA,sha256=T8UctAUpqw_jzfwbOkkAuyrxUj3PY8HesdFd6pfyGfw,4294
|
|
18
|
+
topolib-0.3.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
19
|
+
topolib-0.3.0.dist-info/top_level.txt,sha256=lyqPH1vGGLmvT7EaqN4in6QtMnjU1WVHMbD3AselI2o,34
|
|
20
|
+
topolib-0.3.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
|
|
2
|
+
MIT License
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2025 Danilo Bórquez-Paredes
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
topology/__init__.py
ADDED
topology/path.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Path class for representing a sequence of nodes and links in a topology.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import List, Any
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Path:
|
|
9
|
+
"""
|
|
10
|
+
Represents a path through the network topology as an ordered sequence of nodes and links.
|
|
11
|
+
|
|
12
|
+
Parameters
|
|
13
|
+
----------
|
|
14
|
+
nodes : list
|
|
15
|
+
Ordered list of node objects in the path.
|
|
16
|
+
links : list
|
|
17
|
+
Ordered list of link objects in the path (len(links) == len(nodes) - 1).
|
|
18
|
+
|
|
19
|
+
Raises
|
|
20
|
+
------
|
|
21
|
+
ValueError
|
|
22
|
+
If the number of nodes and links is inconsistent or empty.
|
|
23
|
+
|
|
24
|
+
Attributes
|
|
25
|
+
----------
|
|
26
|
+
nodes : list
|
|
27
|
+
Ordered list of nodes in the path.
|
|
28
|
+
links : list
|
|
29
|
+
Ordered list of links in the path.
|
|
30
|
+
|
|
31
|
+
Examples
|
|
32
|
+
--------
|
|
33
|
+
>>> nodes = [Node(1), Node(2), Node(3)]
|
|
34
|
+
>>> links = [Link('a'), Link('b')]
|
|
35
|
+
>>> path = Path(nodes, links)
|
|
36
|
+
>>> path.length()
|
|
37
|
+
2
|
|
38
|
+
>>> path.endpoints()
|
|
39
|
+
(Node(1), Node(3))
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
def __init__(self, nodes: List[Any], links: List[Any]):
|
|
43
|
+
if not nodes or not links:
|
|
44
|
+
raise ValueError("A path must have at least one node and one link.")
|
|
45
|
+
if len(nodes) != len(links) + 1:
|
|
46
|
+
raise ValueError("Number of nodes must be one more than number of links.")
|
|
47
|
+
self.nodes = nodes
|
|
48
|
+
self.links = links
|
|
49
|
+
|
|
50
|
+
def length(self) -> int:
|
|
51
|
+
"""
|
|
52
|
+
Return the number of links in the path.
|
|
53
|
+
|
|
54
|
+
Returns
|
|
55
|
+
-------
|
|
56
|
+
int
|
|
57
|
+
Number of links in the path.
|
|
58
|
+
"""
|
|
59
|
+
return len(self.links)
|
|
60
|
+
|
|
61
|
+
def hop_count(self) -> int:
|
|
62
|
+
"""
|
|
63
|
+
Return the number of hops (links) in the path.
|
|
64
|
+
|
|
65
|
+
Returns
|
|
66
|
+
-------
|
|
67
|
+
int
|
|
68
|
+
Number of hops (links) in the path.
|
|
69
|
+
"""
|
|
70
|
+
return self.length()
|
|
71
|
+
|
|
72
|
+
def endpoints(self):
|
|
73
|
+
"""
|
|
74
|
+
Return the source and target nodes of the path as a tuple.
|
|
75
|
+
|
|
76
|
+
Returns
|
|
77
|
+
-------
|
|
78
|
+
tuple
|
|
79
|
+
(source_node, target_node)
|
|
80
|
+
"""
|
|
81
|
+
return (self.nodes[0], self.nodes[-1])
|
|
82
|
+
|
|
83
|
+
def __repr__(self):
|
|
84
|
+
return f"Path(nodes={self.nodes}, links={self.links})"
|
topology/topology.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Topology class for optical network topologies.
|
|
3
|
+
|
|
4
|
+
This module defines the Topology class, representing a network topology with nodes and links,
|
|
5
|
+
and providing an adjacency matrix using numpy.
|
|
6
|
+
|
|
7
|
+
This file uses NetworkX (BSD 3-Clause License):
|
|
8
|
+
https://github.com/networkx/networkx/blob/main/LICENSE.txt
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from typing import List, Dict, Any
|
|
12
|
+
import numpy as np
|
|
13
|
+
import networkx as nx
|
|
14
|
+
from topolib.elements.node import Node
|
|
15
|
+
from topolib.elements.link import Link
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Topology:
|
|
19
|
+
"""
|
|
20
|
+
Represents a network topology with nodes and links.
|
|
21
|
+
|
|
22
|
+
:param nodes: Initial list of nodes (optional).
|
|
23
|
+
:type nodes: list[topolib.elements.node.Node] or None
|
|
24
|
+
:param links: Initial list of links (optional).
|
|
25
|
+
:type links: list[topolib.elements.link.Link] or None
|
|
26
|
+
|
|
27
|
+
:ivar nodes: List of nodes in the topology.
|
|
28
|
+
:vartype nodes: list[Node]
|
|
29
|
+
:ivar links: List of links in the topology.
|
|
30
|
+
:vartype links: list[Link]
|
|
31
|
+
|
|
32
|
+
**Examples**
|
|
33
|
+
>>> from topolib.elements.node import Node
|
|
34
|
+
>>> from topolib.elements.link import Link
|
|
35
|
+
>>> from topolib.topology import Topology
|
|
36
|
+
>>> n1 = Node(1, "A", 0.0, 0.0)
|
|
37
|
+
>>> n2 = Node(2, "B", 1.0, 1.0)
|
|
38
|
+
>>> l1 = Link(1, n1, n2, 10.0)
|
|
39
|
+
>>> topo = Topology(nodes=[n1, n2], links=[l1])
|
|
40
|
+
>>> topo.adjacency_matrix()
|
|
41
|
+
array([[0, 1],
|
|
42
|
+
[1, 0]])
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
def __init__(self, nodes: List[Node] = None, links: List[Link] = None):
|
|
46
|
+
"""
|
|
47
|
+
Initialize a Topology object.
|
|
48
|
+
|
|
49
|
+
:param nodes: Initial list of nodes (optional).
|
|
50
|
+
:type nodes: list[topolib.elements.node.Node] or None
|
|
51
|
+
:param links: Initial list of links (optional).
|
|
52
|
+
:type links: list[topolib.elements.link.Link] or None
|
|
53
|
+
"""
|
|
54
|
+
self.nodes: List[Node] = nodes if nodes is not None else []
|
|
55
|
+
self.links: List[Link] = links if links is not None else []
|
|
56
|
+
# Internal NetworkX graph for algorithms and visualization
|
|
57
|
+
self._graph = nx.Graph()
|
|
58
|
+
for node in self.nodes:
|
|
59
|
+
self._graph.add_node(node.id, node=node)
|
|
60
|
+
for link in self.links:
|
|
61
|
+
self._graph.add_edge(link.source.id, link.target.id, link=link)
|
|
62
|
+
|
|
63
|
+
def add_node(self, node: Node) -> None:
|
|
64
|
+
"""
|
|
65
|
+
Add a node to the topology.
|
|
66
|
+
|
|
67
|
+
:param node: Node to add.
|
|
68
|
+
:type node: Node
|
|
69
|
+
"""
|
|
70
|
+
self.nodes.append(node)
|
|
71
|
+
self._graph.add_node(node.id, node=node)
|
|
72
|
+
|
|
73
|
+
def add_link(self, link: Link) -> None:
|
|
74
|
+
"""
|
|
75
|
+
Add a link to the topology.
|
|
76
|
+
|
|
77
|
+
:param link: Link to add.
|
|
78
|
+
:type link: Link
|
|
79
|
+
"""
|
|
80
|
+
self.links.append(link)
|
|
81
|
+
self._graph.add_edge(link.source.id, link.target.id, link=link)
|
|
82
|
+
|
|
83
|
+
def remove_node(self, node_id: int) -> None:
|
|
84
|
+
"""
|
|
85
|
+
Remove a node and all its links by node id.
|
|
86
|
+
|
|
87
|
+
:param node_id: ID of the node to remove.
|
|
88
|
+
:type node_id: int
|
|
89
|
+
"""
|
|
90
|
+
self.nodes = [n for n in self.nodes if n.id != node_id]
|
|
91
|
+
self.links = [
|
|
92
|
+
l for l in self.links if l.source.id != node_id and l.target.id != node_id
|
|
93
|
+
]
|
|
94
|
+
self._graph.remove_node(node_id)
|
|
95
|
+
|
|
96
|
+
def remove_link(self, link_id: int) -> None:
|
|
97
|
+
"""
|
|
98
|
+
Remove a link by its id.
|
|
99
|
+
|
|
100
|
+
:param link_id: ID of the link to remove.
|
|
101
|
+
:type link_id: int
|
|
102
|
+
"""
|
|
103
|
+
# Find the link and remove from graph
|
|
104
|
+
link = next((l for l in self.links if l.id == link_id), None)
|
|
105
|
+
if link:
|
|
106
|
+
self._graph.remove_edge(link.source.id, link.target.id)
|
|
107
|
+
self.links = [l for l in self.links if l.id != link_id]
|
|
108
|
+
|
|
109
|
+
def adjacency_matrix(self) -> np.ndarray:
|
|
110
|
+
"""
|
|
111
|
+
Return the adjacency matrix of the topology as a numpy array.
|
|
112
|
+
|
|
113
|
+
:return: Adjacency matrix (1 if connected, 0 otherwise).
|
|
114
|
+
:rtype: numpy.ndarray
|
|
115
|
+
|
|
116
|
+
**Example**
|
|
117
|
+
>>> topo.adjacency_matrix()
|
|
118
|
+
array([[0, 1],
|
|
119
|
+
[1, 0]])
|
|
120
|
+
"""
|
|
121
|
+
# Usa NetworkX para obtener la matriz de adyacencia
|
|
122
|
+
if not self.nodes:
|
|
123
|
+
return np.zeros((0, 0), dtype=int)
|
|
124
|
+
node_ids = [n.id for n in self.nodes]
|
|
125
|
+
mat = nx.to_numpy_array(self._graph, nodelist=node_ids, dtype=int)
|
|
126
|
+
return mat
|