Skip to content

Commit

Permalink
catch clik errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud De-Mattia committed Feb 3, 2023
1 parent 7eec3fb commit 453a853
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 28 deletions.
54 changes: 31 additions & 23 deletions desilike/likelihoods/cmb/planck2018_clik.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ class BasePlanck2018ClikLikelihood(BaseLikelihood):
theory : ClTheory, default=None
Theory calculator for CMB :math:`C_{\ell}^{xy}`.
If ``None``, instantiated using ``cosmo``.
cosmo : BasePrimordialCosmology, default=None
Optionally, cosmology calculator. Defaults to ``Cosmoprimo()``.
data_dir : str, Path, default=None
Data directory. Defaults to path saved in desilike's configuration,
as provided by :class:`Installer` if likelihood has been installed.
"""
config_fn = 'planck2018_clik.yaml'
installer_section = 'Planck2018ClikLikelihood'
catch_clik_errors = True

def initialize(self, theory=None, cosmo=None, data_dir=None):
super(BasePlanck2018ClikLikelihood, self).initialize()
Expand Down Expand Up @@ -96,7 +97,14 @@ def calculate(self, **params):

# Fill with likelihood parameters
self.vector[self.cumsizes[-1]:] = [params[p] for p in self.nuisance_params]
self.loglikelihood = self.clik(self.vector)[0]
import clik
try:
self.loglikelihood = self.clik(self.vector)[0]
except clik.lkl.CError as exc:
if self.catch_clik_errors:
self.loglikelihood = -np.inf
else:
raise ValueError('clik failed with input vector {}'.format(self.vector)) from exc
# "zero" of clik, and sometimes nan's returned
if np.allclose(self.loglikelihood, -1e30) or np.isnan(self.loglikelihood):
self.loglikelihood = -np.inf
Expand Down Expand Up @@ -192,10 +200,10 @@ class TTHighlPlanck2018PlikLikelihood(BasePlanck2018ClikLikelihood):
theory : ClTheory, default=None
Theory calculator for CMB :math:`C_{\ell}^{xy}`.
If ``None``, instantiated using ``cosmo``.
cosmo : BasePrimordialCosmology, default=None
Optionally, cosmology calculator. Defaults to ``Cosmoprimo()``.
data_dir : str, Path, default=None
Data directory. Defaults to path saved in desilike's configuration,
as provided by :class:`Installer` if likelihood has been installed.
Expand All @@ -207,7 +215,7 @@ class TTHighlPlanck2018PlikLikelihood(BasePlanck2018ClikLikelihood):
class TTHighlPlanck2018PlikLiteLikelihood(BasePlanck2018ClikLikelihood):
r"""
High-:math:`\ell` temperature-only plik likelihood of Planck's 2018 data release, marginalized over the foreground model.
Reference
---------
https://arxiv.org/abs/1807.06209
Expand All @@ -219,10 +227,10 @@ class TTHighlPlanck2018PlikLiteLikelihood(BasePlanck2018ClikLikelihood):
theory : ClTheory, default=None
Theory calculator for CMB :math:`C_{\ell}^{xy}`.
If ``None``, instantiated using ``cosmo``.
cosmo : BasePrimordialCosmology, default=None
Optionally, cosmology calculator. Defaults to ``Cosmoprimo()``.
data_dir : str, Path, default=None
Data directory. Defaults to path saved in desilike's configuration,
as provided by :class:`Installer` if likelihood has been installed.
Expand All @@ -234,7 +242,7 @@ class TTHighlPlanck2018PlikLiteLikelihood(BasePlanck2018ClikLikelihood):
class TTHighlPlanck2018PlikUnbinnedLikelihood(BasePlanck2018ClikLikelihood):
r"""
High-:math:`\ell` temperature-only plik likelihood of Planck's 2018 data release.
Reference
---------
https://arxiv.org/abs/1807.06209
Expand All @@ -246,10 +254,10 @@ class TTHighlPlanck2018PlikUnbinnedLikelihood(BasePlanck2018ClikLikelihood):
theory : ClTheory, default=None
Theory calculator for CMB :math:`C_{\ell}^{xy}`.
If ``None``, instantiated using ``cosmo``.
cosmo : BasePrimordialCosmology, default=None
Optionally, cosmology calculator. Defaults to ``Cosmoprimo()``.
data_dir : str, Path, default=None
Data directory. Defaults to path saved in desilike's configuration,
as provided by :class:`Installer` if likelihood has been installed.
Expand All @@ -273,10 +281,10 @@ class TTTEEEHighlPlanck2018PlikLikelihood(BasePlanck2018ClikLikelihood):
theory : ClTheory, default=None
Theory calculator for CMB :math:`C_{\ell}^{xy}`.
If ``None``, instantiated using ``cosmo``.
cosmo : BasePrimordialCosmology, default=None
Optionally, cosmology calculator. Defaults to ``Cosmoprimo()``.
data_dir : str, Path, default=None
Data directory. Defaults to path saved in desilike's configuration,
as provided by :class:`Installer` if likelihood has been installed.
Expand All @@ -300,10 +308,10 @@ class TTTEEEHighlPlanck2018PlikLiteLikelihood(BasePlanck2018ClikLikelihood):
theory : ClTheory, default=None
Theory calculator for CMB :math:`C_{\ell}^{xy}`.
If ``None``, instantiated using ``cosmo``.
cosmo : BasePrimordialCosmology, default=None
Optionally, cosmology calculator. Defaults to ``Cosmoprimo()``.
data_dir : str, Path, default=None
Data directory. Defaults to path saved in desilike's configuration,
as provided by :class:`Installer` if likelihood has been installed.
Expand All @@ -327,10 +335,10 @@ class TTTEEEHighlPlanck2018PlikUnbinnedLikelihood(BasePlanck2018ClikLikelihood):
theory : ClTheory, default=None
Theory calculator for CMB :math:`C_{\ell}^{xy}`.
If ``None``, instantiated using ``cosmo``.
cosmo : BasePrimordialCosmology, default=None
Optionally, cosmology calculator. Defaults to ``Cosmoprimo()``.
data_dir : str, Path, default=None
Data directory. Defaults to path saved in desilike's configuration,
as provided by :class:`Installer` if likelihood has been installed.
Expand All @@ -354,10 +362,10 @@ class LensingPlanck2018ClikLikelihood(BasePlanck2018ClikLikelihood):
theory : ClTheory, default=None
Theory calculator for CMB :math:`C_{\ell}^{xy}`.
If ``None``, instantiated using ``cosmo``.
cosmo : BasePrimordialCosmology, default=None
Optionally, cosmology calculator. Defaults to ``Cosmoprimo()``.
data_dir : str, Path, default=None
Data directory. Defaults to path saved in desilike's configuration,
as provided by :class:`Installer` if likelihood has been installed.
Expand All @@ -381,10 +389,10 @@ class TTLowlPlanck2018ClikLikelihood(BasePlanck2018ClikLikelihood):
theory : ClTheory, default=None
Theory calculator for CMB :math:`C_{\ell}^{xy}`.
If ``None``, instantiated using ``cosmo``.
cosmo : BasePrimordialCosmology, default=None
Optionally, cosmology calculator. Defaults to ``Cosmoprimo()``.
data_dir : str, Path, default=None
Data directory. Defaults to path saved in desilike's configuration,
as provided by :class:`Installer` if likelihood has been installed.
Expand All @@ -408,10 +416,10 @@ class EELowlPlanck2018ClikLikelihood(BasePlanck2018ClikLikelihood):
theory : ClTheory, default=None
Theory calculator for CMB :math:`C_{\ell}^{xy}`.
If ``None``, instantiated using ``cosmo``.
cosmo : BasePrimordialCosmology, default=None
Optionally, cosmology calculator. Defaults to ``Cosmoprimo()``.
data_dir : str, Path, default=None
Data directory. Defaults to path saved in desilike's configuration,
as provided by :class:`Installer` if likelihood has been installed.
Expand Down
18 changes: 17 additions & 1 deletion desilike/likelihoods/tests/test_cmb.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,29 @@ def test_copy():
likelihood_clik()


def test_error():
from desilike import setup_logging
from desilike.likelihoods.cmb import TTTEEEHighlPlanck2018PlikLiteLikelihood, TTLowlPlanck2018ClikLikelihood, EELowlPlanck2018ClikLikelihood
from desilike.likelihoods import SumLikelihood
from desilike.theories.primordial_cosmology import Cosmoprimo

setup_logging()
cosmo = Cosmoprimo(engine='camb')
likelihoods = [Likelihood(cosmo=cosmo) for Likelihood in [TTTEEEHighlPlanck2018PlikLiteLikelihood, TTLowlPlanck2018ClikLikelihood, EELowlPlanck2018ClikLikelihood]]
likelihood_clik = SumLikelihood(likelihoods=likelihoods)

assert not np.isfinite(likelihood_clik(h=0.71845617, omega_cdm=0.11316231, omega_b=0.02500884, logA=3.25690416, n_s=0.97226037, tau_reio=0.17722994, A_planck=0.9907607))
assert likelihood_clik(**{param.name: param.value for param in likelihood_clik.varied_params}) > -np.inf


if __name__ == '__main__':

setup_logging()
#test_install()
#test_clik()
#test_sum()
#test_gaussian_likelihood()
test_params()
#test_params()
#test_help()
#test_copy()
test_error()
6 changes: 4 additions & 2 deletions desilike/samplers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ def __init__(self, likelihood, rng=None, seed=None, max_tries=1000, chains=None,
Path to or chains to resume from.
ref_scale : float, default=1.
Rescale parameters' :attr:`Parameter.ref` reference distribution by this factor
If no chains to resume from are provided, initial points are sampled from parameters' :attr:`Parameter.ref` reference distributions.
Rescale parameters' :attr:`Parameter.ref` reference distribution by this factor.
save_fn : str, Path, default=None
If not ``None``, save samples to this location.
Expand Down Expand Up @@ -148,7 +149,8 @@ def loglikelihood(self, values):
mask = np.isnan(toret)
toret[mask] = -np.inf
if mask.any() and self.mpicomm.rank == 0:
self.log_warning('loglikelihood is NaN for {}'.format({k: v[mask] for k, v in points.items()}))
import warnings
warnings.warn('loglikelihood is NaN for {}'.format({k: v[mask] for k, v in points.items()}))
return toret

@bcast_values
Expand Down
4 changes: 2 additions & 2 deletions desilike/samplers/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_ensemble():
#theory = LPTVelocileptorsTracerPowerSpectrumMultipoles(template=template)
for param in theory.params.select(basename=['alpha*', 'sn*']): param.update(derived='.marg')
observable = TracerPowerSpectrumMultipolesObservable(klim={0: [0.05, 0.2, 0.01], 2: [0.05, 0.2, 0.01]},
data='../../tests/_pk/data.npy', mocks='../../tests/_pk/mock_*.npy',
data='../../tests/_pk/data.npy', covariance='../../tests/_pk/mock_*.npy',
wmatrix='../../tests/_pk/window.npy',
theory=theory)
likelihood = ObservablesGaussianLikelihood(observables=[observable])
Expand All @@ -36,5 +36,5 @@ def test_fixed():
if __name__ == '__main__':

setup_logging()
#test_ensemble()
test_ensemble()
test_fixed()

0 comments on commit 453a853

Please sign in to comment.