From b390293fbf21c4012cff9bd9da079fbf1ed220e2 Mon Sep 17 00:00:00 2001 From: Joaquin Matres <4514346+joamatab@users.noreply.github.com> Date: Wed, 17 Jul 2024 07:04:00 -0400 Subject: [PATCH 1/4] pin gdsfactory --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 800c39c4..f38086e0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,7 +23,7 @@ classifiers = [ "Operating System :: OS Independent" ] dependencies = [ - "gdsfactory>=8", + "gdsfactory>=8.5.3", "pint", "gdstk", "tqdm" From ec2ee4a8b988f8549cf619d0da3e5692101ace49 Mon Sep 17 00:00:00 2001 From: Joaquin Matres <4514346+joamatab@users.noreply.github.com> Date: Wed, 17 Jul 2024 07:30:56 -0400 Subject: [PATCH 2/4] fix more notebooks --- gplugins/gfviz/netlist.py | 68 ++++++++---------- notebooks/11_get_netlist.ipynb | 121 +++++++-------------------------- 2 files changed, 56 insertions(+), 133 deletions(-) diff --git a/gplugins/gfviz/netlist.py b/gplugins/gfviz/netlist.py index aac52e56..8a631735 100644 --- a/gplugins/gfviz/netlist.py +++ b/gplugins/gfviz/netlist.py @@ -8,7 +8,7 @@ def patch_netlist(netlist): - if not netlist["connections"] and not netlist["ports"] and not netlist["instances"]: + if not netlist.get("nets") and not netlist["ports"] and not netlist["instances"]: netlist = wrap_component_in_netlist(netlist.get("name", "")) netlist = patch_netlist_with_port_info(netlist) netlist = patch_netlist_with_icon_info(netlist) @@ -51,10 +51,8 @@ def patch_netlist_with_connection_info(netlist): i = netlist["info"] = netlist.get("info", {}) s = i["schematic"] = i.get("schematic", {}) ic = s["implicit_connections"] = s.get("implicit_connections", {}) - for ip1, ip2 in netlist["connections"].items(): - if _is_implicit_connection( - ip1, ip2, netlist["instances"], netlist["connections"] - ): + for ip1, ip2 in netlist["nets"].items(): + if _is_implicit_connection(ip1, ip2, netlist["instances"], netlist["nets"]): ic["ip1"] = ip2 return netlist @@ -156,7 +154,7 @@ def get_ports(child, parent=None): p = gf.get_component(parent) c = [r.parent for r in p.references if r.parent.name.startswith(child)][0] - ports1 = natsorted(c.ports) + ports1 = sort_ports(c.ports) try: netlist = get_netlist(c) @@ -165,14 +163,14 @@ def get_ports(child, parent=None): if netlist: ports2 = natsorted(netlist["ports"]) - if len(ports1) > len(ports2): - ports = ports1 - else: - ports = ports2 + ports = ports1 if len(ports1) > len(ports2) else ports2 else: ports = ports1 - [(x0, y0), (x1, y1)] = c.bbox # type: ignore + x0 = c.dxmin + x1 = c.dxmax + y0 = c.dymin + y1 = c.dymax x0, x1 = min(x0, x1), max(x0, x1) y0, y1 = min(y0, y1), max(y0, y1) @@ -214,10 +212,10 @@ def get_icon_poly(name): if "taper" in name or "trans" in name: return taper c = gf.get_component(name) + c.flatten() layer_priority = {lay: i for i, lay in enumerate(most_common_layers())} polys = {} - for p in c.flatten().polygons: - layer = (p.layer, p.datatype) + for layer, p in c.get_polygons_points().items(): if layer not in polys: polys[layer] = [] polys[layer].append(p) @@ -234,8 +232,8 @@ def get_icon_poly(name): poly = rdp(polys[0].points) if (poly.shape[0] < 3) or (poly.shape[0] > 100): return default - poly = (poly - c.bbox[0:1]) / (c.bbox[1:2] - c.bbox[0:1]) - poly = [(x, y) for (x, y) in poly] # noqa: C416 + poly = (poly - c.bbox_np()[0:1]) / (c.bbox_np()[1:2] - c.bbox_np()[0:1]) + poly = list(poly) if "coupler" in name: ymin = min(poly, key=lambda xy: xy[1])[1] poly = [ @@ -257,14 +255,12 @@ def rdp(poly, eps=0.1): index = np.argmax(dists) dmax = dists[index] - if dmax > eps: - result1 = rdp(poly[: index + 1], eps=eps) - result2 = rdp(poly[index:], eps=eps) - result = np.vstack((result1[:-1], result2)) - else: - result = np.array([start, end]) + if dmax <= eps: + return np.array([start, end]) - return result + result1 = rdp(poly[: index + 1], eps=eps) + result2 = rdp(poly[index:], eps=eps) + return np.vstack((result1[:-1], result2)) def _line_dists(points, start, end): @@ -275,6 +271,10 @@ def _line_dists(points, start, end): return np.divide(abs(cross), np.linalg.norm(vec)) +def sort_ports(ports) -> list[gf.Port]: + return natsorted(ports, key=lambda port: port.dx) + + def wrap_component_in_netlist(name): if not name: return {} @@ -282,17 +282,16 @@ def wrap_component_in_netlist(name): _ = c << gf.get_component(name) c.name = name ports = {p: f"{name},{p}" for p in c.references[0].ports} - netlist = { + return { "instances": { f"{name}": { "component": name, "settings": {}, }, }, - "connections": {}, - "ports": {p: ports[p] for p in natsorted(ports)}, + "nets": {}, + "ports": {p: ports[p] for p in sort_ports(ports)}, } - return netlist def _bad_instance(_): @@ -316,17 +315,16 @@ def get_component_name(component_dict: dict | str, default="") -> str: def ensure_netlist_order(netlist): netlist = {**netlist} - new_netlist = { + return { "pdk": netlist.pop("pdk", ""), "instances": netlist.pop("instances", {}), - "connections": netlist.pop("connections", {}), + "nets": netlist.pop("nets", {}), "routes": netlist.pop("routes", {}), "ports": netlist.pop("ports", {}), "placements": netlist.pop("placements", {}), "info": netlist.pop("info", {}), **netlist, } - return new_netlist def replace_cross_sections_recursive(obj): @@ -336,9 +334,7 @@ def replace_cross_sections_recursive(obj): obj[k] = check_cross_section(obj[k]) else: obj[k] = replace_cross_sections_recursive(obj[k]) - return obj - else: - return obj + return obj def remove_instance_info(netlist): @@ -394,13 +390,9 @@ def _is_implicit_connection(ip1, ip2, instances, connections): return False if i2 not in instances: return False - if (connections[ip1] == ip2) or (connections[ip2] == ip1): - return False - return True + return connections[ip1] != ip2 and connections[ip2] != ip1 def _is_hierarchical_pic(name): c = gf.get_component(name) - if c.references: - return True - return False + return bool(c.insts) diff --git a/notebooks/11_get_netlist.ipynb b/notebooks/11_get_netlist.ipynb index b8d0b6b5..56dbfc04 100644 --- a/notebooks/11_get_netlist.ipynb +++ b/notebooks/11_get_netlist.ipynb @@ -256,7 +256,6 @@ "\n", "\n", "c = mmi_with_bend()\n", - "gf.remove_from_cache(c)\n", "c.plot()" ] }, @@ -374,9 +373,9 @@ "metadata": {}, "outputs": [], "source": [ - "coupler_lengths = [10, 20, 30]\n", - "coupler_gaps = [0.1, 0.2, 0.3]\n", - "delta_lengths = [10, 100]\n", + "coupler_lengths = (10, 20, 30)\n", + "coupler_gaps = (0.1, 0.2, 0.3)\n", + "delta_lengths = (10, 100)\n", "\n", "c = gf.components.mzi_lattice(\n", " coupler_lengths=coupler_lengths,\n", @@ -403,9 +402,9 @@ "metadata": {}, "outputs": [], "source": [ - "coupler_lengths = [10, 20, 30, 40]\n", - "coupler_gaps = [0.1, 0.2, 0.4, 0.5]\n", - "delta_lengths = [10, 100, 200]\n", + "coupler_lengths = (10, 20, 30, 40)\n", + "coupler_gaps = (0.1, 0.2, 0.4, 0.5)\n", + "delta_lengths = (10, 100, 200)\n", "\n", "c = gf.components.mzi_lattice(\n", " coupler_lengths=coupler_lengths,\n", @@ -442,7 +441,7 @@ "metadata": {}, "outputs": [], "source": [ - "n_recursive = c.get_netlist_recursive()" + "n_recursive = c.get_netlist(recursive=True)" ] }, { @@ -460,9 +459,7 @@ "id": "38", "metadata": {}, "source": [ - "## get_netlist_flat\n", - "\n", - "You can also flatten the recursive netlist" + "Placement information is accumulated, and connections and ports are mapped, respectively, to the ports of the unique instances or the component top level ports. This can be plotted:" ] }, { @@ -472,67 +469,13 @@ "metadata": {}, "outputs": [], "source": [ - "flat_netlist = c.get_netlist_flat()" + "c.plot_netlist(with_labels=False) # labels get cluttered" ] }, { "cell_type": "markdown", "id": "40", "metadata": {}, - "source": [ - "The flat netlist contains the same keys as a regular netlist:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "41", - "metadata": {}, - "outputs": [], - "source": [ - "flat_netlist.keys()" - ] - }, - { - "cell_type": "markdown", - "id": "42", - "metadata": {}, - "source": [ - "However, its instances are flattened and uniquely renamed according to hierarchy:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "43", - "metadata": {}, - "outputs": [], - "source": [ - "flat_netlist[\"instances\"].keys()" - ] - }, - { - "cell_type": "markdown", - "id": "44", - "metadata": {}, - "source": [ - "Placement information is accumulated, and connections and ports are mapped, respectively, to the ports of the unique instances or the component top level ports. This can be plotted:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "45", - "metadata": {}, - "outputs": [], - "source": [ - "c.plot_netlist_flat(with_labels=False) # labels get cluttered" - ] - }, - { - "cell_type": "markdown", - "id": "46", - "metadata": {}, "source": [ "## allow_multiple_connections\n", "\n", @@ -544,19 +487,23 @@ { "cell_type": "code", "execution_count": null, - "id": "47", + "id": "41", "metadata": {}, "outputs": [], "source": [ - "vdiv = gf.Component(\"voltageDivider\")\n", + "import gdsfactory as gf\n", + "\n", + "vdiv = gf.Component()\n", "r1 = vdiv << gf.components.resistance_sheet()\n", "r2 = vdiv << gf.components.resistance_sheet()\n", - "r3 = vdiv << gf.get_component(gf.components.resistance_sheet).drotate()\n", - "r4 = vdiv << gf.get_component(gf.components.resistance_sheet).drotate()\n", + "r3 = vdiv << gf.get_component(gf.components.resistance_sheet)\n", + "r4 = vdiv << gf.get_component(gf.components.resistance_sheet)\n", "\n", "r1.connect(\"pad2\", r2.ports[\"pad1\"])\n", - "r3.connect(\"pad1\", r2.ports[\"pad1\"], preserve_orientation=True)\n", - "r4.connect(\"pad2\", r2.ports[\"pad1\"], preserve_orientation=True)\n", + "r3.connect(\"pad1\", r2.ports[\"pad1\"])\n", + "r4.connect(\"pad2\", r2.ports[\"pad1\"])\n", + "\n", + "r4.drotate(90)\n", "\n", "vdiv.plot()" ] @@ -564,12 +511,12 @@ { "cell_type": "code", "execution_count": null, - "id": "48", + "id": "42", "metadata": {}, "outputs": [], "source": [ "try:\n", - " vdiv.get_netlist_flat()\n", + " vdiv.get_netlist()\n", "except Exception as exc:\n", " print(exc)" ] @@ -577,17 +524,17 @@ { "cell_type": "code", "execution_count": null, - "id": "49", + "id": "43", "metadata": {}, "outputs": [], "source": [ - "vdiv.get_netlist_flat(allow_multiple=True)" + "vdiv.get_netlist(allow_multiple=True)" ] }, { "cell_type": "code", "execution_count": null, - "id": "50", + "id": "44", "metadata": {}, "outputs": [], "source": [ @@ -598,7 +545,7 @@ { "cell_type": "code", "execution_count": null, - "id": "51", + "id": "45", "metadata": {}, "outputs": [], "source": [ @@ -608,28 +555,12 @@ { "cell_type": "code", "execution_count": null, - "id": "52", + "id": "46", "metadata": {}, "outputs": [], "source": [ "show(gf.components.ring_single())" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "53", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "54", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { From 178aa6b27f2c4c5911a4107c4df2650bceb541ed Mon Sep 17 00:00:00 2001 From: Joaquin Matres <4514346+joamatab@users.noreply.github.com> Date: Wed, 17 Jul 2024 09:44:27 -0400 Subject: [PATCH 3/4] update meow-sim --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index f38086e0..29a7c70d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -92,7 +92,7 @@ meow = [ "jax>=0.4.26", "jaxlib>=0.4.26", "flax>=0.8.2", - "meow-sim~=0.9.0", + "meow-sim~=0.11.0", "tidy3d>=2.5.2,<2.8" ] sax = [ From 42085c14de1e8233de786bc70db990e63f66d41c Mon Sep 17 00:00:00 2001 From: Joaquin Matres <4514346+joamatab@users.noreply.github.com> Date: Wed, 17 Jul 2024 10:05:11 -0400 Subject: [PATCH 4/4] fix meow --- gplugins/meow/meow_eme.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gplugins/meow/meow_eme.py b/gplugins/meow/meow_eme.py index f08d0540..644db6dc 100644 --- a/gplugins/meow/meow_eme.py +++ b/gplugins/meow/meow_eme.py @@ -14,7 +14,6 @@ from gdsfactory.pdk import get_active_pdk, get_layer_stack from gdsfactory.technology import DerivedLayer, LayerStack from gdsfactory.typings import Component, LayerSpec, PathType -from meow.base_model import _array as mw_array from tqdm.auto import tqdm from gplugins.common.utils.get_sparameters_path import ( @@ -420,7 +419,7 @@ def rename(p): for k, v in sp.items() } S, self.port_map = sax.sdense(sdict) - self.S = np.asarray(S).view(mw_array) + self.S = np.asarray(S).view(np.ndarray) return sp else: self.filepath.unlink()