From 41add31c037b71561aa6aadd61d6a6e449b06cb8 Mon Sep 17 00:00:00 2001 From: jan Date: Sun, 15 Oct 2023 00:12:25 -0700 Subject: [PATCH] add Library.map_layers --- masque/library.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/masque/library.py b/masque/library.py index 6594c63..92d302d 100644 --- a/masque/library.py +++ b/masque/library.py @@ -20,10 +20,11 @@ import numpy from numpy.typing import ArrayLike from .error import LibraryError, PatternError -from .utils import rotation_matrix_2d +from .utils import rotation_matrix_2d, layer_t from .shapes import Shape, Polygon from .label import Label from .abstract import Abstract +from .pattern import map_layers if TYPE_CHECKING: from .pattern import Pattern @@ -547,6 +548,28 @@ class ILibrary(ILibraryView, MutableMapping[str, 'Pattern'], metaclass=ABCMeta): del pattern.refs[old_target] return self + def map_layers( + self, + map_layer: Callable[[layer_t], layer_t], + ) -> Self: + """ + Move all the elements in all patterns from one layer onto a different layer. + Can also handle multiple such mappings simultaneously. + + Args: + map_layer: Callable which may be called with each layer present in `elements`, + and should return the new layer to which it will be mapped. + A simple example which maps `old_layer` to `new_layer` and leaves all others + as-is would look like `lambda layer: {old_layer: new_layer}.get(layer, layer)` + + Returns: + self + """ + for pattern in self.values(): + pattern.shapes = map_layers(pattern.shapes, map_layer) + pattern.labels = map_layers(pattern.labels, map_layer) + return self + def mkpat(self, name: str) -> tuple[str, 'Pattern']: """ Convenience method to create an empty pattern, add it to the library,