From 0fcdf63829c285a583dbadd491fe343ab435f8f0 Mon Sep 17 00:00:00 2001 From: Jo Bovy Date: Thu, 6 Jan 2022 14:14:04 -0500 Subject: [PATCH] Deal with precision failures for numpy 1.22 (likely due to numpy/numpy#19478), but only pass for exact 1.22 so we can re-evaluate next version --- galpy/util/coords.py | 11 +++-------- tests/test_coords.py | 9 ++++++--- tests/test_quantity.py | 10 +++++++--- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/galpy/util/coords.py b/galpy/util/coords.py index 309ea608d..51a9567c2 100644 --- a/galpy/util/coords.py +++ b/galpy/util/coords.py @@ -584,14 +584,9 @@ def XYZ_to_lbd(X,Y,Z,degree=False): """ #Whether to use degrees and scalar input is handled by decorators d= numpy.sqrt(X**2.+Y**2.+Z**2.) - b=numpy.arcsin(Z/d) - cosl= X/d/numpy.cos(b) - sinl= Y/d/numpy.cos(b) - l= numpy.arcsin(sinl) - l[cosl < 0.]= numpy.pi-l[cosl < 0.] - l[(cosl >= 0.)*(sinl < 0.)]+= 2.*numpy.pi - out= numpy.empty((len(l),3)) - out[:,0]= l + b= numpy.arcsin(Z/d) + out= numpy.empty((len(d),3)) + out[:,0]= numpy.arctan2(Y,X) out[:,1]= b out[:,2]= d return out diff --git a/tests/test_coords.py b/tests/test_coords.py index 39d5a12ce..d8c5fe095 100644 --- a/tests/test_coords.py +++ b/tests/test_coords.py @@ -1,5 +1,8 @@ -from __future__ import print_function, division +from pkg_resources import parse_version import numpy +_NUMPY_VERSION= parse_version(numpy.__version__) +_NUMPY_1_22= (_NUMPY_VERSION > parse_version('1.21'))\ + *(_NUMPY_VERSION < parse_version('1.23')) from galpy.util import coords import pytest import astropy @@ -11,7 +14,7 @@ def test_radec_to_lb_ngp(): ra, dec= 192.25, 27.4 lb= coords.radec_to_lb(ra,dec,degree=True,epoch=1950.) assert not coords._APY_LOADED, "_APY_LOADED should be False, but isn't" - assert numpy.fabs(lb[1]-90.) < 10.**-6., 'Galactic latitude of the NGP given in ra,dec is not 90' + assert numpy.fabs(lb[1]-90.) < _NUMPY_1_22 * 1e-5 + (1-_NUMPY_1_22) * 1e-6, 'Galactic latitude of the NGP given in ra,dec is not 90' # Also test this for degree=False lb= coords.radec_to_lb(ra/180.*numpy.pi,dec/180.*numpy.pi, degree=False,epoch=1950.) @@ -104,7 +107,7 @@ def test_radec_to_lb_sgp(): ra, dec= 12.25, -27.4 assert not coords._APY_LOADED, "_APY_LOADED should be False, but isn't" lb= coords.radec_to_lb(ra,dec,degree=True,epoch=1950.) - assert numpy.fabs(lb[1]+90.) < 10.**-6., 'Galactic latitude of the SGP given in ra,dec is not 90' + assert numpy.fabs(lb[1]+90.) < _NUMPY_1_22 * 1e-5 + (1-_NUMPY_1_22) * 1e-6, 'Galactic latitude of the SGP given in ra,dec is not 90' # Also test this for degree=False lb= coords.radec_to_lb(ra/180.*numpy.pi,dec/180.*numpy.pi, degree=False,epoch=1950.) diff --git a/tests/test_quantity.py b/tests/test_quantity.py index d4443f931..d156eeab6 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -1,8 +1,12 @@ # Make sure to set configuration, needs to be before any galpy imports +from pkg_resources import parse_version import pytest from galpy.util import config config.__config__.set('astropy','astropy-units','True') import numpy +_NUMPY_VERSION= parse_version(numpy.__version__) +_NUMPY_1_22= (_NUMPY_VERSION > parse_version('1.21'))\ + *(_NUMPY_VERSION < parse_version('1.23')) # For testing 1.22 precision issues from astropy import units, constants sdf_sanders15= None #so we can set this up and then use in other tests sdf_sanders15_nou= None #so we can set this up and then use in other tests @@ -6238,7 +6242,7 @@ def test_streamdf_method_inputAsQuantity(): sdf_bovy14_nou.subhalo_encounters(\ venc=200./vo,sigma=150./vo, nsubhalo=38.35/(4.*25.**3.*numpy.pi/3.)*ro**3., - bmax=1./ro,yoon=False)) < 10.**-8., 'streamdf method subhalo_encounters with Quantity input does not return correct Quantity' + bmax=1./ro,yoon=False)) < 1e-6 * _NUMPY_1_22 + 1e-8 * (1-_NUMPY_1_22), 'streamdf method subhalo_encounters with Quantity input does not return correct Quantity' assert numpy.fabs(sdf_bovy14.pOparapar(0.2/units.Gyr,30.*units.deg)-sdf_bovy14_nou.pOparapar(0.2/conversion.freq_in_Gyr(vo,ro),30.*numpy.pi/180.)) < 10.**-8., 'streamdf method pOparapar with Quantity input does not return correct Quantity' return None @@ -6574,7 +6578,7 @@ def test_streamgapdf_setup_impactparamsAsQuantity(): def test_streamgapdf_inputAsQuantity(): from galpy.util import conversion - assert numpy.fabs(sdf_sanders15.pOparapar(0.2/units.Gyr,30.*units.deg)-sdf_sanders15_nou.pOparapar(0.2/conversion.freq_in_Gyr(sdf_sanders15._vo,sdf_sanders15._ro),30.*numpy.pi/180.)) < 10.**-8., 'streamgapdf method pOparapar with Quantity input does not return correct Quantity' + assert numpy.fabs(sdf_sanders15.pOparapar(0.2/units.Gyr,30.*units.deg)-sdf_sanders15_nou.pOparapar(0.2/conversion.freq_in_Gyr(sdf_sanders15._vo,sdf_sanders15._ro),30.*numpy.pi/180.)) < _NUMPY_1_22 * 1e-4 + (1-_NUMPY_1_22) * 1e-8, 'streamgapdf method pOparapar with Quantity input does not return correct Quantity' return None def test_streamgapdf_sample(): @@ -6584,7 +6588,7 @@ def test_streamgapdf_sample(): RvR= sdf_sanders15.sample(1) numpy.random.seed(1) RvRnou= sdf_sanders15_nou.sample(1) - assert numpy.fabs(RvR[0].to(units.kpc).value/sdf_sanders15._ro-RvRnou[0]) < 10.**-8., 'streamgapdf sample RvR does not return a correct Quantity' + assert numpy.fabs(RvR[0].to(units.kpc).value/sdf_sanders15._ro-RvRnou[0]) <_NUMPY_1_22 * 1e-4 + (1-_NUMPY_1_22) * 1e-8, 'streamgapdf sample RvR does not return a correct Quantity' assert numpy.fabs(RvR[3].to(units.kpc).value/sdf_sanders15._ro-RvRnou[3]) < 10.**-8., 'streamgapdf sample RvR does not return a correct Quantity' assert numpy.fabs(RvR[1].to(units.km/units.s).value/sdf_sanders15._vo-RvRnou[1]) < 10.**-8., 'streamgapdf sample RvR does not return a correct Quantity' assert numpy.fabs(RvR[2].to(units.km/units.s).value/sdf_sanders15._vo-RvRnou[2]) < 10.**-8., 'streamgapdf sample RvR does not return a correct Quantity'