Repository navigation
tests: restore the generic PDK layer table between test modules #552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Alisama20
wants to merge
1
commit into
gdsfactory:main
Choose a base branch
from
Alisama20:ali/fix-flaky-physical-layers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| """``restore_generic_pdk`` heals the shared layout that tests leave behind. | ||
|
|
||
| Regression for test-order failures in ``tests/meep/test_physical_layers.py``: | ||
| after IHP was activated, a raw ``(2, 0)`` registered in a freed slot and the | ||
| generic PDK came back, the layout held two ``(2, 0)`` layers and | ||
| ``LAYER.SLAB150`` wrote to a different one than a ``(2, 0)`` lookup read. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import gdsfactory as gf | ||
| import kfactory as kf | ||
| import pytest | ||
| from kfactory import kdb | ||
|
|
||
| from tests._helpers import restore_generic_pdk | ||
|
|
||
| BOX = kdb.Box(0, 0, 1000, 1000) # 1 um x 1 um in dbu | ||
|
|
||
|
|
||
| def _tuples() -> list[tuple[int, int]]: | ||
| layout = kf.kcl.layout | ||
| infos = (layout.get_info(index) for index in layout.layer_indexes()) | ||
| return [(info.layer, info.datatype) for info in infos] | ||
|
|
||
|
|
||
| def _assert_clean() -> None: | ||
| layout = kf.kcl.layout | ||
| tuples = _tuples() | ||
| for layer in gf.gpdk.LAYER: # ty: ignore[not-iterable] | ||
| info = layout.get_info(int(layer)) | ||
| assert (info.layer, info.datatype, info.name) == ( | ||
| layer.layer, | ||
| layer.datatype, | ||
| layer.name, | ||
| ) | ||
| assert tuples.count((layer.layer, layer.datatype)) == 1 | ||
|
|
||
|
|
||
| def _area(component: gf.Component, layer: tuple[int, int]) -> int: | ||
| """Area (dbu^2) the component holds on whatever layer ``layer`` resolves to.""" | ||
| index = component.kcl.layer(*layer) | ||
| return kdb.Region(component.kdb_cell.begin_shapes_rec(index)).area() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def locked_cell() -> gf.Component: | ||
| """A cached gdsfactory cell. kfactory locks it, which blocks gdsfactory's own | ||
| ``Layout.move_layer`` / ``delete_layer`` when it restores the layer table.""" | ||
| gf.gpdk.PDK.activate() | ||
| cell = gf.components.straight(length=7.0) | ||
| assert cell.kdb_cell.is_locked() | ||
| return cell | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("locked_cell") | ||
| def test_a_duplicated_slab_layer_is_merged_into_the_enum_slot() -> None: | ||
| restore_generic_pdk() | ||
| layout = kf.kcl.layout | ||
| slot = int(gf.gpdk.LAYER.SLAB150) | ||
| # the state gdsfactory leaves: the enum slot unnamed, a named twin elsewhere | ||
| layout.set_info(slot, kdb.LayerInfo(2, 0)) | ||
| twin = layout.insert_layer(kdb.LayerInfo(2, 0, "SLAB150")) | ||
| component = gf.Component() | ||
| component.shapes(slot).insert(BOX) | ||
| component.shapes(twin).insert(BOX.moved(5000, 0)) | ||
| assert _tuples().count((2, 0)) == 2 | ||
|
|
||
| restore_generic_pdk() | ||
|
|
||
| _assert_clean() | ||
| assert layout.find_layer(2, 0) == slot | ||
| assert _area(component, (2, 0)) == 2_000_000 # both boxes, on the one layer | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("locked_cell") | ||
| def test_a_raw_layer_in_a_generic_slot_is_moved_out_with_its_shapes() -> None: | ||
| restore_generic_pdk() | ||
| layout = kf.kcl.layout | ||
| slot = int(gf.gpdk.LAYER.SHALLOW_ETCH) | ||
| layout.set_info(slot, kdb.LayerInfo(250, 0)) | ||
| component = gf.Component() | ||
| component.shapes(slot).insert(BOX) | ||
|
|
||
| restore_generic_pdk() | ||
|
|
||
| _assert_clean() | ||
| assert layout.find_layer(250, 0) not in {None, slot} | ||
| assert _area(component, (250, 0)) == 1_000_000 | ||
| assert _area(component, (2, 6)) == 0 | ||
| drawn = gf.Component() | ||
| drawn.add_polygon([(0, 0), (2, 0), (2, 1), (0, 1)], layer=gf.gpdk.LAYER.SLAB150) | ||
| drawn.add_polygon( | ||
| [(4, 0), (6, 0), (6, 1), (4, 1)], layer=gf.gpdk.LAYER.SHALLOW_ETCH | ||
| ) | ||
| assert _area(drawn, (2, 0)) == 2_000_000 | ||
| assert _area(drawn, (2, 6)) == 2_000_000 | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("locked_cell") | ||
| def test_a_raw_layer_taken_under_ihp_does_not_split_the_slab_layer() -> None: | ||
| ihp = pytest.importorskip("ihp") | ||
| restore_generic_pdk() | ||
| ihp.PDK.activate() | ||
| gf.Pdk(name="restore_generic_pdk_probe").activate() # no layer enum: no pruning | ||
| kf.kcl.layer(2, 0) # lowest free slot while the generic slots are pruned | ||
| restore_generic_pdk() | ||
|
|
||
| _assert_clean() | ||
| assert gf.get_active_pdk().name == gf.gpdk.PDK.name | ||
| component = gf.Component() | ||
| component.add_polygon([(0, 0), (2, 0), (2, 1), (0, 1)], layer=gf.gpdk.LAYER.SLAB150) | ||
| assert _area(component, (2, 0)) == 2_000_000 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a prior module has activated IHP, the generic activation above prunes IHP's empty, fixed layer indices, and
layout.insert_layerhere can reuse one of those indices for the displaced foreign tuple. The added(250, 0)regression takes this branch and leaves nonempty geometry there; if a later default IHP test callsihp.PDK.activate()while the cachedlocked_cellremains, IHP cannot restore its reserved slot without the same locked-cell relocation problem, so its enum may resolve to the foreign layer. Allocate a slot outside all registered enum reservations and add a generic-to-IHP regression.AGENTS.md reference: AGENTS.md:L14-L15
Useful? React with 👍 / 👎.