Skip to content

Commit

Permalink
better serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
joamatab committed May 29, 2024
1 parent 8678a18 commit 52bb9fe
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 49 deletions.
97 changes: 51 additions & 46 deletions gdsfactory/get_netlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,11 @@ def get_netlist(

# Prefer name from settings over c.name
if c.settings:
settings = dict(c.settings)
settings = c.settings.model_dump(exclude_none=True)

if merge_info:
settings.update(
{k: v for k, v in dict(c.info).items() if k in settings}
)
info = c.info.model_dump(exclude_none=True)
settings.update({k: v for k, v in info.items() if k in settings})

settings = clean_value_json(settings)
instance.update(
Expand Down Expand Up @@ -598,9 +597,11 @@ def get_netlist_recursive(
inst_name = get_instance_name(component, ref)
netlist_dict = {"component": f"{rcell.name}{component_suffix}"}
if rcell.settings:
netlist_dict.update(settings=rcell.settings)
netlist_dict.update(
settings=rcell.settings.model_dump(exclude_none=True)
)
if rcell.info:
netlist_dict.update(info=rcell.info)
netlist_dict.update(info=rcell.info.model_dump(exclude_none=True))
netlist["instances"][inst_name] = netlist_dict

return all_netlists
Expand Down Expand Up @@ -638,43 +639,47 @@ def _demo_mzi_lattice() -> None:

if __name__ == "__main__":
import gdsfactory as gf
from gdsfactory.decorators import flatten_offgrid_references

rotation_value = 35
cname = "test_get_netlist_transformed"
c = gf.Component(cname)
i1 = c.add_ref(gf.components.straight(), "i1")
i2 = c.add_ref(gf.components.straight(), "i2")
i1.rotate(rotation_value)
i2.connect("o2", i1.ports["o1"])

# flatten the oddly rotated refs
c = flatten_offgrid_references(c)
print(c.get_dependencies())
c.show()

# perform the initial sanity checks on the netlist
netlist = c.get_netlist()
connections = netlist["connections"]
assert len(connections) == 1, len(connections)
cpairs = list(connections.items())
extracted_port_pair = set(cpairs[0])
expected_port_pair = {"i2,o2", "i1,o1"}
assert extracted_port_pair == expected_port_pair

recursive_netlist = get_netlist_recursive(c)
top_netlist = recursive_netlist[cname]
# the recursive netlist should have 3 entries, for the top level and two
# rotated straights
assert len(recursive_netlist) == 1, len(recursive_netlist)
# confirm that the child netlists have reference attributes properly set

i1_cell_name = top_netlist["instances"]["i1"]["component"]
i1_netlist = recursive_netlist[i1_cell_name]
# currently for transformed netlists, the instance name of the inner cell is None
assert i1_netlist["placements"][None]["rotation"] == rotation_value

i2_cell_name = top_netlist["instances"]["i2"]["component"]
i2_netlist = recursive_netlist[i2_cell_name]
# currently for transformed netlists, the instance name of the inner cell is None
assert i2_netlist["placements"][None]["rotation"] == rotation_value

c = gf.c.mzi()
n = c.get_netlist()

# from gdsfactory.decorators import flatten_offgrid_references

# rotation_value = 35
# cname = "test_get_netlist_transformed"
# c = gf.Component(cname)
# i1 = c.add_ref(gf.components.straight(), "i1")
# i2 = c.add_ref(gf.components.straight(), "i2")
# i1.rotate(rotation_value)
# i2.connect("o2", i1.ports["o1"])

# # flatten the oddly rotated refs
# c = flatten_offgrid_references(c)
# print(c.get_dependencies())
# c.show()

# # perform the initial sanity checks on the netlist
# netlist = c.get_netlist()
# connections = netlist["connections"]
# assert len(connections) == 1, len(connections)
# cpairs = list(connections.items())
# extracted_port_pair = set(cpairs[0])
# expected_port_pair = {"i2,o2", "i1,o1"}
# assert extracted_port_pair == expected_port_pair

# recursive_netlist = get_netlist_recursive(c)
# top_netlist = recursive_netlist[cname]
# # the recursive netlist should have 3 entries, for the top level and two
# # rotated straights
# assert len(recursive_netlist) == 1, len(recursive_netlist)
# # confirm that the child netlists have reference attributes properly set

# i1_cell_name = top_netlist["instances"]["i1"]["component"]
# i1_netlist = recursive_netlist[i1_cell_name]
# # currently for transformed netlists, the instance name of the inner cell is None
# assert i1_netlist["placements"][None]["rotation"] == rotation_value

# i2_cell_name = top_netlist["instances"]["i2"]["component"]
# i2_netlist = recursive_netlist[i2_cell_name]
# # currently for transformed netlists, the instance name of the inner cell is None
# assert i2_netlist["placements"][None]["rotation"] == rotation_value
7 changes: 5 additions & 2 deletions gdsfactory/routing/get_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,14 @@ def get_route_from_waypoints(
ptop = c << gf.components.pad(port_orientation=270)
pbot = c << gf.components.pad(port_orientation=90)

ptop.movex(3)
ptop.movex(300)
ptop.movey(300)
route = get_route_electrical(
route = get_route(
ptop.ports["pad"],
pbot.ports["pad"],
cross_section="xs_sc",
bend=gf.c.wire_corner45,
width=5,
)
c.add(route.references)
c.show()
Expand Down
2 changes: 1 addition & 1 deletion gdsfactory/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def clean_value_json(
from gdsfactory.path import Path

if isinstance(value, pydantic.BaseModel):
return clean_dict(value.model_dump())
return clean_dict(value.model_dump(exclude_none=True))

elif fast_serialization and isinstance(value, Component):
return value.name
Expand Down

0 comments on commit 52bb9fe

Please sign in to comment.