4141lmax = d ["lmax" ]
4242n_min_sims = 500
4343
44+ so_mpi .init (True )
4445
4546mcm_dir = d ["mcm_dir" ]
4647spec_dir = d ['sim_spec_for_tf_dir' ]
6869sv2_list = np .array (sv2_list )
6970m2_list = np .array (m2_list )
7071
71- subtasks_mapsets = so_mpi .taskrange (imin = iStart , imax = iStop - 1 )
72- log .info (f"[Rank { so_mpi .rank } ] Running on sims" )
73- log .info (f"[Rank { so_mpi .rank } ] Number of sims for the mpi loop: { len (subtasks_mapsets )} " )
72+ #subtasks_mapsets = so_mpi.taskrange(imin=iStart, imax=iStop - 1)
73+ subtasks_spectra = so_mpi .taskrange (imin = 0 , imax = n_spec - 1 )
74+ log .info (f"[Rank { so_mpi .rank } ] Number of spectra for the mpi loop: { len (subtasks_spectra )} " )
75+ #log.info(f"[Rank {so_mpi.rank}] Number of sims for the mpi loop: {len(subtasks_mapsets)}")
7476
7577# iteration for alms
76- mapset_iterator = subtasks_mapsets
77- sv_iterator = sv_list
78- map_iterator = map_list
78+ #mapset_iterator = subtasks_mapsets
7979
8080# iteration for spectra
81- sv1_iterator = sv1_list
82- m1_iterator = m1_list
83- sv2_iterator = sv2_list
84- m2_iterator = m2_list
81+ sv1_iterator = sv1_list [subtasks_spectra ]
82+ m1_iterator = m1_list [subtasks_spectra ]
83+ sv2_iterator = sv2_list [subtasks_spectra ]
84+ m2_iterator = m2_list [subtasks_spectra ]
85+
8586
8687spec_list = pspipe_list .get_spec_name_list (d , delimiter = "_" )
8788array_list = [f"{ sv } _{ ar } " for (sv , ar ) in zip (sv_list , map_list )]
9697#for sid, spec in enumerate(spec_list):
9798for sv1 , m1 , sv2 , m2 in zip (sv1_iterator , m1_iterator , sv2_iterator , m2_iterator , strict = True ):
9899 spec = sv1 + "_" + m1 + "x" + sv2 + "_" + m2
99- log .info (f"Read all { spec } sim power spectra" )
100+ log .info (f"[Rank { so_mpi . rank } ] Read all { spec } sim power spectra" )
100101
101102
102103 ps_list [spec ] = {}
103104 for scenario in scenarios :
104- for iii in mapset_iterator :
105-
105+ for iii in range (iStart , iStop ):
106106 if iii == 0 :
107107 ps_list [spec ]["nofilter" , scenario ] = []
108108 ps_list [spec ]["filter" , scenario ] = []
115115 ps_list [spec ]["filter" , scenario ] += [ps_filt ]
116116
117117
118- elements = ["TT_to_TT" , "EE_to_EE" , "BB_to_BB" , "EE_to_BB" , "BB_to_EE" ]
118+ elements = ["TT_to_TT" , "EE_to_EE" , "BB_to_BB" , "EE_to_BB" , "BB_to_EE" , "TE_to_TE" , "ET_to_ET" , "TB_to_TB" , "BT_to_BT" , "EB_to_EB" , "BE_to_BE" , "EE_to_EB" , "EE_to_BE" , "BB_to_EB" , "BB_to_BE" ]
119+
119120kspace_matrix = {}
121+ kspace_dict = {}
122+ std = {}
120123
121- plt .figure (figsize = (12 ,8 ))
122- for spec in spec_list :
123- log .info (f"build kspace filter matrix for { spec } " )
124- kspace_dict , std , kspace_matrix [spec ] = kspace .build_kspace_filter_matrix (lb ,
125- ps_list [spec ],
126- n_sims ,
127- spectra ,
128- return_dict = True )
124+ # first save kspace_matrix per spec
125+ for sv1 , m1 , sv2 , m2 in zip (sv1_iterator , m1_iterator , sv2_iterator , m2_iterator , strict = True ):
126+ spec = sv1 + "_" + m1 + "x" + sv2 + "_" + m2
127+ log .info (f"[Rank { so_mpi .rank } ] build kspace filter matrix for { spec } " )
128+ kspace_dict [spec ], std [spec ], kspace_matrix [spec ] = kspace .build_kspace_filter_matrix (lb ,
129+ ps_list [spec ],
130+ n_sims ,
131+ spectra ,
132+ return_dict = True )
129133
130134 np .save (f"{ tf_dir } /kspace_matrix_{ spec } .npy" , kspace_matrix [spec ])
131- for count , el in enumerate (elements ):
132- plt .subplot (3 , 2 , count + 1 )
133- plt .ylabel (el )
134- plt .xlabel (r"$\ell$" )
135- plt .errorbar (lb , kspace_dict [el ], std [el ] / np .sqrt (n_sims ), label = spec )
136- plt .legend ()
137- plt .savefig (f"{ plot_dir } /kspace_mat.png" , bbox_inches = "tight" )
138- plt .clf ()
139- plt .close ()
140-
141- # lets also make sure that the corrected spectrum is unbiased
135+ so_mpi .barrier ()
142136
137+ # gather dictionary items in rank 0 if needed
138+ if so_mpi .size > 1 :
139+ all_kspace_dict = so_mpi .gather_set_or_dict (kspace_dict , allgather = False , root = 0 )
140+ all_std = so_mpi .gather_set_or_dict (std , allgather = False , root = 0 )
141+ else :
142+ all_kspace_dict = kspace_dict
143+ all_std = std
144+
145+ # then plot
146+ if so_mpi .rank == 0 :
147+ plt .figure (figsize = (12 ,36 ))
148+ for spec in spec_list :
149+ for count , el in enumerate (elements ):
150+ plt .subplot (8 , 2 , count + 1 )
151+ plt .ylabel (el )
152+ plt .xlabel (r"$\ell$" )
153+ if el not in ["TE_to_TE" , "ET_to_ET" , "TB_to_TB" , "BT_to_BT" , "EB_to_EB" , "BE_to_BE" ]:
154+ plt .errorbar (lb , all_kspace_dict [spec ][el ], all_std [spec ][el ] / np .sqrt (n_sims )) #, label = spec)
155+ else :
156+ plt .plot (lb , all_kspace_dict [spec ][el ])
157+ #plt.legend()
158+ plt .savefig (f"{ plot_dir } /kspace_mat.png" , bbox_inches = "tight" )
159+ plt .clf ()
160+ plt .close ()
143161
144- for spec in spec_list :
145- log .info (f"plot uncorrected vs corrected mean for { spec } " )
162+ # lets also make sure that the corrected spectrum is unbiased
163+ # computing some additive corrections first
164+ ps_list_corr = {}
165+ for sv1 , m1 , sv2 , m2 in zip (sv1_iterator , m1_iterator , sv2_iterator , m2_iterator , strict = True ):
166+ spec = sv1 + "_" + m1 + "x" + sv2 + "_" + m2
167+ log .info (f"[Rank { so_mpi .rank } ] correct filtered sims by MC kspace correction, spectrum { spec } " )
146168
147169 Bbl = np .load (opj (f"{ mcm_dir } " , spec + "_Bbl.npy" ))
148170 n1 , n2 = spec .split ("x" )
149171 bin_theory = so_mcm .spin2spin_array_matmul_sparse_dict_vec (Bbl , spectra , cmb_and_fg_dict [n1 , n2 ])
150172
151- for iii in mapset_iterator :
152- lb , ps_list [spec ]["filter" , "standard" ][iii ] = kspace .deconvolve_kspace_filter_matrix (lb ,
153- ps_list [spec ]["filter" , "standard" ][iii ],
154- kspace_matrix [spec ],
155- spectra )
173+ # now ps_list_corr["filter"] is deconvolved by the kspace mc correction
174+ ps_list_corr [spec ] = {}
175+ for iii in range (iStart , iStop ):
176+ lb , ps_list_corr [spec ][iii ] = kspace .deconvolve_kspace_filter_matrix (lb ,
177+ ps_list [spec ]["filter" , "standard" ][iii ],
178+ kspace_matrix [spec ],
179+ spectra )
180+
181+ if n_sims > n_min_sims :
182+ log .info (f"[Rank { so_mpi .rank } ] compute xtra correction for all spectra" )
183+ # not that we only compute this if we have access to a large number of sim (500)
184+ # otherwise the error on the correction will be larger than the correction itself
185+
186+ corr_dict = {}
187+ for spectrum in spectra :
188+ my_list = []
189+
190+ for iii in range (iStart , iStop ):
191+ my_list += [ps_list_corr [spec ][iii ][spectrum ] - ps_list [spec ]["nofilter" , "standard" ][iii ][spectrum ]]
192+
193+ correction = np .mean (my_list , axis = 0 )
194+ sigma = np .std (my_list , axis = 0 )
195+
196+ # write correction to file
197+ corr_dict [spectrum ] = correction
198+
199+
200+ plt .figure (figsize = (12 ,8 ))
201+ plt .plot (lb , bin_theory [spectrum ] * 1 / 100 , color = "black" , label = f"1% { spectrum } " )
202+ plt .errorbar (lb , correction , sigma / np .sqrt (n_sims ), fmt = "-" , label = f"corr { spectrum } { spec } " )
203+ plt .legend ()
204+ plt .show ()
205+ plt .savefig (f"{ plot_dir } /{ spectrum } _correction_{ spec } .png" , bbox_inches = "tight" )
206+ plt .clf ()
207+ plt .close ()
208+
209+ so_spectra .write_ps (f"{ tf_dir } /mc_additive_correction_{ spec } .dat" ,
210+ lb ,
211+ corr_dict ,
212+ type = type ,
213+ spectra = spectra )
214+
156215
157-
158216 for spectrum in spectra :
217+ log .info (f"[Rank { so_mpi .rank } ] plot uncorrected vs corrected mean for { spec } " )
159218 mean , std = {}, {}
219+ mean_uncorr , std_uncorr = {}, {}
220+ my_list_uncorr = []
160221 for filt in ["filter" , "nofilter" ]:
161-
162222 my_list = []
163- for iii in mapset_iterator :
164- my_list += [ps_list [spec ][filt , "standard" ][iii ][spectrum ]]
223+ for iii in range (iStart , iStop ):
224+ if filt == "filter" :
225+ # read the sims corrected for MC kspace and the one only corrected for analytic kspace
226+ my_list += [ps_list_corr [spec ][iii ][spectrum ]]
227+ my_list_uncorr += [ps_list [spec ]["filter" , "standard" ][iii ][spectrum ]]
228+ else :
229+ my_list += [ps_list [spec ]["nofilter" , "standard" ][iii ][spectrum ]]
165230
166231 mean [filt ] = np .mean (my_list , axis = 0 )
167232 std [filt ] = np .std (my_list , axis = 0 )
168233
234+ mean_uncorr ["filter" ] = np .mean (my_list_uncorr , axis = 0 )
235+ std_uncorr ["filter" ] = np .std (my_list_uncorr , axis = 0 )
236+
237+ # plot mean of filtered and corrected (w/ and w/o also additive corrections) and no filtered
169238 plt .figure (figsize = (12 ,8 ))
170239 if spectrum == "TT" :
171240 plt .semilogy ()
172241
173242 plt .plot (lth , cmb_and_fg_dict [n1 , n2 ][spectrum ], color = "grey" , alpha = 0.4 )
174243 plt .plot (lb , bin_theory [spectrum ])
175- plt .errorbar (lb , mean ["nofilter" ], std ["nofilter" ], fmt = "." , color = "red" , label = "no filter" )
176- plt .errorbar (lb , mean ["filter" ], std ["filter" ], fmt = "." , color = "blue" , label = "filter corrected" )
177-
244+ plt .errorbar (lb , mean ["nofilter" ], std ["nofilter" ] / np .sqrt (n_sims ), fmt = "*" , color = "red" , label = "no filter" )
245+ plt .errorbar (lb , mean ["filter" ], std ["filter" ] / np .sqrt (n_sims ), fmt = "." , color = "blue" , label = "filter corrected" )
246+ if n_sims > n_min_sims :
247+ mean_add_corr = mean ["filter" ] - corr_dict [spectrum ]
248+ plt .errorbar (lb , mean_add_corr , std ["filter" ] / np .sqrt (n_sims ), fmt = "+" , color = "green" , label = "filter corrected + additive corrections" )
178249 plt .title (r"$D_{\ell}$" , fontsize = 20 )
179250 plt .xlabel (r"$\ell$" , fontsize = 20 )
180251 plt .legend ()
181252 plt .savefig (f"{ plot_dir } /{ spec } _{ spectrum } .png" , bbox_inches = "tight" )
182253 plt .clf ()
183254 plt .close ()
184255
256+ # plot diff
185257 plt .figure (figsize = (12 ,8 ))
186258 plt .plot (lb , lb * 0 )
187259 plt .errorbar (lb - 10 , mean ["nofilter" ] - bin_theory [spectrum ], std ["nofilter" ] / np .sqrt (n_sims ), fmt = "." , color = "red" , label = "no filter" )
188260 plt .errorbar (lb + 10 , mean ["filter" ] - bin_theory [spectrum ], std ["filter" ] / np .sqrt (n_sims ), fmt = "." , color = "blue" , label = "filter corrected" )
261+ plt .errorbar (lb + 20 , mean_uncorr ["filter" ] - bin_theory [spectrum ], std_uncorr ["filter" ] / np .sqrt (n_sims ), fmt = "+" , color = "black" , label = "filter uncorrected (only analytic)" )
189262 plt .title (r"$\Delta D_{\ell}$" , fontsize = 20 )
190263 plt .xlabel (r"$\ell$" , fontsize = 20 )
191264 plt .legend ()
192265 plt .savefig (f"{ plot_dir } /diff_{ spec } _{ spectrum } .png" , bbox_inches = "tight" )
193266 plt .clf ()
194- plt .close ()
195-
196-
197- if n_sims > n_min_sims :
198- log .info (f"compute xtra correction for TE" )
199-
200- # xtra_correcton for TE/ET
201- # not that we only compute this if we have access to a large number of sim (500)
202- # otherwise the error on the correction will be larger than the correction itself
203-
204- my_list_TE = []
205- my_list_ET = []
206-
207- for iii in mapset_iterator :
208- my_list_TE += [ps_list [spec ]["filter" , "standard" ][iii ]["TE" ] - ps_list [spec ]["nofilter" , "standard" ][iii ]["TE" ]]
209- my_list_ET += [ps_list [spec ]["filter" , "standard" ][iii ]["ET" ] - ps_list [spec ]["nofilter" , "standard" ][iii ]["ET" ]]
210-
211- correction_TE = np .mean (my_list_TE , axis = 0 )
212- correction_ET = np .mean (my_list_ET , axis = 0 )
213- sigma_TE = np .std (my_list_TE , axis = 0 )
214- sigma_ET = np .std (my_list_ET , axis = 0 )
215-
216- plt .figure (figsize = (12 ,8 ))
217- plt .plot (lb , bin_theory ["TE" ] * 1 / 100 , color = "black" , label = "1% TE" )
218- plt .errorbar (lb , correction_TE , sigma_TE / np .sqrt (n_sims ), fmt = "-" , label = f"corr TE { spec } " )
219- plt .errorbar (lb , correction_ET , sigma_ET / np .sqrt (n_sims ), fmt = "--" , label = f"corr ET { spec } " )
220- plt .legend ()
221- plt .show ()
222- plt .savefig (f"{ plot_dir } /TE_correction_{ spec } .png" , bbox_inches = "tight" )
223- plt .clf ()
224- plt .close ()
225-
226- # write correction to file
227- corr_dict = {}
228- for spectrum in spectra :
229- corr_dict [spectrum ] = lb * 0
230- corr_dict ["TE" ] = correction_TE
231- corr_dict ["ET" ] = correction_ET
232-
233- so_spectra .write_ps (f"{ tf_dir } /TE_correction_{ spec } .dat" ,
234- lb ,
235- corr_dict ,
236- type = type ,
237- spectra = spectra )
267+ plt .close ()
0 commit comments