Add SP(k) baryonic suppression model#1291
Conversation
- Introduced `BaryonsSPK` class for SP(k) baryonic suppression using `pyspk>=2.0.0`. - Updated environment.yml to include `pyspk` dependency. - Enhanced installation and API documentation for the new model. - Added unit tests for `BaryonsSPK` functionality and validation.
- Lower default k_min_hmpc from 0.1 to 0.005 h/Mpc so the internal SP(k) grid covers the full physically relevant low-k range and flat left- extrapolation stays near unity (where baryonic effects vanish). - Rewrite _include_baryonic_effects to apply SP(k) only within the calibrated k range [k_min_hmpc, k_max_hmpc] and calibrated redshift range z <= CALIBRATED_Z_MAX (queried from pyspk.constants). CCL's internal Pk2D spline grid extends to k ~ 1500 h/Mpc and a ~ 0.01 (z ~ 99), both far outside pyspk calibration; those cells are left at unity instead of triggering the OOB error policy or returning garbage values. - Relax test_spk_matches_pyspk tolerance from atol=1e-8 to atol=1e-3. CCL evaluates SP(k) on a fixed 128-point internal grid then interpolates, while the test evaluates pyspk directly on the target k array; the resulting interpolation error is O(1e-4), which is well within the ~1% accuracy of the model. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Refactored warning handling in BaryonsSPK to use a dedicated function. - Improved error messages for missing required parameters in relation normalization. - Added support for out-of-bounds policy handling in the include_baryonic_effects method. - Updated tests to cover new out-of-bounds behavior and ensure consistency with reference data. - Introduced a new benchmark data file for SP(k) power law reference boost factors.
…d relation modes for BaryonsSPK
- Introduced a context manager to catch warnings when building the model evaluator. - Ensured that warnings are recorded and processed after the evaluator is created. - This change improves the robustness of the BaryonsSPK class by providing better feedback on potential issues during model evaluation.
…ounds handling - Introduced caching for the SP(k) evaluator and its corresponding k-grid to optimize performance. - Simplified the evaluator building process by removing unnecessary conditional checks. - Enhanced out-of-bounds policy handling by removing warnings for internal spline grid usage. - Updated the `boost_factor` and `include_baryonic_effects` methods to utilize the new caching mechanism. - Modified tests to reflect changes in out-of-bounds policy behavior and ensure consistency in baryonic effects calculations.
…and add relation matrix validation
- Updated the calculation of k_min and k_max to be in h-Mpc instead of Mpc. - Adjusted the generation of the k_grid_hmpc to use the new h-Mpc bounds. - Added a check to return a copy of the suppression grid if the mapped k values match the cached k grid.
- Removed k_min_mpc, k_max_mpc, n_k, and out_of_bounds_policy parameters from BaryonsSPK constructor. - Introduced max_evaluator_cache_size to manage evaluator caching. - Simplified evaluator caching logic using OrderedDict. - Updated boost_factor and include_baryonic_effects methods to handle high-k requests more gracefully. - Adjusted tests to reflect changes in out-of-bounds policy handling and removed unnecessary parameters. - Enhanced documentation and comments for clarity.
damonge
left a comment
There was a problem hiding this comment.
Thanks a lot Jaime! Looks great, just a few minor comments/questions.
jemme07
left a comment
There was a problem hiding this comment.
Thanks for the review @damonge! I addressed all of your comments.
The main change was refactoring the out-of-range handling: instead of the old internal behavior, it now uses explicit user-facing options for both k and z out-of-range cases. I think this makes the behavior clearer and more consistent for users.
Hope this is useful. I’d be happy to make any follow-up tweaks you’d like.
|
Thanks! I think the linter is failing. Maybe check |
Coverage Report for CI Build 25822586014Warning Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes. Coverage decreased (-0.3%) to 97.205%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
|
Also, a bunch of tests seem to be failing. |
|
oh, the failed tests are just to do with coveralls for linux. I can fix those on a different PR, so you can ignore those. Maybe just fix the linting. |
Thanks! I fixed all lint issues. They were all line-length related. I usually run Ruff locally because it is faster, but Flake8 in this repo uses a different line-length setting, so I aligned everything to that. I also improved some documentation.
For the Coveralls/Linux failures, I think they are related to parallel coverage reporting in CI. I made a quick fix, so let’s see if that resolves it. |
|
Hey David @damonge, a unit test check failed on macOS (Python 3.11) but it's unrelated to the SP(k) implementation. The failing test ( I think we can fix this if we check the cache hit counter directly instead of relying on timing. Something like: hits_before = func.cache_info.hits
timeit_(sigma8=s8_arr[2])
assert func.cache_info.hits == hits_before + 1Happy to include it here or leave it for a separate PR... whatever you prefer. |
Overview
This PR adds the SP(k) baryon-suppression model (Salcido et al. 2023) to pyccl as a new optional baryonic wrapper:
pyccl.BaryonsSPKModel references:
The wrapper keeps CCL-facing wavenumbers in
Mpc^-1and performs the internal conversion toh/Mpcrequired by pyspk.What is implemented
1. SP(k) wrapper interface
pyccl/baryons/spk.pyimplements the SP(k) multiplicative correction:power_lawcosmo_power_lawdouble_power_lawbinned2. CCL integration behavior
pyccl/baryons/__init__.pyand documented underpyccl.baryonsAPI pages.boost_factor(cosmo, k, a)._include_baryonic_effects(cosmo, pk).boost_factor(...): raises an error if any requestedkis above the pyspk calibrated rangeinclude_baryonic_effects(...): for out-of-rangek, uses suppression= 1(no baryonic correction) to keepPk2Dstable3. Performance behavior for iterative workflows
pyspk.build_sup_model_evaluator(...)objects for repeated calls on the same effectivek/hgrid.threading.Lock+ LRU-style eviction).4. Documentation and packaging updates
readthedocs/api/pyccl.baryons.spk.rstreadthedocs/source/installation.rstCHANGELOG.md5. Validation tests and benchmarks
pyccl/tests/test_baryons_spk.pycover:Mpc^-1toh/Mpc)kpolicy checksbenchmarks/test_spk_baryons.pycover:power_lawbenchmarks/data/spk_power_law_fk.txtValidation run
Focused commands: