From 5f007349cc9d25b668ac3d395e0e6b0537cde743 Mon Sep 17 00:00:00 2001 From: Troy Tamas Date: Wed, 1 Nov 2023 18:02:55 +0900 Subject: [PATCH 1/3] avoid flattening when necessary and add contains check --- gplugins/klayout/dataprep/regions.py | 11 +++++++++-- gplugins/klayout/tests/test_dataprep_regions.py | 3 +++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/gplugins/klayout/dataprep/regions.py b/gplugins/klayout/dataprep/regions.py index 5d731ea8..6a223b23 100644 --- a/gplugins/klayout/dataprep/regions.py +++ b/gplugins/klayout/dataprep/regions.py @@ -98,6 +98,12 @@ def __setitem__(self, layer: tuple[int, int], region: Region) -> None: raise ValueError(f"Layer must be a tuple of two integers. Got {layer!r}") self.regions[layer] = region + + def __contains__(self, item): + # checks if the layout contains the given layer + layer, datatype = item + return self.lib.find_layer(layer, datatype) is not None + def write_gds(self, gdspath: PathType = GDSDIR_TEMP / "out.gds", **kwargs) -> None: """Write gds. @@ -134,10 +140,11 @@ def get_kcell( c = kf.KCell(cellname, self.lib) if keep_original: c.copy_tree(self.layout) - c.flatten() + for layer in self.regions: + layer_id = self.lib.layer(layer[0], layer[1]) + self.lib.layout.clear_layer(layer_id) for layer, region in self.regions.items(): - c.shapes(self.lib.layer(layer[0], layer[1])).clear() c.shapes(self.lib.layer(layer[0], layer[1])).insert(region) return c diff --git a/gplugins/klayout/tests/test_dataprep_regions.py b/gplugins/klayout/tests/test_dataprep_regions.py index 8390ce2b..ade6ec88 100644 --- a/gplugins/klayout/tests/test_dataprep_regions.py +++ b/gplugins/klayout/tests/test_dataprep_regions.py @@ -92,6 +92,9 @@ def test_RegionCollection_init(region_collection) -> None: # Check if the RegionCollection was initialized properly assert region_collection[(1, 0)] +def test_region_collection_contains(region_collection) -> None: + assert l.FLOORPLAN in region_collection + if __name__ == "__main__": import pathlib From f0c47da00a4058a8d1e0673998362c0aded4891f Mon Sep 17 00:00:00 2001 From: Troy Tamas Date: Wed, 1 Nov 2023 18:09:20 +0900 Subject: [PATCH 2/3] cleaning up layer assertions --- gplugins/klayout/dataprep/regions.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/gplugins/klayout/dataprep/regions.py b/gplugins/klayout/dataprep/regions.py index 6a223b23..6eae3433 100644 --- a/gplugins/klayout/dataprep/regions.py +++ b/gplugins/klayout/dataprep/regions.py @@ -23,6 +23,21 @@ def copy(region: kdb.Region) -> kdb.Region: return region.dup() +def _is_layer(value: any) -> bool: + try: + layer, datatype = value + except Exception: + return False + if isinstance(layer, int) and isinstance(datatype, int): + return True + else: + return False + +def _assert_is_layer(value: any) -> None: + if not _is_layer(value): + raise ValueError(f"Layer must be a tuple of two integers. Got {value!r}") + + class Region(kdb.Region): def __iadd__(self, offset) -> kdb.Region: """Adds an offset to the layer.""" @@ -81,8 +96,7 @@ def __init__(self, gdspath, cell_name: str | None = None) -> None: self.regions = {} def __getitem__(self, layer: tuple[int, int]) -> Region: - if len(layer) != 2: - raise ValueError(f"Layer must be a tuple of two integers. Got {layer!r}") + _assert_is_layer(layer) if layer in self.regions: return self.regions[layer] @@ -94,13 +108,13 @@ def __getitem__(self, layer: tuple[int, int]) -> Region: return region def __setitem__(self, layer: tuple[int, int], region: Region) -> None: - if len(layer) != 2: - raise ValueError(f"Layer must be a tuple of two integers. Got {layer!r}") + _assert_is_layer(layer) self.regions[layer] = region def __contains__(self, item): # checks if the layout contains the given layer + _assert_is_layer(item) layer, datatype = item return self.lib.find_layer(layer, datatype) is not None From d6ba6b9bb71150918a0f12cea37387997cdb2593 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 1 Nov 2023 09:13:20 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- gplugins/klayout/dataprep/regions.py | 4 ++-- gplugins/klayout/tests/test_dataprep_regions.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/gplugins/klayout/dataprep/regions.py b/gplugins/klayout/dataprep/regions.py index 6eae3433..9e101a66 100644 --- a/gplugins/klayout/dataprep/regions.py +++ b/gplugins/klayout/dataprep/regions.py @@ -32,7 +32,8 @@ def _is_layer(value: any) -> bool: return True else: return False - + + def _assert_is_layer(value: any) -> None: if not _is_layer(value): raise ValueError(f"Layer must be a tuple of two integers. Got {value!r}") @@ -111,7 +112,6 @@ def __setitem__(self, layer: tuple[int, int], region: Region) -> None: _assert_is_layer(layer) self.regions[layer] = region - def __contains__(self, item): # checks if the layout contains the given layer _assert_is_layer(item) diff --git a/gplugins/klayout/tests/test_dataprep_regions.py b/gplugins/klayout/tests/test_dataprep_regions.py index ade6ec88..d3051cf4 100644 --- a/gplugins/klayout/tests/test_dataprep_regions.py +++ b/gplugins/klayout/tests/test_dataprep_regions.py @@ -92,6 +92,7 @@ def test_RegionCollection_init(region_collection) -> None: # Check if the RegionCollection was initialized properly assert region_collection[(1, 0)] + def test_region_collection_contains(region_collection) -> None: assert l.FLOORPLAN in region_collection