|
1 | | -# --- |
2 | | -# jupyter: |
3 | | -# jupytext: |
4 | | -# text_representation: |
5 | | -# extension: .py |
6 | | -# format_name: light |
7 | | -# format_version: '1.5' |
8 | | -# jupytext_version: 1.15.1 |
9 | | -# kernelspec: |
10 | | -# display_name: Python 3 |
11 | | -# language: python |
12 | | -# name: python3 |
13 | | -# --- |
14 | | - |
15 | | -# # Calibrate comprehensive catalogue |
16 | | - |
17 | | -# %reload_ext autoreload |
18 | | -# %autoreload 2 |
| 1 | +# %% |
| 2 | +# Calibrate comprehensive catalogue |
19 | 3 |
|
| 4 | +# %% |
| 5 | +from IPython import get_ipython |
| 6 | + |
| 7 | +# %% |
| 8 | +# enable autoreload for interactive sessions |
| 9 | +ipython = get_ipython() |
| 10 | +if ipython is not None: |
| 11 | + ipython.run_line_magic("load_ext", "autoreload") |
| 12 | + ipython.run_line_magic("autoreload", "2") |
| 13 | + ipython.run_line_magic("load_ext", "log_cell_time") |
| 14 | + |
| 15 | +# %% |
20 | 16 | import sys |
21 | 17 | import os |
22 | 18 | import numpy as np |
|
29 | 25 | from sp_validation import calibration |
30 | 26 | import sp_validation.cat as cat |
31 | 27 |
|
| 28 | +# %% |
32 | 29 | # Initialize calibration class instance |
33 | 30 | obj = sp_joint.CalibrateCat() |
34 | 31 |
|
| 32 | +# %% |
35 | 33 | # Read configuration file and set parameters |
36 | 34 | config = obj.read_config_set_params("config_mask.yaml") |
37 | 35 |
|
38 | | -# !pwd |
39 | | - |
40 | | -# + |
| 36 | +# %% |
41 | 37 | # Get data. Set load_into_memory to False for very large files |
42 | | - |
43 | | - |
44 | 38 | dat, dat_ext = obj.read_cat(load_into_memory=False) |
45 | | -# - |
46 | 39 |
|
47 | | -n_test = -1 |
48 | | -#n_test = 100000 |
| 40 | +# %% |
| 41 | +#n_test = -1 |
| 42 | +n_test = 100000 |
49 | 43 | if n_test > 0: |
50 | 44 | print(f"MKDEBUG testing only first {n_test} objects") |
51 | 45 | dat = dat[:n_test] |
52 | 46 | dat_ext = dat_ext[:n_test] |
53 | 47 |
|
| 48 | + |
| 49 | + |
| 50 | +# %% |
54 | 51 | # ## Masking |
55 | 52 |
|
| 53 | +# %% |
56 | 54 | # ### Pre-processing ShapePipe flags |
57 | | - |
58 | | -masks, labels = sp_joint.get_masks_from_config(config, dat, dat_ext) |
| 55 | +masks, labels = sp_joint.get_masks_from_config( |
| 56 | + config, |
| 57 | + dat, |
| 58 | + dat_ext, |
| 59 | + verbose=True |
| 60 | +) |
59 | 61 |
|
60 | 62 | mask_combined = sp_joint.Mask.from_list( |
61 | 63 | masks, |
62 | 64 | label="combined", |
63 | 65 | verbose=obj._params["verbose"], |
64 | 66 | ) |
65 | 67 |
|
| 68 | +# %% |
66 | 69 | # Output some mask statistics |
67 | 70 | sp_joint.print_mask_stats(dat.shape[0], masks, mask_combined) |
68 | 71 |
|
|
74 | 77 |
|
75 | 78 | sp_joint.sky_plots(dat, masks, labels, zoom_ra, zoom_dec) |
76 | 79 |
|
| 80 | +# %% |
77 | 81 | # ### Calibration |
78 | 82 |
|
79 | | -# + |
80 | 83 | # Call metacal |
81 | | - |
82 | 84 | cm = config["metacal"] |
83 | 85 |
|
| 86 | +# %% |
84 | 87 | gal_metacal = metacal( |
85 | 88 | dat, |
86 | 89 | mask_combined._mask, |
|
90 | 93 | rel_size_max=cm["gal_rel_size_max"], |
91 | 94 | size_corr_ell=cm["gal_size_corr_ell"], |
92 | 95 | sigma_eps=cm["sigma_eps_prior"], |
| 96 | + global_R_weight=cm["global_R_weight"], |
93 | 97 | col_2d=False, |
94 | 98 | verbose=True, |
95 | 99 | ) |
96 | | -# - |
97 | 100 |
|
98 | | -g_corr_mc, g_uncorr, w, mask_metacal, c, c_err = calibration.get_calibrated_m_c(gal_metacal) |
| 101 | +# %% |
| 102 | +g_corr_mc, g_uncorr, w, mask_metacal, c, c_err = ( |
| 103 | + calibration.get_calibrated_m_c(gal_metacal) |
| 104 | +) |
99 | 105 |
|
100 | 106 | num_ok = len(g_corr_mc[0]) |
| 107 | +num_obj = len(dat) |
101 | 108 | sp_joint.Mask.print_strings( |
102 | 109 | "metacal", "gal selection", str(num_ok), f"{num_ok / num_obj:10.2%}" |
103 | 110 | ) |
104 | 111 |
|
105 | | -# + |
| 112 | +# %% |
106 | 113 | # Compute DES weights |
107 | 114 |
|
108 | 115 | cat_gal = {} |
|
117 | 124 | num_bins=20, |
118 | 125 | ) |
119 | 126 |
|
120 | | -# + |
| 127 | +# %% |
121 | 128 | # Correct for PSF leakage |
122 | 129 |
|
123 | 130 | alpha_1, alpha_2 = sp_joint.compute_PSF_leakage( |
|
128 | 135 | mask_metacal, |
129 | 136 | num_bins=20, |
130 | 137 | ) |
131 | | -# - |
132 | 138 |
|
133 | 139 | # Compute leakage-corrected ellipticities |
134 | 140 | e1_leak_corrected = g_corr_mc[0] - alpha_1 * cat_gal["e1_PSF"] |
135 | 141 | e2_leak_corrected = g_corr_mc[1] - alpha_2 * cat_gal["e2_PSF"] |
136 | 142 |
|
| 143 | +# %% |
137 | 144 | # Get some memory back |
138 | 145 | for mask in masks: |
139 | 146 | del mask |
140 | 147 |
|
141 | | -# + |
| 148 | +# %% |
| 149 | +# %% |
142 | 150 | # Additional quantities |
143 | | -R_shear = np.mean(gal_metacal.R_shear, 2) |
144 | | - |
145 | 151 | ra = cat.get_col(dat, "RA", mask_combined._mask, mask_metacal) |
146 | 152 | dec = cat.get_col(dat, "Dec", mask_combined._mask, mask_metacal) |
147 | 153 | mag = cat.get_col(dat, "mag", mask_combined._mask, mask_metacal) |
148 | 154 |
|
149 | 155 |
|
150 | | -# + |
151 | | - |
| 156 | +# %% |
| 157 | +# Additional data columns to write to output cat |
152 | 158 | add_cols = [ |
153 | 159 | "w_iv", |
154 | 160 | "FLUX_RADIUS", |
|
161 | 167 | "FLUXERR_AUTO", |
162 | 168 | "FLUX_APER", |
163 | 169 | "FLUXERR_APER", |
| 170 | + "NGMIX_T_NOSHEAR", |
| 171 | + "NGMIX_Tpsf_NOSHEAR", |
| 172 | + "fwhm_PSF", |
164 | 173 | ] |
165 | 174 | add_cols_data = {} |
166 | 175 | for key in add_cols: |
167 | | - add_cols_data[key] = dat[key][mask_combined._mask][mask_metacal] |
168 | | - |
169 | | -# + |
| 176 | + add_cols_data[key] = cat.get_col( |
| 177 | + dat, |
| 178 | + key, |
| 179 | + mask_combined._mask, |
| 180 | + mask_metacal |
| 181 | + ) |
| 182 | + #add_cols_data[key] = dat[key][mask_combined._mask][mask_metacal] |
| 183 | + |
| 184 | +# %% |
| 185 | +# Additional post-processing columns to write to output cat |
| 186 | +add_cols_post = [ |
| 187 | + "R_g11", |
| 188 | + "R_g12", |
| 189 | + "R_g21", |
| 190 | + "R_g22", |
| 191 | + "e1_PSF", |
| 192 | + "e2_PSF", |
| 193 | +] |
| 194 | +for key in add_cols_post: |
| 195 | + add_cols_data[key] = cat_gal[key] |
170 | 196 |
|
| 197 | +# %% |
| 198 | +# Other additional columns |
171 | 199 | add_cols_data["e1_leak_corrected"] = e1_leak_corrected |
172 | 200 | add_cols_data["e2_leak_corrected"] = e2_leak_corrected |
173 | 201 |
|
174 | | -add_cols_data["e1_PSF"] = cat_gal["e1_PSF"] |
175 | | -add_cols_data["e2_PSF"] = cat_gal["e2_PSF"] |
176 | | -add_cols_data["fwhm_PSF"] = cat.get_col( |
177 | | - dat, "fwhm_PSF", mask_combined._mask, mask_metacal |
178 | | -) |
179 | | - |
180 | | -# + |
| 202 | +# %% |
181 | 203 | # Add information to FITS header |
182 | 204 |
|
183 | 205 | # Generate new header |
184 | 206 | header = fits.Header() |
185 | 207 |
|
186 | | -# Add general config information to FITS header |
187 | | -obj.add_params_to_FITS_header(header) |
| 208 | +# Add general and metacal config information to FITS header |
| 209 | +obj.add_params_to_FITS_header(header, cm=cm) |
188 | 210 |
|
| 211 | +# %% |
189 | 212 | # Add mask information to FITS header |
190 | 213 | for my_mask in masks: |
191 | 214 | my_mask.add_summary_to_FITS_header(header) |
192 | 215 |
|
193 | | -# + |
| 216 | + |
| 217 | +# %% |
| 218 | +header |
| 219 | + |
| 220 | +# %% |
194 | 221 | output_shape_cat_path = obj._params["input_path"].replace( |
195 | 222 | "comprehensive", "cut" |
196 | 223 | ) |
|
215 | 242 | add_cols=add_cols_data, |
216 | 243 | add_header=header, |
217 | 244 | ) |
218 | | -# - |
219 | 245 |
|
| 246 | +# %% |
220 | 247 | with open("masks.txt", "w") as f_out: |
221 | 248 | for my_mask in masks: |
222 | 249 | my_mask.print_summary(f_out) |
223 | 250 |
|
| 251 | +# %% |
224 | 252 | from scipy import stats |
225 | 253 |
|
226 | | -# |
227 | | - |
228 | 254 | all_masks = masks[:-3] |
229 | 255 |
|
230 | | -# + |
231 | 256 | if not obj._params["cmatrices"]: |
232 | 257 | print("Skipping cmatric calculations") |
233 | 258 | sys.exit(0) |
234 | 259 |
|
235 | 260 | r_val, r_cl = sp_joint.correlation_matrix(all_masks) |
236 | 261 |
|
237 | | -# + |
238 | | - |
239 | 262 | n = len(all_masks) |
240 | 263 | keys = [my_mask._label for my_mask in all_masks] |
241 | 264 |
|
|
246 | 269 | plt.colorbar() |
247 | 270 | plt.savefig("correlation_matrix.png") |
248 | 271 |
|
249 | | -# - |
250 | | - |
251 | | - |
| 272 | +# %% |
252 | 273 | n_key = len(all_masks) |
253 | 274 | cms = np.zeros((n_key, n_key, 2, 2)) |
254 | 275 | for idx in range(n_key): |
|
261 | 282 | res = sp_joint.confusion_matrix(masks[idx]._mask, masks[jdx]._mask) |
262 | 283 | cms[idx][jdx] = res["cmn"] |
263 | 284 |
|
264 | | -# + |
| 285 | +# %% |
265 | 286 | import seaborn as sns |
266 | 287 |
|
267 | 288 | fig = plt.figure(figsize=(30, 30)) |
|
294 | 315 |
|
295 | 316 | plt.show(block=False) |
296 | 317 | plt.savefig("confusion_matrix.png") |
297 | | -# - |
| 318 | + |
298 | 319 |
|
299 | 320 | obj.close_hd5() |
| 321 | + |
| 322 | +# %% |
0 commit comments