Skip to content

Commit

Permalink
Add optional hostgal info to elasticc2/ltcv and elasticc2/gethottrans…
Browse files Browse the repository at this point in the history
…ients
  • Loading branch information
rknop committed Dec 4, 2024
1 parent cf2272b commit 6eeec4c
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 52 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ TODO: flesh out
You can get lightcurves for objects at the URL `elasticc2/ltcv`. POST to this URL, with the post body a json-encoded dict, with keys:
* `objectid` or `objecitds`: One of these two is required. Either the numerical ID of the object whose lightcurve you want, or a list of such numerical ids.
* `mjd_now`: (Optional.) For testing purposes. Normally, you will get all known photometry for an object. Pass an MJD here, and you'll only get photometry before that mjd.
* `hostgal_info`: (Optional.) 0 or 1, default 0. If 1, return information about the first possible host galaxy for each transient.
* `return_format`: (Optional.) 0, 1, or 2, default 0. (See below.)

Example:
Expand Down Expand Up @@ -318,6 +319,9 @@ In all cases, what you find in `objectid` is what you will use to indicate a giv
}
```

*For all three return formats*

If you specified `include_hostinfo`, there will be additional keys `hostgal_mag_*` and `hostgal_magerr_*` (where * is u, g, r, i, z), as well as `hostgal_ellipticity` and `hostgal_sqradius`. The values of these are all floats (or lists of floats, for return format 2).

## <a name="elasticc2spec"></a>Getting and pushing spectrum information

Expand All @@ -329,6 +333,7 @@ Currently hot transients can be found at the URL `elasticc2/gethottransients`.
* `detected_since_mjd: float` — will return all SNe detected since the indicated mjd. ("Detected" means a LSST alert was sent out, and at least one broker has returned a classification.)
* `detected_in_last_days: float` — will return all SNe detected between this many days before now and now. The TOM will search what it knows about forced photometry, considering any point with S/N>5 as a detection.
* `mjd_now: float` — The mjd of right now. Usually you don't want to specify this, and the server will automatically determine the current MJD. This is here so it can be used with simulations, where "right now" in the simulation may not be the real right now. You will not get back any detections or forced photometry with an MJD newer than this value.
* `hostgal_info`: (Optional.) 0 or 1, default 0. If 1, return information about the first possible host galaxy for each transient.
* `return_format: int` — 0, 1, or 2. Optional, defaults to 0.

Example:
Expand Down
48 changes: 48 additions & 0 deletions tests/test_elasticc2_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,14 @@ def test_alert_api( self, elasticc2_ppdb_class, tomclient ):

class TestLtcv:
def test_ltcvs( self, elasticc2_database_snapshot_class, tomclient ):
# Make sure it objects to an unknown keyword
res = tomclient.post( "elasticc2/ltcv", json={ 'objectid': 1552185, 'foo': 1 } )
assert res.status_code == 500
assert res.text == "Exception in LtcvsView: Unknown parameters: {'foo'}"

# Try with a single objectid
res = tomclient.post( "elasticc2/ltcv", json={ 'objectid': 1552185 } )
assert res.status_code == 200
data = res.json()

assert data['status'] == 'ok'
Expand All @@ -128,20 +134,23 @@ def test_ltcvs( self, elasticc2_database_snapshot_class, tomclient ):

# Get full lightcurves
res = tomclient.post( "elasticc2/ltcv", json={ 'objectid': testobjs } )
assert res.status_code == 200
data = res.json()

assert data['status'] == 'ok'
assert len( data['diaobject'] ) == 3
assert set( data['diaobject'][i]['objectid'] for i in [0,1,2] ) == set( testobjs )
fullltcvlens = []
for i in range(3):
assert set( data['diaobject'][i].keys() ) == { 'objectid', 'ra', 'dec', 'zp', 'photometry' }
fullltcvlens.append( len( data['diaobject'][i]['photometry']['mjd'] ) )
assert fullltcvlens[i] > 0
for field in [ 'band', 'flux', 'fluxerr' ]:
assert len( data['diaobject'][i]['photometry'][field] ) == fullltcvlens[i]

# Test a fake current mjd
res = tomclient.post( "elasticc2/ltcv", json={ 'objectid': testobjs, 'mjd_now': 60420 } )
assert res.status_code == 200
data = res.json()

assert data['status'] == 'ok'
Expand All @@ -153,14 +162,53 @@ def test_ltcvs( self, elasticc2_database_snapshot_class, tomclient ):
assert partialltcvlens[i] < fullltcvlens[i]
assert all ( m < 60420 for m in data['diaobject'][i]['photometry']['mjd'] )


# Make sure that include_hostinfo works
res = tomclient.post( "elasticc2/ltcv", json={ 'objectid': testobjs, 'include_hostinfo': 1 } )
assert res.status_code == 200
data = res.json()
assert data['status'] == 'ok'
assert len( data['diaobject'] ) == 3
assert data['diaobject'][0].keys() == { 'objectid', 'ra','dec', 'zp', 'photometry',
'hostgal_mag_u', 'hostgal_magerr_u',
'hostgal_mag_g', 'hostgal_magerr_g',
'hostgal_mag_r', 'hostgal_magerr_r',
'hostgal_mag_i', 'hostgal_magerr_i',
'hostgal_mag_z', 'hostgal_magerr_z',
'hostgal_mag_y', 'hostgal_magerr_y',
'hostgal_ellipticity', 'hostgal_sqradius' }


# Test returnformat 2 (TODO : returnformat 1)

# Test a fake current mjd
res = tomclient.post( "elasticc2/ltcv", json={ 'objectid': testobjs, 'mjd_now': 60420, 'return_format': 2 } )
assert res.status_code == 200
data = res.json()
assert data['status'] == 'ok'
df = pandas.DataFrame( data['diaobject'] )
assert set( df.columns ) == { 'objectid', 'ra', 'dec', 'zp', 'mjd', 'band', 'flux', 'fluxerr' }
assert set( df.objectid ) == set( testobjs )
assert all( len( df.mjd[i] ) == partialltcvlens[i] for i in range(2) )
assert all( len( df.mjd[i] ) == len( df[field][i] )
for field in [ 'band', 'flux', 'fluxerr' ]
for i in range(2) )

res = tomclient.post( "elasticc2/ltcv", json={ 'objectid': testobjs, 'mjd_now': 60420,
'include_hostinfo': 1,
'return_format': 2 } )
assert res.status_code == 200
data = res.json()
assert data['status'] == 'ok'
df = pandas.DataFrame( data['diaobject'] )
assert set( df.columns ) == { 'objectid', 'ra', 'dec', 'zp', 'mjd', 'band', 'flux', 'fluxerr',
'hostgal_mag_u', 'hostgal_magerr_u',
'hostgal_mag_g', 'hostgal_magerr_g',
'hostgal_mag_r', 'hostgal_magerr_r',
'hostgal_mag_i', 'hostgal_magerr_i',
'hostgal_mag_z', 'hostgal_magerr_z',
'hostgal_mag_y', 'hostgal_magerr_y',
'hostgal_ellipticity', 'hostgal_sqradius' }
assert set( df.objectid ) == set( testobjs )
assert all( len( df.mjd[i] ) == partialltcvlens[i] for i in range(2) )
assert all( len( df.mjd[i] ) == len( df[field][i] )
Expand Down
61 changes: 61 additions & 0 deletions tests/test_elasticc2_spectrumcycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import dateutil.parser
import pytest

import numpy
import pandas
import astropy.time

sys.path.insert( 0, "/tom_desc" )
Expand Down Expand Up @@ -52,12 +54,19 @@ def ask_for_spectra( self, elasticc2_database_snapshot_class, tomclient ):

# TODO : test things other than detected_since_mjd sent to gethottransients
def test_hot_sne( self, elasticc2_database_snapshot_class, tomclient ):
# Make sure it rejects bad keywords
res = tomclient.post( 'elasticc2/gethottransients', json={ 'foo': 0 } )
assert res.status_code == 500
assert res.text == "Error, unknown parameters passed in request body: ['foo']"

# Testing detected_in_last_days is fraught because
# the mjds in elasticc2 are what they are, are
# in the future (as of this comment writing).
# So, go old school and just not test it.
# (Should test with mjd_now...)

res = tomclient.post( 'elasticc2/gethottransients', json={ 'detected_since_mjd': 60660 } )
assert res.status_code == 200
sne = res.json()['diaobject']
assert len(sne) == 8

Expand All @@ -68,6 +77,58 @@ def test_hot_sne( self, elasticc2_database_snapshot_class, tomclient ):
assert set( sne[0].keys() ) == { 'objectid', 'ra', 'dec', 'photometry', 'zp', 'redshift', 'sncode' }
assert set( sne[0]['photometry'].keys() ) == { 'mjd', 'band', 'flux', 'fluxerr' }

# Make sure the include_hostinfo parameter works
res = tomclient.post( 'elasticc2/gethottransients', json={ 'detected_since_mjd': 60660,
'include_hostinfo': 1 } )
assert res.status_code == 200
sne = res.json()['diaobject']
assert len(sne) == 8
snids = { s['objectid'] for s in sne }
assert snids == { 15232, 1913410, 2110476, 416626, 1286131, 1684659, 1045654, 1263066 }
assert set( sne[0].keys() ) == { 'objectid', 'ra', 'dec', 'photometry', 'zp', 'redshift', 'sncode',
'hostgal_mag_u','hostgal_magerr_u',
'hostgal_mag_g','hostgal_magerr_g',
'hostgal_mag_r','hostgal_magerr_r',
'hostgal_mag_i','hostgal_magerr_i',
'hostgal_mag_z','hostgal_magerr_z',
'hostgal_mag_y','hostgal_magerr_y',
'hostgal_ellipticity', 'hostgal_sqradius'
}
assert set( sne[0]['photometry'].keys() ) == { 'mjd', 'band', 'flux', 'fluxerr' }

# Try return format 2
res = tomclient.post( 'elasticc2/gethottransients', json={ 'detected_since_mjd': 60660,
'return_format': 2 } )
assert res.status_code == 200
df = pandas.DataFrame( res.json()['diaobject'] )
assert len(df) == 8
assert set( df.objectid.values ) == { 15232, 1913410, 2110476, 416626, 1286131, 1684659, 1045654, 1263066 }
assert set( df.columns ) == { 'objectid', 'ra', 'dec', 'mjd', 'band', 'flux', 'fluxerr',
'zp', 'redshift', 'sncode' }
assert df.mjd.dtype == numpy.dtype('O')
assert len( df.mjd[0] ) > 1

res = tomclient.post( 'elasticc2/gethottransients', json={ 'detected_since_mjd': 60660,
'include_hostinfo': 1,
'return_format': 2 } )
assert res.status_code == 200
df = pandas.DataFrame( res.json()['diaobject'] )
assert len(df) == 8
assert set( df.objectid.values ) == { 15232, 1913410, 2110476, 416626, 1286131, 1684659, 1045654, 1263066 }
assert set( df.columns ) == { 'objectid', 'ra', 'dec', 'mjd', 'band', 'flux', 'fluxerr',
'zp', 'redshift', 'sncode',
'hostgal_mag_u','hostgal_magerr_u',
'hostgal_mag_g','hostgal_magerr_g',
'hostgal_mag_r','hostgal_magerr_r',
'hostgal_mag_i','hostgal_magerr_i',
'hostgal_mag_z','hostgal_magerr_z',
'hostgal_mag_y','hostgal_magerr_y',
'hostgal_ellipticity', 'hostgal_sqradius'
}
assert df.mjd.dtype == numpy.dtype('O')
assert len( df.mjd[0] ) > 1



def test_ask_for_spectra( self, ask_for_spectra, tomclient ):
objs, prios = ask_for_spectra
Expand Down
Loading

0 comments on commit 6eeec4c

Please sign in to comment.