Skip to content

Commit 11c071c

Browse files
author
Merry Duparc
committed
Updates on masks and windows scripts
1 parent 91858d1 commit 11c071c

File tree

2 files changed

+50
-89
lines changed

2 files changed

+50
-89
lines changed

project/SO/pISO/python/get_windows.py

Lines changed: 14 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,25 @@
2626
apod_pts_source_degree = d["apod_pts_source_degree"]
2727
# the apodisation length of the final survey mask
2828
apod_survey_degree = d["apod_survey_degree"]
29-
# the threshold on the amount of cross linking to keep the data in
30-
cross_link_threshold = d["cross_link_threshold"]
31-
# pixel weight with inverse variance above n_ivar * median are set to ivar
32-
# this ensure that the window is not totally dominated by few pixels with too much weight
33-
n_med_ivar = d["n_med_ivar"]
3429

3530
# we will skip the edges of the survey where the noise is very difficult to model, the default is to skip 0.5 degree for
3631
# constructing the kspace mask and 2 degree for the final survey mask, this parameter can be used to rescale the default
3732
rescale = d["edge_skip_rescale"]
3833

3934
window_dir = d["window_dir"]
40-
skip_ivar = True
4135

4236
pspy_utils.create_directory(window_dir)
4337

44-
patch = None
45-
if "patch" in d:
46-
patch = so_map.read_map(d["patch"])
47-
48-
49-
# here we list the different windows that need to be computed, we will then do a MPI loops over this list
50-
51-
# Use this if you want to only compute one window
38+
# Use this if you want to only compute one window (for testing)
5239
# d["surveys"] = ['SO']
5340
# d["arrays_SO"] = ['i1_f090']
5441

55-
42+
# here we list the different windows that need to be computed, we will then do a MPI loops over this list
5643
n_wins, sv_list, ar_list = pspipe_list.get_arrays_list(d)
5744

5845
log.info(f"number of windows to compute : {n_wins}")
5946

60-
my_masks = {}
47+
my_masks: dict[so_map.so_map] = {}
6148

6249
so_mpi.init(True)
6350
subtasks = so_mpi.taskrange(imin=0, imax=n_wins - 1)
@@ -78,13 +65,8 @@
7865

7966
maps = d[f"maps_{sv}_{ar}"]
8067

81-
if not skip_ivar:
82-
ivar_all = gal_mask.copy()
83-
ivar_all.data[:] = 0
84-
8568
# the first step is to iterate on the maps of a given array to identify pixels with zero values
8669
# we will form a first survey mask as the union of all these split masks
87-
# we also compute the average ivar map
8870

8971
log.info(f"[{task}] create survey mask from ivar maps")
9072

@@ -94,15 +76,6 @@
9476
else:
9577
index = map.find("map.fits")
9678

97-
if not skip_ivar:
98-
ivar_map = map[:index] + "ivar.fits"
99-
ivar_map = so_map.read_map(ivar_map, geometry=template_geom)
100-
my_masks["baseline"].data[ivar_map.data[:] == 0.0] = 0.0
101-
ivar_all.data[:] += ivar_map.data[:]
102-
103-
if not skip_ivar:
104-
ivar_all.data[:] /= np.max(ivar_all.data[:])
105-
10679
if d[f"extra_mask_{sv}_{ar}"] is not None:
10780
log.info(f"[{task}] apply extra mask")
10881
extra_mask = so_map.read_map(d[f"extra_mask_{sv}_{ar}"], geometry=template_geom)
@@ -121,10 +94,6 @@
12194
log.info(f"[{task}] apply galactic mask")
12295
my_masks["baseline"].data *= gal_mask.data
12396

