Skip to content

Comments

Update tidy3d API calls for web and ModalComponentModeler#676

Open
jonbenfri wants to merge 4 commits intogdsfactory:mainfrom
jonbenfri:workflow-3-cascaded-mzi-bugfixes
Open

Update tidy3d API calls for web and ModalComponentModeler#676
jonbenfri wants to merge 4 commits intogdsfactory:mainfrom
jonbenfri:workflow-3-cascaded-mzi-bugfixes

Conversation

@jonbenfri
Copy link

@jonbenfri jonbenfri commented Feb 21, 2026

Solves issue with stale tidy3d notebook example code notebooks/workflow_3_cascaded_mzi.ipynb (issue #674) after adding in solution to activate PDK (issue #673 with related pull request #675):

gf.gpdk.PDK.activate()

Summary by Sourcery

Update tidy3d component modeling and S-parameter utilities to align with the latest web-based API and defaults.

Bug Fixes:

  • Fix S-parameter computation by switching to web.run with explicit result handling instead of directly running the modeler.

Enhancements:

  • Relax the default ModeSpec to remove TE polarization filtering for component modeling and S-parameter utilities.
  • Remove obsolete folder/path/verbose arguments from component modeler construction and rely on updated modeler copying behavior.

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

sourcery-ai bot commented Feb 21, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR updates the Tidy3D integration to align with the newer web-based API and relaxes the default mode specification by removing polarization filtering, affecting both component model construction and S-parameter extraction helpers.

Sequence diagram for updated write_sparameters Tidy3D web workflow

sequenceDiagram
    actor UserCode
    participant WriteSparameters as write_sparameters
    participant ComponentModeler
    participant FileSystem
    participant Tidy3DWebAPI as Tidy3D_web

    UserCode->>WriteSparameters: call write_sparameters(component, ports, freqs, ...)
    WriteSparameters->>ComponentModeler: get_component_modeler(..., mode_spec, boundary_spec, ...)
    ComponentModeler-->>WriteSparameters: return modeler

    WriteSparameters->>FileSystem: check for cached sparameters file
    alt cache_exists
        FileSystem-->>WriteSparameters: load numpy archive
        WriteSparameters-->>UserCode: return cached sparameters
    else cache_missing
        WriteSparameters->>Tidy3DWebAPI: web.run(modeler, verbose, path)
        Tidy3DWebAPI-->>WriteSparameters: modeler_data
        WriteSparameters->>WriteSparameters: s = modeler_data.smatrix()
        WriteSparameters->>FileSystem: save sparameters to numpy archive
        WriteSparameters-->>UserCode: return computed sparameters
    end
Loading

Flow diagram for write_sparameters with caching and web.run

flowchart TD
    A[Start write_sparameters] --> B[Build ComponentModeler via get_component_modeler]
    B --> C[Compute cache filepath]
    C --> D{Cached file exists?}
    D -->|Yes| E[Load sparameters from NPZ]
    E --> F[Return cached sparameters]

    D -->|No| G["Call web.run(modeler, verbose, path)"]
    G --> H[Get modeler_data]
    H --> I[Extract smatrix from modeler_data.smatrix]
    I --> J[Iterate ports and modes to build sparameters dict]
    J --> K[Save sparameters dict to NPZ]
    K --> L[Return computed sparameters]
Loading

File-Level Changes

Change Details Files
Update default mode specification to no longer force TE polarization in modeler and S-parameter helpers.
  • Change default mode_spec from td.ModeSpec(num_modes=1, filter_pol="te") to td.ModeSpec(num_modes=1) in get_component_modeler.
  • Change default mode_spec from td.ModeSpec(num_modes=1, filter_pol="te") to td.ModeSpec(num_modes=1) in write_sparameters and write_sparameters_batch.
  • Update corresponding docstrings to describe the new mode_spec default.
gplugins/tidy3d/component.py
Align ComponentModeler creation and update calls with updated Tidy3D API.
  • Remove folder_name, path_dir, and verbose arguments when constructing the ComponentModeler instance so only supported parameters are passed.
  • Stop setting path_dir via modeler.updated_copy(path_dir=str(path_dir)) and instead call modeler.updated_copy() with no arguments.
gplugins/tidy3d/component.py
Switch S-parameter simulation execution to use the Tidy3D web.run API and consume its result object.
  • Replace direct modeler.run() call with web.run(modeler, verbose=verbose, path=dirpath / "simulation.hdf5").
  • Use the returned modeler_data.smatrix() to obtain the scattering matrix before iterating over ports and modes.
gplugins/tidy3d/component.py

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 2 issues, and left some high level feedback:

  • The folder_name argument to write_sparameters is no longer used after removing it from the ComponentModeler construction and the updated_copy call; consider dropping it from the signature or wiring it into the new web.run path handling to avoid confusion.
  • Switching the default mode_spec from td.ModeSpec(num_modes=1, filter_pol="te") to td.ModeSpec(num_modes=1) changes the default modal behavior; if this is intentional, consider checking for call sites that might implicitly rely on TE filtering and updating them for clarity.
  • With the move to web.run(modeler, verbose=verbose, path=dirpath / "simulation.hdf5"), ensure that dirpath is consistently a Path (or convert explicitly) so that path joining and downstream file handling remain robust across different call patterns.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `folder_name` argument to `write_sparameters` is no longer used after removing it from the `ComponentModeler` construction and the `updated_copy` call; consider dropping it from the signature or wiring it into the new `web.run` path handling to avoid confusion.
- Switching the default `mode_spec` from `td.ModeSpec(num_modes=1, filter_pol="te")` to `td.ModeSpec(num_modes=1)` changes the default modal behavior; if this is intentional, consider checking for call sites that might implicitly rely on TE filtering and updating them for clarity.
- With the move to `web.run(modeler, verbose=verbose, path=dirpath / "simulation.hdf5")`, ensure that `dirpath` is consistently a `Path` (or convert explicitly) so that path joining and downstream file handling remain robust across different call patterns.

## Individual Comments

### Comment 1
<location> `gplugins/tidy3d/component.py:528` </location>
<code_context>

     path_dir = pathlib.Path(dirpath) / modeler._hash_self()
-    modeler = modeler.updated_copy(path_dir=str(path_dir))
+    modeler = modeler.updated_copy()

     sp = {}
</code_context>

<issue_to_address>
**issue:** `path_dir` is computed but no longer used, which suggests either dead code or an incomplete refactor.

After switching to `modeler.updated_copy()` with no arguments, `path_dir` is no longer referenced. If the hashed directory behavior is being removed, `path_dir` should likely be deleted. If that behavior is still needed (for organization/caching), `updated_copy` should probably still receive `path_dir` (or the new target directory) as an argument.
</issue_to_address>

### Comment 2
<location> `gplugins/tidy3d/component.py:590-591` </location>
<code_context>
     else:
         time.sleep(0.2)
-        s = modeler.run()
+        modeler_data = web.run(modeler, verbose=verbose, path=dirpath / "simulation.hdf5")
+        s = modeler_data.smatrix()
         for port_in in s.port_in.values:
             for port_out in s.port_out.values:
</code_context>

<issue_to_address>
**issue (bug_risk):** Using `dirpath / "simulation.hdf5"` assumes `dirpath` is a `Path`, which may be a regression if callers pass a string.

Previously we only wrapped `dirpath` in `Path` when building `path_dir`, so callers could still pass a plain string. With `web.run(..., path=dirpath / "simulation.hdf5")`, a string `dirpath` will now raise a `TypeError`. Please either normalize `dirpath` once at the top (e.g. `dirpath = pathlib.Path(dirpath)`) or change this call to `path=pathlib.Path(dirpath) / "simulation.hdf5"` to handle both strings and `Path` objects safely.
</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.


path_dir = pathlib.Path(dirpath) / modeler._hash_self()
modeler = modeler.updated_copy(path_dir=str(path_dir))
modeler = modeler.updated_copy()
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: path_dir is computed but no longer used, which suggests either dead code or an incomplete refactor.

After switching to modeler.updated_copy() with no arguments, path_dir is no longer referenced. If the hashed directory behavior is being removed, path_dir should likely be deleted. If that behavior is still needed (for organization/caching), updated_copy should probably still receive path_dir (or the new target directory) as an argument.

Comment on lines +590 to +591
modeler_data = web.run(modeler, verbose=verbose, path=dirpath / "simulation.hdf5")
s = modeler_data.smatrix()
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): Using dirpath / "simulation.hdf5" assumes dirpath is a Path, which may be a regression if callers pass a string.

Previously we only wrapped dirpath in Path when building path_dir, so callers could still pass a plain string. With web.run(..., path=dirpath / "simulation.hdf5"), a string dirpath will now raise a TypeError. Please either normalize dirpath once at the top (e.g. dirpath = pathlib.Path(dirpath)) or change this call to path=pathlib.Path(dirpath) / "simulation.hdf5" to handle both strings and Path objects safely.

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