Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bounds checks to radec_to_desiname #207

Merged
merged 11 commits into from
May 14, 2024
17 changes: 9 additions & 8 deletions py/desiutil/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ def radec_to_desiname(target_ra, target_dec):
# 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)))
base_tests = [('NaN values', np.isnan),
('Infinite values', np.isinf),]
inputs = {'target_ra': {'data': target_ra,
'tests': base_tests + [('RA not in range [0, 360)', lambda x: (x < 0) | (x >= 360))]},
'target_dec': {'data': target_dec,
'tests': base_tests + [('Dec not in range [-90, 90]', lambda x: (x < -90) | (x > 90))]}}
for i in inputs:
weaverba137 marked this conversation as resolved.
Show resolved Hide resolved
for key, check in tests:
if (check(inputs[i])).any():
raise ValueError(f"{key} detected in {i}!")
for message, check in inputs[i]['tests']:
if check(inputs[i]['data']).any():
raise ValueError(f"{message} detected in {i}!")

# Number of decimal places in final naming convention
precision = 4
Expand Down
33 changes: 24 additions & 9 deletions py/desiutil/test/test_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,27 @@ def test_radec_to_desiname_bad_values(self):
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!")

ras[2] = np.inf
with self.assertRaises(ValueError) as e:
outnames = radec_to_desiname(ras, decs)
self.assertEqual(str(e.exception), "Infinite values detected in target_ra!")

original_ra = ras[2]
for message, value in [("NaN values detected in target_ra!", np.nan),
("Infinite values detected in target_ra!", np.inf),
("RA not in range [0, 360) detected in target_ra!", -23.914121939862518),
("RA not in range [0, 360) detected in target_ra!", 360.23454570972834)]:
ras[2] = value
with self.assertRaises(ValueError) as e:
outnames = radec_to_desiname(ras, decs)
self.assertEqual(str(e.exception), message)

ras[2] = original_ra

original_dec = decs[2]
for message, value in [("NaN values detected in target_dec!", np.nan),
("Infinite values detected in target_dec!", np.inf),
("Dec not in range [-90, 90] detected in target_dec!", -90.9968423456),
("Dec not in range [-90, 90] detected in target_dec!", 90.9968423456)]:
decs[2] = value
with self.assertRaises(ValueError) as e:
outnames = radec_to_desiname(ras, decs)
self.assertEqual(str(e.exception), message)

decs[2] = original_dec