use ArrayLike when accepting pseudo-ndarrays

nolock
Jan Petykiewicz 3 years ago
parent 3aefa644e5
commit 4796676a4e

@ -8,6 +8,7 @@ from collections import Counter
import numpy # type: ignore import numpy # type: ignore
from numpy import pi from numpy import pi
from numpy.typing import ArrayLike
from ..pattern import Pattern from ..pattern import Pattern
from ..subpattern import SubPattern from ..subpattern import SubPattern
@ -48,7 +49,7 @@ class Port(PositionableImpl, Rotatable, PivotableImpl, Copyable, Mirrorable, met
""" Port types must match to be plugged together if both are non-zero """ """ Port types must match to be plugged together if both are non-zero """
def __init__(self, def __init__(self,
offset: numpy.ndarray, offset: ArrayLike,
rotation: Optional[float], rotation: Optional[float],
ptype: str = 'unk', ptype: str = 'unk',
) -> None: ) -> None:
@ -730,7 +731,7 @@ class Device(Copyable, Mirrorable):
return s return s
def rotate_offsets_around(offsets: numpy.ndarray, pivot: numpy.ndarray, angle: float) -> numpy.ndarray: def rotate_offsets_around(offsets: ArrayLike, pivot: ArrayLike, angle: float) -> numpy.ndarray:
offsets -= pivot offsets -= pivot
offsets[:] = (rotation_matrix_2d(angle) @ offsets.T).T offsets[:] = (rotation_matrix_2d(angle) @ offsets.T).T
offsets += pivot offsets += pivot

@ -11,6 +11,7 @@ from collections import defaultdict
import numpy # type: ignore import numpy # type: ignore
from numpy import inf from numpy import inf
from numpy.typing import ArrayLike
# .visualize imports matplotlib and matplotlib.collections # .visualize imports matplotlib and matplotlib.collections
from .subpattern import SubPattern from .subpattern import SubPattern
@ -333,8 +334,8 @@ class Pattern(LockableImpl, AnnotatableImpl, Mirrorable, metaclass=AutoSlots):
return self return self
def manhattanize(self: P, def manhattanize(self: P,
grid_x: numpy.ndarray, grid_x: ArrayLike,
grid_y: numpy.ndarray, grid_y: ArrayLike,
) -> P: ) -> P:
""" """
Calls `.polygonize()` and `.flatten()` on the pattern, then calls `.manhattanize()` on all the Calls `.polygonize()` and `.flatten()` on the pattern, then calls `.manhattanize()` on all the

@ -8,6 +8,7 @@ import copy
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
import numpy # type: ignore import numpy # type: ignore
from numpy.typing import ArrayLike
from .error import PatternError from .error import PatternError
from .utils import rotation_matrix_2d, vector2, AutoSlots from .utils import rotation_matrix_2d, vector2, AutoSlots
@ -61,9 +62,9 @@ class Grid(LockableImpl, Repetition, metaclass=AutoSlots):
""" Number of instances along the direction specified by the `b_vector` """ """ Number of instances along the direction specified by the `b_vector` """
def __init__(self, def __init__(self,
a_vector: numpy.ndarray, a_vector: ArrayLike,
a_count: int, a_count: int,
b_vector: Optional[numpy.ndarray] = None, b_vector: Optional[ArrayLike] = None,
b_count: Optional[int] = 1, b_count: Optional[int] = 1,
locked: bool = False,): locked: bool = False,):
""" """
@ -303,13 +304,13 @@ class Arbitrary(LockableImpl, Repetition, metaclass=AutoSlots):
return self._displacements return self._displacements
@displacements.setter @displacements.setter
def displacements(self, val: Union[Sequence[Sequence[float]], numpy.ndarray]): def displacements(self, val: ArrayLike):
val = numpy.array(val, float) val = numpy.array(val, float)
val = numpy.sort(val.view([('', val.dtype)] * val.shape[1]), 0).view(val.dtype) # sort rows val = numpy.sort(val.view([('', val.dtype)] * val.shape[1]), 0).view(val.dtype) # sort rows
self._displacements = val self._displacements = val
def __init__(self, def __init__(self,
displacements: numpy.ndarray, displacements: ArrayLike,
locked: bool = False,): locked: bool = False,):
""" """
Args: Args:

@ -4,6 +4,7 @@ from enum import Enum
import numpy # type: ignore import numpy # type: ignore
from numpy import pi, inf from numpy import pi, inf
from numpy.typing import ArrayLike
from . import Shape, normalized_shape_tuple, Polygon, Circle from . import Shape, normalized_shape_tuple, Polygon, Circle
from .. import PatternError from .. import PatternError
@ -102,7 +103,7 @@ class Path(Shape, metaclass=AutoSlots):
return self._vertices return self._vertices
@vertices.setter @vertices.setter
def vertices(self, val: numpy.ndarray): def vertices(self, val: ArrayLike):
val = numpy.array(val, dtype=float) # TODO document that these might not be copied val = numpy.array(val, dtype=float) # TODO document that these might not be copied
if len(val.shape) < 2 or val.shape[1] != 2: if len(val.shape) < 2 or val.shape[1] != 2:
raise PatternError('Vertices must be an Nx2 array') raise PatternError('Vertices must be an Nx2 array')
@ -119,7 +120,7 @@ class Path(Shape, metaclass=AutoSlots):
return self.vertices[:, 0] return self.vertices[:, 0]
@xs.setter @xs.setter
def xs(self, val: numpy.ndarray): def xs(self, val: ArrayLike):
val = numpy.array(val, dtype=float).flatten() val = numpy.array(val, dtype=float).flatten()
if val.size != self.vertices.shape[0]: if val.size != self.vertices.shape[0]:
raise PatternError('Wrong number of vertices') raise PatternError('Wrong number of vertices')
@ -134,18 +135,18 @@ class Path(Shape, metaclass=AutoSlots):
return self.vertices[:, 1] return self.vertices[:, 1]
@ys.setter @ys.setter
def ys(self, val: numpy.ndarray): def ys(self, val: ArrayLike):
val = numpy.array(val, dtype=float).flatten() val = numpy.array(val, dtype=float).flatten()
if val.size != self.vertices.shape[0]: if val.size != self.vertices.shape[0]:
raise PatternError('Wrong number of vertices') raise PatternError('Wrong number of vertices')
self.vertices[:, 1] = val self.vertices[:, 1] = val
def __init__(self, def __init__(self,
vertices: numpy.ndarray, vertices: ArrayLike,
width: float = 0.0, width: float = 0.0,
*, *,
cap: PathCap = PathCap.Flush, cap: PathCap = PathCap.Flush,
cap_extensions: numpy.ndarray = None, cap_extensions: Optional[ArrayLike] = None,
offset: vector2 = (0.0, 0.0), offset: vector2 = (0.0, 0.0),
rotation: float = 0, rotation: float = 0,
mirrored: Sequence[bool] = (False, False), mirrored: Sequence[bool] = (False, False),

@ -3,6 +3,7 @@ import copy
import numpy # type: ignore import numpy # type: ignore
from numpy import pi from numpy import pi
from numpy.typing import ArrayLike
from . import Shape, normalized_shape_tuple from . import Shape, normalized_shape_tuple
from .. import PatternError from .. import PatternError
@ -33,7 +34,7 @@ class Polygon(Shape, metaclass=AutoSlots):
return self._vertices return self._vertices
@vertices.setter @vertices.setter
def vertices(self, val: numpy.ndarray): def vertices(self, val: ArrayLike):
val = numpy.array(val, dtype=float) # TODO document that these might not be copied val = numpy.array(val, dtype=float) # TODO document that these might not be copied
if len(val.shape) < 2 or val.shape[1] != 2: if len(val.shape) < 2 or val.shape[1] != 2:
raise PatternError('Vertices must be an Nx2 array') raise PatternError('Vertices must be an Nx2 array')
@ -50,7 +51,7 @@ class Polygon(Shape, metaclass=AutoSlots):
return self.vertices[:, 0] return self.vertices[:, 0]
@xs.setter @xs.setter
def xs(self, val: numpy.ndarray): def xs(self, val: ArrayLike):
val = numpy.array(val, dtype=float).flatten() val = numpy.array(val, dtype=float).flatten()
if val.size != self.vertices.shape[0]: if val.size != self.vertices.shape[0]:
raise PatternError('Wrong number of vertices') raise PatternError('Wrong number of vertices')
@ -65,14 +66,14 @@ class Polygon(Shape, metaclass=AutoSlots):
return self.vertices[:, 1] return self.vertices[:, 1]
@ys.setter @ys.setter
def ys(self, val: numpy.ndarray): def ys(self, val: ArrayLike):
val = numpy.array(val, dtype=float).flatten() val = numpy.array(val, dtype=float).flatten()
if val.size != self.vertices.shape[0]: if val.size != self.vertices.shape[0]:
raise PatternError('Wrong number of vertices') raise PatternError('Wrong number of vertices')
self.vertices[:, 1] = val self.vertices[:, 1] = val
def __init__(self, def __init__(self,
vertices: numpy.ndarray, vertices: ArrayLike,
*, *,
offset: vector2 = (0.0, 0.0), offset: vector2 = (0.0, 0.0),
rotation: float = 0.0, rotation: float = 0.0,

@ -2,6 +2,7 @@ from typing import List, Tuple, Callable, TypeVar, Optional, TYPE_CHECKING
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
import numpy # type: ignore import numpy # type: ignore
from numpy.typing import ArrayLike
from ..traits import (PositionableImpl, LayerableImpl, DoseableImpl, from ..traits import (PositionableImpl, LayerableImpl, DoseableImpl,
Rotatable, Mirrorable, Copyable, Scalable, Rotatable, Mirrorable, Copyable, Scalable,
@ -93,8 +94,8 @@ class Shape(PositionableImpl, LayerableImpl, DoseableImpl, Rotatable, Mirrorable
---- Non-abstract methods ---- Non-abstract methods
''' '''
def manhattanize_fast(self, def manhattanize_fast(self,
grid_x: numpy.ndarray, grid_x: ArrayLike,
grid_y: numpy.ndarray, grid_y: ArrayLike,
) -> List['Polygon']: ) -> List['Polygon']:
""" """
Returns a list of polygons with grid-aligned ("Manhattan") edges approximating the shape. Returns a list of polygons with grid-aligned ("Manhattan") edges approximating the shape.
@ -200,8 +201,8 @@ class Shape(PositionableImpl, LayerableImpl, DoseableImpl, Rotatable, Mirrorable
return manhattan_polygons return manhattan_polygons
def manhattanize(self, def manhattanize(self,
grid_x: numpy.ndarray, grid_x: ArrayLike,
grid_y: numpy.ndarray grid_y: ArrayLike,
) -> List['Polygon']: ) -> List['Polygon']:
""" """
Returns a list of polygons with grid-aligned ("Manhattan") edges approximating the shape. Returns a list of polygons with grid-aligned ("Manhattan") edges approximating the shape.

@ -5,10 +5,11 @@ from typing import Any, Union, Tuple, Sequence, Dict, List
from abc import ABCMeta from abc import ABCMeta
import numpy # type: ignore import numpy # type: ignore
from numpy.typing import ArrayLike
# Type definitions # Type definitions
vector2 = Union[numpy.ndarray, Tuple[float, float], Sequence[float]] vector2 = ArrayLike
layer_t = Union[int, Tuple[int, int], str] layer_t = Union[int, Tuple[int, int], str]
annotations_t = Dict[str, List[Union[int, float, str]]] annotations_t = Dict[str, List[Union[int, float, str]]]
@ -89,7 +90,7 @@ def normalize_mirror(mirrored: Sequence[bool]) -> Tuple[bool, float]:
return mirror_x, angle return mirror_x, angle
def remove_duplicate_vertices(vertices: numpy.ndarray, closed_path: bool = True) -> numpy.ndarray: def remove_duplicate_vertices(vertices: ArrayLike, closed_path: bool = True) -> numpy.ndarray:
""" """
Given a list of vertices, remove any consecutive duplicates. Given a list of vertices, remove any consecutive duplicates.
@ -107,7 +108,7 @@ def remove_duplicate_vertices(vertices: numpy.ndarray, closed_path: bool = True)
return vertices[~duplicates] return vertices[~duplicates]
def remove_colinear_vertices(vertices: numpy.ndarray, closed_path: bool = True) -> numpy.ndarray: def remove_colinear_vertices(vertices: ArrayLike, closed_path: bool = True) -> numpy.ndarray:
""" """
Given a list of vertices, remove any superflous vertices (i.e. Given a list of vertices, remove any superflous vertices (i.e.
those which lie along the line formed by their neighbors) those which lie along the line formed by their neighbors)

Loading…
Cancel
Save