Skip to content

fix: keep grid_with_text labels on their anchors - #4821

Merged
joamatab merged 1 commit into
gdsfactory:mainfrom
MaxFreedomPollard:fix-grid-with-text-label-placement
Sep 8, 2026
Merged

joamatab merged 1 commit into
gdsfactory:mainfrom
MaxFreedomPollard:fix-grid-with-text-label-placement

Conversation

@MaxFreedomPollard

@MaxFreedomPollard MaxFreedomPollard commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

grid_with_text moved each label to its anchor first and applied text_mirror and text_rotation afterwards. rotate() and dmirror() act about the layout origin, not about the instance, so doing them last dragged every label away from the cell it names. On a 3 cell grid of 1x1 rectangles with text_rotation=180, the labels land at (0, -3), (-6, -3) and (-12, -3) instead of on their anchors at (0, 3), (6, 3) and (12, 3). text_mirror=True scatters them along negative x, and the offset grows with the grid, so on a real mask the labels end up nowhere near the devices.

The fix in gdsfactory/grid.py:171-182 mirrors and rotates the label before moving it to the anchor. That is the order pack already uses (gdsfactory/pack.py:273-280) and the order edge_coupler_array uses (gdsfactory/components/edge_couplers/edge_coupler_array.py:88-91), so this makes grid_with_text agree with the rest of the library rather than inventing a new convention.

Every caller in the repo, pack_doe_grid included, uses the defaults text_rotation=0 and text_mirror=False, where both branches are skipped. No reference GDS, netlist or settings fixture changes.

Test Plan

Added test_grid_with_text_rotation_keeps_labels_on_anchors (parametrized over 0, 90, 180, 270) and test_grid_with_text_mirror_keeps_labels_on_anchors to tests/test_grid.py. Each asserts that every label instance sits at the center anchor of the cell it labels.

uv run pytest tests/test_grid.py on main with only the grid.py change reverted: 4 failed, 3 passed. The 90, 180 and 270 rotations and the mirror case fail, text_rotation=0 passes. With the fix: 7 passed.

uv run pytest -q -n 2 tests/ after make test-data: 1731 passed, 2 skipped, 1 xfailed.

uv run pytest -q tests/components/test_components.py -k "pack_doe or grid": 6 passed, so the pack_doe_grid and bump_pad_grid geometry still matches the gdsfactory-test-data references.

uv run pre-commit run --files gdsfactory/grid.py tests/test_grid.py: all hooks pass, including ruff check, ruff format, codespell and pyright.

Summary by Sourcery

Fix grid_with_text label placement by applying text transformations before anchoring labels to grid cells.

Bug Fixes:

  • Keep grid_with_text labels aligned with their cell anchors when text rotation or mirroring is enabled.

Tests:

  • Add coverage for rotated and mirrored grid_with_text labels to verify they remain on their anchors.

grid_with_text placed each label with t.move() and only then applied
text_mirror and text_rotation. Both rotate() and dmirror() act about the
layout origin, so applying them after the move dragged the label away
from the component it names: with text_rotation=180 the labels ended up
reflected through (0, 0), tens of microns off the grid.

gdsfactory/grid.py now mirrors and rotates the label before moving it to
the anchor, which is the order pack() and edge_coupler_array() already
use.

Every current caller uses the defaults text_rotation=0 and
text_mirror=False, so no reference GDS or netlist changes.
@sourcery-ai

sourcery-ai Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Fixes grid_with_text label placement by applying mirroring and rotation before moving labels to their anchors, with regression tests covering all supported rotations and mirroring.

Sequence diagram for corrected grid label placement

sequenceDiagram
    participant Grid as grid_with_text
    participant Label as LabelInstance
    participant Anchor as CellAnchor

    Grid->>Label: dmirror()
    Grid->>Label: rotate(text_rotation)
    Grid->>Anchor: dsize_info[text_anchor]
    Grid->>Label: move(text_offset + anchor)
    Anchor-->>Label: Label remains on anchor
Loading

File-Level Changes

Change Details Files
Apply label transforms before anchoring them to grid cells, preventing rotation and mirroring from displacing labels relative to their devices.
  • Move mirroring and rotation ahead of the anchor translation.
  • Align behavior with the transform order used by other library placement helpers.
gdsfactory/grid.py
Add regression coverage confirming labels remain at their corresponding cell centers under supported transforms.
  • Test rotations of 0, 90, 180, and 270 degrees.
  • Test mirrored labels on a multi-cell grid.
  • Compare label instance positions with rectangle anchor positions.
tests/test_grid.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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@joamatab joamatab left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nice catch!

@codecov

codecov Bot commented Sep 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.21%. Comparing base (1881df0) to head (f3b4c90).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4821   +/-   ##
=======================================
  Coverage   76.20%   76.21%           
=======================================
  Files         330      330           
  Lines       21696    21696           
  Branches     2810     2810           
=======================================
+ Hits        16533    16535    +2     
  Misses       4140     4140           
+ Partials     1023     1021    -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@joamatab
joamatab merged commit d1d5e25 into gdsfactory:main Sep 8, 2026
16 checks passed
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.

2 participants