Skip to content

Commit

Permalink
Support reducing nodes in plot nets
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosavola committed Nov 21, 2023
1 parent 54ec8b9 commit 338f120
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
24 changes: 24 additions & 0 deletions gplugins/klayout/plot_nets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import itertools
from collections.abc import Collection
from pathlib import Path
from typing import cast

import klayout.db as kdb
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -79,6 +81,7 @@ def plot_nets(
interactive: bool = False,
include_labels: bool = True,
only_most_complex: bool = False,
nodes_to_reduce: Collection[str] | None = None,
) -> None:
"""Plots the connectivity between the components in the KLayout LayoutToNetlist file from :func:`~get_l2n`.
Expand All @@ -91,7 +94,10 @@ def plot_nets(
include_labels: Whether to include labels in the graph connected to corresponding cells.
only_most_complex: Whether to plot only the circuit with most connections or not.
Helpful for not plotting subcircuits separately.
nodes_to_reduce: Nodes to reduce to a single edge. Comparison made with Python ``in`` operator.
Helpful for reducing trivial waveguide elements.
"""

match Path(filepath).suffix:
case ".l2n" | ".txt":
l2n = kdb.LayoutToNetlist()
Expand All @@ -115,6 +121,24 @@ def plot_nets(
only_most_complex=only_most_complex,
)

if nodes_to_reduce:
G_connectivity_tmp = G_connectivity.copy()
for node, degree in G_connectivity.degree:
if degree == 2 and any(e in node for e in nodes_to_reduce):
edges = cast(
tuple[tuple[str, str], tuple[str, str]],
tuple(G_connectivity_tmp.edges(node).__iter__()),
)
a0, b0 = edges[0]
a1, b1 = edges[1]

e0 = a0 if a0 != node else b0
e1 = a1 if a1 != node else b1

G_connectivity_tmp.remove_node(node)
G_connectivity_tmp.add_edge(e0, e1)
G_connectivity = G_connectivity_tmp

# Plotting the graph
if interactive:
try:
Expand Down
9 changes: 8 additions & 1 deletion gplugins/klayout/tests/test_plot_nets.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,22 @@ def spice_netlist(tmpdir_factory) -> str:
@pytest.mark.parametrize("interactive", [True, False])
@pytest.mark.parametrize("include_labels", [True, False])
@pytest.mark.parametrize("only_most_complex", [True, False])
@pytest.mark.parametrize("nodes_to_reduce", [None, {"straight"}, {"straight", "pad"}])
def test_plot_nets(
klayout_netlist, fully_connected, interactive, include_labels, only_most_complex
klayout_netlist,
fully_connected,
interactive,
include_labels,
only_most_complex,
nodes_to_reduce,
):
plot_nets(
klayout_netlist,
fully_connected=fully_connected,
interactive=interactive,
include_labels=include_labels,
only_most_complex=only_most_complex,
nodes_to_reduce=nodes_to_reduce,
)
if interactive:
assert Path("connectivity.html").exists()
Expand Down

0 comments on commit 338f120

Please sign in to comment.