From 21dc214d6b96423cfa94ccfba6ab629bc21fbc00 Mon Sep 17 00:00:00 2001 From: Niko Savola Date: Wed, 18 Oct 2023 11:19:44 -0700 Subject: [PATCH 1/3] Add interactive netlist plotting Implemented with `pyvis` on top of `networkx` --- gplugins/klayout/plot_nets.py | 43 +++++++++++++++++++++++++---------- pyproject.toml | 3 ++- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/gplugins/klayout/plot_nets.py b/gplugins/klayout/plot_nets.py index b014124c..9467ecea 100644 --- a/gplugins/klayout/plot_nets.py +++ b/gplugins/klayout/plot_nets.py @@ -6,13 +6,16 @@ import networkx as nx -def plot_nets(filepath: str | Path, fully_connected: bool = False) -> None: +def plot_nets( + filepath: str | Path, fully_connected: bool = False, interactive: bool = False +) -> None: """Plots the connectivity between the components in the GDS file. Args: - filepath: Path to the GDS file. + filepath: Path to the KLayout netlist file. fully_connected: Whether to plot the graph as elements fully connected to all other ones (True) or going through other elements (False). + interactive: Whether to plot an interactive graph with `pyvis` or not. """ filepath = Path(filepath) code = filepath.read_text() @@ -32,16 +35,32 @@ def plot_nets(filepath: str | Path, fully_connected: bool = False) -> None: ) # Plotting the graph - plt.figure(figsize=(8, 6)) - nx.draw( - G_connectivity, - with_labels=True, - node_size=2000, - node_color="lightpink", - font_size=12, - ) - plt.title("Connectivity") - plt.show() + if interactive: + try: + from pyvis.network import Network + except ModuleNotFoundError as e: + raise UserWarning( + "You need to `pip install pyvis` or `gplugins[klayout]`" + ) from e + + net = Network( + select_menu=True, + filter_menu=True, + ) + net.show_buttons() + net.from_nx(G_connectivity) + net.show("connectivity.html") + else: + plt.figure(figsize=(8, 6)) + nx.draw( + G_connectivity, + with_labels=True, + node_size=2000, + node_color="lightpink", + font_size=12, + ) + plt.title("Connectivity") + plt.show() if __name__ == "__main__": diff --git a/pyproject.toml b/pyproject.toml index db1ef55c..40621311 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -68,7 +68,8 @@ gmsh = [ "meshwell>=1.0.0,<1.1.0" ] klayout = [ - "kfactory[git,ipy]>=0.9.3,<0.10" + "kfactory[git,ipy]>=0.9.3,<0.10", + "pyvis<=0.3.1" ] meow = [ "jaxlib", From 8d1bc09786390a115e2c75e02900744c510d9637 Mon Sep 17 00:00:00 2001 From: Niko Savola Date: Wed, 18 Oct 2023 14:05:14 -0700 Subject: [PATCH 2/3] Test `plot_nets` --- gplugins/klayout/tests/test_plot_nets.py | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 gplugins/klayout/tests/test_plot_nets.py diff --git a/gplugins/klayout/tests/test_plot_nets.py b/gplugins/klayout/tests/test_plot_nets.py new file mode 100644 index 00000000..65484d5c --- /dev/null +++ b/gplugins/klayout/tests/test_plot_nets.py @@ -0,0 +1,27 @@ +from pathlib import Path + +import pytest +from gdsfactory.samples.demo.lvs import pads_correct + +from gplugins.klayout.get_netlist import get_l2n +from gplugins.klayout.plot_nets import plot_nets + + +@pytest.fixture +def klayout_netlist(tmp_path): + c = pads_correct() + + gdspath = c.write_gds(gdsdir=tmp_path) + l2n = get_l2n(gdspath) + netlist_path = str(Path(tmp_path) / f"{c.name}.txt") + l2n.write_l2n(netlist_path) + return netlist_path + + +def test_plot_nets(klayout_netlist): + plot_nets(klayout_netlist) + + +def test_plot_nets_interactive(klayout_netlist): + plot_nets(klayout_netlist, interactive=True) + assert Path("connectivity.html").exists() From 84e67ddf2890c188af57fea1f02f65d0c5b03851 Mon Sep 17 00:00:00 2001 From: Niko Savola Date: Wed, 18 Oct 2023 14:55:10 -0700 Subject: [PATCH 3/3] Add missing klayout options in `make install` This fixes pyvis not being installed for testing --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e1104ea8..8726bea2 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ install: - pip install -e .[dev,docs,devsim,femwell,gmsh,meow,meshwell,ray,sax,schematic,tidy3d,web,vlsir] + pip install -e .[dev,docs,devsim,femwell,gmsh,klayout,meow,meshwell,ray,sax,schematic,tidy3d,web,vlsir] pre-commit install dev: test-data gmsh elmer install