ictdesigntools.draw.graph¶
Network topologies: graphs drawn with their attributes, and routes through them highlighted.
A graph comes in from anywhere: a networkx graph (read by duck typing, so networkx is never
imported), node-link JSON as networkx.node_link_data writes it, or this module's Graph.
Nodes are placed by a given pos (any networkx layout) or by a layout here: layered puts
nodes in rows by an attribute (tier, subset) and orders each row to reduce crossings,
as for Clos and leaf-spine fabrics; tree spreads a point-to-multipoint network such as GPON
from its root. topology turns it into a Drawing: nodes and edges styled by an attribute
(role → colour, media → line), labels from templates ("{name}", "{speed}"),
edges straight or orthogonal with separate lanes, routes (node paths, as nx.shortest_path
returns) drawn over the graph in their own colours, and a legend. Every node, edge and route
carries a Meta with its attributes for to_svg(interactive=True).
>>> g = Graph()
>>> g.add_edge("spine1", "leaf1", speed="400G"); g.add_edge("spine1", "leaf2", speed="400G")
>>> for n in ("spine1",): g.nodes[n]["tier"] = 0
>>> for n in ("leaf1", "leaf2"): g.nodes[n]["tier"] = 1
>>> d = topology(g, layer_key="tier", routes=[Route(["leaf1", "spine1", "leaf2"])])
>>> d.title
'Topology: 3 nodes, 2 edges'
Graph
dataclass
¶
Graph(
nodes: dict[Hashable, dict[str, Any]] = dict(),
edges: list[Edge] = list(),
directed: bool = False,
multigraph: bool = False,
)
Nodes with attributes and edges between them; enough of networkx's shape to draw.
to_networkx ¶
A networkx graph of the same kind (needs networkx).
Source code in src/ictdesigntools/draw/graph.py
Route
dataclass
¶
A path to highlight: nodes in order (nx.shortest_path), or edges as (u, v) or
(u, v, key). style names a drawing style; left empty, routes take colours in turn.
from_networkx ¶
from_networkx(G) -> Graph
A Graph from anything shaped like a networkx graph (nodes(data=True),
edges(data=True), is_directed(), is_multigraph()).
Source code in src/ictdesigntools/draw/graph.py
from_node_link ¶
from_node_link(
data: Mapping[str, Any],
source: str = "source",
target: str = "target",
name: str = "id",
key: str = "key",
edges: str | None = None,
) -> Graph
A Graph from node-link data, as networkx.node_link_data writes it (edges under
edges, or links in older files).
Source code in src/ictdesigntools/draw/graph.py
to_node_link ¶
Node-link data that networkx.node_link_graph(data, edges="edges") reads back.
Source code in src/ictdesigntools/draw/graph.py
as_graph ¶
as_graph(obj) -> Graph
A Graph from a Graph, node-link data or a networkx-shaped graph.
Source code in src/ictdesigntools/draw/graph.py
layered ¶
layered(
g: Graph,
key="tier",
order: Sequence[Any] | None = None,
sweeps: int = 8,
) -> dict[Hashable, tuple[float, float]]
Rows by the attribute key (or a dict of layer → nodes, or a function of node and
attributes), in order or sorted; each row ordered to reduce crossings by alternating
barycentre sweeps, and centred on the widest row.
Source code in src/ictdesigntools/draw/graph.py
crossings ¶
crossings(rows, nbrs, row_of) -> int
Edge crossings between adjacent rows (edges that skip rows are not counted).
Source code in src/ictdesigntools/draw/graph.py
tree ¶
A tree spread from root (or several roots side by side): each leaf its own column,
each parent centred over its children, rows by distance from the root.
Source code in src/ictdesigntools/draw/graph.py
label ¶
A label from a template such as "{name} · {speed}" (missing fields come out empty)
or a function of the node (or edge) and its attributes.
Source code in src/ictdesigntools/draw/graph.py
topology ¶
topology(
graph,
pos: Mapping[Hashable, tuple[float, float]]
| None = None,
*,
layout: str | Callable = "layered",
layer_key="tier",
layer_order=None,
root=None,
direction: str = "down",
node_size: tuple[float, float] = (36.0, 12.0),
gap: tuple[float, float] = (8.0, 26.0),
node_label="{id}",
node_style=None,
node_shape: Mapping[str, str] | None = None,
edge_label=None,
edge_style=None,
edge_width: Callable | None = None,
routing: str = "auto",
merge_parallel: bool = True,
routes: Sequence[Route] = (),
styles: Mapping[str, Style] | None = None,
legend: bool = True,
title: str = "",
flip_y: bool = True,
) -> Drawing
A drawing of a graph in millimetres.
Placement: pos from any networkx layout (y up, flipped unless flip_y is False), else
layout: "layered" by layer_key (default tier), "tree" from root, or a
function of the graph returning grid positions. direction "down" or "right".
Look: node_style / edge_style name an attribute (or a function) whose value picks
the style node-<value> / edge-<value>; values without a style in styles get
colours in turn. node_shape maps those values to "rect" or "circle".
edge_width(edge, attrs) sets a line width in mm. node_label and edge_label are
templates ("{name}", "{speed} · {length_m:g} m") or functions.
Edges: routing "orthogonal" (through the gap between rows, parallel runs in separate
lanes; for layered and tree layouts) or "straight"; parallel edges are drawn once with a
count unless merge_parallel is False. Directed graphs get arrowheads.
routes are drawn over the graph, each in its own colour, with Meta kind route.
Source code in src/ictdesigntools/draw/graph.py
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 | |