changed the contamination handling logic to also exclude contaminated pixels from the source region

This commit is contained in:
Никита Тырин 2025-06-05 13:07:33 +03:00
parent d346bbadbc
commit f680039e9a
3 changed files with 93 additions and 30 deletions

View File

@ -11,6 +11,9 @@ from django.db import transaction
from uplim.models import Pixel
from django.db.models import Max
from tqdm import tqdm
import sys
# DEFINE BATCH SIZE AND BATCH
# **************************************************************
@ -121,6 +124,11 @@ class Command(BaseCommand):
)
start_index = last_hpid + 1
pixels_to_insert = total_pixels - start_index
if pixels_to_insert <= 0:
self.stdout.write("All pixels have already been inserted. Exiting.")
pixel_generator = (
Pixel(
hpid=i,
@ -133,13 +141,23 @@ class Command(BaseCommand):
)
total_inserted = start_index
pbar = tqdm(
total=pixels_to_insert,
unit="pix",
desc=f"Survey {survey_number}",
# file=sys.stdout,
)
# Process in batches
for pixel_batch in batch(pixel_generator, BATCH_SIZE):
with transaction.atomic():
Pixel.objects.bulk_create(pixel_batch)
total_inserted += len(pixel_batch)
percentage = total_inserted / total_pixels * 100
timestamp = datetime.now().strftime("%H:%M:%S")
self.stdout.write(f"[{timestamp}] {percentage:.2f}% inserted")
# total_inserted += len(pixel_batch)
# percentage = total_inserted / total_pixels * 100
# timestamp = datetime.now().strftime("%H:%M:%S")
# self.stdout.write(f"[{timestamp}] {percentage:.2f}% inserted")
pbar.update(BATCH_SIZE)
self.stdout.write(f"Inserted a total of {total_inserted} pixels.")
pbar.close()
self.stdout.write(f"Done: Inserted a total of {total_inserted} pixels.")

View File

@ -161,42 +161,79 @@ class Command(BaseCommand):
nside = 4096
npix = hp.nside2npix(nside)
flux_bins = [0, 125, 250, 2000, 20000, np.inf] # define bin edges
mask_radii_deg = [
0.06,
0.15,
0.5,
0.9,
2.5,
] # corresponding mask radii in degrees
# flux_bins = [0, 125, 250, 2000, 20000, np.inf] # define bin edges
# mask_radii_deg = [
# 0.06,
# 0.15,
# 0.5,
# 0.9,
# 2.5,
# ] # corresponding mask radii in degrees
# Convert mask radii from degrees to radians (required by query_disc)
mask_radii = [np.radians(r) for r in mask_radii_deg]
# # Convert mask radii from degrees to radians (required by query_disc)
# mask_radii = [np.radians(r) for r in mask_radii_deg]
# Use pandas.cut to assign each source a bin index (0, 1, or 2)
# # Use pandas.cut to assign each source a bin index (0, 1, or 2)
# catalog["flux_bin"] = pd.cut(catalog["Flux"], bins=flux_bins, labels=False)
flux_bins = [0, 125, 250, 2000, 20000, np.inf]
catalog["flux_bin"] = pd.cut(catalog["Flux"], bins=flux_bins, labels=False)
bin_to_radius_deg = {
0: 0.06,
1: 0.15,
2: 0.5,
3: 0.9,
4: 2.5,
}
catalog["mask_radius_deg"] = catalog["flux_bin"].map(bin_to_radius_deg)
# manually add and change some sources
# manual_additions = pd.DataFrame(
# [
# {"RAdeg": 279.9804336, "DEdeg": 5.0669542, "flux_bin": 3},
# {"RAdeg": 266.5173685, "DEdeg": -29.1252321, "flux_bin": 3},
# {
# "RAdeg": 194.9350000,
# "DEdeg": 27.9124722,
# "flux_bin": 4,
# }, # Coma Cluster, 2.5 degrees
# {
# "RAdeg": 187.6991667,
# "DEdeg": 12.3852778,
# "flux_bin": 4,
# }, # Virgo cluster, 2.5 degrees
# ]
# )
# catalog = pd.concat([catalog, manual_additions], ignore_index=True)
# catalog.loc[catalog["SrcID"] == 1101, "flux_bin"] = 2
manual_additions = pd.DataFrame(
[
{"RAdeg": 279.9804336, "DEdeg": 5.0669542, "flux_bin": 3},
{"RAdeg": 266.5173685, "DEdeg": -29.1252321, "flux_bin": 3},
{
"RAdeg": 279.9804336,
"DEdeg": 5.0669542,
"mask_radius_deg": 0.9,
},
{"RAdeg": 266.5173685, "DEdeg": -29.1252321, "mask_radius_deg": 0.9},
{
"RAdeg": 194.9350000,
"DEdeg": 27.9124722,
"flux_bin": 4,
}, # Coma Cluster, 2.5 degrees
"mask_radius_deg": 1.17,
}, # Coma, 70 arcmin radius
{
"RAdeg": 187.6991667,
"DEdeg": 12.3852778,
"flux_bin": 4,
}, # Virgo cluster, 2.5 degrees
"mask_radius_deg": 2.5,
}, # Virgo, 2.5 deg radius
]
)
catalog = pd.concat([catalog, manual_additions], ignore_index=True)
catalog.loc[catalog["SrcID"] == 1101, "flux_bin"] = 2
catalog.loc[catalog["SrcID"] == 1101, "mask_radius_deg"] = 0.5 # e.g. override
mask_array = np.ones(npix, dtype=bool)
@ -215,17 +252,24 @@ class Command(BaseCommand):
ra, dec = gal.l.deg, gal.b.deg
flux_bin = row["flux_bin"] # 0, 1, or 2
# Get the corresponding mask radius (in radians) for this flux bin
radius = mask_radii[flux_bin]
# flux_bin = row["flux_bin"] # 0, 1, or 2
# # Get the corresponding mask radius (in radians) for this flux bin
# radius = mask_radii[flux_bin]
# Convert (ra, dec) to HEALPix spherical coordinates
theta = np.radians(90.0 - dec)
phi = np.radians(ra)
vec = hp.ang2vec(theta, phi)
# Query all pixels within the given radius
# 'inclusive=True' makes sure pixels on the edge are included
# # Query all pixels within the given radius
# # 'inclusive=True' makes sure pixels on the edge are included
# pix_indices = hp.query_disc(nside, vec, radius, inclusive=True)
# read the explicit per-source radius in degrees, convert to radians:
radius_deg = row["mask_radius_deg"]
radius = np.radians(radius_deg)
# now query:
pix_indices = hp.query_disc(nside, vec, radius, inclusive=True)
# Mark these pixels as bad (False) in our mask

View File

@ -229,8 +229,9 @@ class UpperLimitView(APIView):
or annulus_pixels.filter(contaminated=True).exists()
)
# exclude contaminated pixels from the background calculations
# exclude contaminated pixels from the background and source regions
annulus_pixels = annulus_pixels.exclude(contaminated=True)
source_pixels = source_pixels.exclude(contaminated=True)
status_int = 0
error_message = ""