topolib 0.3.0__py3-none-any.whl → 0.4.2__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.

@@ -21,13 +21,75 @@ class Node:
21
21
  :type latitude: float
22
22
  :param longitude: Longitude coordinate of the node.
23
23
  :type longitude: float
24
+ :param weight: Node weight (optional, default 0).
25
+ :type weight: float or int
26
+ :param pop: Node population (optional, default 0).
27
+ :type pop: int
28
+ :param dc: Datacenter (DC) value for the node (optional, default 0).
29
+ :type dc: int
30
+ :param ixp: IXP (Internet Exchange Point) value for the node (optional, default 0).
31
+ :type ixp: int
24
32
  """
25
33
 
26
- def __init__(self, id: int, name: str, latitude: float, longitude: float):
34
+ def __init__(
35
+ self,
36
+ id: int,
37
+ name: str,
38
+ latitude: float,
39
+ longitude: float,
40
+ weight: float = 0,
41
+ pop: int = 0,
42
+ dc: int = 0,
43
+ ixp: int = 0,
44
+ ):
27
45
  self._id = id
28
46
  self._name = name
29
47
  self._latitude = latitude
30
48
  self._longitude = longitude
49
+ self._weight = weight
50
+ self._pop = pop
51
+ self._dc = dc
52
+ self._ixp = ixp
53
+
54
+ @property
55
+ def dc(self) -> int:
56
+ """
57
+ Get the datacenter (DC) count or value for the node.
58
+
59
+ :return: Node DC value.
60
+ :rtype: int
61
+ """
62
+ return self._dc
63
+
64
+ @dc.setter
65
+ def dc(self, value: int) -> None:
66
+ """
67
+ Set the datacenter (DC) value for the node.
68
+
69
+ :param value: Node DC value.
70
+ :type value: int
71
+ """
72
+ self._dc = value
73
+
74
+ @property
75
+ def ixp(self) -> int:
76
+ """
77
+ Get the IXP (Internet Exchange Point) count or value for the node.
78
+
79
+ :return: Node IXP value.
80
+ :rtype: int
81
+ """
82
+ return self._ixp
83
+
84
+ @ixp.setter
85
+ def ixp(self, value: int) -> None:
86
+ """
87
+ Set the IXP (Internet Exchange Point) value for the node.
88
+
89
+ :param value: Node IXP value.
90
+ :type value: int
91
+ """
92
+ self._ixp = value
31
93
 
32
94
  @property
33
95
  def id(self) -> int:
@@ -109,6 +171,46 @@ class Node:
109
171
  """
110
172
  self._longitude = value
111
173
 
174
+ @property
175
+ def weight(self) -> float:
176
+ """
177
+ Get the weight of the node.
178
+
179
+ :return: Node weight.
180
+ :rtype: float
181
+ """
182
+ return self._weight
183
+
184
+ @weight.setter
185
+ def weight(self, value: float) -> None:
186
+ """
187
+ Set the weight of the node.
188
+
189
+ :param value: Node weight.
190
+ :type value: float
191
+ """
192
+ self._weight = value
193
+
194
+ @property
195
+ def pop(self) -> int:
196
+ """
197
+ Get the population of the node.
198
+
199
+ :return: Node population.
200
+ :rtype: int
201
+ """
202
+ return self._pop
203
+
204
+ @pop.setter
205
+ def pop(self, value: int) -> None:
206
+ """
207
+ Set the population of the node.
208
+
209
+ :param value: Node population.
210
+ :type value: int
211
+ """
212
+ self._pop = value
213
+
112
214
  def coordinates(self) -> Tuple[float, float]:
113
215
  """
114
216
  Returns the (latitude, longitude) coordinates of the node.
@@ -8,7 +8,7 @@ This file uses NetworkX (BSD 3-Clause License):
8
8
  https://github.com/networkx/networkx/blob/main/LICENSE.txt
9
9
  """
10
10
 
11
- from typing import List, Dict, Any
11
+ from typing import List, Dict, Any, Optional
12
12
  import numpy as np
13
13
  import networkx as nx
14
14
  from topolib.elements.node import Node
@@ -42,17 +42,25 @@ class Topology:
42
42
  [1, 0]])
43
43
  """
44
44
 
45
- def __init__(self, nodes: List[Node] = None, links: List[Link] = None):
45
+ def __init__(
46
+ self,
47
+ nodes: Optional[List[Node]] = None,
48
+ links: Optional[List[Link]] = None,
49
+ name: Optional[str] = None,
50
+ ):
46
51
  """
47
52
  Initialize a Topology object.
