Skip to content

Commit f5abd15

Browse files
committed
Merged harmonic and configuration space covariance to fix bug in inference run (Cosmosis). Added a prior file for harmonic space mocks.
1 parent f18d6dd commit f5abd15

File tree

1 file changed

+71
-45
lines changed

1 file changed

+71
-45
lines changed

cosmo_inference/scripts/cosmosis_fitting.py

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -167,33 +167,6 @@ def cl_to_fits(ell, cl_ee, cl_bb):
167167
return cl_ee_hdu, cl_bb_hdu
168168

169169

170-
def cov_cl_to_fits(cov_file, cov_hdu="COVAR_FULL"):
171-
"""Convert pseudo-C_ell covariance to a CosmoSIS ImageHDU."""
172-
if cov_file.endswith(".fits"):
173-
with fits.open(cov_file) as hdul:
174-
cov_data = np.asarray(hdul[cov_hdu].data, dtype=np.float64)
175-
elif cov_file.endswith(".npy"):
176-
cov_data = np.load(cov_file)
177-
else:
178-
raise NotImplementedError(f"Unsupported pseudo-Cl covariance format: {cov_file}")
179-
180-
if cov_data.shape[0] != cov_data.shape[1]:
181-
raise ValueError("Pseudo-Cl covariance matrix must be square")
182-
183-
cov_hdu = fits.ImageHDU(cov_data, name="COVMAT_CELL")
184-
cov_dict = {
185-
"COVDATA": "True",
186-
"EXTNAME": "COVMAT_CELL",
187-
"NAME_0": "CELL_EE",
188-
"STRT_0": 0,
189-
}
190-
191-
for key, value in cov_dict.items():
192-
cov_hdu.header[key] = value
193-
194-
return cov_hdu
195-
196-
197170
def tau_to_fits(filename, theta=None):
198171
"""
199172
Convert tau statistics to FITS format.
@@ -238,12 +211,16 @@ def rho_to_fits(filename, theta=None):
238211
return rho_stat_hdu
239212

240213

241-
def covdat_to_fits(filename_cov_xi, filename_cov_tau=None):
214+
def covdat_to_fits(filename_cov_xi, filename_cov_tau=None, filename_cov_cl=None, cov_cl_hdu=None):
242215
"""
243216
Convert CosmoCov covariance matrix to FITS format.
244217
245218
If tau covariance provided, block with xi covariance.
246219
"""
220+
if filename_cov_cl is not None:
221+
assert cov_cl_hdu is not None, "Covariance HDU must be provided with cov_cl file."
222+
223+
# Load xi covariance
247224
covmat_xi = np.loadtxt(filename_cov_xi)
248225

249226
if filename_cov_tau is not None:
@@ -259,6 +236,28 @@ def covdat_to_fits(filename_cov_xi, filename_cov_tau=None):
259236
else:
260237
covmat = covmat_xi
261238

239+
covmat_real_space_dim = covmat.shape[0]
240+
# Update the covariance with the pseudo-Cl part if provided
241+
if filename_cov_cl is not None:
242+
if filename_cov_cl.endswith(".fits"):
243+
with fits.open(filename_cov_cl) as hdul:
244+
covmat_cl = np.asarray(hdul[cov_cl_hdu].data, dtype=np.float64)
245+
elif filename_cov_cl.endswith(".npy"):
246+
covmat_cl = np.load(filename_cov_cl)
247+
else:
248+
raise NotImplementedError(f"Unsupported pseudo-Cl covariance format: {filename_cov_cl}")
249+
250+
if covmat_cl.shape[0] != covmat_cl.shape[1]:
251+
raise ValueError("Pseudo-Cl covariance matrix must be square")
252+
253+
covmat = np.block(
254+
[
255+
[covmat, np.zeros((len(covmat), len(covmat_cl)))],
256+
[np.zeros((len(covmat_cl), len(covmat))), covmat_cl],
257+
]
258+
)
259+
260+
262261
if len(covmat) != len(covmat[0]):
263262
raise RuntimeError("Covariance matrix is not square")
264263

@@ -279,6 +278,17 @@ def covdat_to_fits(filename_cov_xi, filename_cov_tau=None):
279278
"NAME_3": "TAU_2_PLUS",
280279
"STRT_3": len(covmat_xi) + int(len(covmat_tau) / 2),
281280
})
281+
282+
if filename_cov_tau is None:
283+
filename_cov_cl and cov_dict.update({
284+
"NAME_2": "CELL_EE",
285+
"STRT_2": covmat_real_space_dim,
286+
})
287+
else:
288+
filename_cov_cl and cov_dict.update({
289+
"NAME_4": "CELL_EE",
290+
"STRT_4": covmat_real_space_dim,
291+
})
282292

283293
for key, value in cov_dict.items():
284294
cov_hdu.header[key] = value
@@ -379,7 +389,7 @@ def _generate_ini_file(
379389
"\ndata_sets=CELL_EE"
380390
)
381391

382-
covmat_line = "covmat_name=COVMAT_CELL" if is_harmonic else "covmat_name=COVMAT"
392+
covmat_line = "covmat_name=COVMAT"
383393

384394
modifications.append((r"^\[2pt_like\]", like_section))
385395
modifications.append((r"^covmat_name=.*", covmat_line))
@@ -436,11 +446,17 @@ def generate_cosmosis_config(args):
436446

437447
if args.cl_file:
438448
template_base_harmonic = "cosmosis_pipeline_A_ia_cell.ini"
449+
450+
values_file_cell = "values_ia.ini"
451+
if args.mock:
452+
priors_file_cell = "priors_mock_cell.ini"
453+
else:
454+
priors_file_cell = "priors.ini"
439455
_generate_ini_file(
440456
args,
441457
template_base_harmonic,
442-
priors_file,
443-
values_file,
458+
priors_file_cell,
459+
values_file_cell,
444460
suffix="_cell",
445461
is_harmonic=True,
446462
)
@@ -643,17 +659,6 @@ def parse_args():
643659
nz_hdu = nz_to_fits(args.nz_file)
644660
print("Loaded n(z)")
645661

646-
cl_ee_hdu = None
647-
cl_bb_hdu = None
648-
cov_cl_hdu = None
649-
if args.cl_file:
650-
print("Loading Cl data...")
651-
ell, cl_ee, cl_bb = load_pseudo_cl(args.cl_file)
652-
print(f"Loaded Cl: {len(ell)} multipoles")
653-
cl_ee_hdu, cl_bb_hdu = cl_to_fits(ell, cl_ee, cl_bb)
654-
cov_cl_hdu = cov_cl_to_fits(args.cov_cl, cov_hdu="COVAR_FULL")
655-
print("Loaded pseudo-Cl covariance")
656-
657662
rho_hdu = None
658663
tau_0_p_hdu = None
659664
tau_2_p_hdu = None
@@ -673,17 +678,38 @@ def parse_args():
673678

674679
cov_hdu = covdat_to_fits(args.cov_xi, filename_cov_tau=args.cov_tau)
675680

681+
cl_ee_hdu = None
682+
cl_bb_hdu = None
683+
if args.cl_file:
684+
print("Loading Cl data...")
685+
ell, cl_ee, cl_bb = load_pseudo_cl(args.cl_file)
686+
print(f"Loaded Cl: {len(ell)} multipoles")
687+
cl_ee_hdu, cl_bb_hdu = cl_to_fits(ell, cl_ee, cl_bb)
688+
if args.use_rho_tau:
689+
cov_hdu = covdat_to_fits(
690+
args.cov_xi,
691+
filename_cov_tau=args.cov_tau,
692+
filename_cov_cl=args.cov_cl,
693+
cov_cl_hdu="COVAR_FULL"
694+
)
695+
else:
696+
cov_hdu = covdat_to_fits(
697+
args.cov_xi,
698+
filename_cov_tau=None,
699+
filename_cov_cl=args.cov_cl,
700+
cov_cl_hdu="COVAR_FULL"
701+
)
702+
print("Loaded pseudo-Cl covariance")
703+
676704
pri_hdr = fits.Header()
677705
pri_hdu = fits.PrimaryHDU(header=pri_hdr)
678706

679707
print("Assembling FITS file...")
680708
hdu_list = [pri_hdu, cov_hdu]
681-
if cov_cl_hdu is not None:
682-
hdu_list.append(cov_cl_hdu)
683709
hdu_list.extend([nz_hdu, xip_hdu, xim_hdu])
684710

685711
if args.cl_file:
686-
hdu_list.extend([cl_ee_hdu, cl_bb_hdu])
712+
hdu_list.extend([cl_ee_hdu])
687713

688714
if args.use_rho_tau:
689715
hdu_list.extend([tau_0_p_hdu, tau_2_p_hdu, rho_hdu])

0 commit comments

Comments
 (0)