Skip to content

Commit 2f7c9d6

Browse files
committed
type hint fixes for adaptive/learner/triangulation.py
1 parent 91407da commit 2f7c9d6

File tree

1 file changed

+29
-55
lines changed

1 file changed

+29
-55
lines changed

adaptive/learner/triangulation.py

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
from collections.abc import Iterable, Sized
44
from itertools import chain, combinations
55
from math import factorial
6-
from typing import Any, Iterator, List, Optional, Set, Tuple, Union
6+
from typing import Any, Iterator, List, Optional, Sequence, Set, Tuple, Union
77

88
import numpy as np
99
import scipy.spatial
10-
from numpy import int32
1110

11+
Simplex = Tuple[int, ...] # XXX: check if this is correct
1212

13-
def fast_norm(v: Union[Tuple[float, float, float], np.ndarray]) -> float:
13+
14+
def fast_norm(v: Union[Tuple[float, ...], np.ndarray]) -> float:
1415
# notice this method can be even more optimised
1516
if len(v) == 2:
1617
return math.sqrt(v[0] * v[0] + v[1] * v[1])
@@ -20,12 +21,8 @@ def fast_norm(v: Union[Tuple[float, float, float], np.ndarray]) -> float:
2021

2122

2223
def fast_2d_point_in_simplex(
23-
point: Union[Tuple[int, int], Tuple[float, float], Tuple[float, float]],
24-
simplex: Union[
25-
List[Union[Tuple[int, int], Tuple[float, float]]],
26-
List[Tuple[float, float]],
27-
np.ndarray,
28-
],
24+
point: Tuple[float, ...],
25+
simplex: Union[List[Tuple[float, ...]], np.ndarray],
2926
eps: float = 1e-8,
3027
) -> Union[bool, np.bool_]:
3128
(p0x, p0y), (p1x, p1y), (p2x, p2y) = simplex
@@ -42,7 +39,7 @@ def fast_2d_point_in_simplex(
4239

4340

4441
def point_in_simplex(
45-
point: Any, simplex: Any, eps: float = 1e-8
42+
point: Any, simplex: Simplex, eps: float = 1e-8
4643
) -> Union[bool, np.bool_]:
4744
if len(point) == 2:
4845
return fast_2d_point_in_simplex(point, simplex, eps)
@@ -143,13 +140,7 @@ def fast_det(matrix: np.ndarray) -> float:
143140
return np.linalg.det(matrix)
144141

145142

146-
def circumsphere(
147-
pts: np.ndarray,
148-
) -> Union[
149-
Tuple[Tuple[float, float, float, float], float],
150-
Tuple[Tuple[float, float], float],
151-
Tuple[Tuple[float, float, float], float],
152-
]:
143+
def circumsphere(pts: np.ndarray,) -> Tuple[Tuple[float, ...], float]:
153144
dim = len(pts) - 1
154145
if dim == 2:
155146
return fast_2d_circumcircle(pts)
@@ -175,7 +166,7 @@ def circumsphere(
175166
return tuple(center), radius
176167

177168

178-
def orientation(face: Any, origin: Any) -> Union[int, float]:
169+
def orientation(face: np.ndarray, origin: np.ndarray) -> int:
179170
"""Compute the orientation of the face with respect to a point, origin.
180171
181172
Parameters
@@ -205,14 +196,7 @@ def is_iterable_and_sized(obj: Any) -> bool:
205196
return isinstance(obj, Iterable) and isinstance(obj, Sized)
206197

207198

208-
def simplex_volume_in_embedding(
209-
vertices: Union[
210-
List[Tuple[float, float, float]],
211-
List[Tuple[float, float, float]],
212-
List[Tuple[float, float, float, float]],
213-
List[Tuple[float, float, float, float, float]],
214-
]
215-
) -> float:
199+
def simplex_volume_in_embedding(vertices: List[Tuple[float, ...]]) -> float:
216200
"""Calculate the volume of a simplex in a higher dimensional embedding.
217201
That is: dim > len(vertices) - 1. For example if you would like to know the
218202
surface area of a triangle in a 3d space.
@@ -293,7 +277,7 @@ class Triangulation:
293277
or more simplices in the
294278
"""
295279

296-
def __init__(self, coords: Any) -> None:
280+
def __init__(self, coords: np.ndarray) -> None:
297281
if not is_iterable_and_sized(coords):
298282
raise TypeError("Please provide a 2-dimensional list of points")
299283
coords = list(coords)
@@ -332,27 +316,29 @@ def __init__(self, coords: Any) -> None:
332316
for simplex in initial_tri.simplices:
333317
self.add_simplex(simplex)
334318

335-
def delete_simplex(self, simplex: Any) -> None:
319+
def delete_simplex(self, simplex: Simplex) -> None:
336320
simplex = tuple(sorted(simplex))
337321
self.simplices.remove(simplex)
338322
for vertex in simplex:
339323
self.vertex_to_simplices[vertex].remove(simplex)
340324

341-
def add_simplex(self, simplex: Any) -> None:
325+
def add_simplex(self, simplex: Simplex) -> None:
342326
simplex = tuple(sorted(simplex))
343327
self.simplices.add(simplex)
344328
for vertex in simplex:
345329
self.vertex_to_simplices[vertex].add(simplex)
346330

347-
def get_vertices(self, indices: Any) -> Any:
331+
def get_vertices(self, indices: Sequence[int]) -> Any:
348332
return [self.get_vertex(i) for i in indices]
349333

350-
def get_vertex(self, index: Optional[Union[int32, int]]) -> Any:
334+
def get_vertex(self, index: Optional[int]) -> Any:
351335
if index is None:
352336
return None
353337
return self.vertices[index]
354338

355-
def get_reduced_simplex(self, point: Any, simplex: Any, eps: float = 1e-8) -> list:
339+
def get_reduced_simplex(
340+
self, point: Any, simplex: Simplex, eps: float = 1e-8
341+
) -> list:
356342
"""Check whether vertex lies within a simplex.
357343
358344
Returns
@@ -378,7 +364,7 @@ def get_reduced_simplex(self, point: Any, simplex: Any, eps: float = 1e-8) -> li
378364
return [simplex[i] for i in result]
379365

380366
def point_in_simplex(
381-
self, point: Any, simplex: Any, eps: float = 1e-8
367+
self, point: Any, simplex: Simplex, eps: float = 1e-8
382368
) -> Union[bool, np.bool_]:
383369
vertices = self.get_vertices(simplex)
384370
return point_in_simplex(point, vertices, eps)
@@ -466,12 +452,8 @@ def _extend_hull(self, new_vertex: Any, eps: float = 1e-8) -> Any:
466452
return new_simplices
467453

468454
def circumscribed_circle(
469-
self, simplex: Any, transform: np.ndarray
470-
) -> Union[
471-
Tuple[Tuple[float, float, float, float], float],
472-
Tuple[Tuple[float, float], float],
473-
Tuple[Tuple[float, float, float], float],
474-
]:
455+
self, simplex: Simplex, transform: np.ndarray
456+
) -> Tuple[Tuple[float, ...], float]:
475457
"""Compute the center and radius of the circumscribed circle of a simplex.
476458
477459
Parameters
@@ -488,7 +470,7 @@ def circumscribed_circle(
488470
return circumsphere(pts)
489471

490472
def point_in_cicumcircle(
491-
self, pt_index: int, simplex: Any, transform: np.ndarray
473+
self, pt_index: int, simplex: Simplex, transform: np.ndarray
492474
) -> np.bool_:
493475
# return self.fast_point_in_circumcircle(pt_index, simplex, transform)
494476
eps = 1e-8
@@ -567,10 +549,10 @@ def bowyer_watson(
567549
new_triangles = self.vertex_to_simplices[pt_index]
568550
return bad_triangles - new_triangles, new_triangles - bad_triangles
569551

570-
def _simplex_is_almost_flat(self, simplex: Any) -> np.bool_:
552+
def _simplex_is_almost_flat(self, simplex: Simplex) -> np.bool_:
571553
return self._relative_volume(simplex) < 1e-8
572554

573-
def _relative_volume(self, simplex: Any) -> float:
555+
def _relative_volume(self, simplex: Simplex) -> float:
574556
"""Compute the volume of a simplex divided by the average (Manhattan)
575557
distance of its vertices. The advantage of this is that the relative
576558
volume is only dependent on the shape of the simplex and not on the
@@ -635,7 +617,7 @@ def add_point(
635617
self.vertices.append(point)
636618
return self.bowyer_watson(pt_index, actual_simplex, transform)
637619

638-
def volume(self, simplex: Any) -> float:
620+
def volume(self, simplex: Simplex) -> float:
639621
prefactor = np.math.factorial(self.dim)
640622
vertices = np.array(self.get_vertices(simplex))
641623
vectors = vertices[1:] - vertices[0]
@@ -658,10 +640,10 @@ def vertex_invariant(self, vertex):
658640
"""Simplices originating from a vertex don't overlap."""
659641
raise NotImplementedError
660642

661-
def get_neighbors_from_vertices(self, simplex: Any) -> Any:
643+
def get_neighbors_from_vertices(self, simplex: Simplex) -> Any:
662644
return set.union(*[self.vertex_to_simplices[p] for p in simplex])
663645

664-
def get_face_sharing_neighbors(self, neighbors: Any, simplex: Any) -> Any:
646+
def get_face_sharing_neighbors(self, neighbors: Any, simplex: Simplex) -> Any:
665647
"""Keep only the simplices sharing a whole face with simplex."""
666648
return {
667649
simpl for simpl in neighbors if len(set(simpl) & set(simplex)) == self.dim
@@ -672,15 +654,7 @@ def get_simplices_attached_to_points(self, indices: Any) -> Any:
672654
neighbors = self.get_neighbors_from_vertices(indices)
673655
return self.get_face_sharing_neighbors(neighbors, indices)
674656

675-
def get_opposing_vertices(
676-
self,
677-
simplex: Union[
678-
Tuple[int32, int, int],
679-
Tuple[int32, int32, int],
680-
Tuple[int32, int32, int32],
681-
Tuple[int, int, int],
682-
],
683-
) -> Any:
657+
def get_opposing_vertices(self, simplex: Simplex,) -> Any:
684658
if simplex not in self.simplices:
685659
raise ValueError("Provided simplex is not part of the triangulation")
686660
neighbors = self.get_simplices_attached_to_points(simplex)
@@ -698,7 +672,7 @@ def find_opposing_vertex(vertex):
698672
return result
699673

700674
@property
701-
def hull(self) -> Union[Set[int32], Set[int], Set[Union[int32, int]]]:
675+
def hull(self) -> Set[int]:
702676
"""Compute hull from triangulation.
703677
704678
Parameters

0 commit comments

Comments
 (0)