Skip to content

Commit 797402f

Browse files
author
martinkilbinger
committed
compute footprint area of hsp masks
1 parent 2217ec5 commit 797402f

File tree

2 files changed

+172
-1
lines changed

2 files changed

+172
-1
lines changed

notebooks/demo_binned_mask.py

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# %%
2+
from IPython import get_ipython
3+
4+
# %%
5+
# enable autoreload for interactive sessions
6+
ipython = get_ipython()
7+
if ipython is not None:
8+
ipython.run_line_magic("load_ext", "autoreload")
9+
ipython.run_line_magic("autoreload", "2")
10+
ipython.run_line_magic("load_ext", "log_cell_time")
11+
12+
# %%
13+
import sys
14+
import os
15+
import numpy as np
16+
from astropy.io import fits
17+
import matplotlib.pylab as plt
18+
import healpy as hp
19+
import healsparse as hsp
20+
21+
from sp_validation import run_joint_cat as sp_joint
22+
from sp_validation import util
23+
from sp_validation.basic import metacal
24+
from sp_validation import calibration
25+
import sp_validation.cat as cat
26+
27+
# %%
28+
# Initialize calibration class instance
29+
obj = sp_joint.CalibrateCat()
30+
31+
# %%
32+
# Read configuration file and set parameters
33+
config = obj.read_config_set_params("config_mask.yaml")
34+
35+
# %%
36+
# Get data. Set load_into_memory to False for very large files
37+
dat, dat_ext = obj.read_cat(load_into_memory=False)
38+
# %%
39+
key_ra = "RA"
40+
key_dec = "Dec"
41+
42+
# %%
43+
# Create healsparse mask instance
44+
hsp_obj = sp_joint.ApplyHspMasks()
45+
46+
# %% Mask directory
47+
hsp_obj._params["mask_dir"] = f"{os.environ['HOME']}/masks"
48+
# %%
49+
# Masks to use
50+
51+
# Bits
52+
hsp_obj._params["bits"] = 64
53+
54+
# Names
55+
masks_to_apply = [
56+
"64_r",
57+
]
58+
59+
masks, labels = sp_joint.get_masks_from_config(
60+
config,
61+
dat,
62+
dat_ext,
63+
masks_to_apply=masks_to_apply,
64+
verbose=True
65+
)
66+
67+
# %%
68+
def get_areas(hsp_obj, masks):
69+
70+
areas_deg2 = []
71+
72+
73+
# Path to mask file(s)
74+
paths = hsp_obj.get_paths_bit_masks()
75+
76+
for key, mask in zip(paths, masks):
77+
print(f"Reading hsp mask file {paths[key]}...")
78+
hsp_mask = hsp.HealSparseMap.read(paths[key])
79+
80+
print(f"sentinel = {hsp_mask.sentinel}")
81+
82+
# Pixel area
83+
pix_area_deg2 = hp.nside2pixarea(hsp_mask.nside_sparse, degrees=True)
84+
85+
# Check total area
86+
Npix = hp.nside2npix(hsp_mask.nside_sparse)
87+
A_tot_deg2 = Npix * pix_area_deg2
88+
print(f"Total area: {A_tot_deg2:.3f} deg^2")
89+
90+
nside_frac = hsp_mask.nside_coverage * 2
91+
92+
fracdet = hsp_mask.fracdet_map(nside_frac)
93+
vals = fracdet.get_values_pix(fracdet.valid_pixels).astype(float)
94+
pix_area_deg2_frac = hp.nside2pixarea(nside_frac, degrees=True)
95+
area_unmasked_deg2 = vals.sum() * pix_area_deg2_frac
96+
area_footprint_deg2 = (vals > 0).sum() * pix_area_deg2_frac
97+
print(f"Unmasked ≈ {area_unmasked_deg2:.1f} deg²")
98+
print(f"Footprint ≈ {area_footprint_deg2:.1f} deg²")
99+
100+
cov_mask = hsp_mask.coverage_mask
101+
npop_pix = np.count_nonzero(cov_mask)
102+
area_deg2 = npop_pix * pix_area_deg2
103+
print(f"{mask._col_name}: Area of coverage: {area_deg2:.3f} deg^2")
104+
105+
# Get valid (active) pixels and convert to array
106+
vals = hsp_mask.get_values_pix(hsp_mask.valid_pixels)
107+
n_valid = len(vals)
108+
area_deg2 = n_valid * pix_area_deg2
109+
print(f"{mask._col_name}: Area of valid pixels: {area_deg2:.3f} deg^2")
110+
111+
112+
# TODO: Use class Mask evaluation and config file entries
113+
#if mask._value == True:
114+
#n_unmasked = int(np.count_nonzero(vals))
115+
#elif mask._value == False:
116+
#n_unmasked = int(np.count_nonzero(vals == False))
117+
#else:
118+
#print("Not implemented yet:", mask._value)
119+
120+
121+
# Testing
122+
for value in (True, False):
123+
n_unmasked = int(np.count_nonzero(vals == value))
124+
125+
# Footprint area
126+
area_deg2 = n_unmasked * pix_area_deg2
127+
128+
print(f"{mask._col_name}: Area with value={value}: {area_deg2:.3f} deg^2")
129+
130+
areas_deg2.append(area_deg2)
131+
132+
return areas_deg2
133+
134+
# %%
135+
areas_deg2 = get_areas(hsp_obj, masks)
136+
# %%
137+
# Bin data
138+
for mask in masks:
139+
mask.apply(dat_ext)
140+
141+
142+
# %%
143+
def get_binned_area(ra, dec, nside=512):
144+
145+
# Pixel list of input data
146+
ipix = hp.ang2pix(nside, ra, dec, lonlat=True)
147+
148+
# Number of occupied pixels
149+
Nocc = np.unique(ipix).size
150+
151+
# Pixel area
152+
pix_area_deg2 = hp.nside2pixarea(nside, degrees=True)
153+
154+
# Footprint area
155+
area_deg2 = Nocc * pix_area_deg2
156+
157+
158+
return area_deg2
159+
160+
# %%
161+
for mask in masks:
162+
print("Mask:", mask._col_name)
163+
ra = dat[key_ra][mask._mask]
164+
dec = dat[key_dec][mask._mask]
165+
166+
for nside in [256, 512, 1024]:
167+
168+
area_binned_deg2 = get_binned_area(ra, dec, nside=nside)
169+
print(f"binned area (nside={nside}) ≈ {area_binned_deg2:.3f} deg^2")
170+
# %%
171+
print(areas_deg2)
172+
# %%

src/sp_validation/run_joint_cat.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,6 @@ def sky_plots(dat, masks, labels, zoom_ra, zoom_dec):
12381238
plot_area_mask(ra, dec, zoom, mask=m_halos)
12391239

12401240

1241-
12421241
def plot_area_mask(ra, dec, zoom, mask=None):
12431242
"""Plot Area Mask.
12441243

0 commit comments

Comments
 (0)