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 7032aa0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
25 changes: 25 additions & 0 deletions gplugins/klayout/plot_nets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import itertools
from collections.abc import Collection
from itertools import combinations
from pathlib import Path

import klayout.db as kdb
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,25 @@ def plot_nets(
only_most_complex=only_most_complex,
)

if nodes_to_reduce:

def _removal_condition(node: str, degree: int) -> bool:
return degree == 2 and any(e in node for e in nodes_to_reduce)

while any(
_removal_condition(node, degree) for node, degree in G_connectivity.degree
):
G_connectivity_tmp = G_connectivity.copy()
for node, degree in G_connectivity.degree:
if _removal_condition(node, degree):
to_connect = [e[1] for e in G_connectivity.edges(node)]
dst_pairs_to_link = list(combinations(to_connect, r=2))
for pair in dst_pairs_to_link:
G_connectivity_tmp.add_edge(pair[0], pair[1])
G_connectivity_tmp.remove_node(node)
break
G_connectivity = G_connectivity_tmp

# Plotting the graph
if interactive:
try:
Expand Down
11 changes: 10 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,24 @@ 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", "bend_euler"}]
)
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 7032aa0

Please sign in to comment.