Skip to content

Commit 3342888

Browse files
authored
Merge pull request #2264 from rickynilsson/fix_issue_2239
Fix_issue_2239: Nasa Exoplanet Archive query_object method suddenly produces InvalidTableError
2 parents 4e35238 + 8732c6d commit 3342888

File tree

7 files changed

+199
-36
lines changed

7 files changed

+199
-36
lines changed

CHANGES.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ gaia
4141
the name provided by the user for the output file when the results are
4242
returned by the TAP in compressed format. [#2077]
4343

44+
ipac.nexsci.nasa_exoplanet_archive
45+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
46+
47+
- Fixes to alias query, and regularize keyword removed from deprecated query_star() method. [#2264]
48+
4449
mast
4550
^^^^
4651

@@ -1163,4 +1168,4 @@ Infrastructure, Utility and Other Changes and Additions
11631168
0.1 (2013-09-19)
11641169
================
11651170

1166-
- Initial release. Includes features!
1171+
- Initial release. Includes features!

astroquery/ipac/nexsci/nasa_exoplanet_archive/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class Conf(_config.ConfigNamespace):
2121
url_tap = _config.ConfigItem(
2222
"https://exoplanetarchive.ipac.caltech.edu/TAP/",
2323
"URL for the NASA Exoplanet Archive TAP")
24+
url_aliaslookup = _config.ConfigItem(
25+
"https://exoplanetarchive.ipac.caltech.edu/cgi-bin/Lookup/nph-aliaslookup.py?objname=",
26+
"URL for the NASA Exoplanet Archive aliaslookup")
2427
timeout = _config.ConfigItem(
2528
600, "Time limit for requests from the NASA Exoplanet Archive servers")
2629
cache = _config.ConfigItem(False, "Should the requests be cached?")

astroquery/ipac/nexsci/nasa_exoplanet_archive/core.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import io
66
import re
77
import warnings
8+
import requests
9+
import json
810

911
# Import various astropy modules
1012
import astropy.coordinates as coord
@@ -105,6 +107,8 @@ def get_access_url(service='tap'):
105107
url = conf.url_tap
106108
elif service == 'api':
107109
url = conf.url_api
110+
elif service == 'aliaslookup':
111+
url = conf.url_aliaslookup
108112
return url
109113

110114

@@ -138,7 +142,7 @@ class NasaExoplanetArchiveClass(BaseQuery):
138142
"""
139143

140144
# When module us imported, __init__.py runs and loads a configuration object,
141-
# setting the configuration parameters con.url, conf.timeout and conf.cache
145+
# setting the configuration parameters conf.url, conf.timeout and conf.cache
142146
URL_API = conf.url_api
143147
URL_TAP = conf.url_tap
144148
TIMEOUT = conf.timeout
@@ -377,7 +381,7 @@ def query_aliases(self, object_name, *, cache=None):
377381
Parameters
378382
----------
379383
object_name : str
380-
The name of a planet or star to regularize using the ``aliastable`` table.
384+
The name of a planet or star to regularize using the ``aliaslookup`` service.
381385
cache : bool, optional
382386
Should the request result be cached? This can be useful for large repeated queries,
383387
but since the data in the archive is updated regularly, this defaults to ``False``.
@@ -387,24 +391,47 @@ def query_aliases(self, object_name, *, cache=None):
387391
response : list
388392
A list of aliases found for the object name. The default name will be listed first.
389393
"""
390-
return list(
391-
self.query_criteria(
392-
"aliastable", objname=object_name.strip(), cache=cache, format="csv"
393-
)["aliasdis"]
394-
)
394+
data = self._request_query_aliases(object_name)
395+
396+
try:
397+
objname_split = object_name.split()
398+
if len(objname_split) > 1 and len(objname_split[-1]) == 1 and objname_split[-1].isalpha():
399+
pl_letter = object_name.split()[-1]
400+
else:
401+
pl_letter = ''
402+
403+
default_objname = [data['system']['system_info']['alias_set']['default_name']]
404+
other_objnames = list(set(data['system']['objects']['stellar_set']['stars'][default_objname[0]]['alias_set']['aliases']) - set(default_objname))
405+
other_objnames.sort()
406+
aliases = default_objname + other_objnames
407+
408+
if pl_letter:
409+
aliases = [a + ' ' + pl_letter for a in aliases]
410+
411+
except KeyError:
412+
aliases = []
413+
warnings.warn("No aliases found for name: '{0}'".format(object_name), NoResultsWarning)
414+
415+
return aliases
395416

396417
@class_or_instance
397418
def _regularize_object_name(self, object_name):
398-
"""Regularize the name of a planet or planet host using the ``aliastable`` table"""
419+
"""Regularize the name of a planet or planet host using the ``aliaslookup`` service"""
399420
try:
400421
aliases = self.query_aliases(object_name, cache=False)
401-
except RemoteServiceError:
422+
except KeyError:
402423
aliases = []
403424
if aliases:
404425
return aliases[0]
405426
warnings.warn("No aliases found for name: '{0}'".format(object_name), NoResultsWarning)
406427
return object_name
407428

429+
def _request_query_aliases(self, object_name):
430+
"""Service request for query_aliases()"""
431+
url = requests.get(get_access_url('aliaslookup')+object_name)
432+
response = json.loads(url.text)
433+
return response
434+
408435
# Look for response errors. This might need to be updated for TAP
409436
def _handle_error(self, text):
410437
"""
@@ -653,7 +680,7 @@ def _request_to_sql(self, request_payload):
653680
@deprecated(since="v0.4.1", alternative="query_object")
654681
@deprecated_renamed_argument(["show_progress", "table_path"],
655682
[None, None], "v0.4.1", arg_in_kwargs=True)
656-
def query_planet(self, planet_name, cache=None, regularize=True, **criteria):
683+
def query_planet(self, planet_name, cache=None, **criteria):
657684
"""
658685
Search the ``exoplanets`` table for a confirmed planet
659686
@@ -665,22 +692,18 @@ def query_planet(self, planet_name, cache=None, regularize=True, **criteria):
665692
cache : bool, optional
666693
Should the request result be cached? This can be useful for large repeated queries,
667694
but since the data in the archive is updated regularly, this defaults to ``False``.
668-
regularize : bool, optional
669-
If ``True``, the ``aliastable`` will be used to regularize the target name.
670695
**criteria
671696
Any other filtering criteria to apply. Values provided using the ``where`` keyword will
672697
be ignored.
673698
"""
674-
if regularize:
675-
planet_name = self._regularize_object_name(planet_name)
676699
criteria = self._handle_all_columns_argument(**criteria)
677700
criteria["where"] = "pl_name='{0}'".format(planet_name.strip())
678701
return self.query_criteria("exoplanets", cache=cache, **criteria)
679702

680703
@deprecated(since="v0.4.1", alternative="query_object")
681704
@deprecated_renamed_argument(["show_progress", "table_path"],
682705
[None, None], "v0.4.1", arg_in_kwargs=True)
683-
def query_star(self, host_name, cache=None, regularize=True, **criteria):
706+
def query_star(self, host_name, cache=None, **criteria):
684707
"""
685708
Search the ``exoplanets`` table for a confirmed planet host
686709
@@ -692,14 +715,10 @@ def query_star(self, host_name, cache=None, regularize=True, **criteria):
692715
cache : bool, optional
693716
Should the request result be cached? This can be useful for large repeated queries,
694717
but since the data in the archive is updated regularly, this defaults to ``False``.
695-
regularize : bool, optional
696-
If ``True``, the ``aliastable`` will be used to regularize the target name.
697718
**criteria
698719
Any other filtering criteria to apply. Values provided using the ``where`` keyword will
699720
be ignored.
700721
"""
701-
if regularize:
702-
host_name = self._regularize_object_name(host_name)
703722
criteria = self._handle_all_columns_argument(**criteria)
704723
criteria["where"] = "pl_hostname='{0}'".format(host_name.strip())
705724
return self.query_criteria("exoplanets", cache=cache, **criteria)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{
2+
"manifest": {
3+
"requested_name": "bet pic",
4+
"resolved_name": "bet Pic",
5+
"lookup_status": "OK",
6+
"compilation_date": "2022-01-28 14:31:40.254838",
7+
"system_snapshot": {
8+
"number_of_stars": 1,
9+
"number_of_planets": 2
10+
}
11+
},
12+
"system": {
13+
"system_info": {
14+
"id": "bet Pic",
15+
"alias_set": {
16+
"item_count": 1,
17+
"default_name": "bet Pic",
18+
"aliases": [
19+
"bet Pic"
20+
]
21+
}
22+
},
23+
"objects": {
24+
"stellar_set": {
25+
"item_count": 1,
26+
"stars": {
27+
"bet Pic": {
28+
"alias_set": {
29+
"item_count": 14,
30+
"aliases": [
31+
"TIC 270577175",
32+
"Gaia DR2 4792774797545105664",
33+
"GJ 219",
34+
"HR 2020",
35+
"bet Pic",
36+
"HD 39060",
37+
"HIP 27321",
38+
"SAO 234134",
39+
"CD-51 1620",
40+
"CPD-51 774",
41+
"IRAS 05460-5104",
42+
"TYC 8099-01392-1",
43+
"2MASS J05471708-5103594",
44+
"WISE J054717.10-510358.4"
45+
]
46+
}
47+
}
48+
}
49+
},
50+
"planet_set": {
51+
"item_count": 2,
52+
"planets": {
53+
"bet Pic b": {
54+
"alias_set": {
55+
"item_count": 14,
56+
"aliases": [
57+
"GJ 219 b",
58+
"HR 2020 b",
59+
"bet Pic b",
60+
"HD 39060 b",
61+
"HIP 27321 b",
62+
"SAO 234134 b",
63+
"CD-51 1620 b",
64+
"CPD-51 774 b",
65+
"IRAS 05460-5104 b",
66+
"TYC 8099-01392-1 b",
67+
"2MASS J05471708-5103594 b",
68+
"WISE J054717.10-510358.4 b",
69+
"TIC 270577175 b",
70+
"Gaia DR2 4792774797545105664 b"
71+
]
72+
}
73+
},
74+
"bet Pic c": {
75+
"alias_set": {
76+
"item_count": 14,
77+
"aliases": [
78+
"GJ 219 c",
79+
"HR 2020 c",
80+
"bet Pic c",
81+
"HD 39060 c",
82+
"HIP 27321 c",
83+
"SAO 234134 c",
84+
"CD-51 1620 c",
85+
"CPD-51 774 c",
86+
"IRAS 05460-5104 c",
87+
"TYC 8099-1392-1 c",
88+
"2MASS J05471708-5103594 c",
89+
"WISE J054717.10-510358.4 c",
90+
"TIC 270577175 c",
91+
"Gaia DR2 4792774797545105664 c"
92+
]
93+
}
94+
}
95+
}
96+
}
97+
}
98+
}
99+
}

astroquery/ipac/nexsci/nasa_exoplanet_archive/tests/test_nasa_exoplanet_archive.py

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from astroquery.exceptions import NoResultsWarning
1616
from astroquery.utils.mocks import MockResponse
17-
from astroquery.ipac.nexsci.nasa_exoplanet_archive.core import NasaExoplanetArchiveClass, conf, InvalidTableError
17+
from astroquery.ipac.nexsci.nasa_exoplanet_archive.core import NasaExoplanetArchiveClass, conf, InvalidTableError, get_access_url
1818
try:
1919
from unittest.mock import Mock, patch, PropertyMock
2020
except ImportError:
@@ -119,16 +119,53 @@ def patch_get(request): # pragma: nocover
119119
return mp
120120

121121

122-
def test_regularize_object_name(patch_get):
123-
NasaExoplanetArchiveMock = NasaExoplanetArchiveClass()
122+
# aliaslookup file in data/
123+
LOOKUP_DATA_FILE = 'bpic_aliaslookup.json'
124+
125+
126+
def data_path(filename):
127+
data_dir = os.path.join(os.path.dirname(__file__), 'data')
128+
return os.path.join(data_dir, filename)
129+
130+
131+
# monkeypatch replacement request function
132+
def query_aliases_mock(self, *args, **kwargs):
133+
with open(data_path(LOOKUP_DATA_FILE), 'rb') as f:
134+
response = json.load(f)
135+
return response
136+
137+
138+
# use a pytest fixture to create a dummy 'requests.get' function,
139+
# that mocks(monkeypatches) the actual 'requests.get' function:
140+
@pytest.fixture
141+
def query_aliases_request(request):
142+
mp = request.getfixturevalue("monkeypatch")
143+
mp.setattr(NasaExoplanetArchiveClass, '_request_query_aliases', query_aliases_mock)
144+
return mp
145+
146+
147+
def test_query_aliases(query_aliases_request):
148+
nasa_exoplanet_archive = NasaExoplanetArchiveClass()
149+
result = nasa_exoplanet_archive.query_aliases(object_name='bet Pic')
150+
assert len(result) > 10
151+
assert 'GJ 219' in result
152+
assert 'bet Pic' in result
153+
assert '2MASS J05471708-5103594' in result
154+
155+
156+
def test_query_aliases_planet(query_aliases_request):
157+
nasa_exoplanet_archive = NasaExoplanetArchiveClass()
158+
result = nasa_exoplanet_archive.query_aliases('bet Pic b')
159+
assert len(result) > 10
160+
assert 'GJ 219 b' in result
161+
assert 'bet Pic b' in result
162+
assert '2MASS J05471708-5103594 b' in result
124163

125-
NasaExoplanetArchiveMock._tap_tables = ['list']
126-
assert NasaExoplanetArchiveMock._regularize_object_name("kepler 2") == "HAT-P-7"
127-
assert NasaExoplanetArchiveMock._regularize_object_name("kepler 1 b") == "TrES-2 b"
128164

129-
with pytest.warns(NoResultsWarning) as warning:
130-
NasaExoplanetArchiveMock._regularize_object_name("not a planet")
131-
assert "No aliases found for name: 'not a planet'" == str(warning[0].message)
165+
def test_get_access_url():
166+
assert get_access_url('tap') == conf.url_tap
167+
assert get_access_url('api') == conf.url_api
168+
assert get_access_url('aliaslookup') == conf.url_aliaslookup
132169

133170

134171
def test_backwards_compat(patch_get):

astroquery/ipac/nexsci/nasa_exoplanet_archive/tests/test_nasa_exoplanet_archive_remote.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def test_query_region():
159159
def test_query_aliases():
160160
name = "bet Pic"
161161
aliases = NasaExoplanetArchive.query_aliases(name)
162-
assert len(aliases) == 12
162+
assert len(aliases) > 10
163163
assert "HD 39060" in aliases
164164

165165

0 commit comments

Comments
 (0)