type annotation improvements
This commit is contained in:
parent
f38e27d33d
commit
6f5335dcc1
8 changed files with 38 additions and 35 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
from typing import List, Dict, Any
|
from typing import Any
|
||||||
import numpy
|
import numpy
|
||||||
from numpy.linalg import norm
|
from numpy.linalg import norm
|
||||||
from numpy.typing import ArrayLike, NDArray
|
from numpy.typing import ArrayLike, NDArray
|
||||||
|
|
@ -9,8 +9,8 @@ from .util import _get_maps, find_components, find_faces
|
||||||
|
|
||||||
def planes_from_boundary(
|
def planes_from_boundary(
|
||||||
vertices: ArrayLike,
|
vertices: ArrayLike,
|
||||||
faces: List[ArrayLike]
|
faces: list[ArrayLike]
|
||||||
) -> List[plane_t]:
|
) -> list[plane_t]:
|
||||||
'''
|
'''
|
||||||
Generate a list of planes (normal, signed distance) from the faces of a boundary-
|
Generate a list of planes (normal, signed distance) from the faces of a boundary-
|
||||||
representation polyhedron.
|
representation polyhedron.
|
||||||
|
|
@ -38,7 +38,7 @@ def planes_from_boundary(
|
||||||
return planes
|
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
|
Generate a 3-verted-connected Schlegel diagram representation
|
||||||
corresponding to the provided boundary-representation polyhedron.
|
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}
|
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
|
Generate a boundary represeentation corresponding to the provided
|
||||||
3-vertex-connected Schlegel diagram.
|
3-vertex-connected Schlegel diagram.
|
||||||
|
|
@ -279,7 +279,7 @@ def boundary_from_poly(poly: poly_t) -> List[Dict[str, Any]]:
|
||||||
faces = find_faces(neighbors)
|
faces = find_faces(neighbors)
|
||||||
num_components = owning_component.max() + 1
|
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:
|
for face in faces:
|
||||||
component_ind = owning_component[face[0]]
|
component_ind = owning_component[face[0]]
|
||||||
components[component_ind].append(face)
|
components[component_ind].append(face)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
from typing import Dict, Tuple
|
|
||||||
import numpy
|
import numpy
|
||||||
from numpy.typing import NDArray
|
from numpy.typing import NDArray
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
from typing import List
|
|
||||||
import numpy
|
import numpy
|
||||||
from numpy.linalg import norm
|
from numpy.linalg import norm
|
||||||
from numpy.typing import ArrayLike
|
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.
|
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]
|
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.
|
Construct a list of planes corresponding to the faces of a tetrahedron.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
from typing import Tuple, Union
|
|
||||||
import numpy
|
import numpy
|
||||||
|
|
||||||
from . import poly_t, plane_t
|
from . import poly_t, plane_t
|
||||||
|
|
@ -23,7 +22,7 @@ def clip(poly: poly_t, plane: plane_t) -> poly_t:
|
||||||
return left_poly
|
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.
|
Clip a polyhedron with a plane, keeping both parts.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,12 @@
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
import logging
|
import logging
|
||||||
|
import warnings
|
||||||
|
from time import perf_counter
|
||||||
import pytest
|
import pytest
|
||||||
from pytest import approx
|
from pytest import approx
|
||||||
import numpy
|
import numpy
|
||||||
from numpy.linalg import norm
|
from numpy.linalg import norm
|
||||||
from numpy.testing import assert_allclose, assert_array_equal
|
from numpy.testing import assert_allclose, assert_array_equal
|
||||||
from time import perf_counter
|
|
||||||
|
|
||||||
import adze
|
import adze
|
||||||
import adze.util
|
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 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
|
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__)
|
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
|
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')
|
warnings.warn('Moments failed tolerance in voxels')
|
||||||
|
|
||||||
|
|
||||||
def visualize_isosurface(grid,
|
def visualize_isosurface(
|
||||||
|
grid,
|
||||||
edges,
|
edges,
|
||||||
level: float = None,
|
level: float | None = None,
|
||||||
show_edges: bool = True,
|
show_edges: bool = True,
|
||||||
finalize: bool = True):
|
finalize: bool = True,
|
||||||
|
) -> tuple['matplotlib.Figure', 'matplotlib.Axes']:
|
||||||
"""
|
"""
|
||||||
Draw an isosurface plot of the device.
|
Draw an isosurface plot of the device.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
from typing import Union
|
|
||||||
import numpy
|
import numpy
|
||||||
from numpy import sin, cos
|
from numpy import sin, cos
|
||||||
from numpy.typing import ArrayLike
|
from numpy.typing import ArrayLike
|
||||||
|
|
@ -43,7 +42,7 @@ def translate(poly: poly_t, shift: ArrayLike) -> poly_t:
|
||||||
return poly
|
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.
|
Scale the polyhedron.
|
||||||
Can scale by a single value, or scale each axis individually
|
Can scale by a single value, or scale each axis individually
|
||||||
|
|
|
||||||
22
adze/util.py
22
adze/util.py
|
|
@ -1,10 +1,9 @@
|
||||||
from typing import Dict, Tuple, List
|
|
||||||
import numpy
|
import numpy
|
||||||
from numpy.typing import ArrayLike, NDArray
|
from numpy.typing import ArrayLike, NDArray
|
||||||
|
|
||||||
|
|
||||||
plane_t = Tuple[NDArray, float]
|
plane_t = tuple[NDArray, float]
|
||||||
poly_t = Dict[str, NDArray]
|
poly_t = dict[str, NDArray]
|
||||||
|
|
||||||
|
|
||||||
def build_empty() -> poly_t:
|
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.
|
Given an adjacency list, flood-fill to find and label connected components.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -32,7 +31,7 @@ def find_components(neighbors: NDArray[numpy.int_]) -> NDArray[numpy.int_]:
|
||||||
Returns:
|
Returns:
|
||||||
1D ndarray with N entries. Entries are integer labels denoting
|
1D ndarray with N entries. Entries are integer labels denoting
|
||||||
which connected component the vertex belongs to.
|
which connected component the vertex belongs to.
|
||||||
'''
|
"""
|
||||||
num_vertices = neighbors.shape[0]
|
num_vertices = neighbors.shape[0]
|
||||||
current_component = 1
|
current_component = 1
|
||||||
component = numpy.zeros(num_vertices, dtype=int)
|
component = numpy.zeros(num_vertices, dtype=int)
|
||||||
|
|
@ -50,8 +49,8 @@ def find_components(neighbors: NDArray[numpy.int_]) -> NDArray[numpy.int_]:
|
||||||
return component - 1
|
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
|
Traverse the Schlegel diagram to find chordless cycles
|
||||||
(i.e., faces of the polyhedron). Assumes 3-vertex-connected
|
(i.e., faces of the polyhedron). Assumes 3-vertex-connected
|
||||||
graph with exactly 3 neighbors per vertex. Neighbors are ordered
|
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:
|
Returns:
|
||||||
List of ndarrays, each of which contains the indices in a given
|
List of ndarrays, each of which contains the indices in a given
|
||||||
face, starting with the lowest index in the face.
|
face, starting with the lowest index in the face.
|
||||||
'''
|
"""
|
||||||
num_verts = neighbors.shape[0]
|
num_verts = neighbors.shape[0]
|
||||||
|
|
||||||
edge_id = numpy.arange(3 * num_verts)
|
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_current = v_start.copy()
|
||||||
v_next = neighbors[v_current, nxt_edge]
|
v_next = neighbors[v_current, nxt_edge]
|
||||||
|
|
||||||
'''
|
"""
|
||||||
Choose a method for storing the face info based on expected
|
Choose a method for storing the face info based on expected
|
||||||
runtime vs unnecessary memory allocation
|
runtime vs unnecessary memory allocation
|
||||||
'''
|
"""
|
||||||
|
faces: list[list[NDArray[numpy.integer]]] | NDArray[numpy.integer]
|
||||||
if num_verts < 1000:
|
if num_verts < 1000:
|
||||||
# ~3n^2 in memory, but much faster in cases where n isn't prohibitively large
|
# ~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)
|
faces = numpy.full((3*num_verts, num_verts-1), -1, dtype=int)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
from typing import Tuple, List
|
from collections.abc import Sequence
|
||||||
import numpy
|
import numpy
|
||||||
from numpy.typing import ArrayLike, NDArray
|
from numpy.typing import ArrayLike, NDArray
|
||||||
|
|
||||||
|
|
@ -10,7 +10,7 @@ from .shapes import planes_from_box
|
||||||
|
|
||||||
def voxelize(
|
def voxelize(
|
||||||
poly: poly_t,
|
poly: poly_t,
|
||||||
planes: List[ArrayLike],
|
planes: Sequence[ArrayLike],
|
||||||
include_outside: bool = False,
|
include_outside: bool = False,
|
||||||
order: int = 0,
|
order: int = 0,
|
||||||
) -> NDArray[numpy.float64]:
|
) -> NDArray[numpy.float64]:
|
||||||
|
|
@ -41,7 +41,7 @@ def voxelize(
|
||||||
correspond to portions of the polyhedron that protruded outside the outermost
|
correspond to portions of the polyhedron that protruded outside the outermost
|
||||||
planes. N is `number_of_moments(order)`.
|
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)
|
num_planes = numpy.array([e.size for e in aplanes], dtype=int)
|
||||||
if (num_planes < 2).any():
|
if (num_planes < 2).any():
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue