Skip to content

Commit fbe64d3

Browse files
authored
Adds new endpoint for bulk query by altid (#79)
* adding new endpoint for query by altid * removing unneeded test * updating docstring * updating docstring * expanding altid enum
1 parent b829883 commit fbe64d3

File tree

4 files changed

+63
-10
lines changed

4 files changed

+63
-10
lines changed

python/valis/db/queries.py

+30
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,9 @@ def get_sdssid_by_altid(id: str | int, idtype: str = None) -> peewee.ModelSelect
962962
elif idtype == 'gaiaid':
963963
# gaia dr3 id , e.g. 4110508934728363520
964964
field = 'gaia_dr3_source_id'
965+
elif idtype == 'sdssid':
966+
# sdss id, e.g. 23326
967+
field = 'sdss_id'
965968
else:
966969
field = 'catalogid'
967970

@@ -991,6 +994,10 @@ def get_target_by_altid(id: str | int, idtype: str = None) -> peewee.ModelSelect
991994
peewee.ModelSelect
992995
the ORM query
993996
"""
997+
# if idtype is explicitly sdss_id, return it directly
998+
if idtype == 'sdssid':
999+
return get_targets_by_sdss_id(id)
1000+
9941001
# get the sdss_id
9951002
targ = get_sdssid_by_altid(id, idtype=idtype)
9961003
res = targ.get_or_none() if targ else None
@@ -1001,6 +1008,29 @@ def get_target_by_altid(id: str | int, idtype: str = None) -> peewee.ModelSelect
10011008
return get_targets_by_sdss_id(res.sdss_id)
10021009

10031010

1011+
def get_targets_by_altid(ids: list, idtype: str = None) -> peewee.ModelSelect:
1012+
""" Get a list of targets by altid
1013+
1014+
Gets targets from a list of alternative identifier. Iterates
1015+
to get the sdss_id for each id, then retrieves the list
1016+
of objects at once.
1017+
1018+
Parameters
1019+
----------
1020+
id : str | int
1021+
the input alternative id
1022+
idtype : str, optional
1023+
the type of integer id, by default None
1024+
1025+
Returns
1026+
-------
1027+
peewee.ModelSelect
1028+
the ORM query
1029+
"""
1030+
res = (j.sdss_id for i in ids for j in get_sdssid_by_altid(i, idtype=idtype) if j)
1031+
return get_targets_by_sdss_id(list(res))
1032+
1033+
10041034
def create_temporary_table(query: peewee.ModelSelect,
10051035
indices: list[str] | None = None) -> Generator[None, None, peewee.Table]:
10061036
"""Create a temporary table from a query."""

python/valis/routes/query.py

+29-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from enum import Enum
66
from typing import List, Union, Dict, Annotated, Optional
7-
from fastapi import APIRouter, Depends, Query, HTTPException
7+
from fastapi import APIRouter, Depends, Query, HTTPException, Body
88
from fastapi_restful.cbv import cbv
99
from pydantic import BaseModel, Field, BeforeValidator
1010

@@ -16,7 +16,7 @@
1616
carton_program_list, carton_program_map,
1717
get_targets_by_sdss_id, get_targets_by_catalog_id,
1818
get_targets_obs, get_paged_target_list_by_mapper,
19-
get_target_by_altid)
19+
get_target_by_altid, get_targets_by_altid)
2020
from valis.routes.auth import set_auth
2121
from sdssdb.peewee.sdss5db import database, catalogdb
2222

@@ -60,6 +60,23 @@ class SDSSIdsModel(BaseModel):
6060
"""Request body for the endpoint returning targets from an sdss_id list"""
6161
sdss_id_list: List[int] = Field(description='List of sdss_id values', example=[67660076, 67151446])
6262

63+
class AltEnum(str, Enum):
64+
""" Enum for the alternative id types """
65+
apogeeid = 'apogeeid'
66+
catalogid = 'catalogid'
67+
gaiaid = 'gaiaid'
68+
sdssid = 'sdssid'
69+
twomassid = 'twomassid'
70+
specobjid = 'specobjid'
71+
platefibermjd = 'platefibermjd'
72+
photoobjid = 'photoobjid'
73+
fieldmjdcatalogid = 'fieldmjdcatalogid'
74+
75+
class AltIdsModel(BaseModel):
76+
"""Request body for the endpoint returning targets from an altid list"""
77+
altid_list: List[str|int] = Field(description='List of altid values', example=['2M10193634+1952122', '2M14030226+5112480'])
78+
idtype: Optional[AltEnum] = Field(None, description='For ambiguous integer ids, the type of id, e.g. "catalogid"', example=['apogeeid'])
79+
6380

6481
router = APIRouter()
6582

@@ -164,11 +181,19 @@ async def sdss_id_search(self, sdss_id: Annotated[int, Query(description='Value
164181
return targets or {}
165182

166183
@router.post('/sdssid', summary='Perform a search for SDSS targets based on a list of sdss_id values',
167-
response_model=List[SDSSidStackedBase],
184+
response_model=List[SDSSModel],
168185
dependencies=[Depends(get_pw_db), Depends(set_auth)])
169186
async def sdss_ids_search(self, body: SDSSIdsModel):
170187
""" Perform a search for SDSS targets based on a list of input sdss_id values."""
171-
return list(get_targets_by_sdss_id(body.sdss_id_list))
188+
return list(append_pipes(get_targets_by_sdss_id(body.sdss_id_list), release=self.release).dicts())
189+
#return list(get_targets_by_sdss_id(body.sdss_id_list))
190+
191+
@router.post('/altids', summary='Performa search for SDSS targets based on a list of alternative ids',
192+
response_model=List[SDSSModel],
193+
dependencies=[Depends(get_pw_db), Depends(set_auth)])
194+
async def altids_search(self, body: AltIdsModel):
195+
""" Perform a search for SDSS targets based on a list of input altid values."""
196+
return list(append_pipes(get_targets_by_altid(body.altid_list, idtype=body.idtype), release=self.release).dicts())
172197

173198
@router.get('/catalogid', summary='Perform a search for SDSS targets based on the catalog_id',
174199
response_model=List[SDSSidStackedBase],

python/valis/routes/target.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,10 @@ async def get_target_altid(self,
178178
idtype: Annotated[str, Query(enum=['catalogid', 'gaiaid'], description='For ambiguous integer ids, the type of id, e.g. "catalogid"', example=None)] = None
179179
):
180180
""" Return target metadata for a given sdss_id """
181-
query = append_pipes(get_target_by_altid(id, idtype=idtype), observed=False)
181+
query = get_target_by_altid(id, idtype=idtype)
182+
if not query:
183+
return {}
184+
query = append_pipes(query, observed=False)
182185
return query.dicts().first() or {}
183186

184187
@router.get('/spectra/{sdss_id}', summary='Retrieve a spectrum for a target sdss_id',

tests/test_utils.py

-5
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,3 @@ def test_build_file_path():
3737
path = build_file_path(astra_res, 'mwmStar', 'IPL3', defaults={'component': ''}, ignore_existence=True)
3838
assert 'sas/ipl-3/spectro/astra/0.5.0/spectra' in path
3939
assert '25/44/mwmStar-0.5.0-54392544.fits' in path
40-
41-
def test_build_fails():
42-
""" test build filepath fails correctly """
43-
with pytest.raises(ValueError, match="Not all path keywords found in model fields or tags: ['component']*"):
44-
build_file_path(astra_res, 'mwmStar', 'IPL3', ignore_existence=True)

0 commit comments

Comments
 (0)