From 91fdd083abf60fd8b53df3e47bf15de33b8f3c9f Mon Sep 17 00:00:00 2001 From: Nathaniel Starkman Date: Tue, 19 Mar 2024 22:30:27 -0400 Subject: [PATCH] test: allclose (#73) * test: allclose * test: fix * fix: looser test Signed-off-by: nstarman --- tests/test_d2.py | 22 +++++++++++------ tests/test_d3.py | 62 ++++++++++++++++++++++++++++-------------------- 2 files changed, 51 insertions(+), 33 deletions(-) diff --git a/tests/test_d2.py b/tests/test_d2.py index a24a132f..db90775f 100644 --- a/tests/test_d2.py +++ b/tests/test_d2.py @@ -65,8 +65,9 @@ def test_cartesian2d_to_polar(self, vector): assert isinstance(polar, cx.PolarVector) assert qnp.array_equal(polar.r, qnp.hypot(vector.x, vector.y)) assert qnp.allclose( - polar.phi.value, - xp.asarray([1.3734008, 1.2490457, 1.1659045, 1.1071488]), + polar.phi, + Quantity([1.3734008, 1.2490457, 1.1659045, 1.1071488], "rad"), + atol=Quantity(1e-8, "deg"), ) # def test_cartesian2d_to_lnpolar(self, vector): @@ -97,8 +98,9 @@ def test_cartesian2d_to_spherical(self, vector): assert isinstance(spherical, cx.SphericalVector) assert qnp.array_equal(spherical.r, qnp.hypot(vector.x, vector.y)) assert qnp.allclose( - spherical.phi.to_value("rad"), - xp.asarray([1.3734008, 1.2490457, 1.1659045, 1.1071488]), + spherical.phi, + Quantity([1.3734008, 1.2490457, 1.1659045, 1.1071488], "rad"), + atol=Quantity(1e-8, "rad"), ) assert qnp.array_equal( spherical.theta, Quantity(xp.full(4, fill_value=xp.pi / 2), "rad") @@ -139,7 +141,9 @@ def test_polar_to_cartesian1d(self, vector): assert isinstance(cart1d, cx.Cartesian1DVector) assert qnp.allclose( - cart1d.x.to_value("kpc"), xp.asarray([1.0, 1.0806047, -1.2484405, -3.95997]) + cart1d.x, + Quantity([1.0, 1.0806047, -1.2484405, -3.95997], "kpc"), + atol=Quantity(1e-8, "kpc"), ) assert qnp.array_equal(cart1d.x, vector.r * xp.cos(vector.phi)) @@ -161,11 +165,15 @@ def test_polar_to_cartesian2d(self, vector): assert qnp.array_equal( cart2d.x, Quantity([1.0, 1.0806046, -1.2484405, -3.95997], "kpc") ) - assert qnp.allclose(cart2d.x.value, (vector.r * xp.cos(vector.phi)).value) + assert qnp.allclose( + cart2d.x, (vector.r * xp.cos(vector.phi)), atol=Quantity(1e-8, "kpc") + ) assert qnp.array_equal( cart2d.y, Quantity([0.0, 1.6829419, 2.7278922, 0.56448], "kpc") ) - assert qnp.allclose(cart2d.y.value, (vector.r * xp.sin(vector.phi)).value) + assert qnp.allclose( + cart2d.y, (vector.r * xp.sin(vector.phi)), atol=Quantity(1e-8, "kpc") + ) def test_polar_to_polar(self, vector): """Test ``coordinax.represent_as(PolarVector)``.""" diff --git a/tests/test_d3.py b/tests/test_d3.py index 4b2b2bb5..213e6508 100644 --- a/tests/test_d3.py +++ b/tests/test_d3.py @@ -1,7 +1,6 @@ """Test :mod:`coordinax._builtin`.""" import astropy.coordinates as apyc -import jax.numpy as jnp import numpy as np import pytest from astropy.coordinates.tests.test_representation import representation_equal @@ -155,9 +154,10 @@ def test_cartesian3d_to_spherical(self, vector): assert qnp.array_equal( spherical.phi, Quantity([1.3734008, 1.2490457, 1.1659045, 1.1071488], "rad") ) - assert jnp.allclose( - spherical.theta.to_value("rad"), - xp.asarray([0.51546645, 0.5639427, 0.6055685, 0.64052236]), + assert qnp.allclose( + spherical.theta, + Quantity([0.51546645, 0.5639427, 0.6055685, 0.64052236], "rad"), + atol=Quantity(1e-8, "rad"), ) def test_cartesian3d_to_spherical_astropy(self, vector, apyvector): @@ -217,9 +217,10 @@ def test_spherical_to_cartesian1d(self, vector): cart1d = vector.represent_as(cx.Cartesian1DVector) assert isinstance(cart1d, cx.Cartesian1DVector) - assert jnp.allclose( - cart1d.x.to_value("kpc"), - xp.asarray([0, 0.49681753, -1.3060151, -4.1700245e-15]), + assert qnp.allclose( + cart1d.x, + Quantity([0, 0.49681753, -1.3060151, -4.1700245e-15], "kpc"), + atol=Quantity(1e-8, "kpc"), ) @pytest.mark.filterwarnings("ignore:Irreversible dimension change") @@ -372,8 +373,10 @@ def test_cylindrical_to_cartesian1d(self, vector): cart1d = vector.represent_as(cx.Cartesian1DVector) assert isinstance(cart1d, cx.Cartesian1DVector) - assert jnp.allclose( - cart1d.x.to_value("kpc"), xp.asarray([1.0, 1.0806047, -1.2484405, -3.95997]) + assert qnp.allclose( + cart1d.x, + Quantity([1.0, 1.0806047, -1.2484405, -3.95997], "kpc"), + atol=Quantity(1e-8, "kpc"), ) @pytest.mark.filterwarnings("ignore:Irreversible dimension change") @@ -585,16 +588,20 @@ def test_cartesian3d_to_spherical(self, difntl, vector): spherical = difntl.represent_as(cx.SphericalDifferential, vector) assert isinstance(spherical, cx.SphericalDifferential) - assert jnp.allclose( - spherical.d_r.to_value("km/s"), - xp.asarray([10.344081, 11.832159, 13.379088, 14.966629]), + assert qnp.allclose( + spherical.d_r, + Quantity([10.344081, 11.832159, 13.379088, 14.966629], "km/s"), + atol=Quantity(1e-8, "km/s"), ) - assert jnp.allclose( - spherical.d_phi.to_value("mas/Myr"), xp.asarray([0, 0, 0.00471509, 0]) + assert qnp.allclose( + spherical.d_phi, + Quantity([0, 0, 0.00471509, 0], "mas/Myr"), + atol=Quantity(1e-8, "mas/Myr"), ) - assert jnp.allclose( - spherical.d_theta.to_value("mas/Myr"), - xp.asarray([0.03221978, -0.05186598, -0.01964621, -0.01886036]), + assert qnp.allclose( + spherical.d_theta, + Quantity([0.03221978, -0.05186598, -0.01964621, -0.01886036], "mas/Myr"), + atol=Quantity(1e-8, "mas/Myr"), ) def test_cartesian3d_to_spherical_astropy( @@ -627,8 +634,10 @@ def test_cartesian3d_to_cylindrical(self, difntl, vector): cylindrical.d_rho, Quantity([5.0990195, 6.324555, 7.6157727, 8.944272], "km/s"), ) - assert jnp.allclose( - cylindrical.d_phi.to_value("mas/Myr"), xp.asarray([0, 0, 0.00471509, 0]) + assert qnp.allclose( + cylindrical.d_phi, + Quantity([0, 0, 0.00471509, 0], "mas/Myr"), + atol=Quantity(1e-8, "mas/Myr"), ) assert qnp.array_equal(cylindrical.d_z, Quantity([9, 10, 11, 12], "km/s")) @@ -893,17 +902,18 @@ def test_cylindrical_to_cartesian3d(self, difntl, vector, apydifntl, apyvector): def test_cylindrical_to_spherical(self, difntl, vector): """Test ``coordinax.represent_as(SphericalDifferential)``.""" - spherical = difntl.represent_as(cx.SphericalDifferential, vector) + dsph = difntl.represent_as(cx.SphericalDifferential, vector) - assert isinstance(spherical, cx.SphericalDifferential) + assert isinstance(dsph, cx.SphericalDifferential) assert qnp.array_equal( - spherical.d_r, + dsph.d_r, Quantity([9.055385, 10.198039, 11.401753, 12.64911], "km/s"), ) - assert qnp.array_equal(spherical.d_phi, Quantity([5, 6, 7, 8], "mas/yr")) - assert jnp.allclose( - spherical.d_theta.to_value("mas/Myr"), - xp.asarray([-0.08428223, 0.07544143, -0.0326127, -0.01571696]), + assert qnp.array_equal(dsph.d_phi, Quantity([5, 6, 7, 8], "mas/yr")) + assert qnp.allclose( + dsph.d_theta, + Quantity([0.0, 0, 0, 0], "mas/yr"), + atol=Quantity(5e-7, "mas/yr"), ) def test_cylindrical_to_spherical_astropy(