48
53
 
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
54
+ :param nodes: Initial list of nodes (optional).
55
+ :type nodes: list[topolib.elements.node.Node] or None
56
+ :param links: Initial list of links (optional).
57
+ :type links: list[topolib.elements.link.Link] or None
58
+ :param name: Name of the topology (optional).
59
+ :type name: str or None
53
60
  """
54
- self.nodes: List[Node] = nodes if nodes is not None else []
55
- self.links: List[Link] = links if links is not None else []
61
+ self.nodes = nodes if nodes is not None else []
62
+ self.links = links if links is not None else []
63
+ self.name = name
56
64
  # Internal NetworkX graph for algorithms and visualization
57
65
  self._graph = nx.Graph()
58
66
  for node in self.nodes:
@@ -60,6 +68,44 @@ class Topology:
60
68
  for link in self.links:
61
69
  self._graph.add_edge(link.source.id, link.target.id, link=link)
62
70
 
71
+ @classmethod
72
+ def from_json(cls, json_path: str) -> "Topology":
73
+ """
74
+ Create a Topology object from a JSON file.
75
+
76
+ :param json_path: Path to the JSON file containing the topology.
77
+ :type json_path: str
78
+ :return: Topology instance loaded from the file.
79
+ :rtype: Topology
80
+ """
81
+ import json
82
+ from topolib.elements.node import Node
83
+ from topolib.elements.link import Link
84
+
85
+ with open(json_path, "r", encoding="utf-8") as f:
86
+ data = json.load(f)
87
+ nodes = [
88
+ Node(
89
+ n["id"],
90
+ n["name"],
91
+ n["latitude"],
92
+ n["longitude"],
93
+ n.get("weight", 0),
94
+ n.get("pop", 0),
95
+ n.get("dc", n.get("DC", 0)),
96
+ n.get("ixp", n.get("IXP", 0)),
97
+ )
98
+ for n in data["nodes"]
99
+ ]
100
+ # Crear un dict para mapear id a Node
101
+ node_dict = {n.id: n for n in nodes}
102
+ links = [
103
+ Link(l["id"], node_dict[l["src"]], node_dict[l["dst"]], l["length"])
104
+ for l in data["links"]
105
+ ]
106
+ name = data.get("name", None)
107
+ return cls(nodes=nodes, links=links, name=name)
108
+
63
109
  def add_node(self, node: Node) -> None:
64
110
  """
65
111
  Add a node to the topology.
