11import itertools
22from collections import Counter
33from math import factorial
4+ from typing import List , Optional , Tuple , Union
45
56import numpy as np
67import pytest
8+ from numpy import ndarray
79
810from adaptive .learner .triangulation import Triangulation
911
1012with_dimension = pytest .mark .parametrize ("dim" , [2 , 3 , 4 ])
1113
1214
13- def _make_triangulation (points ) :
15+ def _make_triangulation (points : ndarray ) -> Triangulation :
1416 num_vertices = points .shape [1 ] + 1
1517 first_simplex , points = points [:num_vertices ], points [num_vertices :]
1618 t = Triangulation (first_simplex )
@@ -19,16 +21,16 @@ def _make_triangulation(points):
1921 return t
2022
2123
22- def _make_standard_simplex (dim ) :
24+ def _make_standard_simplex (dim : int ) -> ndarray :
2325 """Return the vertices of the standard simplex in dimension 'dim'."""
2426 return np .vstack ((np .zeros (dim ), np .eye (dim )))
2527
2628
27- def _standard_simplex_volume (dim ) :
29+ def _standard_simplex_volume (dim : int ) -> float :
2830 return 1 / factorial (dim )
2931
3032
31- def _check_simplices_are_valid (t ) :
33+ def _check_simplices_are_valid (t : Triangulation ) -> None :
3234 """Check that 'simplices' and 'vertex_to_simplices' are consistent."""
3335 vertex_to_simplices = [set () for _ in t .vertices ]
3436
@@ -38,26 +40,42 @@ def _check_simplices_are_valid(t):
3840 assert vertex_to_simplices == t .vertex_to_simplices
3941
4042
41- def _check_faces_are_valid (t ) :
43+ def _check_faces_are_valid (t : Triangulation ) -> None :
4244 """Check that a 'dim-1'-D face is shared by no more than 2 simplices."""
4345 counts = Counter (t .faces ())
4446 assert not any (i > 2 for i in counts .values ()), counts
4547
4648
47- def _check_hull_is_valid (t ) :
49+ def _check_hull_is_valid (t : Triangulation ) -> None :
4850 """Check that the stored hull is consistent with one computed from scratch."""
4951 counts = Counter (t .faces ())
5052 hull = {point for face , count in counts .items () if count == 1 for point in face }
5153 assert t .hull == hull
5254
5355
54- def _check_triangulation_is_valid (t ) :
56+ def _check_triangulation_is_valid (t : Triangulation ) -> None :
5557 _check_simplices_are_valid (t )
5658 _check_faces_are_valid (t )
5759 _check_hull_is_valid (t )
5860
5961
60- def _add_point_with_check (tri , point , simplex = None ):
62+ def _add_point_with_check (
63+ tri : Triangulation ,
64+ point : Union [
65+ ndarray ,
66+ Tuple [float , float , float , float ],
67+ Tuple [float , float ],
68+ List [int ],
69+ Tuple [float , float , float ],
70+ ],
71+ simplex : Optional [
72+ Union [
73+ Tuple [int , int , int , int ],
74+ Tuple [int , int , int ],
75+ Tuple [int , int , int , int , int ],
76+ ]
77+ ] = None ,
78+ ) -> None :
6179 """Check that the difference in simplices before and after adding a point
6280 is returned by tri.add_point"""
6381 old_simplices = tri .simplices .copy ()
@@ -68,15 +86,15 @@ def _add_point_with_check(tri, point, simplex=None):
6886 assert created_simplices == new_simplices - old_simplices
6987
7088
71- def test_triangulation_raises_exception_for_1d_list ():
89+ def test_triangulation_raises_exception_for_1d_list () -> None :
7290 # We could support 1d, but we don't for now, because it is not relevant
7391 # so a user has to be aware
7492 pts = [0 , 1 ]
7593 with pytest .raises (TypeError ):
7694 Triangulation (pts )
7795
7896
79- def test_triangulation_raises_exception_for_1d_points ():
97+ def test_triangulation_raises_exception_for_1d_points () -> None :
8098 # We could support 1d, but we don't for now, because it is not relevant
8199 # so a user has to be aware
82100 pts = [(0 ,), (1 ,)]
@@ -85,7 +103,7 @@ def test_triangulation_raises_exception_for_1d_points():
85103
86104
87105@with_dimension
88- def test_triangulation_of_standard_simplex (dim ) :
106+ def test_triangulation_of_standard_simplex (dim : int ) -> None :
89107 t = Triangulation (_make_standard_simplex (dim ))
90108 expected_simplex = tuple (range (dim + 1 ))
91109 assert t .simplices == {expected_simplex }
@@ -94,7 +112,7 @@ def test_triangulation_of_standard_simplex(dim):
94112
95113
96114@with_dimension
97- def test_zero_volume_initial_simplex_raises_exception (dim ) :
115+ def test_zero_volume_initial_simplex_raises_exception (dim : int ) -> None :
98116 points = _make_standard_simplex (dim )[:- 1 ]
99117 linearly_dependent_point = np .dot (np .random .random (dim ), points )
100118 zero_volume_simplex = np .vstack ((points , linearly_dependent_point ))
@@ -106,7 +124,9 @@ def test_zero_volume_initial_simplex_raises_exception(dim):
106124
107125
108126@with_dimension
109- def test_adding_point_outside_circumscribed_hypersphere_in_positive_orthant (dim ):
127+ def test_adding_point_outside_circumscribed_hypersphere_in_positive_orthant (
128+ dim : int ,
129+ ) -> None :
110130 t = Triangulation (_make_standard_simplex (dim ))
111131
112132 point_outside_circumscribed_sphere = (1.1 ,) * dim
@@ -133,7 +153,7 @@ def test_adding_point_outside_circumscribed_hypersphere_in_positive_orthant(dim)
133153
134154
135155@with_dimension
136- def test_adding_point_outside_standard_simplex_in_negative_orthant (dim ) :
156+ def test_adding_point_outside_standard_simplex_in_negative_orthant (dim : int ) -> None :
137157 t = Triangulation (_make_standard_simplex (dim ))
138158 new_point = list (range (- dim , 0 ))
139159
@@ -168,7 +188,7 @@ def test_adding_point_outside_standard_simplex_in_negative_orthant(dim):
168188
169189@with_dimension
170190@pytest .mark .parametrize ("provide_simplex" , [True , False ])
171- def test_adding_point_inside_standard_simplex (dim , provide_simplex ) :
191+ def test_adding_point_inside_standard_simplex (dim : int , provide_simplex : bool ) -> None :
172192 t = Triangulation (_make_standard_simplex (dim ))
173193 first_simplex = tuple (range (dim + 1 ))
174194 inside_simplex = (0.1 ,) * dim
@@ -192,7 +212,7 @@ def test_adding_point_inside_standard_simplex(dim, provide_simplex):
192212
193213
194214@with_dimension
195- def test_adding_point_on_standard_simplex_face (dim ) :
215+ def test_adding_point_on_standard_simplex_face (dim : int ) -> None :
196216 pts = _make_standard_simplex (dim )
197217 t = Triangulation (pts )
198218 on_simplex = np .average (pts [1 :], axis = 0 )
@@ -213,7 +233,7 @@ def test_adding_point_on_standard_simplex_face(dim):
213233
214234
215235@with_dimension
216- def test_adding_point_on_standard_simplex_edge (dim ) :
236+ def test_adding_point_on_standard_simplex_edge (dim : int ) -> None :
217237 pts = _make_standard_simplex (dim )
218238 t = Triangulation (pts )
219239 on_edge = np .average (pts [:2 ], axis = 0 )
@@ -231,7 +251,7 @@ def test_adding_point_on_standard_simplex_edge(dim):
231251
232252
233253@with_dimension
234- def test_adding_point_colinear_with_first_edge (dim ) :
254+ def test_adding_point_colinear_with_first_edge (dim : int ) -> None :
235255 pts = _make_standard_simplex (dim )
236256 t = Triangulation (pts )
237257 edge_extension = np .multiply (pts [1 ], 2 )
@@ -246,7 +266,7 @@ def test_adding_point_colinear_with_first_edge(dim):
246266
247267
248268@with_dimension
249- def test_adding_point_coplanar_with_a_face (dim ) :
269+ def test_adding_point_coplanar_with_a_face (dim : int ) -> None :
250270 pts = _make_standard_simplex (dim )
251271 t = Triangulation (pts )
252272 face_extension = np .sum (pts [:- 1 ], axis = 0 ) * 2
@@ -261,7 +281,7 @@ def test_adding_point_coplanar_with_a_face(dim):
261281
262282
263283@with_dimension
264- def test_adding_point_inside_circumscribed_circle (dim ) :
284+ def test_adding_point_inside_circumscribed_circle (dim : int ) -> None :
265285 pts = _make_standard_simplex (dim )
266286 t = Triangulation (pts )
267287 on_simplex = (0.6 ,) * dim
@@ -280,7 +300,7 @@ def test_adding_point_inside_circumscribed_circle(dim):
280300
281301
282302@with_dimension
283- def test_triangulation_volume_is_less_than_bounding_box (dim ) :
303+ def test_triangulation_volume_is_less_than_bounding_box (dim : int ) -> None :
284304 eps = 1e-8
285305 points = np .random .random ((10 , dim )) # all within the unit hypercube
286306 t = _make_triangulation (points )
@@ -290,23 +310,23 @@ def test_triangulation_volume_is_less_than_bounding_box(dim):
290310
291311
292312@with_dimension
293- def test_triangulation_is_deterministic (dim ) :
313+ def test_triangulation_is_deterministic (dim : int ) -> None :
294314 points = np .random .random ((10 , dim ))
295315 t1 = _make_triangulation (points )
296316 t2 = _make_triangulation (points )
297317 assert t1 .simplices == t2 .simplices
298318
299319
300320@with_dimension
301- def test_initialisation_raises_when_not_enough_points (dim ) :
321+ def test_initialisation_raises_when_not_enough_points (dim : int ) -> None :
302322 deficient_simplex = _make_standard_simplex (dim )[:- 1 ]
303323
304324 with pytest .raises (ValueError ):
305325 Triangulation (deficient_simplex )
306326
307327
308328@with_dimension
309- def test_initialisation_raises_when_points_coplanar (dim ) :
329+ def test_initialisation_raises_when_points_coplanar (dim : int ) -> None :
310330 zero_volume_simplex = _make_standard_simplex (dim )[:- 1 ]
311331
312332 new_point1 = np .average (zero_volume_simplex , axis = 0 )
@@ -318,7 +338,7 @@ def test_initialisation_raises_when_points_coplanar(dim):
318338
319339
320340@with_dimension
321- def test_initialisation_accepts_more_than_one_simplex (dim ) :
341+ def test_initialisation_accepts_more_than_one_simplex (dim : int ) -> None :
322342 points = _make_standard_simplex (dim )
323343 new_point = [1.1 ] * dim # Point oposing the origin but outside circumsphere
324344 points = np .vstack ((points , new_point ))
0 commit comments