Skip to content

Commit 947254a

Browse files
snbiancoCopilot
andcommitted
Docs fixes, guards around regions import
Co-authored-by: Copilot <copilot@github.com> More doc fixes Co-authored-by: Copilot <copilot@github.com> Typos Co-authored-by: Copilot <copilot@github.com>
1 parent 481efb0 commit 947254a

7 files changed

Lines changed: 102 additions & 85 deletions

File tree

astroquery/mast/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ class Conf(_config.ConfigNamespace):
2020
ssoserver = _config.ConfigItem(
2121
'https://ssoportal.stsci.edu',
2222
'MAST SSO Portal server.')
23-
catalogs_server = _config.ConfigItem(
24-
'https://catalogs.mast.stsci.edu',
25-
'Catalogs.MAST server.')
2623
timeout = _config.ConfigItem(
2724
600,
2825
'Time limit for requests from the STScI server.')
@@ -38,6 +35,7 @@ class Conf(_config.ConfigNamespace):
3835
conf = Conf()
3936

4037
from . import utils
38+
from .catalog_collection import CatalogCollection
4139
from .collections import Catalogs, CatalogsClass
4240
from .cutouts import Hapcut, HapcutClass, Tesscut, TesscutClass, Zcut, ZcutClass
4341
from .missions import MastMissions, MastMissionsClass
@@ -48,6 +46,7 @@ class Conf(_config.ConfigNamespace):
4846
"ObservationsClass",
4947
"Catalogs",
5048
"CatalogsClass",
49+
"CatalogCollection",
5150
"MastMissions",
5251
"MastMissionsClass",
5352
"Mast",

astroquery/mast/catalog_collection.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def supported_adql_functions(self):
208208

