Added status, error message fields, contamination map to response

This commit is contained in:
Никита Тырин 2025-05-26 15:26:15 +03:00
parent cc410f58dc
commit a36163f731

View File

@ -12,7 +12,7 @@ from astropy.stats import poisson_conf_interval
from collections import defaultdict from collections import defaultdict
from django.db.models import Sum from django.db.models import Sum, Max
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from rest_framework.views import APIView from rest_framework.views import APIView
from rest_framework.response import Response from rest_framework.response import Response
@ -221,17 +221,15 @@ class UpperLimitView(APIView):
# exclude contaminated pixels from the background calculations # exclude contaminated pixels from the background calculations
annulus_pixels = annulus_pixels.exclude(contaminated=True) annulus_pixels = annulus_pixels.exclude(contaminated=True)
status_int = 0
error_message = ""
if not source_pixels.exists() or not annulus_pixels.exists(): if not source_pixels.exists() or not annulus_pixels.exists():
return Response( status_int = 1 # status 1 if there are no source or bg pixels
{
"detail": "No background and/or source pixel data for the given survey selection."
},
status=status.HTTP_404_NOT_FOUND,
)
# COMPUTE COUNTS, BACKGROUND ESTIMATE, EXPOSURE # COMPUTE COUNTS, BACKGROUND ESTIMATE, EXPOSURE
# ************************************************************** # **************************************************************
try:
# summing counts across all surveys # summing counts across all surveys
N = sum(obj.counts for obj in source_pixels) N = sum(obj.counts for obj in source_pixels)
@ -285,7 +283,8 @@ class UpperLimitView(APIView):
bayesian_count_UL = ( bayesian_count_UL = (
sp.gammaincinv( sp.gammaincinv(
N + 1, confidence_level * sp.gammaincc(N + 1, B) + sp.gammainc(N + 1, B) N + 1,
confidence_level * sp.gammaincc(N + 1, B) + sp.gammainc(N + 1, B),
) )
- B - B
) )
@ -334,6 +333,21 @@ class UpperLimitView(APIView):
Flux = max(FL, 0) # flux cannot be lower than zero Flux = max(FL, 0) # flux cannot be lower than zero
# handle exceptions: write error text to the response
# zero out all math results
except Exception as e:
error_message = str(e)
N = Nnpix = Bcounts = Bnpix = B = t = S = CR = BR = 0.0
Flux = 0.0
classic_count_ul = classic_count_ll = classic_count_UL = 0.0
classic_rate_ul = classic_rate_ll = classic_rate_UL = 0.0
classic_flux_ul = classic_flux_ll = classic_flux_UL = 0.0
bayesian_count_ul = bayesian_count_ll = bayesian_count_UL = 0.0
bayesian_rate_ul = bayesian_rate_ll = bayesian_rate_UL = 0.0
bayesian_flux_ul = bayesian_flux_ll = bayesian_flux_UL = 0.0
# NEARBY SOURCES CHECK # NEARBY SOURCES CHECK
# **************************************************************** # ****************************************************************
@ -372,7 +386,7 @@ class UpperLimitView(APIView):
} }
) )
# SQUARE REGION IMAGE SERVING # REGION IMAGE SERVING
# **************************************************************** # ****************************************************************
# default value if not specified in the query # default value if not specified in the query
@ -396,7 +410,10 @@ class UpperLimitView(APIView):
map_pixels_qs = ( map_pixels_qs = (
Pixel.objects.filter(hpid__in=map_pixel_list, survey__in=survey_numbers) Pixel.objects.filter(hpid__in=map_pixel_list, survey__in=survey_numbers)
.values("hpid") .values("hpid")
.annotate(counts=Sum("counts")) .annotate(
counts=Sum("counts"),
contaminated=Max("contaminated"),
)
.order_by("hpid") .order_by("hpid")
) )
@ -406,6 +423,7 @@ class UpperLimitView(APIView):
# get lists of healpix indices and count values # get lists of healpix indices and count values
map_healpix_list = [d["hpid"] for d in map_pixels_list] map_healpix_list = [d["hpid"] for d in map_pixels_list]
map_counts_list = [d["counts"] for d in map_pixels_list] map_counts_list = [d["counts"] for d in map_pixels_list]
map_contaminated_list = [d["contaminated"] for d in map_pixels_list]
# set map nside # set map nside
map_nside = 4096 map_nside = 4096
@ -417,6 +435,7 @@ class UpperLimitView(APIView):
map_dict = { map_dict = {
"healpix": map_healpix_list, "healpix": map_healpix_list,
"counts": map_counts_list, "counts": map_counts_list,
"contaminated": map_contaminated_list,
"nside": map_nside, "nside": map_nside,
"order": map_order, "order": map_order,
"radius_as": map_radius, "radius_as": map_radius,
@ -426,6 +445,10 @@ class UpperLimitView(APIView):
# **************************************************************** # ****************************************************************
result = { result = {
"Status": status_int,
# 0 OK
# 1 either source or bg pixels missing
"ErrorMessage": error_message,
# frequentist limits # frequentist limits
"ClassicUpperLimit": classic_count_ul, "ClassicUpperLimit": classic_count_ul,
"ClassicLowerLimit": classic_count_ll, "ClassicLowerLimit": classic_count_ll,