Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to use syslibrary more like fgspectra #87

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions mflike/mflike.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from cobaya.conventions import data_path, packages_path_input
from cobaya.likelihoods.base_classes import InstallableLikelihood
from cobaya.log import LoggedError
from syslibrary import syslib_mflike, syslib


class _MFLike(InstallableLikelihood):
Expand All @@ -68,7 +69,7 @@ class _MFLike(InstallableLikelihood):

def initialize(self):
# Set default values to data member not initialized via yaml file
self.l_bpws = None
self.l_bpws: Optional[np.ndarray] = None
self.spec_meta = []

# Set path to data
Expand Down Expand Up @@ -101,6 +102,9 @@ def initialize(self):
# Initialize template for marginalization, if needed
self._init_template_from_file()

self.rotation = syslib_mflike.RotationAlm(ell=self.l_bpws, nu=self.experiments,
requested_cls=self.requested_cls)

self._constant_nuisance: Optional[dict] = None
self.log.info("Initialized!")

Expand Down Expand Up @@ -484,7 +488,7 @@ def get_modified_theory(self, Dls, fg_totals, **nuis_params):

# Introduce templates of systematics from file, if needed
if self.systematics_template:
cmbfg_dict = self._get_template_from_file(cmbfg_dict, **nuis_params)
self._add_template_from_file(cmbfg_dict, **nuis_params)

# Built theory
dls_dict = {}
Expand Down Expand Up @@ -616,11 +620,7 @@ def _get_rotated_spectra(self, dls_dict, **nuis_params):
if not any(rot_pars):
return dls_dict

from syslibrary import syslib_mflike as syl

rot = syl.Rotation_alm(ell=self.l_bpws, spectra=dls_dict)

return rot(rot_pars, nu=self.experiments, cls=self.requested_cls)
return self.rotation.get_rotated_cl(dls_dict, rot_pars)

###########################################################################
## This part deals with template marginalization
Expand All @@ -635,44 +635,40 @@ def _init_template_from_file(self):
the ``syslibrary.syslib.ReadTemplateFromFile``
function.
"""

if not (root := self.systematics_template.get("rootname")):
raise LoggedError(self.log, "Missing 'rootname' for systematics template!")

from syslibrary import syslib

# decide where to store systematics template.
# Currently stored inside syslibrary package
templ_from_file = syslib.ReadTemplateFromFile(rootname=root)
self.dltempl_from_file = templ_from_file(ell=self.l_bpws)
self.dltempl_from_file = syslib.TemplateResidual(ell=self.l_bpws, file_root_name=root)

def _get_template_from_file(self, dls_dict, **nuis_params):
def _add_template_from_file(self, dls_dict, **_nuis_params):
r"""
Adds the systematics template, modulated by ``nuis_params['templ_freq']``
parameters, to the :math:`D_{\ell}`.

:param dls_dict: the CMB+foregrounds :math:`D_{\ell}` dictionary
:param \**nuis_params: dictionary of nuisance parameters

:return: dictionary of CMB+foregrounds :math:`D_{\ell}`
with systematics templates
"""
# templ_pars=[nuis_params['templ_'+str(exp)] for exp in self.experiments]
# templ_pars currently hard-coded
# but ideally should be passed as input nuisance

# Note code currently does nothing
templ_pars = {
cls: np.zeros((len(self.experiments), len(self.experiments)))
for cls in self.requested_cls
}

dcl = self.dltempl_from_file.get_delta_cl()
for cls in self.requested_cls:
for i1, exp1 in enumerate(self.experiments):
for i2, exp2 in enumerate(self.experiments):
dls_dict[cls, exp1, exp2] += (
templ_pars[cls][i1][i2] * self.dltempl_from_file[cls, exp1, exp2]
templ_pars[cls][i1][i2] * dcl[cls, exp1, exp2]
)

return dls_dict


class TTTEEE(_MFLike):
...
Expand Down
Loading