From fb013255239a76f12add6c25257bfdbd9767200d Mon Sep 17 00:00:00 2001 From: Benjamin Alan Weaver Date: Thu, 29 Feb 2024 13:12:53 -0700 Subject: [PATCH 01/11] activate recent numpy tests --- .github/workflows/python-package.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 327cafe..00c329c 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -18,7 +18,7 @@ jobs: matrix: os: [ubuntu-latest] python-version: ['3.9', '3.10'] #, '3.11'] There are still issues with Numpy <1.23 and 3.11. - numpy-version: ['<1.23'] + numpy-version: ['<1.23', '<1.24', '<2.0'] astropy-version: ['<5.1', '<6.0', '<7.0'] steps: @@ -47,7 +47,7 @@ jobs: matrix: os: [ubuntu-latest] python-version: ['3.10'] - astropy-version: ['<5.1'] + # astropy-version: ['<5.1'] not used at this time. steps: - name: Checkout code From 6d69fc9655250d6b8d3e90faa33b4c184a1a8009 Mon Sep 17 00:00:00 2001 From: Benjamin Alan Weaver Date: Thu, 29 Feb 2024 13:26:36 -0700 Subject: [PATCH 02/11] add 5.0.0 test --- .github/workflows/python-package.yml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 00c329c..1b792ad 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -19,15 +19,15 @@ jobs: os: [ubuntu-latest] python-version: ['3.9', '3.10'] #, '3.11'] 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'] + astropy-version: ['==5.0', '<5.1', '<6.0', '<7.0'] steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 0 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - name: Install Python dependencies @@ -51,11 +51,11 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 0 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - name: Install Python dependencies @@ -81,11 +81,11 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 0 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - name: Install Python dependencies @@ -104,11 +104,11 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 0 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - name: Install Python dependencies @@ -131,11 +131,11 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 0 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - name: Install Python dependencies From 43b4f4b3794aea67a7bb660a9a75ff3d82ea9abd Mon Sep 17 00:00:00 2001 From: Benjamin Alan Weaver Date: Thu, 29 Feb 2024 13:33:34 -0700 Subject: [PATCH 03/11] add 3.11 test --- .github/workflows/python-package.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 1b792ad..ae2d44a 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -17,7 +17,7 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest] - python-version: ['3.9', '3.10'] #, '3.11'] There are still issues with Numpy <1.23 and 3.11. + python-version: ['3.9', '3.10', '3.11'] # There are still issues with Numpy <1.23 and 3.11. numpy-version: ['<1.23', '<1.24', '<2.0'] astropy-version: ['==5.0', '<5.1', '<6.0', '<7.0'] From 3c9af830d1fd6846e6b24380af5d18cf6ba7c330 Mon Sep 17 00:00:00 2001 From: Benjamin Alan Weaver Date: Thu, 29 Feb 2024 13:36:52 -0700 Subject: [PATCH 04/11] remove bin from setup.cfg --- setup.cfg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.cfg b/setup.cfg index 90a6c3e..a1f0c4c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -28,12 +28,12 @@ package_dir = =py packages = find: include_package_data = True -python_requires = >=3.8 +python_requires = >=3.9 # setup_requires = setuptools_scm install_requires = requests pyyaml - numpy<1.23 + numpy<2.0 astropy>=5.0 healpy matplotlib From 6de811fd9ed5c41f4c7ea18eec06606932556f4a Mon Sep 17 00:00:00 2001 From: Benjamin Alan Weaver Date: Thu, 29 Feb 2024 13:42:15 -0700 Subject: [PATCH 05/11] reduce range of tests --- .github/workflows/python-package.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index ae2d44a..36d593a 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -17,9 +17,9 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest] - python-version: ['3.9', '3.10', '3.11'] # There are still issues with Numpy <1.23 and 3.11. + python-version: ['3.9', '3.10'] #, '3.11'] # There are still issues with Numpy <1.23 and 3.11. numpy-version: ['<1.23', '<1.24', '<2.0'] - astropy-version: ['==5.0', '<5.1', '<6.0', '<7.0'] + astropy-version: ['<5.1', '<6.0', '<7.0'] steps: - name: Checkout code From ddfb947e0a017edc2d29e3bccbb053328eba1048 Mon Sep 17 00:00:00 2001 From: Benjamin Alan Weaver Date: Thu, 29 Feb 2024 13:49:07 -0700 Subject: [PATCH 06/11] retry 3.11 --- .github/workflows/python-package.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 36d593a..177c988 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -17,7 +17,7 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest] - python-version: ['3.9', '3.10'] #, '3.11'] # There are still issues with Numpy <1.23 and 3.11. + python-version: ['3.10', '3.11'] # 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'] From 21a3dc40817594539cc959220be9b3ec4bac8e28 Mon Sep 17 00:00:00 2001 From: Benjamin Alan Weaver Date: Tue, 7 May 2024 15:48:07 -0700 Subject: [PATCH 07/11] Add tests for bounds --- .github/workflows/python-package.yml | 2 +- py/desiutil/names.py | 24 +++++++++++++++++++----- py/desiutil/test/test_names.py | 16 ++++++++++++++-- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 177c988..3017f07 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -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'] diff --git a/py/desiutil/names.py b/py/desiutil/names.py index a02d0bb..cd92f79 100644 --- a/py/desiutil/names.py +++ b/py/desiutil/names.py @@ -14,19 +14,19 @@ 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 ------- @@ -34,10 +34,24 @@ def radec_to_desiname(target_ra, target_dec): 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 diff --git a/py/desiutil/test/test_names.py b/py/desiutil/test/test_names.py index e4e1155..d7148db 100644 --- a/py/desiutil/test/test_names.py +++ b/py/desiutil/test/test_names.py @@ -4,6 +4,7 @@ """ import unittest import numpy as np +from ..names import radec_to_desiname class TestNames(unittest.TestCase): @@ -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, @@ -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!") From 106cfff45bcbfa3d241b6e02904d3b8cb0b5ecc1 Mon Sep 17 00:00:00 2001 From: Benjamin Alan Weaver Date: Tue, 7 May 2024 15:54:07 -0700 Subject: [PATCH 08/11] specify order of tests --- py/desiutil/names.py | 14 +++++++------- py/desiutil/test/test_names.py | 5 +++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/py/desiutil/names.py b/py/desiutil/names.py index cd92f79..fe56799 100644 --- a/py/desiutil/names.py +++ b/py/desiutil/names.py @@ -43,14 +43,14 @@ def radec_to_desiname(target_ra, target_dec): 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)} + 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}!") + for key, check in tests: + if (check(inputs[i])).any(): + raise ValueError(f"{key} detected in {i}!") # Number of decimal places in final naming convention precision = 4 diff --git a/py/desiutil/test/test_names.py b/py/desiutil/test/test_names.py index d7148db..49ab2f6 100644 --- a/py/desiutil/test/test_names.py +++ b/py/desiutil/test/test_names.py @@ -56,3 +56,8 @@ def test_radec_to_desiname_bad_values(self): 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!") From c49ff1a712d32470d69e5f3f558721f8d4bb13ac Mon Sep 17 00:00:00 2001 From: Benjamin Alan Weaver Date: Tue, 7 May 2024 16:28:37 -0700 Subject: [PATCH 09/11] fix up parameterization of tests and tests of tests --- py/desiutil/names.py | 17 +++++++++-------- py/desiutil/test/test_names.py | 33 ++++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/py/desiutil/names.py b/py/desiutil/names.py index fe56799..2919e8f 100644 --- a/py/desiutil/names.py +++ b/py/desiutil/names.py @@ -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: - 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 diff --git a/py/desiutil/test/test_names.py b/py/desiutil/test/test_names.py index 49ab2f6..7e6dd14 100644 --- a/py/desiutil/test/test_names.py +++ b/py/desiutil/test/test_names.py @@ -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 From f92b33b917cc99bf019aa8b2449c7c10a729797c Mon Sep 17 00:00:00 2001 From: Benjamin Alan Weaver Date: Tue, 7 May 2024 17:20:58 -0700 Subject: [PATCH 10/11] update change log --- doc/changes.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/changes.rst b/doc/changes.rst index 510396d..f5f4d0e 100644 --- a/doc/changes.rst +++ b/doc/changes.rst @@ -14,7 +14,9 @@ Change Log 3.4.3 (unreleased) ------------------ -* No changes yet. +* Check input bounds in :func:`~desiutil.names.radec_to_desiname` (PR `#207`_). + +.. _`#207`: https://github.com/desihub/desiutil/pull/207 3.4.2 (2023-11-29) ------------------ From afea0816f742c3df1500258a459fccd3970b3cfd Mon Sep 17 00:00:00 2001 From: Benjamin Alan Weaver Date: Wed, 8 May 2024 09:46:39 -0700 Subject: [PATCH 11/11] change variable name --- py/desiutil/names.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/py/desiutil/names.py b/py/desiutil/names.py index 2919e8f..283efe5 100644 --- a/py/desiutil/names.py +++ b/py/desiutil/names.py @@ -48,10 +48,10 @@ def radec_to_desiname(target_ra, target_dec): '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: - for message, check in inputs[i]['tests']: - if check(inputs[i]['data']).any(): - raise ValueError(f"{message} detected in {i}!") + for coord in inputs: + for message, check in inputs[coord]['tests']: + if check(inputs[coord]['data']).any(): + raise ValueError(f"{message} detected in {coord}!") # Number of decimal places in final naming convention precision = 4