diff --git a/fink_filters/ztf/filter_anomaly_notification/filter.py b/fink_filters/ztf/filter_anomaly_notification/filter.py index a4fca10..c9a705f 100755 --- a/fink_filters/ztf/filter_anomaly_notification/filter.py +++ b/fink_filters/ztf/filter_anomaly_notification/filter.py @@ -14,7 +14,6 @@ # limitations under the License. import pandas as pd import numpy as np - from astropy import units as u from astropy.coordinates import SkyCoord @@ -133,7 +132,7 @@ def anomaly_notification_( ... df_proc = df.select( ... 'objectId', 'candidate.ra', ... 'candidate.dec', 'candidate.rb', - ... f'anomaly_score{model}', 'timestamp') + ... f'anomaly_score{model}', 'timestamp', 'candid') ... df_out = anomaly_notification_(df_proc, send_to_tg=False, ... send_to_slack=False, send_to_anomaly_base=True, model=model) @@ -141,7 +140,7 @@ def anomaly_notification_( >>> df_proc = df.select( ... 'objectId', 'candidate.ra', ... 'candidate.dec', 'candidate.rb', - ... 'anomaly_score', 'timestamp') + ... 'anomaly_score', 'timestamp', 'candid') >>> pdf_anomalies = anomaly_notification_(df_proc, threshold=10, ... send_to_tg=True, channel_id='@fink_test', ... send_to_slack=False, channel_name=None) @@ -161,7 +160,9 @@ def anomaly_notification_( cut_count = df_proc.count() if cut_count == 0: return pd.DataFrame() - + df_proc = df_proc.filter(f"not isnan(anomaly_score{model})") + if df_proc.rdd.isEmpty(): + return pd.DataFrame() # Compute the median for the night buf_df = df_proc.select(f"anomaly_score{model}") med = buf_df.approxQuantile(f"anomaly_score{model}", [0.5], 0.05) @@ -242,6 +243,7 @@ def anomaly_notification_( {t5_}""".replace("\n", " \n"), cutout, curve, + row.candid, )) init_msg = f"Median anomaly score overnight: {med}." diff --git a/fink_filters/ztf/filter_anomaly_notification/filter_utils.py b/fink_filters/ztf/filter_anomaly_notification/filter_utils.py index 59a2051..f8a8641 100755 --- a/fink_filters/ztf/filter_anomaly_notification/filter_utils.py +++ b/fink_filters/ztf/filter_anomaly_notification/filter_utils.py @@ -445,19 +445,19 @@ def load_to_anomaly_base(data, model, timeout=60): tg_id_data = int(tg_id_data.replace('"', "")) except ValueError: tg_id_data = "ND" - for ztf_id, text_data, cutout, curve in data: + for ztf_id, text_data, cutout, curve, candid_id in data: cutout.seek(0) curve.seek(0) files = {"image1": cutout, "image2": curve} - data = {"description": text_data} - params = {"ztf_id": ztf_id} + data_payload = {"description": text_data} + params = {"ztf_id": ztf_id, "candid_id": candid_id} headers = {"Authorization": f"Bearer {access_token}"} response = send_post_request_with_retry( session=session, url="https://anomaly.fink-broker.org:443/images/upload", files=files, params=params, - data=data, + data=data_payload, headers=headers, timeout=timeout, source="upload_to_anomaly_base", @@ -467,11 +467,14 @@ def load_to_anomaly_base(data, model, timeout=60): curve.seek(0) if tg_id_data == "ND": continue + callback_anomaly = f"A_{candid_id}_{ztf_id}" + callback_not_anomaly = f"NA_{candid_id}_{ztf_id}" + inline_keyboard = { "inline_keyboard": [ [ - {"text": "Anomaly", "callback_data": f"A_{ztf_id}"}, - {"text": "Not anomaly", "callback_data": f"NA_{ztf_id}"}, + {"text": "Anomaly", "callback_data": callback_anomaly}, + {"text": "Not anomaly", "callback_data": callback_not_anomaly}, ] ] }