From c579381cf187f5ba6276f5bb23c49b52017ca2d4 Mon Sep 17 00:00:00 2001 From: Diego Rodriguez Date: Mon, 15 Mar 2021 12:16:14 -0300 Subject: [PATCH] added deprecation warning --- alerce/api.py | 116 ++++++++++++++++++++++++++++++-------------------- tests/api.py | 58 +++++++++++-------------- 2 files changed, 94 insertions(+), 80 deletions(-) diff --git a/alerce/api.py b/alerce/api.py index 086ab1e..102d436 100644 --- a/alerce/api.py +++ b/alerce/api.py @@ -30,6 +30,11 @@ class AlerceAPI(object): """ def __init__(self, **kwargs): + warnings.warn( + "This python client will be deprecated soon. Please visit https://github.com/alercebroker/alerce_client_new for information on the future client", + DeprecationWarning, + stacklevel=2, + ) self.ztf_url = "http://ztf.alerce.online" if "ztf_url" in kwargs.keys(): @@ -68,7 +73,7 @@ def query(self, params, format="votable"): //Number of detections nobs : { min : int , - max : int + max : int }, //Late Classifier (Random Forest) classrf : string or int , @@ -76,18 +81,18 @@ def query(self, params, format="votable"): //Early Classifier (Stamp Classifier) classearly : list, string or int , pclassearly : float [0-1] , - } + } //Coordinate based search (RA,DEC) and Search Radius. coordinates : { ra : float degrees , dec : float degrees , - sr : float degrese + sr : float degrese }, dates : { //First detection (Discovery date) firstmjd : { min : float mjd , - max : float mjd + max : float mjd } } } @@ -115,7 +120,7 @@ def query(self, params, format="votable"): if format == "pandas": query_results = query_results.to_pandas() - query_results.set_index('oid', inplace=True) + query_results.set_index("oid", inplace=True) return query_results @@ -136,7 +141,7 @@ def get_sql(self, params): r = self.session.post(url="%s/get_sql" % self.ztf_url, json=params) - return r.content.decode('utf-8') + return r.content.decode("utf-8") def get_detections(self, oid, format="votable"): """Get detections for an object. @@ -159,9 +164,7 @@ def get_detections(self, oid, format="votable"): """ # oid - params = { - "oid": oid - } + params = {"oid": oid} # show api results r = self.session.post(url="%s/get_detections" % self.ztf_url, json=params) @@ -178,7 +181,7 @@ def get_detections(self, oid, format="votable"): if format == "pandas": detections = detections.to_pandas() detections.sort_values("mjd", inplace=True) - detections.set_index('candid', inplace=True) + detections.set_index("candid", inplace=True) return detections return detections @@ -200,9 +203,7 @@ def get_non_detections(self, oid, format="votable"): """ - params = { - "oid": oid - } + params = {"oid": oid} # show api results r = self.session.post(url="%s/get_non_detections" % self.ztf_url, json=params) @@ -219,7 +220,7 @@ def get_non_detections(self, oid, format="votable"): if format == "pandas": non_detections = non_detections.to_pandas() non_detections.sort_values("mjd", inplace=True) - non_detections.set_index('mjd', inplace=True) + non_detections.set_index("mjd", inplace=True) return non_detections @@ -242,9 +243,7 @@ def get_stats(self, oid, format="votable"): """ - params = { - "oid": oid - } + params = {"oid": oid} r = self.session.post(url="%s/get_stats" % self.ztf_url, json=params) @@ -261,7 +260,7 @@ def get_stats(self, oid, format="votable"): self.firstMJD = stats["firstmjd"] if format == "votable": - stats = {key:[stats[key]] for key in stats} + stats = {key: [stats[key]] for key in stats} stats = Table(stats) elif format == "pandas": stats = pd.Series(stats) @@ -271,7 +270,7 @@ def get_stats(self, oid, format="votable"): return stats def get_probabilities(self, oid, early=True, late=True, format="votable"): - """ Get probabilities for a given object. + """Get probabilities for a given object. Parameters @@ -298,9 +297,7 @@ def get_probabilities(self, oid, early=True, late=True, format="votable"): """ - params = { - "oid": oid - } + params = {"oid": oid} # show api results r = self.session.post(url="%s/get_probabilities" % self.ztf_url, json=params) @@ -308,7 +305,9 @@ def get_probabilities(self, oid, early=True, late=True, format="votable"): if early: try: if format == "pandas": - df_early = pd.Series(r.json()["result"]["probabilities"]["early_classifier"]) + df_early = pd.Series( + r.json()["result"]["probabilities"]["early_classifier"] + ) elif format == "votable": resp = r.json()["result"]["probabilities"]["early_classifier"] df_early = Table({key: [resp[key]] for key in resp}) @@ -321,7 +320,9 @@ def get_probabilities(self, oid, early=True, late=True, format="votable"): if late: try: if format == "pandas": - df_late = pd.Series(r.json()["result"]["probabilities"]["late_classifier"]) + df_late = pd.Series( + r.json()["result"]["probabilities"]["late_classifier"] + ) elif format == "votable": resp = r.json()["result"]["probabilities"]["late_classifier"] df_late = Table({key: [resp[key]] for key in resp}) @@ -356,9 +357,7 @@ def get_features(self, oid, format="votable"): """ - params = { - "oid": oid - } + params = {"oid": oid} # show api results r = self.session.post(url="%s/get_features" % self.ztf_url, json=params) @@ -379,7 +378,7 @@ def get_features(self, oid, format="votable"): return features def catsHTM_conesearch(self, oid, radius, catalog_name="all", format="votable"): - """ catsHTM conesearch given an object and catalog_name. + """catsHTM conesearch given an object and catalog_name. Parameters ---------- @@ -409,12 +408,16 @@ def catsHTM_conesearch(self, oid, radius, catalog_name="all", format="votable"): "catalog": "%s" % catalog_name, "ra": "%f" % self.meanra, "dec": "%f" % self.meandec, - "radius": "%f" % radius + "radius": "%f" % radius, } if catalog_name != "all": - result = self.session.get(url="%s/conesearch" % self.catsHTM_url, params=params) + result = self.session.get( + url="%s/conesearch" % self.catsHTM_url, params=params + ) else: - result = self.session.get(url="%s/conesearch_all" % self.catsHTM_url, params=params) + result = self.session.get( + url="%s/conesearch_all" % self.catsHTM_url, params=params + ) votables = {} @@ -431,9 +434,11 @@ def catsHTM_conesearch(self, oid, radius, catalog_name="all", format="votable"): continue t = Table() for field in r[key].keys(): - data = r[key][field]['values'] # list(map(lambda x: x["value"], r[key][field])) + data = r[key][field][ + "values" + ] # list(map(lambda x: x["value"], r[key][field])) t.add_column(Column(data, name=field)) - t[field].unit = r[key][field]['units'] + t[field].unit = r[key][field]["units"] t["cat_name"] = Column(["catsHTM_%s" % key], name="cat_name") if format == "pandas": t = t.to_pandas() @@ -442,7 +447,7 @@ def catsHTM_conesearch(self, oid, radius, catalog_name="all", format="votable"): return votables def catsHTM_crossmatch(self, oid, radius=100, catalog_name="all", format="votable"): - """ catsHTM crossmatch given an object and catalog_name. + """catsHTM crossmatch given an object and catalog_name. Parameters ---------- @@ -472,12 +477,16 @@ def catsHTM_crossmatch(self, oid, radius=100, catalog_name="all", format="votabl "catalog": "%s" % catalog_name, "ra": "%f" % self.meanra, "dec": "%f" % self.meandec, - "radius": "%f" % radius + "radius": "%f" % radius, } if catalog_name != "all": - result = self.session.get(url="%s/crossmatch" % self.catsHTM_url, params=params) + result = self.session.get( + url="%s/crossmatch" % self.catsHTM_url, params=params + ) else: - result = self.session.get(url="%s/crossmatch_all" % self.catsHTM_url, params=params) + result = self.session.get( + url="%s/crossmatch_all" % self.catsHTM_url, params=params + ) votables = {} try: @@ -497,14 +506,16 @@ def catsHTM_crossmatch(self, oid, radius=100, catalog_name="all", format="votabl catalog_data = catalog_result[catalog_name] for field in catalog_data: - data = catalog_data[field]['value'] - unit = catalog_data[field]['unit'] + data = catalog_data[field]["value"] + unit = catalog_data[field]["unit"] if format == "pandas": t[field] = data elif format == "votable": t.add_column(Column([data], name=field)) t[field].unit = unit - t["cat_name"] = Column(["catsHTM_%s" % catalog_name], name="cat_name") + t["cat_name"] = Column( + ["catsHTM_%s" % catalog_name], name="cat_name" + ) else: return None if format == "pandas": @@ -543,13 +554,13 @@ def catsHTM_redshift(self, oid, radius, format="votable"): # check whether redshift exists for catname in xmatches: for col in xmatches[catname].keys(): - if col == 'z': + if col == "z": return float(xmatches[catname][col]) # check whether photometric redshift exists for catname in xmatches: for col in xmatches[catname].keys(): - if col in ['zphot', 'z_phot']: + if col in ["zphot", "z_phot"]: return float(xmatches[catname][col]) return @@ -558,6 +569,7 @@ def _in_ipynb(self): try: from IPython import get_ipython import os + if "IPKernelApp" not in get_ipython().config: # pragma: no cover raise ImportError("console") return False @@ -585,7 +597,7 @@ def plot_stamp(self, oid, candid=None): Display the stamps on a jupyter notebook. """ - '' + "" # if candid is None, get minimum candid if candid is None: @@ -596,7 +608,10 @@ def plot_stamp(self, oid, candid=None): return science = "%s/get_stamp?oid=%s&candid=%s&type=science&format=png" % ( - self.avro_url, oid, candid) + self.avro_url, + oid, + candid, + ) images = """
ZTF oid: %s, candid: %s
      @@ -610,7 +625,13 @@ def plot_stamp(self, oid, candid=None):
