From 52bb9fefbf9546c6932ff1678e8a9b07c2f66ba2 Mon Sep 17 00:00:00 2001 From: Joaquin Matres <4514346+joamatab@users.noreply.github.com> Date: Wed, 29 May 2024 09:22:19 -0700 Subject: [PATCH] better serialization --- gdsfactory/get_netlist.py | 97 +++++++++++++++++---------------- gdsfactory/routing/get_route.py | 7 ++- gdsfactory/serialization.py | 2 +- 3 files changed, 57 insertions(+), 49 deletions(-) diff --git a/gdsfactory/get_netlist.py b/gdsfactory/get_netlist.py index 85cc48dd2..d393267ff 100644 --- a/gdsfactory/get_netlist.py +++ b/gdsfactory/get_netlist.py @@ -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( @@ -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 @@ -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 diff --git a/gdsfactory/routing/get_route.py b/gdsfactory/routing/get_route.py index 5ef7a3303..20ad9d821 100644 --- a/gdsfactory/routing/get_route.py +++ b/gdsfactory/routing/get_route.py @@ -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() diff --git a/gdsfactory/serialization.py b/gdsfactory/serialization.py index 84232a02d..2e51294c1 100644 --- a/gdsfactory/serialization.py +++ b/gdsfactory/serialization.py @@ -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