Skip to content

fixes delta lengths and netlist patching for notebook workflow_3_cascaded_mzi.ipynb#678

Open
jonbenfri wants to merge 2 commits intogdsfactory:mainfrom
jonbenfri:workflow3-notebook-677-bugfix
Open

fixes delta lengths and netlist patching for notebook workflow_3_cascaded_mzi.ipynb#678
jonbenfri wants to merge 2 commits intogdsfactory:mainfrom
jonbenfri:workflow3-notebook-677-bugfix

Conversation

@jonbenfri
Copy link
Contributor

@jonbenfri jonbenfri commented Feb 26, 2026

Fixes issue #677.

Summary by Sourcery

Update the cascaded MZI workflow notebook to correct MZI path length definitions and enable proper recursive netlist patching and simulation plotting.

New Features:

  • Add a recursive netlist patching helper and use it to run and plot circuit simulations directly in the notebook.

Bug Fixes:

  • Correct the MZI path length difference definitions and delta length values used in the layout generation.
  • Fix netlist patching by filtering model settings correctly and handling recursive netlists when replacing components.

Documentation:

  • Fix minor wording and typo issues in the notebook narrative to improve clarity.

Chores:

  • Update notebook kernel metadata to use the current Python 3 ipykernel and version.

@jonbenfri jonbenfri requested a review from joamatab as a code owner February 26, 2026 00:35
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Feb 26, 2026

Reviewer's Guide

Updates the cascaded MZI workflow notebook to correct the MZI path length definitions, preserve signed delta lengths in the layout, and introduce a recursive netlist patching helper that is exercised in an active simulation cell, along with minor text and environment metadata cleanup.

Sequence diagram for using patch_netlist_mod in circuit simulation

sequenceDiagram
  actor User
  participant Notebook
  participant patch_netlist_mod
  participant sax
  participant circuit

  User->>Notebook: Run simulation cell
  Notebook->>Notebook: layout = gf.c.mzi_lattice(...)
  Notebook->>patch_netlist_mod: patch_netlist_mod(netlist=layout.get_netlist(recursive=True), models, models_to_patch)
  patch_netlist_mod-->>Notebook: patched_netlist, patched_models
  Notebook->>sax: circuit(patched_netlist, patched_models)
  sax-->>Notebook: circuit
  Notebook->>circuit: s = circuit(wl=lda)
  circuit-->>Notebook: scattering_parameters s
  Notebook->>Notebook: Plot responses o1->o3 and o1->o4
  Notebook-->>User: Display transmission spectra
Loading

File-Level Changes

Change Details Files
Correct MZI path-length sign conventions and propagate them through the simulation layout.
  • Update markdown description of MZI path length differences to match the intended sign convention and physical design
  • Adjust mzi_deltas tuple to use signed length_delta values and new combination with L_pi
  • Pass mzi_deltas directly into the mzi_lattice delta_lengths parameter instead of their absolute values
notebooks/workflow_3_cascaded_mzi.ipynb
Improve netlist patching logic and apply it to recursive netlists.
  • Fix typo in filtered settings variable name to avoid using an undefined variable during model instantiation
  • Introduce patch_netlist_mod helper that iterates over recursive netlist models and instances, filters settings against the target model signature, and rewrites components to unique versioned names
  • Use patch_netlist_mod in an active plotting cell to build a sax circuit from a recursive netlist and plot transmission responses with a marker at the central wavelength
notebooks/workflow_3_cascaded_mzi.ipynb
Minor documentation and environment metadata updates.
  • Fix textual typo from 'latter' to 'later' in the notebook intro description
  • Update notebook kernelspec display name and Python version metadata
notebooks/workflow_3_cascaded_mzi.ipynb

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The new patch_netlist_mod function largely duplicates the logic in patch_netlist; consider refactoring into a single, more general helper (e.g., with a recursive flag or shared inner routine) to avoid divergence between the two implementations.
  • In patch_netlist_mod, the loop variable model is reused both as the key for netlist[model] and then as the instance dict (model = instances[name]), which is confusing and error-prone; use distinct variable names for the key and the instance object.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `patch_netlist_mod` function largely duplicates the logic in `patch_netlist`; consider refactoring into a single, more general helper (e.g., with a `recursive` flag or shared inner routine) to avoid divergence between the two implementations.
- In `patch_netlist_mod`, the loop variable `model` is reused both as the key for `netlist[model]` and then as the instance dict (`model = instances[name]`), which is confusing and error-prone; use distinct variable names for the key and the instance object.

## Individual Comments

### Comment 1
<location path="notebooks/workflow_3_cascaded_mzi.ipynb" line_range="715-729" />
<code_context>
+    "    # For recursive netlist\n",
+    "    for model in netlist.keys():\n",
+    "        instances = netlist[model][\"instances\"]\n",
+    "        for name in instances:\n",
+    "            model = instances[name]\n",
+    "            if model[\"component\"] in models_to_patch:\n",
+    "                component = model[\"component\"]\n",
</code_context>
<issue_to_address>
**suggestion:** Avoid reusing `model` as both the outer key and the inner instance dict.

Reusing `model` for both the netlist key and the instance dict (`for model in netlist.keys():` then `model = instances[name]`) makes the code hard to follow and easy to misuse later (e.g., confusing the key with the instance data). Please use distinct names like `netlist_name` and `instance` to clarify what’s being iterated and mutated.

```suggestion
    "def patch_netlist_mod(netlist, models, models_to_patch):\n",
    "    # For recursive netlist\n",
    "    for netlist_name in netlist.keys():\n",
    "        instances = netlist[netlist_name][\"instances\"]\n",
    "        for name in instances:\n",
    "            instance = instances[name]\n",
    "            if instance[\"component\"] in models_to_patch:\n",
    "                component = instance[\"component\"]\n",
    "                i = 0\n",
    "                new_component = f\"{component}_v{i}\"\n",
    "                while new_component in models:\n",
    "                    i += 1\n",
    "                    new_component = f\"{component}_v{i}\"\n",
    "                settings = instance[\"settings\"]\n",
    "                settings_filtered = {\n",
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant