Skip to content

Commit

Permalink
Merge branch 'main' into albireox/issue33
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Aug 8, 2024
2 parents f87c3ce + b30d03d commit d5612e1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Change Log
* Adds ``db_reset`` option to avoid closing and resetting the database connection
* Updates main, cone, and carton/program search to use new ``has_been_observed`` column; default is True.
* Updates the ``append_pipes`` query to return the new ``has_been_observed`` column
* Include parent catalog associations in the ``/target/catalogs`` endpoint (#37).
* Adds endpoint to get parent catalog data for a given target (#38).

0.1.0 (10-24-2023)
Expand Down
14 changes: 12 additions & 2 deletions python/valis/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import datetime
import math
from typing import Optional, Annotated, Any, TypeVar
from pydantic import ConfigDict, BaseModel, Field, BeforeValidator, field_validator, FieldValidationInfo
from pydantic import ConfigDict, BaseModel, Field, BeforeValidator, FieldSerializationInfo, field_serializer, field_validator, FieldValidationInfo
from enum import Enum


Expand Down Expand Up @@ -204,7 +204,17 @@ class ParentCatalogModel(PeeweeBase):

class CatalogResponse(CatalogModel, SDSSidFlatBase):
""" Response model for source catalog and sdss_id information """
pass

parent_catalogs: dict[str, Any] = Field(..., description='The parent catalog associations for a given catalogid')

@field_serializer('parent_catalogs')
def serialize_parent_catalogs(v: dict[str, Any], info: FieldSerializationInfo) -> dict[str, Any]:
""" Serialize the parent catalogs, excluding None values and trimming strings."""

if info.exclude_none:
return {k: v.strip() if isinstance(v, str) else v for k, v in v.items() if v is not None}
else:
return v


class CartonModel(PeeweeBase):
Expand Down
14 changes: 8 additions & 6 deletions python/valis/db/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import itertools
import packaging
from typing import Union, Generator
from typing import Sequence, Union, Generator

import astropy.units as u
import deepmerge
Expand Down Expand Up @@ -113,8 +113,8 @@ def convert_coords(ra: Union[str, float], dec: Union[str, float]) -> tuple:
"""
is_hms = set('hms: ') & set(str(ra))
if is_hms:
ra = str(ra).replace(' ', ':')
dec = str(dec).replace(' ', ':')
ra = str(ra).strip().replace(' ', ':')
dec = str(dec).strip().replace(' ', ':')
unit = ('hourangle', 'degree') if is_hms else ('degree', 'degree')
coord = SkyCoord(f'{ra} {dec}', unit=unit)
ra = round(coord.ra.value, 5)
Expand Down Expand Up @@ -182,7 +182,7 @@ def get_targets_by_sdss_id(sdss_id: Union[int, list[int]] = []) -> peewee.ModelS
peewee.ModelSelect
the ORM query
"""
if type(sdss_id) is int:
if type(sdss_id) in (int, str):
sdss_id = [sdss_id]

return vizdb.SDSSidStacked.select().where(vizdb.SDSSidStacked.sdss_id.in_(sdss_id))
Expand Down Expand Up @@ -690,8 +690,10 @@ def get_catalog_sources(sdss_id: int) -> peewee.ModelSelect:
"""

s = vizdb.SDSSidFlat.select(vizdb.SDSSidFlat).where(vizdb.SDSSidFlat.sdss_id == sdss_id).alias('s')
return cat.Catalog.select(cat.Catalog, starfields(s)).\
join(s, on=(s.c.catalogid == cat.Catalog.catalogid)).order_by(cat.Catalog.version.desc())
return cat.Catalog.select(cat.Catalog, cat.SDSS_ID_To_Catalog, starfields(s)).\
join(s, on=(s.c.catalogid == cat.Catalog.catalogid)).\
join(cat.SDSS_ID_To_Catalog, on=(s.c.catalogid == cat.SDSS_ID_To_Catalog.catalogid)).\
order_by(cat.Catalog.version.desc())


def get_parent_catalog_data(sdss_id: int, catalog: str) -> peewee.ModelSelect:
Expand Down
9 changes: 7 additions & 2 deletions python/valis/routes/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ async def main_search(self, body: SearchModel):
""" Main query for UI and for combining queries together """

print('form data', body)
query = None

# build the coordinate query
if body.ra and body.dec:
Expand All @@ -97,9 +98,13 @@ async def main_search(self, body: SearchModel):
'program' if body.program else 'carton',
query=query)
# append query to pipes
query = append_pipes(query, observed=body.observed)
if query:
query = append_pipes(query, observed=body.observed)

return {'status': 'success', 'data': query.dicts().iterator(), 'msg': 'data successfully retrieved'}
# query iterator
res = query.dicts().iterator() if query else []

return {'status': 'success', 'data': res, 'msg': 'data successfully retrieved'}

@router.get('/cone', summary='Perform a cone search for SDSS targets with sdss_ids',
response_model=List[SDSSModel], dependencies=[Depends(get_pw_db)])
Expand Down
16 changes: 14 additions & 2 deletions python/valis/routes/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re
import httpx
import orjson
from typing import Tuple, List, Union, Optional, Annotated
from typing import Any, Tuple, List, Union, Optional, Annotated
from pydantic import field_validator, model_validator, BaseModel, Field, model_serializer
from fastapi import APIRouter, HTTPException, Query, Path, Depends
from fastapi_restful.cbv import cbv
Expand Down Expand Up @@ -182,7 +182,19 @@ async def get_spectrum(self, sdss_id: Annotated[int, Path(title="The sdss_id of
response_model_exclude_unset=True, response_model_exclude_none=True)
async def get_catalogs(self, sdss_id: int = Path(title="The sdss_id of the target to get", example=23326)):
""" Return catalog information for a given sdss_id """
return get_catalog_sources(sdss_id).dicts().iterator()

sdss_id_data = get_catalog_sources(sdss_id).dicts()

# The response has the parent catalogs at the same level as the other
# columns. For the response we want to nest them under a parent_catalogs key.
# This takes advantage that all the parent catalog columns have '__' in the name.
response_data: list[dict[str, Any]] = []
for row in sdss_id_data:
s_data = {k: v for k, v in row.items() if '__' not in k}
cat_data = {k.split('__')[0]: v for k, v in row.items() if '__' in k}
response_data.append({**s_data, 'parent_catalogs': cat_data})

return response_data

@router.get('/parents/{parent_catalog}/{sdss_id}',
dependencies=[Depends(get_pw_db)],
Expand Down

0 comments on commit d5612e1

Please sign in to comment.