@@ -0,0 +1 @@
1
+ from .mapview import MapView
@@ -0,0 +1,75 @@
1
+ """
2
+ MapView class for visualizing network topologies.
3
+
4
+ This module provides visualization methods for Topology objects.
5
+ """
6
+
7
+ from typing import Any
8
+ from topolib.topology import Topology
9
+ import matplotlib.pyplot as plt
10
+ import contextily as ctx
11
+ import geopandas as gpd
12
+ from shapely.geometry import Point
13
+
14
+
15
+ class MapView:
16
+ """
17
+ Provides visualization methods for Topology objects.
18
+ """
19
+
20
+ def __init__(self, topology: Topology) -> None:
21
+ """
22
+ Initialize MapView with a Topology object.
23
+ :param topology: Topology object
24
+ """
25
+ self.topology = topology
26
+
27
+ def show_map(self) -> None:
28
+ """
29
+ Show all nodes and links of the topology on an OpenStreetMap base using contextily and Matplotlib.
30
+ The figure and plot title will be the topology name if available.
31
+ """
32
+ lons: list[float] = [node.longitude for node in self.topology.nodes]
33
+ lats: list[float] = [node.latitude for node in self.topology.nodes]
34
+ names: list[str] = [node.name for node in self.topology.nodes]
35
+ gdf: gpd.GeoDataFrame = gpd.GeoDataFrame(
36
+ {"name": names},
37
+ geometry=[Point(x, y) for x, y in zip(lons, lats)],
38
+ crs="EPSG:4326",
39
+ )
40
+ gdf = gdf.to_crs(epsg=3857)
41
+
42
+ # Map node id to projected coordinates
43
+ node_id_to_xy = {
44
+ node.id: (pt.x, pt.y) for node, pt in zip(self.topology.nodes, gdf.geometry)
45
+ }
46
+
47
+ # Try to get topology name (from attribute or fallback)
48
+ topo_name = getattr(self.topology, "name", None)
49
+ if topo_name is None:
50
+ # Try to get from dict if loaded from JSON
51
+ topo_name = getattr(self.topology, "_name", None)
52
+ if topo_name is None:
53
+ topo_name = "Topology"
54
+
55
+ fig, ax = plt.subplots(figsize=(10, 7))
56
+ fig.suptitle(topo_name, fontsize=16)
57
+ # Draw links as simple lines
58
+ for link in getattr(self.topology, "links", []):
59
+ src_id = getattr(link, "source").id
60
+ tgt_id = getattr(link, "target").id
61
+ if src_id in node_id_to_xy and tgt_id in node_id_to_xy:
62
+ x0, y0 = node_id_to_xy[src_id]
63
+ x1, y1 = node_id_to_xy[tgt_id]
64
+ ax.plot(
65
+ [x0, x1], [y0, y1], color="gray", linewidth=1, alpha=0.7, zorder=2
66
+ )
67
+ # Draw nodes
68
+ gdf.plot(ax=ax, color="blue", markersize=40, zorder=5)
69
+ for x, y, name in zip(gdf.geometry.x, gdf.geometry.y, gdf["name"]):
70
+ ax.text(x, y, name, fontsize=8, ha="right", va="bottom", color="black")
71
+ ctx.add_basemap(ax, source=ctx.providers.OpenStreetMap.Mapnik)
72
+ ax.set_axis_off()
73
+ ax.set_title(f"Nodes and links ({topo_name})")
74
+ plt.tight_layout()
75
+ plt.show()
@@ -0,0 +1,119 @@
1
+ Metadata-Version: 2.4
2
+ Name: topolib
3
+ Version: 0.4.2
4
+ Summary: A compact Python library for modeling, analyzing, and visualizing optical network topologies.
5
+ License: MIT
6
+ License-File: LICENSE
7
+ Keywords: network,topology,optical,analysis,visualization
8
+ Author: Danilo Bórquez-Paredes
9
+ Author-email: danilo.borquez.p@uai.cl
10
+ Requires-Python: >=3.10
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Programming Language :: Python :: 3.14
20
+ Classifier: Topic :: Scientific/Engineering :: Information Analysis
21
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
22
+ Requires-Dist: contextily
23
+ Requires-Dist: geopandas
24
+ Requires-Dist: matplotlib
25
+ Requires-Dist: networkx (>=2.6)
26
+ Requires-Dist: numpy (>=1.21)
27
+ Requires-Dist: shapely
28
+ Project-URL: Documentation, https://topolib.readthedocs.io/
29
+ Project-URL: Homepage, https://gitlab.com/DaniloBorquez/topolib
30
+ Project-URL: Repository, https://gitlab.com/DaniloBorquez/topolib
31
+ Description-Content-Type: text/markdown
32
+
33
+ # Topolib 🚀
34
+ # Topolib 🚀
35
+
36
+ [![Python Version](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/)
37
+ [![License](https://img.shields.io/badge/license-MIT-lightgrey.svg)](LICENSE)
38
+ [![Issues](https://img.shields.io/badge/issues-on%20GitLab-blue.svg)](https://gitlab.com/DaniloBorquez/topolib/-/issues)
39
+ [![Develop coverage](https://gitlab.com/DaniloBorquez/topolib/badges/develop/coverage.svg)](https://gitlab.com/DaniloBorquez/topolib/-/pipelines?ref=develop)
40
+ [![Release coverage](https://gitlab.com/DaniloBorquez/topolib/badges/release/coverage.svg)](https://gitlab.com/DaniloBorquez/topolib/-/pipelines?ref=release)
41
+ [![Documentation Status](https://readthedocs.org/projects/topolib/badge/?version=latest)](https://topolib.readthedocs.io/en/latest/?badge=latest)
42
+
43
+ > **Topolib** is a compact, modular Python library for modeling, analyzing, and visualizing optical network topologies.
44
+ > **Goal:** Provide researchers and engineers with a simple, extensible toolkit for working with nodes, links, metrics, and map-based visualizations.
45
+ >
46
+ > 🌐 **Model** | 📊 **Analyze** | 🗺️ **Visualize** | 🧩 **Extend**
47
+
48
+ ---
49
+
50
+ ## 📂 Examples
51
+
52
+ Explore ready-to-run usage examples in the [`examples/`](examples/) folder!
53
+ - [Show topology on a map](examples/show_topology_in_map.py) 🗺️
54
+ - More coming soon!
55
+
56
+ ---
57
+
58
+ ## 🧭 Overview
59
+
60
+ Topolib is organized into four main modules:
61
+
62
+ - 🧱 **Elements:** `Node`, `Link` — basic building blocks
63
+ - 🕸️ **Topology:** `Topology`, `Path` — manage nodes, links, paths, and adjacency
64
+ - 📈 **Analysis:** `Metrics` — compute node degree, link stats, connection matrices
65
+ - 🖼️ **Visualization:** `MapView` — plot topologies on real maps
66
+
67
+ ---
68
+
69
+ ## ✨ Features
70
+
71
+ - Modular, extensible design
72
+ - Easy-to-use classes for nodes, links, and paths
73
+ - Built-in metrics and analysis helpers
74
+ - JSON import/export and interoperability
75
+ - Ready for Sphinx, Read the Docs, and PyPI
76
+
77
+ ---
78
+
79
+ ## ⚡ Quickstart
80
+
81
+ ```bash
82
+ python -m venv .venv
83
+ source .venv/bin/activate
84
+ pip install -U pip
85
+ pip install topolib
86
+ ```
87
+
88
+ ---
89
+
90
+ ## 📚 Documentation
91
+
92
+ Full documentation: [https://topolib.readthedocs.io/](https://topolib.readthedocs.io/)
93
+
94
+ ---
95
+
96
+ ## 📝 Basic usage
97
+
98
+ ```python
99
+ from topolib.elements.node import Node
100
+ from topolib.topology.topology import Topology
101
+
102
+ n1 = Node(1, 'A', 10.0, 20.0)
103
+ n2 = Node(2, 'B', 11.0, 21.0)
104
+ topo = Topology(nodes=[n1, n2])
105
+ # Add links, compute metrics, visualize, etc.
106
+ ```
107
+
108
+ ---
109
+
110
+ ## 🛠️ Development
111
+
112
+ See [`CONTRIBUTING.md`](CONTRIBUTING.md) for development guidelines, commit message rules, and pre-commit setup.
113
+
114
+ ---
115
+
116
+ ## 📄 License
117
+
118
+ MIT — see [`LICENSE`](LICENSE) for details.
119
+
@@ -0,0 +1,18 @@
1
+ topolib/__init__.py,sha256=iLmy2rOkHS_4KZWMD8BgT7R3tLMKeaTCDVf3B4FyYxM,91
2
+ topolib/analysis/__init__.py,sha256=qvUC9wV_jQNQIlwJXk7LJ-dkTXhH1Ttn0lKWIdwBbrc,52
3
+ topolib/analysis/metrics.py,sha256=2o5PlMVzepDWwSQjzamtKemggqkxh9JuzzCUaeahfok,2555
4
+ topolib/assets/Abilene_IXP.json,sha256=5cnPMbjsNDjmpySdQDYQ2yd83zOFvCRjlAnbX1wyzgk,6010
5
+ topolib/assets/China_pop.json,sha256=v-bs5qib8-CB81rvBNNM9ssoelrNGuFrPEyRCht9YpY,22262
6
+ topolib/assets/DT-50_pop.json,sha256=hCv1MCvipgdrQ34VT-qXJ68BdATriQ8ZObh0fLPqar4,29719
7
+ topolib/elements/__init__.py,sha256=ZWSd5uwVtppJtMnZ30zrAdzXhUAYIK62RrUaH9rsWu0,180
8
+ topolib/elements/link.py,sha256=BwjdCR8peh7dDpKunZodlIsjKcfvwjKxyQ3tmp1obb8,3778
9
+ topolib/elements/node.py,sha256=uOnljwA3jcVR4utUfI-om4SIt_QBsEQt8QLW9uvjr3Y,4974
10
+ topolib/topology/__init__.py,sha256=2VRhVm4ZvKBiTDB2T8BDmLZBpwGCVFRF3fvtxxC_d28,86
11
+ topolib/topology/path.py,sha256=oUNwmpBcS6LMMAJIxokROm3MVqr7vRR44M3Fh5ADq_w,2057
12
+ topolib/topology/topology.py,sha256=PzPx80TxWMcOwpOCh4fohcWT2YUra-4m04tBjqyMU40,5605
13
+ topolib/visualization/__init__.py,sha256=wv065-KB5uDbTaQIASPVfMMW5sE76Bs-q0oai48vAzk,29
14
+ topolib/visualization/mapview.py,sha256=4iKJMYsH6oFNj9tOcgSKLVRtoYfUuH3XLjShsGwUXcM,2799
15
+ topolib-0.4.2.dist-info/METADATA,sha256=O2V7bL8xtAJOhNVmbLX0mHLj1ic5ZZu_3bbnFFbEzAw,3993
16
+ topolib-0.4.2.dist-info/WHEEL,sha256=zp0Cn7JsFoX2ATtOhtaFYIiE2rmFAD4OcMhtUki8W3U,88
17
+ topolib-0.4.2.dist-info/licenses/LICENSE,sha256=kbnIP0XU6f2ualiTjEawdlU81IGPBbwc-_GF3N-1e9E,1081
18
+ topolib-0.4.2.dist-info/RECORD,,
@@ -1,5 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.9.0)
2
+ Generator: poetry-core 2.2.1
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
-