topolib.topology package

Submodules

topolib.topology.path module

Path class for representing a sequence of nodes and links in a topology.

class topolib.topology.path.Path(nodes: List[Any], links: List[Any])

Bases: object

Represents a path through the network topology as an ordered sequence of nodes and links.

Parameters:
  • nodes (list) – Ordered list of node objects in the path.

  • links (list) – Ordered list of link objects in the path (len(links) == len(nodes) - 1).

Raises:

ValueError – If the number of nodes and links is inconsistent or empty.

nodes

Ordered list of nodes in the path.

Type:

list

Ordered list of links in the path.

Type:

list

Examples

>>> nodes = [Node(1), Node(2), Node(3)]
>>> links = [Link('a'), Link('b')]
>>> path = Path(nodes, links)
>>> path.length()
2
>>> path.endpoints()
(Node(1), Node(3))
endpoints()

Return the source and target nodes of the path as a tuple.

Returns:

(source_node, target_node)

Return type:

tuple

hop_count() int

Return the number of hops (links) in the path.

Returns:

Number of hops (links) in the path.

Return type:

int

length() int

Return the number of links in the path.

Returns:

Number of links in the path.

Return type:

int

topolib.topology.topology module

Topology class for optical network topologies.

This module defines the Topology class, representing a network topology with nodes and links, and providing an adjacency matrix using numpy.

This file uses NetworkX (BSD 3-Clause License): https://github.com/networkx/networkx/blob/main/LICENSE.txt

class topolib.topology.topology.Topology(nodes: List[Node] | None = None, links: List[Link] | None = None, name: str | None = None, reference: dict[str, str] | None = None)

Bases: object

Represents a network topology with nodes and links.

Parameters:
Variables:
  • nodes (list[Node]) – List of nodes in the topology.

  • links (list[Link]) – List of links in the topology.

Examples
>>> from topolib.elements.node import Node
>>> from topolib.elements.link import Link
>>> from topolib.topology import Topology
>>> n1 = Node(1, "A", 0.0, 0.0)
>>> n2 = Node(2, "B", 1.0, 1.0)
>>> l1 = Link(1, n1, n2, 10.0)
>>> topo = Topology(nodes=[n1, n2], links=[l1])
>>> topo.adjacency_matrix()
array([[0, 1],
       [1, 0]])

Add a link to the topology.

Parameters:

link (Link) – Link to add.

add_node(node: Node) None

Add a node to the topology.

Parameters:

node (Node) – Node to add.

adjacency_matrix() ndarray[tuple[Any, ...], dtype[int64]]

Return the adjacency matrix of the topology as a numpy array.

Returns:

Adjacency matrix (1 if connected, 0 otherwise).

Return type:

numpy.ndarray

Example
>>> topo.adjacency_matrix()
array([[0, 1],
       [1, 0]])
export_to_csv(filename_prefix: str) None

Export the topology to two CSV files: one for nodes and one for links. The files will be named as <filename_prefix>_nodes.csv and <filename_prefix>_links.csv.

Parameters:

filename_prefix (str) – Prefix for the output files (e.g., ‘topology1’).

Example

>>> topo.export_to_csv("mytopo")
# Generates 'mytopo_nodes.csv' and 'mytopo_links.csv'
export_to_flexnetsim_json(file_path: str, slots: int) None

Export the current topology to a JSON file compatible with Flex Net Sim.

Parameters:
  • file_path (str) – Path where the JSON file will be saved.

  • slots (int) – Number of slots for each link.

The generated format includes the following fields: - alias: short name of the topology (uses self.name if available) - name: full name of the topology (uses self.name if available) - nodes: list of nodes with ‘id’ field - links: list of links with id, src, dst, length, slots

export_to_flexnetsim_ksp_json(file_path: str, k: int = 3) None

Export the k-shortest paths between all node pairs to a JSON file compatible with Flex Net Sim.

Parameters:
  • file_path (str) – Path where the JSON file will be saved.

  • k (int) – Number of shortest paths to compute for each node pair (default: 3).

Example output format:

{
    "name": self.name,
    "alias": self.name,
    "routes": [
        {"src": <id>, "dst": <id>, "paths": [[id, ...], ...]},
        ...
    ]
}
export_to_json(file_path: str) None

Export the current topology to the JSON format used in the assets folder.

Parameters:

file_path (str) – Path where the JSON file will be saved.

Example usage:

topo.export_to_json("/path/output.json")

Example output format:

{
    "name": "Abilene",
    "nodes": [
        {
            "id": 0,
            "name": "Seattle",
            ...
        }
    ],
    "links": [
        {
            "id": 0,
            "src": 0,
            "dst": 1,
            "length": 1482.26
        }
    ]
}
classmethod from_json(json_path: str) Topology

Create a Topology object from a JSON file.

Parameters:

json_path (str) – Path to the JSON file containing the topology.

Returns:

Topology instance loaded from the file.

Return type:

Topology

get_reference_info() dict[str, str]

Get a human-readable representation of the reference information.

Returns ‘Not available’ for fields that are empty or None.

Returns:

Dictionary with formatted reference information.

Return type:

dict[str, str]

property graph: nx.DiGraph[Any]

Return the internal NetworkX directed graph representation.

Returns:

NetworkX directed graph with nodes and edges.

Return type:

networkx.DiGraph

Return the list of links in the topology.

Returns:

List of links.

Return type:

list[Link]

classmethod list_available_topologies() list[dict[str, Any]]

List available topologies in the assets folder.

Returns a list of dictionaries with keys: - ‘name’: topology name (filename without extension) - ‘nodes’: number of nodes - ‘links’: number of links

Returns:

List of available topologies with metadata.

Return type:

list[dict[str, Any]]

static load_default_topology(name: str) Topology

Load a default topology from the assets folder by name (filename without extension).

Parameters:

name (str) – Name of the topology asset (without .json extension)

Returns:

Topology instance loaded from the asset file

Return type:

Topology

Raises:

FileNotFoundError – If the asset file does not exist

property name: str | None

Return the name of the topology.

Returns:

Topology name.

Return type:

str or None

property nodes: List[Node]

Return the list of nodes in the topology.

Returns:

List of nodes.

Return type:

list[Node]

property reference: dict[str, str]

Return the reference information dictionary containing url, paper, and doi.

Fields may contain empty strings if the information is not available. Note: When loaded from JSON files with null values, they are normalized to empty strings.

Returns:

Dictionary with ‘url’, ‘paper’, and ‘doi’ keys.

Return type:

dict[str, str]

Remove a link by its id.

Parameters:

link_id (int) – ID of the link to remove.

remove_node(node_id: int) None

Remove a node and all its links by node id.

Parameters:

node_id (int) – ID of the node to remove.

weighted_adjacency_matrix() ndarray[tuple[Any, ...], dtype[float64]]

Return the weighted adjacency matrix with link distances in kilometers.

Instead of binary values (0/1), this matrix contains the actual distance in kilometers for each link. If no link exists between two nodes, the value is 0.0.

Returns:

Weighted adjacency matrix with link distances (km). 0.0 if not connected.

Return type:

numpy.ndarray

Example
>>> topo.weighted_adjacency_matrix()
array([[  0. , 100.5],
       [100.5,   0. ]])