diff --git a/adze/boundary.py b/adze/boundary.py index 1c31ba1..03ee744 100644 --- a/adze/boundary.py +++ b/adze/boundary.py @@ -1,4 +1,4 @@ -from typing import List, Dict, Any +from typing import Any import numpy from numpy.linalg import norm from numpy.typing import ArrayLike, NDArray @@ -9,8 +9,8 @@ from .util import _get_maps, find_components, find_faces def planes_from_boundary( vertices: ArrayLike, - faces: List[ArrayLike] - ) -> List[plane_t]: + faces: list[ArrayLike] + ) -> list[plane_t]: ''' Generate a list of planes (normal, signed distance) from the faces of a boundary- representation polyhedron. @@ -38,7 +38,7 @@ def planes_from_boundary( return planes -def build_polyhedron(vertices: ArrayLike, faces: List[ArrayLike]) -> poly_t: +def build_polyhedron(vertices: ArrayLike, faces: list[ArrayLike]) -> poly_t: ''' Generate a 3-verted-connected Schlegel diagram representation corresponding to the provided boundary-representation polyhedron. @@ -259,7 +259,7 @@ def build_polyhedron(vertices: ArrayLike, faces: List[ArrayLike]) -> poly_t: return {'vertices': final_vertices, 'neighbors': neighbors} -def boundary_from_poly(poly: poly_t) -> List[Dict[str, Any]]: +def boundary_from_poly(poly: poly_t) -> list[dict[str, Any]]: ''' Generate a boundary represeentation corresponding to the provided 3-vertex-connected Schlegel diagram. @@ -279,7 +279,7 @@ def boundary_from_poly(poly: poly_t) -> List[Dict[str, Any]]: faces = find_faces(neighbors) num_components = owning_component.max() + 1 - components: List[List[NDArray[numpy.int_]]] = [[] for _ in range(num_components)] + components: list[list[NDArray[numpy.integer]]] = [[] for _ in range(num_components)] for face in faces: component_ind = owning_component[face[0]] components[component_ind].append(face) diff --git a/adze/moments.py b/adze/moments.py index e61dd18..c3a81e9 100644 --- a/adze/moments.py +++ b/adze/moments.py @@ -1,4 +1,3 @@ -from typing import Dict, Tuple import numpy from numpy.typing import NDArray diff --git a/adze/shapes.py b/adze/shapes.py index cfd45b9..fd42c69 100644 --- a/adze/shapes.py +++ b/adze/shapes.py @@ -1,4 +1,3 @@ -from typing import List import numpy from numpy.linalg import norm from numpy.typing import ArrayLike @@ -67,7 +66,7 @@ def build_box(box_corners: ArrayLike) -> poly_t: } -def planes_from_box(box_corners: ArrayLike) -> List[plane_t]: +def planes_from_box(box_corners: ArrayLike) -> list[plane_t]: ''' Construct a list of planes corresponding to the faces of a box. @@ -91,7 +90,7 @@ def planes_from_box(box_corners: ArrayLike) -> List[plane_t]: return [(numpy.array(n), d) for n, d in faces] -def planes_from_tetrahedron(vertices: ArrayLike) -> List[plane_t]: +def planes_from_tetrahedron(vertices: ArrayLike) -> list[plane_t]: ''' Construct a list of planes corresponding to the faces of a tetrahedron. diff --git a/adze/split.py b/adze/split.py index 8e07b1a..b43310f 100644 --- a/adze/split.py +++ b/adze/split.py @@ -1,4 +1,3 @@ -from typing import Tuple, Union import numpy from . import poly_t, plane_t @@ -23,7 +22,7 @@ def clip(poly: poly_t, plane: plane_t) -> poly_t: return left_poly -def split(poly: poly_t, plane: plane_t) -> Tuple[poly_t, poly_t]: +def split(poly: poly_t, plane: plane_t) -> tuple[poly_t, poly_t]: ''' Clip a polyhedron with a plane, keeping both parts. diff --git a/adze/test/test_voxel.py b/adze/test/test_voxel.py index f433f28..ead5b04 100644 --- a/adze/test/test_voxel.py +++ b/adze/test/test_voxel.py @@ -1,10 +1,12 @@ +from typing import TYPE_CHECKING import logging +import warnings +from time import perf_counter import pytest from pytest import approx import numpy from numpy.linalg import norm from numpy.testing import assert_allclose, assert_array_equal -from time import perf_counter import adze import adze.util @@ -12,11 +14,14 @@ import adze.util from .utils import TOL_FAIL, TOL_WARN, PRNG, MIN_VOLUME, flip_plane, tetrahedron_volume from .utils import build_rand_tetrahedron, gen_plane_through_centroid, plane_generators, gen_torus_brep, rand_tetrahedron_vertices +if TYPE_CHECKING: + import matplotlib + logger = logging.getLogger(__name__) -def test_voxelization(poly_order, grid_resolution=None): +def test_voxelization(poly_order: int, grid_resolution: int | None = None) -> None: """ Test that the voxelized moments sum to those of the original input """ @@ -42,11 +47,13 @@ def test_voxelization(poly_order, grid_resolution=None): warnings.warn('Moments failed tolerance in voxels') -def visualize_isosurface(grid, - edges, - level: float = None, - show_edges: bool = True, - finalize: bool = True): +def visualize_isosurface( + grid, + edges, + level: float | None = None, + show_edges: bool = True, + finalize: bool = True, + ) -> tuple['matplotlib.Figure', 'matplotlib.Axes']: """ Draw an isosurface plot of the device. diff --git a/adze/transform.py b/adze/transform.py index 453494b..6150d80 100644 --- a/adze/transform.py +++ b/adze/transform.py @@ -1,4 +1,3 @@ -from typing import Union import numpy from numpy import sin, cos from numpy.typing import ArrayLike @@ -43,7 +42,7 @@ def translate(poly: poly_t, shift: ArrayLike) -> poly_t: return poly -def scale(poly: poly_t, factor: Union[ArrayLike, float]) -> poly_t: +def scale(poly: poly_t, factor: ArrayLike | float) -> poly_t: ''' Scale the polyhedron. Can scale by a single value, or scale each axis individually diff --git a/adze/util.py b/adze/util.py index a888c58..15862c4 100644 --- a/adze/util.py +++ b/adze/util.py @@ -1,10 +1,9 @@ -from typing import Dict, Tuple, List import numpy from numpy.typing import ArrayLike, NDArray -plane_t = Tuple[NDArray, float] -poly_t = Dict[str, NDArray] +plane_t = tuple[NDArray, float] +poly_t = dict[str, NDArray] def build_empty() -> poly_t: @@ -20,8 +19,8 @@ def build_empty() -> poly_t: } -def find_components(neighbors: NDArray[numpy.int_]) -> NDArray[numpy.int_]: - ''' +def find_components(neighbors: NDArray[numpy.integer]) -> NDArray[numpy.integer]: + """ Given an adjacency list, flood-fill to find and label connected components. Args: @@ -32,7 +31,7 @@ def find_components(neighbors: NDArray[numpy.int_]) -> NDArray[numpy.int_]: Returns: 1D ndarray with N entries. Entries are integer labels denoting which connected component the vertex belongs to. - ''' + """ num_vertices = neighbors.shape[0] current_component = 1 component = numpy.zeros(num_vertices, dtype=int) @@ -50,8 +49,8 @@ def find_components(neighbors: NDArray[numpy.int_]) -> NDArray[numpy.int_]: return component - 1 -def find_faces(neighbors: NDArray[numpy.int_]) -> List[NDArray[numpy.int_]]: - ''' +def find_faces(neighbors: NDArray[numpy.integer]) -> list[NDArray[numpy.integer]]: + """ Traverse the Schlegel diagram to find chordless cycles (i.e., faces of the polyhedron). Assumes 3-vertex-connected graph with exactly 3 neighbors per vertex. Neighbors are ordered @@ -64,7 +63,7 @@ def find_faces(neighbors: NDArray[numpy.int_]) -> List[NDArray[numpy.int_]]: Returns: List of ndarrays, each of which contains the indices in a given face, starting with the lowest index in the face. - ''' + """ num_verts = neighbors.shape[0] edge_id = numpy.arange(3 * num_verts) @@ -72,10 +71,11 @@ def find_faces(neighbors: NDArray[numpy.int_]) -> List[NDArray[numpy.int_]]: v_current = v_start.copy() v_next = neighbors[v_current, nxt_edge] - ''' + """ Choose a method for storing the face info based on expected runtime vs unnecessary memory allocation - ''' + """ + faces: list[list[NDArray[numpy.integer]]] | NDArray[numpy.integer] if num_verts < 1000: # ~3n^2 in memory, but much faster in cases where n isn't prohibitively large faces = numpy.full((3*num_verts, num_verts-1), -1, dtype=int) diff --git a/adze/voxel.py b/adze/voxel.py index 38851e7..3e1e8fa 100644 --- a/adze/voxel.py +++ b/adze/voxel.py @@ -1,4 +1,4 @@ -from typing import Tuple, List +from collections.abc import Sequence import numpy from numpy.typing import ArrayLike, NDArray @@ -10,7 +10,7 @@ from .shapes import planes_from_box def voxelize( poly: poly_t, - planes: List[ArrayLike], + planes: Sequence[ArrayLike], include_outside: bool = False, order: int = 0, ) -> NDArray[numpy.float64]: @@ -41,7 +41,7 @@ def voxelize( correspond to portions of the polyhedron that protruded outside the outermost planes. N is `number_of_moments(order)`. ''' - aplanes = [numpy.array(p, copy=False) for p in planes] + aplanes = [numpy.asarray(p) for p in planes] num_planes = numpy.array([e.size for e in aplanes], dtype=int) if (num_planes < 2).any():