- """ % (oid, candid, science, science.replace("science", "template"), science.replace("science", "difference")) + """ % ( + oid, + candid, + science, + science.replace("science", "template"), + science.replace("science", "difference"), + ) display(HTML(images)) def get_stamps(self, oid, candid=None): @@ -639,8 +660,9 @@ def get_stamps(self, oid, candid=None): hdulist = HDUList() for stamp_type in ["science", "template", "difference"]: tmp_hdulist = fits_open( - "%s/get_stamp?oid=%s&candid=%s&type=%s&format=fits" % ( - self.avro_url, oid, candid, stamp_type)) + "%s/get_stamp?oid=%s&candid=%s&type=%s&format=fits" + % (self.avro_url, oid, candid, stamp_type) + ) hdu = tmp_hdulist[0] hdu.header["STAMP_TYPE"] = stamp_type hdulist.append(hdu) diff --git a/tests/api.py b/tests/api.py index b095481..4bfafd0 100644 --- a/tests/api.py +++ b/tests/api.py @@ -2,25 +2,26 @@ import pandas as pd from alerce.api import AlerceAPI from astropy.table import Table +import warnings class TestAlerceAPI(unittest.TestCase): - - oid = "ZTF19abueupg" - api = AlerceAPI() + def setUp(self): + self.oid = "ZTF19abueupg" + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + self.api = AlerceAPI() + assert len(w) == 1 + assert issubclass(w[-1].category, DeprecationWarning) + assert "deprecated" in str(w[-1].message) def test_query(self): params = { - "query_parameters": - {"filters": - {"nobs": - {"min": 2, - "max": 3 - } - } - } - } - + "query_parameters": {"filters": {"nobs": {"min": 2, "max": 3}}}, + "page": 1, + "perPage": 10, + "total": 10, + } resp = self.api.query(params) self.assertEqual(type(resp), Table) @@ -28,18 +29,9 @@ def test_query(self): self.assertEqual(type(resp), pd.DataFrame) def test_get_sql(self): - params = { - "query_parameters":{ - "filters":{ - "nobs":{ - "min": 3, - "max": 5 - } - } - } - } + params = {"query_parameters": {"filters": {"nobs": {"min": 3, "max": 5}}}} sql = self.api.get_sql(params) - self.assertEqual(type(sql),str) + self.assertEqual(type(sql), str) def test_get_detections(self): detections = self.api.get_detections(self.oid) @@ -114,23 +106,23 @@ def test_catsHTM_conesearch(self): self.assertEqual(type(conesearch[key]), Table) def test_catsHTM_xmatch(self): - xmatch = self.api.catsHTM_crossmatch(self.oid,radius=100) - self.assertEqual(type(xmatch),dict) + xmatch = self.api.catsHTM_crossmatch(self.oid, radius=100) + self.assertEqual(type(xmatch), dict) for key in xmatch: - self.assertEqual(type(xmatch[key]),Table) + self.assertEqual(type(xmatch[key]), Table) - xmatch = self.api.catsHTM_crossmatch(self.oid,radius=100,format="pandas") - self.assertEqual(type(xmatch),dict) + xmatch = self.api.catsHTM_crossmatch(self.oid, radius=100, format="pandas") + self.assertEqual(type(xmatch), dict) for key in xmatch: - self.assertEqual(type(xmatch[key]),pd.Series) + self.assertEqual(type(xmatch[key]), pd.Series) def test_catsHTM_redshift(self): - redshift = self.api.catsHTM_redshift(self.oid,radius=100) - self.assertEqual(type(redshift),float) + redshift = self.api.catsHTM_redshift(self.oid, radius=100) + self.assertEqual(type(redshift), float) def test_get_stamps(self): stamps = self.api.get_stamps(self.oid) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main()