Skip to content

Commit

Permalink
Dev check ell range mflike (#171)
Browse files Browse the repository at this point in the history
* change to lmax requirements

* removing comments in modified files

* solving style issues

* solving issues in test mflike

* solving codestyle issues

* switched from hard-coded to l_bpws-depndt multipole

* fixed codestyle

---------

Co-authored-by: Martina Gerbino <bradamante@martina.local>
Co-authored-by: Ian Harrison <itrharrison@gmail.com>
  • Loading branch information
3 people authored Apr 26, 2024
1 parent 3128eb7 commit efa801e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
5 changes: 3 additions & 2 deletions soliket/foreground.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def initialize(self):
self.requested_cls = self.spectra["polarizations"]
self.lmin = self.spectra["lmin"]
self.lmax = self.spectra["lmax"]
self.ell = np.arange(self.lmin, self.lmax + 1)
self.exp_ch = self.spectra["exp_ch"]
self.eff_freqs = self.spectra["eff_freqs"]

Expand Down Expand Up @@ -169,6 +168,7 @@ def _get_foreground_model(self,
# useful to make tests at different l_max than the data
if not hasattr(ell, '__len__'):
ell = self.ell

ell_0 = self.fg_ell_0
nu_0 = self.fg_nu_0

Expand Down Expand Up @@ -307,7 +307,7 @@ def must_provide(self, **requirements):
if "fg_dict" in requirements:
req = requirements["fg_dict"]
self.requested_cls = req.get("requested_cls", self.requested_cls)
self.ell = req.get("ell", self.ell)
self.ell = req.get("ell", None)
self.bands = req.get("bands", self.bands)
self.exp_ch = req.get("exp_ch", self.exp_ch)
return {"bandint_freqs": {"bands": self.bands}}
Expand Down Expand Up @@ -344,6 +344,7 @@ def calculate(self, state, want_derived=False, **params_values_dict):

fg_params = {k: params_values_dict[k] for k in self.expected_params_fg}
state["fg_dict"] = self._get_foreground_model(requested_cls=self.requested_cls,
ell=self.ell,
exp_ch=self.exp_ch,
bandint_freqs=self.bandint_freqs,
**fg_params)
Expand Down
4 changes: 2 additions & 2 deletions soliket/mflike/mflike.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ def _get_power_spectra(self, cmbfg):
"""
ps_vec = np.zeros_like(self.data_vec)
DlsObs = dict()
# Note we rescale l_bpws because cmbfg spectra start from l=2
ell = self.l_bpws - 2
# Rescale l_bpws because cmbfg spectra start from first element of l_bpws (l=2)
ell = self.l_bpws - self.l_bpws[0]

for m in self.spec_meta:
p = m["pol"]
Expand Down
13 changes: 8 additions & 5 deletions soliket/mflike/theoryforge_MFLike.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ def must_provide(self, **requirements):
if "cmbfg_dict" in requirements:
req = requirements["cmbfg_dict"]
self.ell = req.get("ell", self.ell)
#self.log.info('%d', self.ell[0])
#self.log.info('%d', self.ell[-1])
self.requested_cls = req.get("requested_cls", self.requested_cls)
self.lcuts = req.get("lcuts", self.lcuts)
self.exp_ch = req.get("exp_ch", self.exp_ch)
Expand All @@ -149,7 +151,7 @@ def must_provide(self, **requirements):
# Be sure that CMB is computed at lmax > lmax_data (lcuts from mflike here)
reqs["Cl"] = {k: max(c, self.lmax_boltzmann + 1) for k, c in self.lcuts.items()}
reqs["fg_dict"] = {"requested_cls": self.requested_cls,
"ell": np.arange(max(self.ell[-1], self.lmax_fg + 1)),
"ell": self.ell,
"exp_ch": self.exp_ch, "bands": self.bands}
return reqs

Expand All @@ -161,10 +163,12 @@ def get_foreground_theory(self, **params):

def calculate(self, state, want_derived=False, **params_values_dict):
Dls = self.get_cmb_theory(**params_values_dict)
#self.Dls = {s: Dls[s][self.ell] for s, _ in self.lcuts.items()}
Dls_cut = {s: Dls[s][self.ell] for s, _ in self.lcuts.items()}
params_values_nocosmo = {k: params_values_dict[k] for k in (
self.expected_params_nuis)}
fg_dict = self.get_foreground_theory(**params_values_nocosmo)
state["cmbfg_dict"] = self.get_modified_theory(Dls,
state["cmbfg_dict"] = self.get_modified_theory(Dls_cut,
fg_dict, **params_values_nocosmo)

def get_cmbfg_dict(self):
Expand All @@ -185,7 +189,6 @@ def get_modified_theory(self, Dls, fg_dict, **params):
:return: the CMB+foregrounds :math:`D_{\ell}` dictionary,
modulated by systematics
"""
self.Dls = Dls

nuis_params = {k: params[k] for k in self.expected_params_nuis}

Expand All @@ -194,8 +197,8 @@ def get_modified_theory(self, Dls, fg_dict, **params):
for f1 in self.exp_ch:
for f2 in self.exp_ch:
for s in self.requested_cls:
cmbfg_dict[s, f1, f2] = (self.Dls[s][self.ell] +
fg_dict[s, 'all', f1, f2][self.ell])
cmbfg_dict[s, f1, f2] = (Dls[s] +
fg_dict[s, 'all', f1, f2])

# Apply alm based calibration factors
cmbfg_dict = self._get_calibrated_spectra(cmbfg_dict, **nuis_params)
Expand Down
21 changes: 9 additions & 12 deletions soliket/tests/test_mflike.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import soliket # noqa
from soliket.mflike import TestMFLike

import numpy as np

packages_path = os.environ.get("COBAYA_PACKAGES_PATH") or os.path.join(
tempfile.gettempdir(), "LAT_packages"
)
Expand Down Expand Up @@ -103,10 +101,8 @@ def test_mflike(self):
FG = soliket.Foreground()
TF = soliket.TheoryForge_MFLike()

ell = np.arange(lmax + 1)
bands = TF.bands
exp_ch = TF.exp_ch
print(exp_ch, bands)

requested_cls = TF.requested_cls
BP.bands = bands
Expand All @@ -115,14 +111,6 @@ def test_mflike(self):

bandpass = BP._bandpass_construction(**nuisance_params)

fg_dict = FG._get_foreground_model(requested_cls=requested_cls,
ell=ell,
exp_ch=exp_ch,
bandint_freqs=bandpass,
**nuisance_params)

dlobs_dict = TF.get_modified_theory(cl_dict, fg_dict, **nuisance_params)

for select, chi2 in chi2s.items():

my_mflike = TestMFLike(
Expand All @@ -144,6 +132,15 @@ def test_mflike(self):
}
)

ell_cut = my_mflike.l_bpws
dls_cut = {s: cl_dict[s][ell_cut] for s, _ in my_mflike.lcuts.items()}
fg_dict = FG._get_foreground_model(requested_cls=requested_cls,
ell=ell_cut,
exp_ch=exp_ch,
bandint_freqs=bandpass,
**nuisance_params)
dlobs_dict = TF.get_modified_theory(dls_cut, fg_dict, **nuisance_params)

loglike = my_mflike.loglike(dlobs_dict)

self.assertAlmostEqual(-2 * (loglike - my_mflike.logp_const), chi2, 2)
Expand Down

0 comments on commit efa801e

Please sign in to comment.