Skip to content

Commit

Permalink
Deal with precision failures for numpy 1.22 (likely due to numpy/nump…
Browse files Browse the repository at this point in the history
…y#19478), but only pass for exact 1.22 so we can re-evaluate next version
  • Loading branch information
jobovy committed Jan 6, 2022
1 parent ce00fb3 commit 0fcdf63
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
11 changes: 3 additions & 8 deletions galpy/util/coords.py
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions 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
Expand All @@ -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.)
Expand Down Expand Up @@ -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.)
Expand Down
10 changes: 7 additions & 3 deletions 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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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():
Expand All @@ -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'
Expand Down

0 comments on commit 0fcdf63

Please sign in to comment.