Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove or handle warnings raised by test functions #241

Merged
merged 5 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
)


Expand Down
5 changes: 2 additions & 3 deletions tests/test_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions tests/test_polyhedron.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
64 changes: 35 additions & 29 deletions tests/test_shape_families.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
9 changes: 8 additions & 1 deletion tests/test_shape_getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json

import numpy as np
import pytest
from pytest import approx

from conftest import data_filenames_mark
Expand Down Expand Up @@ -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)