33Optionnally 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
77from pspipe_utils import log
88
99from pixell import enmap , enplot
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
3332mask_intersect = True # intersection of every single mask
3433mask_union = False # union of every single mask
4241else :
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'
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
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 )
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
113122log .info (f'All intersection survey solid angle : { so_window .get_survey_solid_angle (so_map .from_enmap (mask_intersect )):.5f} ' )
114123log .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