Skip to content

Commit

Permalink
test: allclose (#73)
Browse files Browse the repository at this point in the history
* test: allclose
* test: fix
* fix: looser test

Signed-off-by: nstarman <nstarman@users.noreply.github.com>
  • Loading branch information
nstarman authored Mar 20, 2024
1 parent e09b920 commit 91fdd08
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 33 deletions.
22 changes: 15 additions & 7 deletions tests/test_d2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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))

Expand All @@ -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)``."""
Expand Down
62 changes: 36 additions & 26 deletions tests/test_d3.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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"))

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 91fdd08

Please sign in to comment.