Skip to content

Commit

Permalink
Merge pull request #2900 from gdsfactory/fix_plot_netlist
Browse files Browse the repository at this point in the history
fix plot_netlist
  • Loading branch information
joamatab authored Jun 21, 2024
2 parents 95fd962 + 8cd5060 commit be8ec0f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
26 changes: 18 additions & 8 deletions gdsfactory/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,8 @@ def plot_netlist(
import matplotlib.pyplot as plt
import networkx as nx

from gdsfactory.get_netlist import _nets_to_connections

plt.figure()
netlist = self.get_netlist(recursive=recursive, **kwargs)
G = nx.Graph()
Expand All @@ -934,7 +936,9 @@ def plot_netlist(
pos = {}
labels = {}
for net in netlist.values():
connections = net["connections"]
nets = net.get("nets", [])
connections = net.get("connections", {})
connections = _nets_to_connections(nets, connections)
placements = net["placements"]
G.add_edges_from(
[
Expand All @@ -946,7 +950,9 @@ def plot_netlist(
labels |= {k: ",".join(k.split(",")[:1]) for k in placements.keys()}

else:
nets = netlist.get("nets", [])
connections = netlist.get("connections", {})
connections = _nets_to_connections(nets, connections)
placements = netlist["placements"]
G.add_edges_from(
[
Expand Down Expand Up @@ -1166,15 +1172,17 @@ def component_with_function(


if __name__ == "__main__":
import matplotlib.pyplot as plt

import gdsfactory as gf

c = gf.Component()
b = c << gf.c.bend_circular()
s = c << gf.c.straight()
s.connect("o1", b.ports["o2"])
p = c.get_polygons()
p1 = c.get_polygons(by="name")
# c = gf.c.mzi()
# c = gf.Component()
# b = c << gf.c.bend_circular()
# s = c << gf.c.straight()
# s.connect("o1", b.ports["o2"])
# p = c.get_polygons()
# p1 = c.get_polygons(by="name")
c = gf.c.mzi_lattice()
# c = gf.c.array(spacing=(300, 300), columns=2)
# c.show()
# n0 = c.get_netlist()
Expand All @@ -1183,6 +1191,8 @@ def component_with_function(
# gdspath = c.write_gds("test.gds")
# c = gf.import_gds(gdspath)
# n = c.get_netlist()
c.plot_netlist(recursive=True)
plt.show()
c.show()
# import matplotlib.pyplot as plt

Expand Down
34 changes: 34 additions & 0 deletions gdsfactory/get_netlist.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# type: ignore
"""Extract netlist from component port connectivity.
Assumes two ports are connected when they have same width, x, y
Expand Down Expand Up @@ -32,6 +33,39 @@
from gdsfactory.typings import LayerSpec


def _nets_to_connections(nets: list[dict], connections: dict) -> dict[str, str]:
connections = dict(connections)
inverse_connections = {v: k for k, v in connections.items()}

def _is_connected(p):
return (p in connections) or (p in inverse_connections)

def _add_connection(p, q):
connections[p] = q
inverse_connections[q] = p

def _get_connected_port(p):
return connections[p] if p in connections else inverse_connections[p]

for net in nets:
p = net["p1"]
q = net["p2"]
if _is_connected(p):
_q = _get_connected_port(p)
raise ValueError(
"SAX currently does not support multiply connected ports. "
f"Got {p}<->{q} and {p}<->{_q}"
)
if _is_connected(q):
_p = _get_connected_port(q)
raise ValueError(
"SAX currently does not support multiply connected ports. "
f"Got {p}<->{q} and {_p}<->{q}"
)
_add_connection(p, q)
return connections


def get_default_connection_validators():
return {"optical": validate_optical_connection, "electrical": _null_validator}

Expand Down

0 comments on commit be8ec0f

Please sign in to comment.