From 0255b74368c1a41afaf1acb14f9d765c4835ea0d Mon Sep 17 00:00:00 2001 From: Miro Hrončok Date: May 11 2018 14:02:16 +0000 Subject: Add tolerance to tests that check float equality Fixes test failure on ppc64le. --- diff --git a/numpy-stl-allclose.patch b/numpy-stl-allclose.patch new file mode 100644 index 0000000..f3468ea --- /dev/null +++ b/numpy-stl-allclose.patch @@ -0,0 +1,212 @@ +From d1c02b07eb8cb3534b91c771e4b158885606bc1c Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= +Date: Fri, 11 May 2018 15:52:47 +0200 +Subject: [PATCH] Use numpy.allclose() in tests to check float arrays equality + +On some platforms, a regular (a == b).all() might not work due to +float32 not being very precise. In one particular case, we also +increase the tolerance for allclose(), because on ppc64le 3 +rotations are enough to divert too much from the mathematically +correct result. + +We cannot increase the precision, because STL has 32bit floats. + +Fixes https://github.com/WoLpH/numpy-stl/issues/78 +--- + tests/test_mesh.py | 47 ++++++++++++++++++++++------------------------- + tests/test_rotate.py | 50 ++++++++++++++++++++++++++------------------------ + 2 files changed, 48 insertions(+), 49 deletions(-) + +diff --git a/tests/test_mesh.py b/tests/test_mesh.py +index 6c9906a..d272126 100644 +--- a/tests/test_mesh.py ++++ b/tests/test_mesh.py +@@ -33,12 +33,9 @@ def test_units_2d(): + mesh = Mesh(data, remove_empty_areas=False) + mesh.update_units() + +- assert (mesh.areas == [.5, .5]).all() +- assert (mesh.normals == [[0, 0, 1.], +- [0, 0, -1.]]).all() +- +- assert (mesh.units == [[0, 0, 1], +- [0, 0, -1]]).all() ++ assert numpy.allclose(mesh.areas, [.5, .5]) ++ assert numpy.allclose(mesh.normals, [[0, 0, 1.], [0, 0, -1.]]) ++ assert numpy.allclose(mesh.units, [[0, 0, 1], [0, 0, -1]]) + + + def test_units_3d(): +@@ -51,7 +48,7 @@ def test_units_3d(): + mesh.update_units() + + assert (mesh.areas - 2 ** .5) < 0.0001 +- assert (mesh.normals == [0, -1, 1]).all() ++ assert numpy.allclose(mesh.normals, [0, -1, 1]) + + units = mesh.units[0] + assert units[0] == 0 +@@ -102,28 +99,28 @@ def test_duplicate_polygons(): + mesh = Mesh(data, remove_duplicate_polygons=True) + assert mesh.data.size == 3 + +- assert (mesh.vectors[0] == numpy.array([[1, 0, 0], +- [0, 0, 0], +- [0, 0, 0]])).all() +- assert (mesh.vectors[1] == numpy.array([[2, 0, 0], +- [0, 0, 0], +- [0, 0, 0]])).all() +- assert (mesh.vectors[2] == numpy.array([[0, 0, 0], +- [0, 0, 0], +- [0, 0, 0]])).all() ++ assert numpy.allclose(mesh.vectors[0], numpy.array([[1, 0, 0], ++ [0, 0, 0], ++ [0, 0, 0]])) ++ assert numpy.allclose(mesh.vectors[1], numpy.array([[2, 0, 0], ++ [0, 0, 0], ++ [0, 0, 0]])) ++ assert numpy.allclose(mesh.vectors[2], numpy.array([[0, 0, 0], ++ [0, 0, 0], ++ [0, 0, 0]])) + + mesh = Mesh(data, remove_duplicate_polygons=RemoveDuplicates.ALL) + assert mesh.data.size == 3 + +- assert (mesh.vectors[0] == numpy.array([[1, 0, 0], +- [0, 0, 0], +- [0, 0, 0]])).all() +- assert (mesh.vectors[1] == numpy.array([[2, 0, 0], +- [0, 0, 0], +- [0, 0, 0]])).all() +- assert (mesh.vectors[2] == numpy.array([[0, 0, 0], +- [0, 0, 0], +- [0, 0, 0]])).all() ++ assert numpy.allclose(mesh.vectors[0], numpy.array([[1, 0, 0], ++ [0, 0, 0], ++ [0, 0, 0]])) ++ assert numpy.allclose(mesh.vectors[1], numpy.array([[2, 0, 0], ++ [0, 0, 0], ++ [0, 0, 0]])) ++ assert numpy.allclose(mesh.vectors[2], numpy.array([[0, 0, 0], ++ [0, 0, 0], ++ [0, 0, 0]])) + + + def test_remove_all_duplicate_polygons(): +diff --git a/tests/test_rotate.py b/tests/test_rotate.py +index 65900ea..5275fad 100644 +--- a/tests/test_rotate.py ++++ b/tests/test_rotate.py +@@ -49,14 +49,16 @@ def test_rotation(): + # substracting .5 + data['vectors'] += .5 + +- assert (mesh.vectors == numpy.array([ ++ # We use a slightly higher absolute tolerance here, for ppc64le ++ # https://github.com/WoLpH/numpy-stl/issues/78 ++ assert numpy.allclose(mesh.vectors, numpy.array([ + [[1, 0, 0], [0, 1, 0], [0, 0, 0]], + [[0, 1, 0], [1, 0, 0], [1, 1, 0]], + [[0, 1, 1], [0, 1, 0], [1, 1, 1]], + [[1, 1, 0], [0, 1, 0], [1, 1, 1]], + [[0, 0, 1], [0, 1, 1], [0, 1, 0]], + [[0, 0, 1], [0, 0, 0], [0, 1, 0]], +- ])).all() ++ ]), atol=1e-07) + + + def test_rotation_over_point(): +@@ -127,13 +129,13 @@ def test_no_rotation(): + + # Rotate by 0 degrees + mesh.rotate([0.5, 0.0, 0.0], math.radians(0)) +- assert (mesh.vectors == numpy.array([ +- [[0, 1, 1], [1, 0, 1], [0, 0, 1]]])).all() ++ assert numpy.allclose(mesh.vectors, numpy.array([ ++ [[0, 1, 1], [1, 0, 1], [0, 0, 1]]])) + + # Use a zero rotation matrix + mesh.rotate([0.0, 0.0, 0.0], math.radians(90)) +- assert (mesh.vectors == numpy.array([ +- [[0, 1, 1], [1, 0, 1], [0, 0, 1]]])).all() ++ assert numpy.allclose(mesh.vectors, numpy.array([ ++ [[0, 1, 1], [1, 0, 1], [0, 0, 1]]])) + + + def test_no_translation(): +@@ -144,13 +146,13 @@ def test_no_translation(): + [0, 0, 1]]) + + mesh = Mesh(data, remove_empty_areas=False) +- assert (mesh.vectors == numpy.array([ +- [[0, 1, 1], [1, 0, 1], [0, 0, 1]]])).all() ++ assert numpy.allclose(mesh.vectors, numpy.array([ ++ [[0, 1, 1], [1, 0, 1], [0, 0, 1]]])) + + # Translate mesh with a zero vector + mesh.translate([0.0, 0.0, 0.0]) +- assert (mesh.vectors == numpy.array([ +- [[0, 1, 1], [1, 0, 1], [0, 0, 1]]])).all() ++ assert numpy.allclose(mesh.vectors, numpy.array([ ++ [[0, 1, 1], [1, 0, 1], [0, 0, 1]]])) + + + def test_translation(): +@@ -161,13 +163,13 @@ def test_translation(): + [0, 0, 1]]) + + mesh = Mesh(data, remove_empty_areas=False) +- assert (mesh.vectors == numpy.array([ +- [[0, 1, 1], [1, 0, 1], [0, 0, 1]]])).all() ++ assert numpy.allclose(mesh.vectors, numpy.array([ ++ [[0, 1, 1], [1, 0, 1], [0, 0, 1]]])) + + # Translate mesh with vector [1, 2, 3] + mesh.translate([1.0, 2.0, 3.0]) +- assert (mesh.vectors == numpy.array([ +- [[1, 3, 4], [2, 2, 4], [1, 2, 4]]])).all() ++ assert numpy.allclose(mesh.vectors, numpy.array([ ++ [[1, 3, 4], [2, 2, 4], [1, 2, 4]]])) + + + def test_no_transformation(): +@@ -178,14 +180,14 @@ def test_no_transformation(): + [0, 0, 1]]) + + mesh = Mesh(data, remove_empty_areas=False) +- assert (mesh.vectors == numpy.array([ +- [[0, 1, 1], [1, 0, 1], [0, 0, 1]]])).all() ++ assert numpy.allclose(mesh.vectors, numpy.array([ ++ [[0, 1, 1], [1, 0, 1], [0, 0, 1]]])) + + # Transform mesh with identity matrix + mesh.transform(numpy.eye(4)) +- assert (mesh.vectors == numpy.array([ +- [[0, 1, 1], [1, 0, 1], [0, 0, 1]]])).all() +- assert numpy.all(mesh.areas == 0.5) ++ assert numpy.allclose(mesh.vectors, numpy.array([ ++ [[0, 1, 1], [1, 0, 1], [0, 0, 1]]])) ++ assert numpy.allclose(mesh.areas, 0.5) + + + def test_transformation(): +@@ -196,14 +198,14 @@ def test_transformation(): + [0, 0, 1]]) + + mesh = Mesh(data, remove_empty_areas=False) +- assert (mesh.vectors == numpy.array([ +- [[0, 1, 1], [1, 0, 1], [0, 0, 1]]])).all() ++ assert numpy.allclose(mesh.vectors, numpy.array([ ++ [[0, 1, 1], [1, 0, 1], [0, 0, 1]]])) + + # Transform mesh with identity matrix + tr = numpy.zeros((4, 4)) + tr[0:3, 0:3] = Mesh.rotation_matrix([0, 0, 1], 0.5 * numpy.pi) + tr[0:3, 3] = [1, 2, 3] + mesh.transform(tr) +- assert (mesh.vectors == numpy.array([ +- [[0, 2, 4], [1, 3, 4], [1, 2, 4]]])).all() +- assert numpy.all(mesh.areas == 0.5) ++ assert numpy.allclose(mesh.vectors, numpy.array([ ++ [[0, 2, 4], [1, 3, 4], [1, 2, 4]]])) ++ assert numpy.allclose(mesh.areas, 0.5) diff --git a/python-numpy-stl.spec b/python-numpy-stl.spec index 8c1bd71..7593132 100644 --- a/python-numpy-stl.spec +++ b/python-numpy-stl.spec @@ -9,7 +9,8 @@ License: BSD URL: https://github.com/WoLpH/numpy-stl/ Source0: https://files.pythonhosted.org/packages/source/n/%{pypi_name}/%{pypi_name}-%{version}.tar.gz -Patch0: allclose.patch +# https://github.com/WoLpH/numpy-stl/issues/78 +Patch0: %{pypi_name}-allclose.patch BuildRequires: python3-devel BuildRequires: python3-Cython @@ -82,6 +83,7 @@ rm -rf html/.{doctrees,buildinfo} - Updated to 2.4.1 (#1475307) - Enable automatic dependency generator, drop nine from BRs - Enable tests on armv7hl once again (it works now) +- Add tolerance to tests that check float equality, fixes test failure on ppc64le * Wed Feb 14 2018 Miro Hrončok - 2.3.2-1 - Updated to 2.3.2 to fix FTBFS