124-
if patch is not None:
125-
log.info(f"[{task}] apply patch mask")
126-
127-
my_masks["baseline"].data *= patch.data
12897
# with this we can create the k space mask this will only be used for applying the kspace filter
12998
log.info(
13099
f"[{task}] appodize kspace mask with {rescale:.2f} apod and write it to disk"
@@ -174,35 +143,18 @@
174143

175144
my_masks[mask_type].write_map(f"{window_dir}/window_{sv}_{ar}_{mask_type}.fits")
176145

177-
# we also make a version of the windows taking into account the ivar of the maps
178-
if not skip_ivar:
179-
log.info(f"[{task}] include ivar ")
180-
181-
mask_type_w = mask_type + "_ivar"
182-
183-
my_masks[mask_type_w] = my_masks[mask_type].copy()
184-
id = np.where(ivar_all.data[:] * my_masks[mask_type_w].data[:] != 0)
185-
med = np.median(ivar_all.data[id])
186-
ivar_all.data[ivar_all.data[:] > n_med_ivar * med] = n_med_ivar * med
187-
my_masks[mask_type_w].data[:] *= ivar_all.data[:]
188-
189-
my_masks[mask_type_w].data = my_masks[mask_type_w].data.astype(np.float32)
190-
my_masks[mask_type_w].write_map(
191-
f"{window_dir}/window_{sv}_{ar}_{mask_type_w}.fits"
192-
)
193-
194146
for mask_type, mask in my_masks.items():
195147
log.info(f"[{task}] downgrade and plot {mask_type} ")
196-
mask = mask.downgrade(4)
197-
mask.plot(file_name=f"{window_dir}/window_{sv}_{ar}_{mask_type}")
148+
mask.downgrade(4).plot(file_name=f"{window_dir}/window_{sv}_{ar}_{mask_type}")
198149

199150
if f"{sv}_{ar}" in d["plot_windowed_maps"]:
200-
pspy_utils.create_directory(f"{window_dir}/windowed maps")
201-
maps_to_plot = so_map.read_map(d[f"maps_{sv}_{ar}"][0])
202-
maps_to_plot.data *= my_masks["baseline"].data
203-
maps_to_plot.downgrade(2).calibrate(
204-
cal=d[f"cal_{sv}_{ar}"], pol_eff=d[f"pol_eff_{sv}_{ar}"]
205-
).plot(
206-
file_name=f"{window_dir}/windowed maps/{sv}_{ar}",
207-
color_range=(300, 100, 100),
208-
)
151+
pspy_utils.create_directory(f"{window_dir}/windowed_maps")
152+
for s, sv_ar_split in enumerate(d[f"maps_{sv}_{ar}"]):
153+
maps_to_plot = so_map.read_map(sv_ar_split)
154+
maps_to_plot.data *= my_masks["baseline"].data
155+
maps_to_plot.downgrade(4).calibrate(
156+
cal=d[f"cal_{sv}_{ar}"], pol_eff=d[f"pol_eff_{sv}_{ar}"]
157+
).plot(
158+
file_name=f"{window_dir}/windowed_maps/{sv}_{ar}_split{s}",
159+
color_range=(300, 100, 100),
160+
)

project/SO/pISO/python/masks/get_xtra_mask.py

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Optionnally plots all ivar and maps
44
"""
55

6-
from pspy import so_dict, so_map, so_window
6+
from pspy import so_dict, so_map, so_window, so_mpi
77
from pspipe_utils import log
88

99
from pixell import enmap, enplot
@@ -26,9 +26,8 @@
2626
os.makedirs(maps_ivar_dir, exist_ok=True)
2727

2828
# get reasonable ivar, top 90% of nonzero values seems to work decently
29-
# TODO: make dynamic
30-
ivar_smooth_deg = 0.2
31-
ivar_quantile = d['xtra_mask_ivar_quantile']
29+
ivar_smooth_deg = d['ivar_smooth_deg']
30+
ivar_quantile = d['ivar_quantile']
3231

3332
mask_intersect = True # intersection of every single mask
3433
mask_union = False # union of every single mask
@@ -42,29 +41,39 @@
4241
else:
4342
additional_mask = True
4443

45-
for sv in d['surveys']:
46-
for m in d[f'arrays_{sv}']:
47-
44+
n_ar = len(d['arrays_SO'])
45+
46+
log.info(f"number of arrays for the mpi loop : {n_ar}")
47+
so_mpi.init(True)
48+
subtasks = so_mpi.taskrange(imin=0, imax=n_ar-1)
49+
50+
sv = 'SO' # we want to run tthise script only on SO
51+
52+
for task in subtasks:
53+
m = d[f'arrays_{sv}'][task]
4854
ivar_mask = True # intersection of masks just for this map (over splits)
4955

5056
map_fns = d[f'maps_{sv}_{m}']
5157
for i, map_fn in enumerate(map_fns):
5258

5359
map_dir_fn, map_base_fn = os.path.split(map_fn)
5460
if d[f"src_free_maps_{sv}"] == True:
55-
ivar_base_fn = map_base_fn.replace('map_srcfree', 'ivar')
61+
ivar_fn = map_fn.replace('_map_srcfree', '_ivar')
5662
else:
57-
ivar_base_fn = map_base_fn.replace('map', 'ivar')
58-
ivar_fn = os.path.join(map_dir_fn, ivar_base_fn)
63+
ivar_fn = map_fn.replace('_map', '_ivar')
5964

6065
# mask is based on the smoothed ivar map
6166
# only the pixels were the original ivar were nonzero though
62-
ivar = enmap.read_map(ivar_fn)
63-
ivar_smooth = enmap.smooth_gauss(ivar, np.deg2rad(ivar_smooth_deg))
64-
ivar_set_mask = ivar_smooth > np.quantile(ivar_smooth[ivar > 0], ivar_quantile)
65-
ivar_set_mask *= ivar > 0
66-
ivar_set_mask = np.logical_and(ivar_set_mask, additional_mask)
67+
ivar = enmap.read_map(ivar_fn).astype(bool)
68+
while len(ivar.shape) > 2:
69+
ivar = ivar[0].astype(np.float32)
6770

71+
# ivar_set_mask = ivar_smooth > np.quantile(ivar_smooth[ivar > 0], ivar_quantile)
72+
# ivar_set_mask = np.logical_and(ivar_set_mask, additional_mask)
73+
74+
ivar_smooth = enmap.smooth_gauss(ivar, np.deg2rad(ivar_smooth_deg))
75+
ivar_set_mask = enmap.zeros(ivar.shape, ivar.wcs, dtype=bool)
76+
ivar_set_mask[ivar_smooth > np.quantile(ivar_smooth[ivar > 0], ivar_quantile)] = 1
6877
# check that inside of ivar_mask, there are no zero ivar
6978
assert np.all(ivar[ivar_set_mask] > 0), \
7079
f'{sv}, {m}, set{i} has zero ivar inside ivar_mask'
@@ -76,11 +85,11 @@
7685
map = enmap.read_map(map_fn)
7786
p = enplot.plot(map, downgrade=8, ticks=1, colorbar=True, range=[500, 100, 100])
7887
map_plot_fn = os.path.basename(map_fn)
79-
enplot.write(os.path.join(maps_ivar_dir, map_plot_fn), p)
88+
enplot.write(os.path.join(maps_ivar_dir, map_plot_fn)[:-5], p)
8089

8190
p = enplot.plot(ivar, downgrade=8, ticks=1, colorbar=True)
8291
ivar_plot_fn = os.path.basename(ivar_fn)
83-
enplot.write(os.path.join(maps_ivar_dir, ivar_plot_fn), p)
92+
enplot.write(os.path.join(maps_ivar_dir, ivar_plot_fn)[:-5], p)
8493

8594
log.info(f'{sv}, {m}, set{i} survey solid angle : {so_window.get_survey_solid_angle(so_map.from_enmap(ivar_set_mask)):.5f}')
8695

@@ -91,8 +100,8 @@
91100
# save plot of mask
92101
if save_plot_mask:
93102
p = enplot.plot(ivar_set_mask, downgrade=8, ticks=1, colorbar=True)
94-
enplot.write(ivar_set_mask_fn, p)
95-
103+
enplot.write(ivar_set_mask_fn[:-5], p)
104+
96105
# also build xtra masks that are the union and intersection
97106
# of all the xtra masks
98107
mask_intersect = np.logical_and(mask_intersect, ivar_set_mask)
@@ -108,16 +117,16 @@
108117
# save plot of mask
109118
if save_plot_mask:
110119
p = enplot.plot(ivar_mask, downgrade=8, ticks=1, colorbar=True)
111-
enplot.write(ivar_mask_fn, p)
120+
enplot.write(ivar_mask_fn[:-5], p)
112121

113122
log.info(f'All intersection survey solid angle : {so_window.get_survey_solid_angle(so_map.from_enmap(mask_intersect)):.5f}')
114123
log.info(f'All union survey solid angle : {so_window.get_survey_solid_angle(so_map.from_enmap(mask_union)):.5f}')
115124

116-
# save union and intersect masks
117-
enmap.write_map(os.path.join(mask_dir, f'xtra_mask_intersect.fits'), mask_intersect.astype(np.float32))
118-
enmap.write_map(os.path.join(mask_dir, f'xtra_mask_union.fits'), mask_union.astype(np.float32))
125+
# plot and save union and intersect masks
126+
p = enplot.plot(mask_intersect, downgrade=8, ticks=1, colorbar=True)
127+
enplot.write(os.path.join(mask_dir, f'xtra_mask_intersect'), p)
128+
enmap.write_map(os.path.join(mask_dir, f'xtra_mask_intersect.fits'), mask_intersect.astype(np.float32))\
119129

120-
# save plot of union and intersect masks
121-
if save_plot_mask:
122-
p = enplot.plot([mask_intersect, mask_union], downgrade=8, colorbar=True, ticks=1)
123-
enplot.write(os.path.join(mask_dir, 'xtra_mask_intersect_and_union'), p)
130+
p = enplot.plot(mask_union, downgrade=8, ticks=1, colorbar=True)
131+
enplot.write(os.path.join(mask_dir, f'xtra_mask_union'), p)
132+
enmap.write_map(os.path.join(mask_dir, f'xtra_mask_union.fits'), mask_union.astype(np.float32))

0 commit comments

Comments
 (0)