Skip to content

Commit da3e8e5

Browse files
authored
Merry spt (#206)
* Add compare_Bbls script * Update spt_plot_spectra with binning residuals
1 parent e8d9bb5 commit da3e8e5

2 files changed

Lines changed: 113 additions & 32 deletions

File tree

project/SPT_dev/compare_Bbls.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from pspy import so_map, so_spectra, pspy_utils, so_mcm, so_dict
2+
import numpy as np
3+
import sys
4+
import candl
5+
import spt_candl_data
6+
from matplotlib import pyplot as plt
7+
8+
d = so_dict.so_dict()
9+
d.read_from_file(sys.argv[1])
10+
11+
spin_pairs = ["spin0xspin0", "spin0xspin2", "spin2xspin0", "spin2xspin2"]
12+
spectra = ["TT", "TE", "TB", "ET", "BT", "EE", "EB", "BE", "BB"]
13+
l, Dl = so_spectra.read_ps("/pscratch/sd/m/merrydup/LAT_ISO/spectra/cmb.dat", spectra=spectra)
14+
LMAX = d["lmax"]
15+
_, _, ell, _ = pspy_utils.read_binning_file(d["binning_file"], lmax=LMAX)
16+
17+
18+
mcms_dir = "mcms/"
19+
20+
# Load SPT window functions from candl data
21+
candl_like = candl.Like(spt_candl_data.SPT3G_D1_TnE)
22+
window_function_dict = {spec_to_load: candl_like.window_functions[spec_id] for spec_id, spec_to_load in enumerate(candl_like.spec_order)}
23+
24+
# SPT 90GHz is named 95 in Thibaut's paramfile :(
25+
freq_mapping = {
26+
"90": "95",
27+
"150": "150",
28+
"220": "220",
29+
}
30+
31+
# Load Bbl from pipeline outputs (mcms dir) and compute binned theory spectra
32+
Bbl_dict = {}
33+
Dl_wf = {} # spectra binned with SPT window function
34+
Dl_bbl = {} # spectra binned with Bbl
35+
for freqs in [["90", "90"], ["90", "150"], ["90", "220"], ["150", "150"], ["150", "220"], ["220", "220"]]:
36+
freqs_str = 'x'.join(freqs)
37+
38+
mbb_inv, Bbl = so_mcm.read_coupling(
39+
prefix=f"{mcms_dir}/spt_{int(freq_mapping[freqs[0]]):03d}xspt_{int(freq_mapping[freqs[1]]):03d}",
40+
spin_pairs=spin_pairs
41+
)
42+
43+
Bbl_dict[f"TT {freqs_str}"] = Bbl["spin0xspin0"]
44+
Bbl_dict[f"TE {freqs_str}"] = Bbl["spin0xspin2"]
45+
46+
# Load EE spin2xspin2
47+
nbins, my_lmax = int(Bbl["spin2xspin2"].shape[0]/4), int(Bbl["spin2xspin2"].shape[1]/4)
48+
Bbl_dict[f"EE {freqs_str}"] = Bbl["spin2xspin2"][:nbins, :my_lmax]
49+
50+
for spec in ["TT", "TE", "EE"]:
51+
Dl_wf[f"{spec} {freqs_str}"] = np.dot(window_function_dict[f"{spec} {freqs_str}"].T, Dl[spec][:window_function_dict[f"{spec} {freqs_str}"].shape[0]])
52+
Dl_bbl[f"{spec} {freqs_str}"] = np.dot(Bbl_dict[f"{spec} {freqs_str}"], Dl[spec][:LMAX])
53+
54+
# SPT has different lmin and lmax depending on spectra and idk how to get these from candl_data so I had to figure out these
55+
lmin_id = {
56+
"TT":8,
57+
"TE":8,
58+
"EE":8,
59+
}
60+
lmax_id = {
61+
"TT":60,
62+
"TE":71,
63+
"EE":71,
64+
}
65+
66+
for freqs in [["90", "90"], ["90", "150"], ["90", "220"], ["150", "150"], ["150", "220"], ["220", "220"]]:
67+
freqs_str = 'x'.join(freqs)
68+
for spec in ["TT", "TE", "EE"]:
69+
fig, ax = plt.subplots(2, figsize=(8, 6))
70+
ax[0].plot(ell[lmin_id[spec]:lmax_id[spec]], Dl_wf[f"{spec} {freqs_str}"][:61], label="win func SPT * theory")
71+
ax[0].plot(ell[lmin_id[spec]:lmax_id[spec]], Dl_bbl[f"{spec} {freqs_str}"][lmin_id[spec]:lmax_id[spec]], label="bbl * theory")
72+
ax[0].legend()
73+
ax[0].set_ylabel(fr"$D_\ell^{{{spec}}}$", fontsize=15)
74+
75+
Dls_res = Dl_wf[f"{spec} {freqs_str}"][:61] - Dl_bbl[f"{spec} {freqs_str}"][lmin_id[spec]:lmax_id[spec]]
76+
77+
ax[1].plot(ell[lmin_id[spec]:lmax_id[spec]], Dls_res, label="win func SPT * theory - bbl * theory")
78+
ax[1].legend()
79+
ax[1].set_ylabel(fr"$\Delta D_\ell^{{{spec}}}$", fontsize=15)
80+
81+
np.savetxt(f"{mcms_dir}/res_bbl_{spec}_{freqs_str}.txt", np.array([ell[lmin_id[spec]:lmax_id[spec]], Dls_res]).T)
82+
plt.savefig(f"plots/{spec}_{freqs_str}_compare_binning")

project/SPT_dev/spt_plot_spectra.py

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def correct_spt_transfer_function(lb, ps, spec_name, Bbl):
3030

3131
name = spec_name.replace("spt_", "")
3232
fa, fb = name.split("x")
33-
print(fa, fb)
3433

3534
tf = {}
3635
for mode in ["tt", "te", "et", "ee"]:
@@ -57,7 +56,6 @@ def correct_spt_additive_bias(lb, ps, spec_name, Bbl):
5756

5857
name = spec_name.replace("spt_", "")
5958
fa, fb = name.split("x")
60-
print(fa, fb)
6159

6260
additive_bias = {}
6361
for mode in ["tt", "te", "et", "ee"]:
@@ -118,16 +116,16 @@ def correct_spt_additive_bias(lb, ps, spec_name, Bbl):
118116

119117
for spec in ["TB", "EB", "BB"]:
120118
for spec_name in spec_name_list:
121-
plt.plot(l_th[:3500], ps_th[spec][:3500], color="black")
122-
plt.plot(lb, Db_dict_bias_tf_corr[spec_name][spec], label=f"{spec} {spec_name} (uncorrected)")
123-
plt.legend()
124-
plt.savefig(f"{plot_dir}/{spec_name}_{spec}.png", bbox_inches="tight")
125-
plt.clf()
126-
plt.close()
127-
119+
plt.plot(l_th[:3500], ps_th[spec][:3500], color="black")
120+
plt.plot(lb, Db_dict_bias_tf_corr[spec_name][spec], label=f"{spec} {spec_name} (uncorrected)")
121+
plt.legend()
122+
plt.savefig(f"{plot_dir}/{spec_name}_{spec}.png", bbox_inches="tight")
123+
plt.clf()
124+
plt.close()
128125

129126
for spec in ["TT", "TE", "EE"]:
130127
for spec_name in spec_name_list:
128+
fig, ax = plt.subplots(3, sharex=True, figsize=(9, 8), gridspec_kw={'hspace':0.1})
131129

132130
spec_to_plot = f"{spec} {camphuis_conv[spec_name]}"
133131
ix_of_spec = candl_like.spec_order.index(spec_to_plot)
@@ -143,30 +141,31 @@ def correct_spt_additive_bias(lb, ps, spec_name, Bbl):
143141
lb_redo, Db_redo = lb[id_redo], Db_dict[spec_name][spec][id_redo]
144142
Db_redo_tf_corr, Db_redo_bias_tf_corr = Db_dict_tf_corr[spec_name][spec][id_redo], Db_dict_bias_tf_corr[spec_name][spec][id_redo]
145143

146-
plt.figure(figsize=(12,8))
147-
148-
plt.subplot(311)
149144
if spec in ["TT", "EE"]:
150-
plt.semilogy()
151-
plt.errorbar(l_spt, Db, sigmab, lw=0.5, marker="o", ms=3, elinewidth=1, label=f"SPT {spec_name}")
152-
plt.xlabel(r"$\ell$", fontsize=14)
153-
plt.ylabel(r"$D_\ell$", fontsize=14)
154-
plt.plot(lb_redo, Db_redo_bias_tf_corr, label=f"SPT redo {spec_name}, bias tf corrected")
155-
156-
plt.legend()
157-
plt.subplot(312)
158-
plt.errorbar(l_spt, l_spt*0)
159-
plt.errorbar(l_spt, Db-Db_redo_bias_tf_corr, sigmab, lw=0.5, marker="o", ms=3, elinewidth=1, label=f"SPT - SPT redo, bias tf corrected {spec_name}")
160-
plt.legend()
161-
plt.xlabel(r"$\ell$", fontsize=14)
162-
plt.ylabel(r"$D_\ell - D^{\rm redo}_\ell$", fontsize=14)
163-
164-
plt.subplot(313)
165-
plt.errorbar(l_spt, Db/Db_redo_bias_tf_corr, lw=0.5, marker="o", ms=3, elinewidth=1, label=f"SPT/ SPT redo {spec_name}")
166-
plt.xlabel(r"$\ell$", fontsize=14)
167-
plt.ylabel(r"$D_\ell / D^{\rm redo}_\ell$", fontsize=14)
168-
169-
plt.ylim(0.95, 1.05)
145+
ax[0].semilogy()
146+
ax[0].errorbar(l_spt, Db, sigmab, lw=0.5, marker="o", ms=3, elinewidth=1, label=f"SPT {spec_name}")
147+
ax[0].set_xlabel(r"$\ell$", fontsize=14)
148+
ax[0].set_ylabel(r"$D_\ell$", fontsize=14)
149+
ax[0].plot(lb_redo, Db_redo_bias_tf_corr, label=f"SPT redo {spec_name}, bias tf corrected")
150+
ax[0].legend()
151+
152+
try:
153+
l_res, dl_res = np.loadtxt(f"mcms/res_bbl_{spec}_{camphuis_conv[spec_name]}.txt").T
154+
ax[1].plot(l_res, dl_res, label="Res Bbls", color='red')
155+
except:
156+
log.info(f"Couldn't plot {spec_to_plot} Bbl res")
157+
158+
ax[1].errorbar(l_spt, l_spt*0)
159+
ax[1].errorbar(l_spt, Db-Db_redo_bias_tf_corr, sigmab, lw=0.5, marker="o", ms=3, elinewidth=1, label=f"SPT - SPT redo, bias tf corrected {spec_name}")
160+
ax[1].legend()
161+
ax[1].set_xlabel(r"$\ell$", fontsize=14)
162+
ax[1].set_ylabel(r"$D_\ell - D^{\rm redo}_\ell$", fontsize=14)
163+
164+
ax[2].errorbar(l_spt, Db/Db_redo_bias_tf_corr, lw=0.5, marker="o", ms=3, elinewidth=1, label=f"SPT/ SPT redo {spec_name}")
165+
ax[2].set_xlabel(r"$\ell$", fontsize=14)
166+
ax[2].set_ylabel(r"$D_\ell / D^{\rm redo}_\ell$", fontsize=14)
167+
168+
ax[2].set_ylim(0.95, 1.05)
170169
plt.savefig(f"{plot_dir}/{spec_name}_{spec}.png", bbox_inches="tight")
171170
plt.clf()
172171
plt.close()

0 commit comments

Comments
 (0)