209209
def get_catalog_metadata(self, catalog):
210210
"""
211-
For a given catalog, cache and return metadata about its columns and capabilties.
211+
For a given catalog, cache and return metadata about its columns and capabilities.
212212
213213
Parameters
214214
----------
@@ -310,8 +310,8 @@ def _fetch_catalogs(self):
310310
311311
Returns
312312
-------
313-
list of str
314-
List of catalog names.
313+
`~astropy.table.Table`
314+
A table containing the catalog names and descriptions for this collection.
315315
"""
316316
log.debug(f"Fetching available tables for collection '{self.name}' from MAST TAP service.")
317317
query = "SELECT TOP 5000 table_name, description FROM tap_schema.tables"
@@ -334,8 +334,9 @@ def _fetch_adql_supported_functions(self):
334334
335335
Returns
336336
-------
337-
list
338-
A list of supported ADQL functions.
337+
set
338+
A set of supported ADQL geometry functions (e.g. "POINT", "CIRCLE", "CONTAINS", etc.) for
339+
this collection's TAP service.
339340
"""
340341
adql_functions = ["CIRCLE", "POLYGON", "POINT", "CONTAINS", "INTERSECTS"]
341342
supported = set()

astroquery/mast/collections.py

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from astropy.table import Row, Table
2020
from astropy.time import Time
2121
from astropy.utils.decorators import deprecated, deprecated_renamed_argument
22-
from regions import CircleSkyRegion, PolygonSkyRegion
2322

2423
from .. import log
2524
from ..exceptions import InputWarning, InvalidQueryError, NoResultsWarning
@@ -29,6 +28,12 @@
2928
from .catalog_collection import CatalogCollection
3029
from .core import MastQueryWithLogin
3130

31+
try:
32+
from regions import CircleSkyRegion, PolygonSkyRegion
33+
HAS_REGIONS = True
34+
except ImportError:
35+
HAS_REGIONS = False
36+
3237
__all__ = ["Catalogs", "CatalogsClass"]
3338

3439

@@ -232,7 +237,7 @@ def query_criteria(
232237
):
233238
"""
234239
Query a MAST catalog from a given collection using criteria filters. To return columns for a given
235-
collection and catalog, use `~astroquery.mast.collections.get_catalog_metadata`.
240+
collection and catalog, use `~astroquery.mast.CatalogsClass.get_column_metadata`.
236241
237242
Parameters
238243
----------
@@ -243,10 +248,10 @@ def query_criteria(
243248
coordinates : str or `~astropy.coordinates` object, optional
244249
The target around which to search. It may be specified as a string (e.g., '350 -80') or as an
245250
Astropy coordinates object.
246-
region : str | iterable | `~astropy.regions.CircleSkyRegion` | `~astropy.regions.PolygonSkyRegion`, optional
251+
region : str | iterable | `~regions.CircleSkyRegion` | `~regions.PolygonSkyRegion`, optional
247252
The region to search within. It may be specified as a STC-S POLYGON or CIRCLE string
248253
(e.g., 'CIRCLE 350 -80 0.2'), an iterable of coordinate pairs, or as an
249-
`~astropy.regions.CircleSkyRegion` or `~astropy.regions.PolygonSkyRegion`.
254+
`~regions.CircleSkyRegion` or `~regions.PolygonSkyRegion`.
250255
object_name : str, optional
251256
The name of the object to resolve and search around.
252257
radius : str or `~astropy.units.Quantity` object, optional
@@ -264,17 +269,17 @@ def query_criteria(
264269
sort_by : str or list of str, optional
265270
Column name(s) to sort the results by.
266271
sort_desc : bool or list of bool, optional
267-
Indicates whether to sort in descending order for each column in `sort_by`. If a single bool,
268-
applies to all columns. If a list, must match length of `sort_by`. Default is False (ascending order).
272+
Indicates whether to sort in descending order for each column in ``sort_by``. If a single bool,
273+
applies to all columns. If a list, must match length of ``sort_by``. Default is False (ascending order).
269274
filters : dict, optional
270275
Another parameter to specify criteria filters as a dictionary. Use this option when the name of a column
271276
conflicts with a named parameter of this method.
272277
version : str, optional
273-
Deprecated. The version argument is no longer used. Please use `collection` and `catalog` instead.
278+
Deprecated. The version argument is no longer used. Please use ``collection`` and ``catalog`` instead.
274279
pagesize : int, optional
275-
Deprecated. The pagesize argument is no longer used. Please use `limit` instead.
280+
Deprecated. The pagesize argument is no longer used. Please use ``limit`` instead.
276281
page : int, optional
277-
Deprecated. The page argument is no longer used. Please use `offset` instead.
282+
Deprecated. The page argument is no longer used. Please use ``offset`` instead.
278283
**criteria
279284
Keyword arguments representing criteria filters to apply.
280285
@@ -464,7 +469,7 @@ def query_region(
464469
):
465470
"""
466471
Query for MAST catalog entries within a specified region using criteria filters. To return columns for a given
467-
collection and catalog, use `~astroquery.mast.collections.get_catalog_metadata`.
472+
collection and catalog, use `~astroquery.mast.CatalogsClass.get_column_metadata`.
468473
469474
Parameters
470475
----------
@@ -473,10 +478,10 @@ def query_region(
473478
Astropy coordinates object.
474479
radius : str or `~astropy.units.Quantity` object, optional
475480
The search radius around the target coordinates or object. Default 0.2 degrees.
476-
region : str | iterable | `~astropy.regions.CircleSkyRegion` | `~astropy.regions.PolygonSkyRegion`, optional
481+
region : str | iterable | `~regions.CircleSkyRegion` | `~regions.PolygonSkyRegion`, optional
477482
The region to search within. It may be specified as a STC-S POLYGON or CIRCLE string
478483
(e.g., 'CIRCLE 350 -80 0.2'), an iterable of coordinate pairs, or as an
479-
`~astropy.regions.CircleSkyRegion` or `~astropy.regions.PolygonSkyRegion`.
484+
`~regions.CircleSkyRegion` or `~regions.PolygonSkyRegion`.
480485
collection : str, optional
481486
The collection to be queried. If None, uses the instance's `collection` attribute.
482487
catalog : str, optional
@@ -492,17 +497,17 @@ def query_region(
492497
sort_by : str or list of str, optional
493498
Column name(s) to sort the results by.
494499
sort_desc : bool or list of bool, optional
495-
Indicates whether to sort in descending order for each column in `sort_by`. If a single bool,
496-
applies to all columns. If a list, must match length of `sort_by`. Default is False (ascending order).
500+
Indicates whether to sort in descending order for each column in ``sort_by``. If a single bool,
501+
applies to all columns. If a list, must match length of ``sort_by``. Default is False (ascending order).
497502
filters : dict, optional
498503
Another parameter to specify criteria filters as a dictionary. Use this option when the name of a column
499504
conflicts with a named parameter of this method.
500505
version : str, optional
501-
Deprecated. The version argument is no longer used. Please use `collection` and `catalog` instead.
506+
Deprecated. The version argument is no longer used. Please use ``collection`` and ``catalog`` instead.
502507
pagesize : int, optional
503-
Deprecated. The pagesize argument is no longer used. Please use `limit` instead.
508+
Deprecated. The pagesize argument is no longer used. Please use ``limit`` instead.
504509
page : int, optional
505-
Deprecated. The page argument is no longer used. Please use `offset` instead.
510+
Deprecated. The page argument is no longer used. Please use ``offset`` instead.
506511
**criteria
507512
Keyword arguments representing criteria filters to apply.
508513
@@ -597,7 +602,7 @@ def query_object(
597602
):
598603
"""
599604
Query for MAST catalog entries around a specified object name using criteria filters. To return columns
600-
for a given collection and catalog, use `~astroquery.mast.collections.get_catalog_metadata`.
605+
for a given collection and catalog, use `~astroquery.mast.CatalogsClass.get_column_metadata`.
601606
602607
Parameters
603608
----------
@@ -622,17 +627,17 @@ def query_object(
622627
sort_by : str or list of str, optional
623628
Column name(s) to sort the results by.
624629
sort_desc : bool or list of bool, optional
625-
Indicates whether to sort in descending order for each column in `sort_by`. If a single bool,
626-
applies to all columns. If a list, must match length of `sort_by`. Default is False (ascending order).
630+
Indicates whether to sort in descending order for each column in ``sort_by``. If a single bool,
631+
applies to all columns. If a list, must match length of ``sort_by``. Default is False (ascending order).
627632
filters : dict, optional
628633
Another parameter to specify criteria filters as a dictionary. Use this option when the name of a column
629634
conflicts with a named parameter of this method.
630635
version : str, optional
631-
Deprecated. The version argument is no longer used. Please use `collection` and `catalog` instead.
636+
Deprecated. The version argument is no longer used. Please use ``collection`` and ``catalog`` instead.
632637
pagesize : int, optional
633-
Deprecated. The pagesize argument is no longer used. Please use `limit` instead.
638+
Deprecated. The pagesize argument is no longer used. Please use ``limit`` instead.
634639
page : int, optional
635-
Deprecated. The page argument is no longer used. Please use `offset` instead.
640+
Deprecated. The page argument is no longer used. Please use ``offset`` instead.
636641
**criteria
637642
Keyword arguments representing criteria filters to apply.
638643
@@ -1083,17 +1088,18 @@ def _create_adql_region(self, region):
10831088

10841089
# Case 2: region is an astropy region object
10851090
# TODO: When released, change these to use `CircleSphericalSkyRegion` and `PolygonSphericalSkyRegion`
1086-
elif isinstance(region, CircleSkyRegion):
1087-
center = region.center.icrs
1088-
radius = region.radius.to(u.deg).value
1089-
return f"CIRCLE('ICRS',{center.ra.deg},{center.dec.deg},{radius})"
1090-
elif isinstance(region, PolygonSkyRegion):
1091-
verts = region.vertices.icrs
1092-
point_string = ",".join(f"{v.ra.deg},{v.dec.deg}" for v in verts)
1093-
return f"POLYGON('ICRS',{point_string})"
1091+
if HAS_REGIONS:
1092+
if isinstance(region, CircleSkyRegion):
1093+
center = region.center.icrs
1094+
radius = region.radius.to(u.deg).value
1095+
return f"CIRCLE('ICRS',{center.ra.deg},{center.dec.deg},{radius})"
1096+
elif isinstance(region, PolygonSkyRegion):
1097+
verts = region.vertices.icrs
1098+
point_string = ",".join(f"{v.ra.deg},{v.dec.deg}" for v in verts)
1099+
return f"POLYGON('ICRS',{point_string})"
10941100

10951101
# Case 3: region is an iterable of coordinate pairs
1096-
elif isinstance(region, Iterable):
1102+
if isinstance(region, Iterable):
10971103
# Expect something like [(ra1, dec1), (ra2, dec2), ...]
10981104
try:
10991105
points = [float(x) for point in region for x in point]

astroquery/mast/tests/data/README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ To generate `~astroquery.mast.tests.data.tap_columns.vot`, use the following:
9999
>>> result = tap_service.run_sync(query)
100100
>>> writeto(result.votable, 'tap_columns.vot') # doctest: +SKIP
101101

102-
To generate `~astroquery.mast.tests.data.tap_capabilities.vot`, use the following:
102+
To generate `~astroquery.mast.tests.data.tap_capabilities.xml`, use the following:
103103

104104
.. doctest-remote-data::
105105

astroquery/mast/tests/test_mast.py

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from pyvo.dal import TAPResults
2222
from pyvo.dal.exceptions import DALQueryError
2323
from pyvo.io.vosi import parse_capabilities
24-
from regions import CirclePixelRegion, CircleSkyRegion, PixCoord, PolygonSkyRegion
2524
from requests import HTTPError, Response
2625

2726
from astroquery.exceptions import (
@@ -34,6 +33,7 @@
3433
ResolverError,
3534
)
3635
from astroquery.mast import (
36+
CatalogCollection,
3737
Catalogs,
3838
Mast,
3939
MastMissions,
@@ -50,14 +50,21 @@
5050
from astroquery.mast.cloud import CloudAccess
5151
from astroquery.utils.mocks import MockResponse
5252

53-
from ..catalog_collection import DEFAULT_CATALOGS, CatalogCollection, CatalogMetadata
53+
from ..catalog_collection import DEFAULT_CATALOGS, CatalogMetadata
5454

5555
try:
5656
# Optional dependency import for cloud access functionality
5757
from botocore.exceptions import ClientError
5858
except ImportError:
5959
pass
6060

61+
try:
62+
# Optional dependency import for region handling in collections queries
63+
from regions import CirclePixelRegion, CircleSkyRegion, PixCoord, PolygonSkyRegion
64+
HAS_REGIONS = True
65+
except ImportError:
66+
HAS_REGIONS = False
67+
6168
DATA_FILES = {'Mast.Caom.Cone': 'caom.json',
6269
'Mast.Name.Lookup': 'resolver.json',
6370
'mission_search_results': 'mission_results.json',
@@ -315,7 +322,7 @@ def run_sync_mock(query, **kwargs):
315322
# Queries to get column metadata
316323
filename = data_path(DATA_FILES['tap_columns'])
317324
elif 'WHERE' in query:
318-
# Queries with results, keep in mind this is not meaninful and results won't match the query
325+
# Queries with results, keep in mind this is not meaningful and results won't match the query
319326
filename = data_path(DATA_FILES['tap_results'])
320327
votable = parse(filename)
321328

@@ -1797,25 +1804,6 @@ def test_catalogs_create_adql_region(patch_tap):
17971804
)
17981805
assert adql_region == "POLYGON('ICRS',202.4656816,+47.1999842,202.5656816,+47.2999842,202.3656816,+47.0999842)"
17991806

1800-
# Astropy region objects
1801-
cone_region = CircleSkyRegion(
1802-
center=SkyCoord(10.8, 6.5, unit="deg"),
1803-
radius=Angle(0.5, unit="deg")
1804-
)
1805-
adql_region = Catalogs._create_adql_region(region=cone_region)
1806-
assert adql_region == "CIRCLE('ICRS',10.8,6.5,0.5)"
1807-
1808-
polygon_region = PolygonSkyRegion(
1809-
SkyCoord(
1810-
[57.376, 56.391, 56.025, 56.616],
1811-
[24.053, 24.622, 24.049, 24.291],
1812-
frame="icrs",
1813-
unit="deg",
1814-
)
1815-
)
1816-
adql_region = Catalogs._create_adql_region(region=polygon_region)
1817-
assert adql_region == "POLYGON('ICRS',57.376,24.053,56.391,24.622,56.025,24.049,56.616,24.291)"
1818-
18191807
# Iterable coord pairs
18201808
adql_region = Catalogs._create_adql_region(
18211809
region=[
@@ -1827,6 +1815,26 @@ def test_catalogs_create_adql_region(patch_tap):
18271815
)
18281816
assert adql_region == "POLYGON('ICRS',57.376,24.053,56.391,24.622,56.025,24.049,56.616,24.291)"
18291817

1818+
if HAS_REGIONS:
1819+
# Astropy region objects
1820+
cone_region = CircleSkyRegion(
1821+
center=SkyCoord(10.8, 6.5, unit="deg"),
1822+
radius=Angle(0.5, unit="deg")
1823+
)
1824+
adql_region = Catalogs._create_adql_region(region=cone_region)
1825+
assert adql_region == "CIRCLE('ICRS',10.8,6.5,0.5)"
1826+
1827+
polygon_region = PolygonSkyRegion(
1828+
SkyCoord(
1829+
[57.376, 56.391, 56.025, 56.616],
1830+
[24.053, 24.622, 24.049, 24.291],
1831+
frame="icrs",
1832+
unit="deg",
1833+
)
1834+
)
1835+
adql_region = Catalogs._create_adql_region(region=polygon_region)
1836+
assert adql_region == "POLYGON('ICRS',57.376,24.053,56.391,24.622,56.025,24.049,56.616,24.291)"
1837+
18301838

18311839
def test_catalogs_invalid_create_adql_region(patch_tap):
18321840
# Polygon without points
@@ -1855,11 +1863,12 @@ def test_catalogs_invalid_create_adql_region(patch_tap):
18551863
region=[57.376, 24.053, 56.391, 24.622, 56.025, 24.049, 56.616, 24.291]
18561864
)
18571865

1858-
# Invalid astropy region
1859-
with pytest.raises(TypeError, match="Unsupported region type"):
1860-
Catalogs._create_adql_region(
1861-
region=CirclePixelRegion(PixCoord(x=42, y=43), 4.2)
1862-
)
1866+
if HAS_REGIONS:
1867+
# Invalid astropy region
1868+
with pytest.raises(TypeError, match="Unsupported region type"):
1869+
Catalogs._create_adql_region(
1870+
region=CirclePixelRegion(PixCoord(x=42, y=43), 4.2)
1871+
)
18631872

18641873

18651874
def test_catalogs_parse_numeric_expression(patch_tap):

astroquery/mast/tests/test_mast_remote.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from requests.models import Response
1616

1717
from astroquery.mast import (
18+
CatalogCollection,
1819
Catalogs,
1920
Hapcut,
2021
Mast,
@@ -31,7 +32,7 @@
3132
MaxResultsWarning,
3233
NoResultsWarning,
3334
)
34-
from ..catalog_collection import DEFAULT_CATALOGS, CatalogCollection, CatalogMetadata
35+
from ..catalog_collection import DEFAULT_CATALOGS, CatalogMetadata
3536
from ..utils import ResolverError
3637

3738

@@ -1163,11 +1164,12 @@ def test_catalogs_query_criteria(self):
11631164
assert not any(result["known_binary"])
11641165
assert all(np.isin(result["sp_class"], ["O", "B"]))
11651166
assert all(
1166-
(result["gaia_parallax"] < -0.01) | (result["gaia_parallax"] >= 0) | (result["gaia_parallax"] >= -0.3)
1167+
((result["gaia_parallax"] < -0.01) | (result["gaia_parallax"] >= 0))
1168+
& ~(result["gaia_parallax"] < -0.3)
11671169
)
11681170
assert all((result["star_teff"] >= 30000) & (result["star_teff"] <= 50000))
11691171
assert all(result["coordinate_epoch"] == 2016)
1170-
assert all(np.isin(result["spectral_type_ref"], [51, 18, 59, 1]))
1172+
assert all(np.isin(result["spectral_type_ref"], [51, 18, 59]))
11711173
assert all(c in result.colnames for c in select_cols)
11721174

11731175
# Test offset

0 commit comments

Comments
 (0)