Skip to content

Commit

Permalink
Add tests for bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverba137 committed May 7, 2024
1 parent ddfb947 commit 21a3dc4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
python-version: ['3.10', '3.11'] # There are still issues with Numpy <1.23 and 3.11.
python-version: ['3.10'] # There are still issues with Numpy <1.23 and 3.11.
numpy-version: ['<1.23', '<1.24', '<2.0']
astropy-version: ['<5.1', '<6.0', '<7.0']

Expand Down
24 changes: 19 additions & 5 deletions py/desiutil/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,44 @@
def radec_to_desiname(target_ra, target_dec):
"""Convert the right ascension and declination of a DESI target
into the corresponding "DESINAME" for reference in publications.
Length of target_ra and target_dec must be the same if providing an
Length of `target_ra` and `target_dec` must be the same if providing an
array or list. Note that these names are not unique for roughly
one percent of DESI targets, so also including TARGETID in
publications is highly recommended for uniqueness.
Parameters
----------
target_ra: array of :class:`float64`
target_ra: array of :class:`~numpy.float64`
Right ascension in degrees of target object(s). Can be float, double,
or array/list of floats or doubles
target_dec: array of :class:`float64`
or array/list of floats or doubles.
target_dec: array of :class:`~numpy.float64`
Declination in degrees of target object(s). Can be float, double,
or array/list of floats or doubles
or array/list of floats or doubles.
Returns
-------
array of :class:`str`
The DESI names referring to the input target RA and DEC's. Array is
the same length as the input arrays.
Raises
------
ValueError
If any input values are out of bounds.
"""
# Convert to numpy array in case inputs are scalars or lists
target_ra, target_dec = np.atleast_1d(target_ra), np.atleast_1d(target_dec)

inputs = {'target_ra': target_ra, 'target_dec': target_dec}
tests = {'NaN values': np.isnan,
'Infinite values': np.isinf,
'RA not in range [0, 360)': lambda x: (x < 0) | (x >= 360),
'Dec not in range [-90, 90]': lambda x: (x < -90) | (x > 90)}
for i in inputs:
for t in tests:
if (tests[t](inputs[i])).any():
raise ValueError(f"{t} detected in {i}!")

# Number of decimal places in final naming convention
precision = 4

Expand Down
16 changes: 14 additions & 2 deletions py/desiutil/test/test_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
import unittest
import numpy as np
from ..names import radec_to_desiname


class TestNames(unittest.TestCase):
Expand All @@ -19,9 +20,8 @@ def tearDownClass(cls):
pass

def test_radec_to_desiname(self):
"""Test MaskedArrayWithLimits
"""Test computation of desiname.
"""
from ..names import radec_to_desiname
ras = [6.2457354547234, 23.914121939862518, 36.23454570972834,
235.25235223446, 99.9999999999999]
decs = [29.974787585945496, -42.945872347904356, -0.9968423456,
Expand All @@ -44,3 +44,15 @@ def test_radec_to_desiname(self):
outnames = radec_to_desiname(np.array(ras),
np.array(decs))
self.assertTrue(np.alltrue(outnames == correct_names))

def test_radec_to_desiname_bad_values(self):
"""Test exceptions when running radec_to_desiname with bad values.
"""
ras = [6.2457354547234, 23.914121939862518, 36.23454570972834,
235.25235223446, 99.9999999999999]
decs = [29.974787585945496, -42.945872347904356, -0.9968423456,
8.45677345352345, 89.234958294953]
ras[2] = np.nan
with self.assertRaises(ValueError) as e:
outnames = radec_to_desiname(ras, decs)
self.assertEqual(str(e.exception), "NaN values detected in target_ra!")

0 comments on commit 21a3dc4

Please sign in to comment.