• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

desy-multimessenger / nuztf / 13725459265

07 Mar 2025 04:58PM UTC coverage: 71.901% (-1.7%) from 73.615%
13725459265

push

github

web-flow
Add Kowalski Backend (#490)

471 of 640 new or added lines in 26 files covered. (73.59%)

15 existing lines in 4 files now uncovered.

1978 of 2751 relevant lines covered (71.9%)

0.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

23.33
/nuztf/kowalski/kowalski_cutout.py
1
"""
2
This module contains functions to query Kowalski for cutouts
3
"""
4

5
import logging
1✔
6
from base64 import b64encode
1✔
7

8
from penquins import Kowalski
1✔
9

10
from nuztf.kowalski.config import get_kowalski
1✔
11

12
logger = logging.getLogger(__name__)
1✔
13

14

15
def kowalski_api_cutout(
1✔
16
    candid: int,
17
    kowalski: Kowalski | None = None,
18
):
19
    """
20
    Download alert data from Kowalski
21

22
    :param candid: Candidate ID
23
    :param kowalski: Kowalski object
24
    :return: Alert data
25
    """
NEW
26
    if kowalski is None:
×
NEW
27
        kowalski = get_kowalski()
×
28

NEW
29
    query_config = {
×
30
        "query_type": "find",
31
        "query": {
32
            "catalog": "ZTF_alerts",
33
            "filter": {
34
                "candid": {"$eq": int(candid)},
35
            },
36
            "projection": {
37
                "cutoutScience": 1,
38
                "cutoutTemplate": 1,
39
                "cutoutDifference": 1,
40
            },
41
        },
42
    }
43

NEW
44
    query_result = kowalski.query(query_config)
×
45

NEW
46
    if "data" in query_result:
×
NEW
47
        alerts = query_result["data"]
×
48
    else:
NEW
49
        alerts = query_result.get("default").get("data")
×
50

NEW
51
    cutouts = alerts[-1]
×
52

NEW
53
    return cutouts
×
54

55

56
def ensure_kowalski_cutouts(alert: list[dict]) -> list[dict]:
1✔
57
    """
58
    Make sure alert contains cutouts (if not, query them from Kowalski API)
59

60
    :param alert: Alert data
61
    :return: Alert data with cutouts
62
    """
NEW
63
    candid = alert[0]["candid"]
×
NEW
64
    ztf_id = alert[0]["objectId"]
×
65

NEW
66
    if "cutoutScience" in alert[0].keys():
×
NEW
67
        if "stampData" in alert[0]["cutoutScience"].keys():
×
NEW
68
            logger.debug("Alert already contains cutouts.")
×
NEW
69
            return alert
×
70

NEW
71
    logger.debug(f"{ztf_id}: Querying API for cutouts.")
×
72

NEW
73
    cutouts = kowalski_api_cutout(candid)
×
74

NEW
75
    for k in ["Science", "Difference", "Template"]:
×
NEW
76
        key = f"cutout{k}"
×
NEW
77
        cutouts[key]["stampData"] = b64encode((cutouts[key]["stampData"]))
×
NEW
78
        alert[0][key] = cutouts[key]
×
79

NEW
80
    logger.debug(f"{ztf_id}: Added cutouts.")
×
81

NEW
82
    return alert
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc