Skip to content

Commit e74d3cf

Browse files
committed
cosmosis_fitting fixes
1 parent f9a8179 commit e74d3cf

File tree

1 file changed

+26
-44
lines changed

1 file changed

+26
-44
lines changed

cosmo_inference/scripts/cosmosis_fitting.py

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -99,32 +99,28 @@ def parse_combined_xi_fits(filepath):
9999
return xip_hdu, xim_hdu
100100

101101

102-
def load_glass_cl(cl_file):
103-
"""
104-
Load GLASS mock C_ell from .npy file.
105-
Expected shape: (5, 32) where row 0=ell, row 1=EE, row 4=BB
106-
"""
107-
cl_block = np.load(cl_file)
108-
if cl_block.shape[0] < 5:
109-
raise ValueError(f"Unexpected C_ell array shape {cl_block.shape} for {cl_file}")
110-
111-
ell = np.asarray(cl_block[0], dtype=np.float64)
112-
cl_ee = np.asarray(cl_block[1], dtype=np.float64)
113-
cl_bb = np.asarray(cl_block[4], dtype=np.float64)
114-
115-
return ell, cl_ee, cl_bb
102+
def load_pseudo_cl(cl_file):
103+
"""Load pseudo-C_ell spectra from .npy or FITS formats."""
104+
if cl_file.endswith(".npy"):
105+
cl_block = np.load(cl_file)
106+
if cl_block.shape[0] < 5:
107+
raise ValueError(
108+
f"Unexpected C_ell array shape {cl_block.shape} for {cl_file}"
109+
)
110+
ell = np.asarray(cl_block[0], dtype=np.float64)
111+
cl_ee = np.asarray(cl_block[1], dtype=np.float64)
112+
cl_bb = np.asarray(cl_block[4], dtype=np.float64)
113+
return ell, cl_ee, cl_bb
116114

115+
if cl_file.endswith(".fits"):
116+
with fits.open(cl_file) as hdul:
117+
data = hdul[1].data
118+
ell = np.asarray(data["ELL"], dtype=np.float64)
119+
cl_ee = np.asarray(data["EE"], dtype=np.float64)
120+
cl_bb = np.asarray(data["BB"], dtype=np.float64)
121+
return ell, cl_ee, cl_bb
117122

118-
def load_pseudo_cl_fits(cl_file):
119-
"""
120-
Load pseudo-C_ell data stored in FITS format with columns (ELL, EE, EB, BB).
121-
"""
122-
with fits.open(cl_file) as hdul:
123-
data = hdul[1].data
124-
ell = np.asarray(data["ELL"], dtype=np.float64)
125-
cl_ee = np.asarray(data["EE"], dtype=np.float64)
126-
cl_bb = np.asarray(data["BB"], dtype=np.float64)
127-
return ell, cl_ee, cl_bb
123+
raise NotImplementedError(f"Cl format not supported: {cl_file}")
128124

129125

130126
def cl_to_fits(ell, cl_ee, cl_bb):
@@ -170,25 +166,18 @@ def cl_to_fits(ell, cl_ee, cl_bb):
170166
return cl_ee_hdu, cl_bb_hdu
171167

172168

173-
def cov_cl_to_fits(cov_file, nbins):
169+
def cov_cl_to_fits(cov_file, cov_hdu="COVAR_FULL"):
174170
"""Convert pseudo-C_ell covariance to a CosmoSIS ImageHDU."""
175171
if cov_file.endswith(".fits"):
176172
with fits.open(cov_file) as hdul:
177-
if "COVAR_EE_EE" in hdul:
178-
cov_data = np.asarray(hdul["COVAR_EE_EE"].data, dtype=np.float64)
179-
else:
180-
cov_data = np.asarray(hdul[0].data, dtype=np.float64)
173+
cov_data = np.asarray(hdul[cov_hdu].data, dtype=np.float64)
181174
elif cov_file.endswith(".npy"):
182175
cov_data = np.load(cov_file)
183176
else:
184177
raise NotImplementedError(f"Unsupported pseudo-Cl covariance format: {cov_file}")
185178

186179
if cov_data.shape[0] != cov_data.shape[1]:
187180
raise ValueError("Pseudo-Cl covariance matrix must be square")
188-
if cov_data.shape[0] != nbins:
189-
raise ValueError(
190-
"Pseudo-Cl covariance dimension does not match C_ell data length"
191-
)
192181

193182
cov_hdu = fits.ImageHDU(cov_data, name="COVMAT_CELL")
194183
cov_dict = {
@@ -608,17 +597,10 @@ def parse_args():
608597
cov_cl_hdu = None
609598
if args.cl_file:
610599
print("Loading Cl data...")
611-
if args.cl_file.endswith(".npy"):
612-
ell, cl_ee, cl_bb = load_glass_cl(args.cl_file)
613-
cl_ee_hdu, cl_bb_hdu = cl_to_fits(ell, cl_ee, cl_bb)
614-
print(f"Loaded Cl: {len(ell)} multipoles")
615-
elif args.cl_file.endswith(".fits"):
616-
ell, cl_ee, cl_bb = load_pseudo_cl_fits(args.cl_file)
617-
cl_ee_hdu, cl_bb_hdu = cl_to_fits(ell, cl_ee, cl_bb)
618-
print(f"Loaded Cl: {len(ell)} multipoles (FITS pseudo-Cl)")
619-
else:
620-
raise NotImplementedError(f"Cl format not supported: {args.cl_file}")
621-
cov_cl_hdu = cov_cl_to_fits(args.cov_cl, len(ell))
600+
ell, cl_ee, cl_bb = load_pseudo_cl(args.cl_file)
601+
print(f"Loaded Cl: {len(ell)} multipoles")
602+
cl_ee_hdu, cl_bb_hdu = cl_to_fits(ell, cl_ee, cl_bb)
603+
cov_cl_hdu = cov_cl_to_fits(args.cov_cl, cov_hdu="COVAR_FULL")
622604
print("Loaded pseudo-Cl covariance")
623605

624606
rho_hdu = None

0 commit comments

Comments
 (0)