Add and test handling of CCL integration errors#535
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a new compute_loglike_for_sampling method to handle CCL integration errors by returning -np.inf instead of propagating exceptions, updates all connectors to use this new method, and adds tests to verify the error-handling behavior.
- Added
compute_loglike_for_samplingtoLikelihoodto catch CCL integration errors. - Updated NumCosmo, CosmoSIS, and Cobaya connectors to call the new sampling-safe method.
- Added tests in
test_likelihood.pyfor integration-error and non-integration-error handling.
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| firecrown/likelihood/likelihood.py | Added compute_loglike_for_sampling to swallow integration errors. |
| firecrown/connector/numcosmo/numcosmo.py | Switched compute_loglike to compute_loglike_for_sampling in m2lnL. |
| firecrown/connector/cosmosis/likelihood.py | Switched compute_loglike to compute_loglike_for_sampling in execute. |
| firecrown/connector/cobaya/likelihood.py | Switched compute_loglike to compute_loglike_for_sampling in logp. |
| tests/likelihood/test_likelihood.py | Added tests for sampling-safe loglike and propagation of other errors. |
Comments suppressed due to low confidence (3)
firecrown/likelihood/likelihood.py:140
- The
warningsmodule is used here but not imported; please addimport warningsat the top of the file.
warnings.warn(f"CCL error:\n{e}\nin likelihood, returning -inf")
tests/likelihood/test_likelihood.py:9
- Docstring contains an extra quote before the closing triple quotes; please remove the stray '"'.
"""A likelihood that always throws a pyccl integration error exception"."""
tests/likelihood/test_likelihood.py:20
- Docstring contains an extra quote before the closing triple quotes; please remove the stray '"'.
"""A likelihood that always throws a pyccl integration error exception"."""
| ) | ||
| def test_other_error_propagates(): | ||
| likelihood = LikelihoodThatThrowsUnhandledError() | ||
| with pytest.raises(RuntimeError): |
There was a problem hiding this comment.
The test expects RuntimeError, but compute_loglike_for_sampling will rethrow the original pyccl.errors.CCLError. Update the test to catch the correct exception type.
| with pytest.raises(RuntimeError): | |
| with pytest.raises(pyccl.errors.CCLError): |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #535 +/- ##
=======================================
Coverage 100.0% 100.0%
=======================================
Files 49 49
Lines 4837 4846 +9
Branches 543 544 +1
=======================================
+ Hits 4837 4846 +9
🚀 New features to boost your workflow:
|
Co-authored-by: Copilot <[email protected]>
Description
CCL will sometimes throw exceptions indicating an error has occurred during integration. Currently this results in Firecrown's
likelihood.compute_loglikepropagating the exception. This is inconvenient when Firecrown is being used for sampling.This PR makes the connectors for Firecrown all capture any integration error raised by CCL. Rather than propagating the exception, a log-likelihood of
-np.infis returned.Type of change
Please delete the bullet items below that do not apply to this pull request.
Checklist:
The following checklist will make sure that you are following the code style and
guidelines of the project as described in the
contributing page.
bash pre-commit-checkand fixed any issues