@@ -30,27 +30,29 @@ def get_proj_pattern(test, map_set, ref_map_set):
3030# log calib infos from calib yaml file
3131with open (d ['calib_yaml' ], "r" ) as f :
3232 calib_dict : dict = yaml .safe_load (f )
33- calib_infos = calib_dict ['get_calibs.py' ]
33+ calib_infos : dict = calib_dict ['get_calibs.py' ]
3434
3535planck_corr = calib_infos ['planck_corr' ]
3636subtract_bf_fg = calib_infos ['subtract_bf_fg' ]
3737
38+ data_dir = d ["data_dir" ]
39+ plot_dir = d ["plots_dir" ]
3840spec_dir = d ['spec_dir' ]
3941cov_dir = d ['cov_dir' ]
40- bestfit_dir = d ['bestfits_dir ' ]
42+ bestfit_dir = d ['best_fits_dir ' ]
4143
4244# Create output dirs
43- output_dir = d ['calib_dir' ]
44- residual_output_dir = f"{ output_dir } /residuals"
45- plot_output_dir = f"{ output_dir } /plots "
46- chains_dir = f" { output_dir } /chains"
45+ calib_dir = d ['calib_dir' ]
46+ residual_output_dir = f"{ calib_dir } /residuals"
47+ chains_dir = f"{ calib_dir } /chains "
48+ plot_output_dir = plot_dir + '/calib/'
4749
4850if planck_corr :
4951 spec_dir = "spectra_leak_corr_planck_bias_corr"
50- output_dir += "_planck_bias_corrected"
52+ calib_dir += "_planck_bias_corrected"
5153
5254if subtract_bf_fg :
53- output_dir += "_fg_sub"
55+ calib_dir += "_fg_sub"
5456
5557_ , _ , lb , _ = pspy_utils .read_binning_file (d ["binning_file" ], d ["lmax" ])
5658n_bins = len (lb )
@@ -62,7 +64,7 @@ def get_proj_pattern(test, map_set, ref_map_set):
6264else :
6365 modes = spectra
6466
65- pspy_utils .create_directory (output_dir )
67+ pspy_utils .create_directory (calib_dir )
6668pspy_utils .create_directory (residual_output_dir )
6769pspy_utils .create_directory (chains_dir )
6870pspy_utils .create_directory (plot_output_dir )
@@ -78,110 +80,104 @@ def get_proj_pattern(test, map_set, ref_map_set):
7880tests = ["AxA-AxB" , "AxA-BxB" , "BxB-AxB" ]
7981
8082for test in tests :
81- for sv in calib_infos ["surveys_to_calib" ]:
82- for ar in d [f"arrays_{ sv } " ]:
83+ # Loop over each map_set-ref_map_set combination
84+ # (so you can measure calib on a same map_set with different ref_map_set)
85+ for map_set , ref_map_set in calib_infos [f"ref_map_sets" ].items ():
86+ name , proj_pattern = get_proj_pattern (test , map_set , ref_map_set )
8387
84- map_set = f"{ sv } _{ ar } "
85- ref_map_set = calib_infos [f"ref_map_sets" ][map_set ]
86-
87- name , proj_pattern = get_proj_pattern (test , map_set , ref_map_set )
88-
89- spectra_for_cal = [
90- (ref_map_set , ref_map_set , "TT" ),
91- (ref_map_set , map_set , "TT" ),
92- (map_set , map_set , "TT" ),
93- # (map_set, map_set, "TT"),
94- # (map_set, ref_map_set, "TT"),
95- # (ref_map_set, ref_map_set, "TT"),
96- ]
97-
98- # Load spectra and cov
99- ps_dict = {}
100- cov_dict = {}
101- for i , (ms1 , ms2 , m1 ) in enumerate (spectra_for_cal ):
102- _ , ps = so_spectra .read_ps (f"{ spec_dir } /Dl_{ ms1 } x{ ms2 } _cross.dat" ,
103- spectra = spectra )
104-
105- ps_dict [ms1 , ms2 , m1 ] = ps [m1 ]
88+ spectra_for_cal = [
89+ (ref_map_set , ref_map_set , "TT" ),
90+ (ref_map_set , map_set , "TT" ),
91+ (map_set , map_set , "TT" ),
92+ # (map_set, map_set, "TT"),
93+ # (map_set, ref_map_set, "TT"),
94+ # (ref_map_set, ref_map_set, "TT"),
95+ ]
96+
97+ # Load spectra and cov
98+ ps_dict = {}
99+ cov_dict = {}
100+ for i , (ms1 , ms2 , m1 ) in enumerate (spectra_for_cal ):
101+ _ , ps = so_spectra .read_ps (f"{ spec_dir } /Dl_{ ms1 } x{ ms2 } _cross.dat" ,
102+ spectra = spectra )
106103
107- if (m1 == "TT" ) & (subtract_bf_fg ):
108- log .info (f"remove fg { m1 } { ms1 } x { ms2 } " )
109- l_fg , bf_fg = so_spectra .read_ps (f"{ bestfit_dir } /fg_{ ms1 } x{ ms2 } .dat" , spectra = spectra )
110- _ , bf_fg_TT_binned = pspy_utils .naive_binning (l_fg , bf_fg ["TT" ], d ["binning_file" ], d ["lmax" ])
111- ps_dict [ms1 , ms2 , m1 ] -= bf_fg_TT_binned
104+ ps_dict [ms1 , ms2 , m1 ] = ps [m1 ]
105+
106+ if (m1 == "TT" ) & (subtract_bf_fg ):
107+ log .info (f"remove fg { m1 } { ms1 } x { ms2 } " )
108+ l_fg , bf_fg = so_spectra .read_ps (f"{ bestfit_dir } /fg_{ ms1 } x{ ms2 } .dat" , spectra = spectra )
109+ _ , bf_fg_TT_binned = pspy_utils .naive_binning (l_fg , bf_fg ["TT" ], d ["binning_file" ], d ["lmax" ])
110+ ps_dict [ms1 , ms2 , m1 ] -= bf_fg_TT_binned
112111
113112
114- for j , (ms3 , ms4 , m2 ) in enumerate (spectra_for_cal ):
115- if j < i : continue
116- cov = np .load (f"{ cov_dir } /analytic_cov_{ ms1 } x{ ms2 } _{ ms3 } x{ ms4 } .npy" )
117- cov = so_cov .selectblock (cov , modes , n_bins = n_bins , block = m1 + m2 )
118- cov_dict [(ms1 , ms2 , m1 ), (ms3 , ms4 , m2 )] = cov
113+ for j , (ms3 , ms4 , m2 ) in enumerate (spectra_for_cal ):
114+ if j < i : continue
115+ cov = np .load (f"{ cov_dir } /analytic_cov_{ ms1 } x{ ms2 } _{ ms3 } x{ ms4 } .npy" )
116+ cov = so_cov .selectblock (cov , modes , n_bins = n_bins , block = m1 + m2 )
117+ cov_dict [(ms1 , ms2 , m1 ), (ms3 , ms4 , m2 )] = cov
119118
120- # Concatenation
121- spec_vec , full_cov = consistency .append_spectra_and_cov (ps_dict , cov_dict , spectra_for_cal )
119+ # Concatenation
120+ spec_vec , full_cov = consistency .append_spectra_and_cov (ps_dict , cov_dict , spectra_for_cal )
122121
123- # Save and plot residuals before calibration
124- res_spectrum , res_cov = consistency .project_spectra_vec_and_cov (spec_vec , full_cov , proj_pattern )
125- np .savetxt (f"{ residual_output_dir } /residual_{ name } _before.dat" , np .array ([lb , res_spectrum ]).T )
126- np .savetxt (f"{ residual_output_dir } /residual_cov_{ name } .dat" , res_cov )
122+ # Save and plot residuals before calibration
123+ res_spectrum , res_cov = consistency .project_spectra_vec_and_cov (spec_vec , full_cov , proj_pattern )
124+ np .savetxt (f"{ residual_output_dir } /residual_{ name } _before.dat" , np .array ([lb , res_spectrum ]).T )
125+ np .savetxt (f"{ residual_output_dir } /residual_cov_{ name } .dat" , res_cov )
127126
128- lmin , lmax = calib_infos ['ell_ranges' ][map_set ]
129- id = np .where ((lb >= lmin ) & (lb <= lmax ))[0 ]
130- consistency .plot_residual (lb , res_spectrum , {"analytical" : res_cov }, "TT" , f"{ map_set } { test } " ,
131- f"{ plot_output_dir } /residual_{ name } _before" ,
132- lrange = id , l_pow = 1 , ylims = calib_infos ['y_lims_plot_TT' ])
127+ lmin , lmax = calib_infos ['ell_ranges' ][map_set ]
128+ id = np .where ((lb >= lmin ) & (lb <= lmax ))[0 ]
129+ consistency .plot_residual (lb , res_spectrum , {"analytical" : res_cov }, "TT" , f"{ map_set } { test } " ,
130+ f"{ plot_output_dir } /residual_{ name } _before" ,
131+ lrange = id , l_pow = 1 , ylims = calib_infos ['y_lims_plot_TT' ])
133132
134- # Calibrate the spectra
135- cal_mean , cal_std = consistency .get_calibration_amplitudes (spec_vec , full_cov ,
136- proj_pattern , "TT" , id ,
137- f"{ chains_dir } /{ name } " )
133+ # Calibrate the spectra
134+ cal_mean , cal_std = consistency .get_calibration_amplitudes (spec_vec , full_cov ,
135+ proj_pattern , "TT" , id ,
136+ f"{ chains_dir } /{ name } " , )
138137
139138
140- results_dict [test , ar ] = {"multipole_range" : calib_infos ['ell_ranges' ][map_set ],
141- "ref_map_set" : ref_map_set ,
142- "calibs" : [cal_mean , cal_std ]}
139+ results_dict [test , map_set ] = {"multipole_range" : calib_infos ['ell_ranges' ][map_set ],
140+ "ref_map_set" : ref_map_set ,
141+ "calibs" : [cal_mean , cal_std ]}
143142
144- calib_vec = np .array ([cal_mean ** 2 , cal_mean , 1 ])
145- res_spectrum , res_cov = consistency .project_spectra_vec_and_cov (spec_vec , full_cov ,
146- proj_pattern ,
147- calib_vec = calib_vec )
148-
149- np .savetxt (f"{ residual_output_dir } /residual_{ name } _after.dat" , np .array ([lb , res_spectrum ]).T )
150- consistency .plot_residual (lb , res_spectrum , {"analytical" : res_cov }, "TT" , f"{ map_set } { test } " ,
151- f"{ plot_output_dir } /residual_{ name } _after" ,
152- lrange = id , l_pow = 1 , ylims = calib_infos [f"y_lims_plot_TT" ])
143+ calib_vec = np .array ([cal_mean ** 2 , cal_mean , 1 ])
144+ res_spectrum , res_cov = consistency .project_spectra_vec_and_cov (spec_vec , full_cov ,
145+ proj_pattern ,
146+ calib_vec = calib_vec )
147+
148+ np .savetxt (f"{ residual_output_dir } /residual_{ name } _after.dat" , np .array ([lb , res_spectrum ]).T )
149+ consistency .plot_residual (lb , res_spectrum , {"analytical" : res_cov }, "TT" , f"{ map_set } { test } " ,
150+ f"{ plot_output_dir } /residual_{ name } _after" ,
151+ lrange = id , l_pow = 1 , ylims = calib_infos [f"y_lims_plot_TT" ])
153152
154153
155154
156155# plot the cal factors
157156color_list = ["blue" , "red" , "green" ]
158157
159- for sv in calib_infos ['surveys_to_calib' ]:
160- for i , ar in enumerate (d [f"arrays_{ sv } " ]):
161- map_set = f"{ sv } _{ ar } "
162- ref_map_set = calib_infos [f"ref_map_sets" ][map_set ]
163- print (f"**************" )
164- print (f"calibration { map_set } with { ref_map_set } " )
165-
166- for j , test in enumerate (tests ):
167- cal , std = results_dict [test , ar ]["calibs" ]
168- print (f"{ test } , cal: { cal } , sigma cal: { std } " )
169-
170- plt .errorbar (i + 0.9 + j * 0.1 , cal , std , label = test ,
171- color = color_list [j ], marker = "." ,
172- ls = "None" ,
173- markersize = 6.5 ,
174- markeredgewidth = 2 )
175-
176- if i == 0 :
177- plt .legend (fontsize = 15 )
178-
179- x = np .arange (1 , len (d [f"arrays_{ sv } " ]) + 1 )
180- plt .xticks (x , d [f"arrays_{ sv } " ])
181- plt .ylim (0.967 , 1.06 )
182- plt .tight_layout ()
183- plt .savefig (f"{ plot_output_dir } /calibs_summary.pdf" , bbox_inches = "tight" )
184- plt .clf ()
185- plt .close ()
186-
187- pickle .dump (results_dict , open (f"{ output_dir } /calibs_dict.pkl" , "wb" ))
158+ for i , (map_set , ref_map_set ) in enumerate (calib_infos [f"ref_map_sets" ].items ()):
159+ print (f"**************" )
160+ print (f"calibration { map_set } with { ref_map_set } " )
161+
162+ for j , test in enumerate (tests ):
163+ cal , std = results_dict [test , map_set ]["calibs" ]
164+ print (f"{ test } , cal: { cal } , sigma cal: { std } " )
165+
166+ plt .errorbar (i + 0.9 + j * 0.1 , cal , std , label = test ,
167+ color = color_list [j ], marker = "." ,
168+ ls = "None" ,
169+ markersize = 6.5 ,
170+ markeredgewidth = 2 )
171+
172+ if i == 0 :
173+ plt .legend (fontsize = 15 )
174+
175+ x = np .arange (1 , len (calib_infos [f"ref_map_sets" ]) + 1 )
176+ plt .xticks (x , calib_infos [f"ref_map_sets" ].keys ())
177+ # plt.ylim(0.967, 1.06)
178+ plt .tight_layout ()
179+ plt .savefig (f"{ plot_output_dir } /calibs_summary.pdf" , bbox_inches = "tight" )
180+ plt .clf ()
181+ plt .close ()
182+
183+ pickle .dump (results_dict , open (f"{ calib_dir } /calibs_dict.pkl" , "wb" ))
0 commit comments