From 08b62426ea7e936d93bb4a0657228206f1c32829 Mon Sep 17 00:00:00 2001 From: Jen Bradley <55467578+janbridley@users.noreply.github.com> Date: Mon, 16 Sep 2024 14:56:34 -0400 Subject: [PATCH] Remove or handle warnings raised by test functions (#241) * Remove depretated calls in conftest * Add pytest.warns for backward compatibility tests * Add nonzero minimum for setter tests * Fix quaternion generation in test_polygon.py --- tests/conftest.py | 28 +++++++++++----- tests/test_polygon.py | 5 ++- tests/test_polyhedron.py | 6 ++-- tests/test_shape_families.py | 64 ++++++++++++++++++++---------------- tests/test_shape_getters.py | 9 ++++- 5 files changed, 68 insertions(+), 44 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 9c126d3d..e20b56d9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -15,12 +15,16 @@ CatalanFamily, JohnsonFamily, PlatonicFamily, - PrismAntiprismFamily, - PyramidDipyramidFamily, RegularNGonFamily, + UniformAntiprismFamily, + UniformDipyramidFamily, + UniformPrismFamily, + UniformPyramidFamily, ) from coxeter.shapes import ConvexPolyhedron, ConvexSpheropolyhedron, Polyhedron, Shape2D +MAX_INFINITE_FAMILY_N = 11 + # Define a function to combine marks in order to more compactly test shape families def combine_marks(*marks): @@ -186,7 +190,7 @@ def assert_distance_to_surface_2d(shape, angles, computed_distance): def quaternion_from_axis_angle(x, y, z, theta): """Generate a quaternion from axis [x, y, z] and angle theta.""" - if x == y == z == 0: + if np.allclose([x, y, z], 0): return np.array([1, 0, 0, 0]) axis = np.array([x, y, z]) axis /= np.linalg.norm(axis) @@ -284,22 +288,28 @@ def johnson_solids(): ids=_johnson_shape_names, ) -_prismantiprism_shape_names = PrismAntiprismFamily.data.keys() named_prismantiprism_mark = pytest.mark.parametrize( argnames="poly", argvalues=[ - PrismAntiprismFamily.get_shape(name) for name in _prismantiprism_shape_names + *[UniformPrismFamily.get_shape(n) for n in range(3, MAX_INFINITE_FAMILY_N)], + *[UniformAntiprismFamily.get_shape(n) for n in range(3, MAX_INFINITE_FAMILY_N)], + ], + ids=[ + *[f"prism-{n}" for n in range(3, MAX_INFINITE_FAMILY_N)], + *[f"antiprism-{n}" for n in range(3, MAX_INFINITE_FAMILY_N)], ], - ids=_prismantiprism_shape_names, ) -_pyramiddipyramid_shape_names = PyramidDipyramidFamily.data.keys() named_pyramiddipyramid_mark = pytest.mark.parametrize( argnames="poly", argvalues=[ - PyramidDipyramidFamily.get_shape(name) for name in _pyramiddipyramid_shape_names + *[UniformPyramidFamily.get_shape(n) for n in range(3, 6)], + *[UniformDipyramidFamily.get_shape(n) for n in range(3, 6)], + ], + ids=[ + *[f"pyramid-{n}" for n in range(3, 6)], + *[f"dipyramid-{n}" for n in range(3, 6)], ], - ids=_pyramiddipyramid_shape_names, ) diff --git a/tests/test_polygon.py b/tests/test_polygon.py index 2e057930..b24aa493 100644 --- a/tests/test_polygon.py +++ b/tests/test_polygon.py @@ -6,7 +6,7 @@ import rowan from hypothesis import assume, example, given, settings from hypothesis.extra.numpy import arrays -from hypothesis.strategies import floats +from hypothesis.strategies import builds, floats, integers from pytest import approx from scipy.spatial import ConvexHull @@ -186,10 +186,9 @@ def test_convex_area(points): assert np.isclose(hull.volume, poly.area) -@given(random_quat=arrays(np.float64, (4,), elements=floats(-1, 1, width=64))) +@given(random_quat=builds(lambda i: rowan.random.rand(100)[i], integers(0, 99))) def test_rotation_signed_area(random_quat): """Ensure that rotating does not change the signed area.""" - assume(not np.allclose(random_quat, 0)) random_quat = rowan.normalize(random_quat) rotated_points = rowan.rotate(random_quat, get_square_points()) poly = Polygon(rotated_points) diff --git a/tests/test_polyhedron.py b/tests/test_polyhedron.py index de59f408..d0d3663a 100644 --- a/tests/test_polyhedron.py +++ b/tests/test_polyhedron.py @@ -31,6 +31,8 @@ from coxeter.shapes.utils import rotate_order2_tensor, translate_inertia_tensor from utils import compute_centroid_mc, compute_inertia_mc +MIN_REALISTIC_PROPERTY = 2e-16 + def test_normal_detection(convex_cube): detected_normals = [tuple(n) for n in convex_cube.normals] @@ -155,7 +157,7 @@ def test_volume_damasceno_shapes(shape): @settings(max_examples=10 if is_not_ci() else 50) @named_damasceno_shapes_mark -@given(v_test=floats(0, 10, exclude_min=True)) +@given(v_test=floats(MIN_REALISTIC_PROPERTY, 10, exclude_min=True)) def test_set_volume_damasceno_shapes(shape, v_test): if shape["name"] in ("RESERVED", "Sphere"): return @@ -179,7 +181,7 @@ def test_surface_area_damasceno_shapes(shape): @settings(max_examples=10 if is_not_ci() else 50) @named_damasceno_shapes_mark -@given(a_test=floats(0, 10, exclude_min=True)) +@given(a_test=floats(MIN_REALISTIC_PROPERTY, 10, exclude_min=True)) def test_set_surface_area_damasceno_shapes(shape, a_test): if shape["name"] in ("RESERVED", "Sphere"): return diff --git a/tests/test_shape_families.py b/tests/test_shape_families.py index 22098fcf..cc2db7ce 100644 --- a/tests/test_shape_families.py +++ b/tests/test_shape_families.py @@ -262,35 +262,41 @@ def test_uniform_dipyramids(n): def test_new_prism_antiprism(): - for i, nameshape in enumerate(PrismAntiprismFamily): - name, shape = nameshape - - if "Anti" in name: - n = i + 3 # count + min_n - comparative_shape = UniformAntiprismFamily.get_shape(n) - else: - n = (i + 3) - 8 # count + min_n + n_prism - comparative_shape = UniformPrismFamily.get_shape(n) - - np.testing.assert_allclose(shape.volume, comparative_shape.volume, atol=ATOL) - np.testing.assert_allclose( - shape.edge_lengths, comparative_shape.edge_lengths, atol=ATOL - ) + with pytest.warns(DeprecationWarning, match="deprecated in favor of"): + for i, nameshape in enumerate(PrismAntiprismFamily): + name, shape = nameshape + + if "Anti" in name: + n = i + 3 # count + min_n + comparative_shape = UniformAntiprismFamily.get_shape(n) + else: + n = (i + 3) - 8 # count + min_n + n_prism + comparative_shape = UniformPrismFamily.get_shape(n) + + np.testing.assert_allclose( + shape.volume, comparative_shape.volume, atol=ATOL + ) + np.testing.assert_allclose( + shape.edge_lengths, comparative_shape.edge_lengths, atol=ATOL + ) def test_new_pyramid_dipyramid(): - for i, nameshape in enumerate(PyramidDipyramidFamily): - name, shape = nameshape - - if "Di" in name: - n = i + 3 # count + min_n - comparative_shape = UniformDipyramidFamily.get_shape(n) - else: - n = (i + 3) - 3 # count + min_n + n_pyramid - comparative_shape = UniformPyramidFamily.get_shape(n) - - np.testing.assert_allclose(comparative_shape.centroid, 0.0, atol=ATOL) - np.testing.assert_allclose(shape.volume, comparative_shape.volume, atol=ATOL) - np.testing.assert_allclose( - shape.edge_lengths, comparative_shape.edge_lengths, atol=ATOL - ) + with pytest.warns(DeprecationWarning, match="deprecated in favor of"): + for i, nameshape in enumerate(PyramidDipyramidFamily): + name, shape = nameshape + + if "Di" in name: + n = i + 3 # count + min_n + comparative_shape = UniformDipyramidFamily.get_shape(n) + else: + n = (i + 3) - 3 # count + min_n + n_pyramid + comparative_shape = UniformPyramidFamily.get_shape(n) + + np.testing.assert_allclose(comparative_shape.centroid, 0.0, atol=ATOL) + np.testing.assert_allclose( + shape.volume, comparative_shape.volume, atol=ATOL + ) + np.testing.assert_allclose( + shape.edge_lengths, comparative_shape.edge_lengths, atol=ATOL + ) diff --git a/tests/test_shape_getters.py b/tests/test_shape_getters.py index 95ff4707..078f9d93 100644 --- a/tests/test_shape_getters.py +++ b/tests/test_shape_getters.py @@ -4,6 +4,7 @@ import json import numpy as np +import pytest from pytest import approx from conftest import data_filenames_mark @@ -117,6 +118,12 @@ def test_json_data_families(family): # Extract the stored shape keys shapes = list(fam_data.keys()) for shape in shapes: - module_vertices = module.get_shape(shape).vertices + if "pyramid" in family or "prism" in family: + with pytest.warns( + DeprecationWarning, match="deprecated in favor of" + ): + module_vertices = module.get_shape(shape).vertices + else: + module_vertices = module.get_shape(shape).vertices json_vertices = fam_data[shape]["vertices"] assert np.all(module_vertices == json_vertices)