Skip to content

add lumerical from sax#680

Open
joamatab wants to merge 1 commit intomainfrom
lumerical_from_sax
Open

add lumerical from sax#680
joamatab wants to merge 1 commit intomainfrom
lumerical_from_sax

Conversation

@joamatab
Copy link
Contributor

@joamatab joamatab commented Feb 27, 2026

Summary by Sourcery

Add a utility to export SAX S-parameter models to Lumerical Interconnect DAT files.

New Features:

  • Introduce sax_to_lumerical_dat helper to sweep a SAX model over wavelength and write S-parameters in Lumerical DAT format.
  • Support optional port renaming and automatic port ordering when generating Lumerical-compatible S-parameter data.

Tests:

  • Add a simple main script example that generates a coupler DAT file and verifies it by reading it back with the existing Lumerical reader.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Feb 27, 2026

Reviewer's Guide

Adds a utility to export SAX S-parameter models into Lumerical Interconnect .dat format, including wavelength sweep, optional port remapping, and correct frequency-ordered S-parameter serialization plus a small CLI-style example/verification block.

Sequence diagram for sax_to_lumerical_dat example execution

sequenceDiagram
    actor User
    participant sax_to_lumerical_dat
    participant SAXModel
    participant FileSystem
    participant LumericalReader

    User->>sax_to_lumerical_dat: call sax_to_lumerical_dat(model, filepath, port_map, wavelength_start, wavelength_stop, wavelength_points, model_kwargs)
    activate sax_to_lumerical_dat

    sax_to_lumerical_dat->>sax_to_lumerical_dat: build wl array (linspace)
    sax_to_lumerical_dat->>SAXModel: model(wl, model_kwargs)
    activate SAXModel
    SAXModel-->>sax_to_lumerical_dat: sdict[(port_src, port_dst)] -> complex array
    deactivate SAXModel

    sax_to_lumerical_dat->>sax_to_lumerical_dat: apply port_map to sdict (optional)
    sax_to_lumerical_dat->>sax_to_lumerical_dat: infer port_names if None
    sax_to_lumerical_dat->>sax_to_lumerical_dat: compute frequencies from wl (reversed)

    loop for each src in port_names
        loop for each dst in port_names
            sax_to_lumerical_dat->>sax_to_lumerical_dat: get sdict[(src, dst)] or zeros
            sax_to_lumerical_dat->>sax_to_lumerical_dat: reverse values, compute magnitude and angle
            sax_to_lumerical_dat->>sax_to_lumerical_dat: append DAT header and data lines
        end
    end

    sax_to_lumerical_dat->>FileSystem: write_text(joined lines) -> filepath.dat
    deactivate sax_to_lumerical_dat
    sax_to_lumerical_dat-->>User: return Path(filepath.dat)

    User->>LumericalReader: read_sparameters_file(filepath.dat, numports)
    activate LumericalReader
    LumericalReader->>FileSystem: open and parse DAT file
    FileSystem-->>LumericalReader: raw DAT content
    LumericalReader-->>User: port_names, F, S
    deactivate LumericalReader
Loading

File-Level Changes

Change Details Files
Introduce sax_to_lumerical_dat helper to sweep a SAX model over wavelength and write Lumerical Interconnect-compatible DAT S-parameter files.
  • Define SPEED_OF_LIGHT constant and compute wavelengths and corresponding ascending frequencies from user-specified sweep parameters.
  • Evaluate the provided SAX model over the wavelength grid, optionally apply a port mapping, and infer ordered port names when not explicitly provided, preserving first-seen order from the S-dict keys.
  • Generate full S-parameter blocks for all source/destination port combinations, defaulting missing entries to zero, reverse data to match ascending-frequency ordering, and write frequency/magnitude/phase triplets in Lumerical DAT text format.
  • Write port declarations and S-parameter data lines to the target .dat file path and return it as a pathlib.Path object.
gplugins/lumerical/write_sparameters_lumerical_from_sax.py
Add a simple executable example for manual testing and verification of the written DAT file.
  • Use sax.models.coupler as an example model and call sax_to_lumerical_dat with a realistic port mapping and coupler parameters.
  • Read the generated .dat file using gplugins.lumerical.read.read_sparameters_file to verify port names, frequency/S-matrix shapes, and printed frequency range.
gplugins/lumerical/write_sparameters_lumerical_from_sax.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 1 issue, and left some high level feedback:

  • Consider adding basic validation (e.g., that wavelength_start < wavelength_stop, wavelength_points > 0, and that the arrays returned in sdict all match len(wl)) to fail fast on misconfigured sweeps or inconsistent model outputs rather than silently producing malformed DAT files.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider adding basic validation (e.g., that `wavelength_start < wavelength_stop`, `wavelength_points > 0`, and that the arrays returned in `sdict` all match `len(wl)`) to fail fast on misconfigured sweeps or inconsistent model outputs rather than silently producing malformed DAT files.

## Individual Comments

### Comment 1
<location path="gplugins/lumerical/write_sparameters_lumerical_from_sax.py" line_range="81-82" />
<code_context>
+    for src in port_names:
+        for dst in port_names:
+            key = (src, dst)
+            if key in sdict:
+                values = np.asarray(sdict[key], dtype=complex)
+            else:
+                # Missing entries (e.g. reflections) default to zero
</code_context>
<issue_to_address>
**issue (bug_risk):** Validate that each S-parameter vector length matches `wavelength_points` to avoid silent shape mismatches.

`values` is implicitly assumed to have length `n_wl`. If the model returns a different-length array (e.g., from wavelength sweep rounding or a model bug), the output will be malformed without any error. Please add a check like `assert values.shape[0] == n_wl` (or at least log a warning) before reversing and writing so these issues fail fast instead of silently corrupting the DAT file.
</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.

Comment on lines +81 to +82
if key in sdict:
values = np.asarray(sdict[key], dtype=complex)
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): Validate that each S-parameter vector length matches wavelength_points to avoid silent shape mismatches.

values is implicitly assumed to have length n_wl. If the model returns a different-length array (e.g., from wavelength sweep rounding or a model bug), the output will be malformed without any error. Please add a check like assert values.shape[0] == n_wl (or at least log a warning) before reversing and writing so these issues fail fast instead of silently corrupting the DAT file.

@github-actions github-actions bot added the enhancement New feature or request label Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant