cosmo: add CPJ neural network cache and refactor helper functions - #17
Open
MarcoOriani00 wants to merge 1 commit into
Open
cosmo: add CPJ neural network cache and refactor helper functions#17MarcoOriani00 wants to merge 1 commit into
MarcoOriani00 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors pybird/cosmo.py to (1) move the CPJ_custom helper functions (to_Mpc_per_h_jax, get_pk_lin_from_cpj_custom, get_growth) out of set_cosmo to module scope so they aren't re-defined on every call, and (2) cache CosmoPowerJAX model instances via functools.lru_cache so the four trained networks are loaded only once across MCMC iterations. It also adds a CPJ_TRAINED_MODELS_DIR env-var lookup and small helpers (clear_cpj_model_cache, cpj_cache_info) for cache management.
Changes:
- Add module-level
_get_cpj_model(lru_cache-backed) plus_get_pk_lin_cpj_custom/_get_growth_cpj_customhelpers and a models-directory resolver. - Hoist
to_Mpc_per_h_jaxout ofset_cosmo's CPJ/CPJ_custom branches; comment out the now-unusedget_smooth_wiggle_rescblock. - Add a
models_dir=Nonekwarg toset_cosmo(currently unused) and switch CPJ construction fromfilename=<relative>tofilepath=<absolute>.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+34
to
+35
| ilogpk_ = interp1d(log(_kk), log(_pk), fill_value='extrapolate') | ||
| return exp(ilogpk_(log(_kk*h))) * h**3 |
| return module.casefold() == module_name.casefold() | ||
|
|
||
| def set_cosmo(self, cosmo_dict, module='class', engine=None): | ||
| def set_cosmo(self, cosmo_dict, module='class', engine=None, models_dir=None): |
Comment on lines
+12
to
+15
| return CPJ(probe=probe, filepath=filepath) | ||
|
|
||
| def _cpj_path(models_dir, *parts): | ||
| return os.path.join(models_dir, *parts) |
| idx = abs(z_arr - z).argmin().astype(int) | ||
|
|
||
| M_H = _get_cpj_model('custom_log', _cpj_path(models_dir, 'mnu', 'growth-and-distances', 'HZ_mnu_v1.npz')) | ||
| M_DA = _get_cpj_model('custom', _cpj_path(models_dir, 'mnu', 'growth-and-distances', 'DAZ_mnu_v1.npz')) |
| # ──────────────────── cache modelli ──────────────────── | ||
| from functools import lru_cache | ||
|
|
||
| @lru_cache(maxsize=4) # number of CPJ model expected to be loaded, change if you modify cosmo.py to load more than 4 models |
| import numpy as np | ||
| import jax | ||
|
|
||
| # ──────────────────── cache modelli ──────────────────── |
| cosmo["Omega0_m"] = M.Omega0_m() | ||
| # if "w0_fld" in cosmo_dict: | ||
| # if "w0_fld" in cosmo_dict: | ||
| # cosmo["w0_fld"] = cosmo_dict["w0_fld"] |
Comment on lines
+179
to
+186
| zm = 5. | ||
| def scale_factor(z): return 1/(1.+z) | ||
| Omega0_m = cosmo["Omega0_m"] | ||
| w = cosmo["w0_fld"] | ||
| GF = GreenFunction(Omega0_m, w=w, quintessence=True) | ||
| Dq = GF.D(scale_factor(zfid)) / GF.D(scale_factor(zm)) | ||
| Dm = M.scale_independent_growth_factor(self.c["z"]) / M.scale_independent_growth_factor(zm) | ||
| cosmo["pk_lin"] *= Dq**2 / Dm**2 * ( 1 + (1+w)/(1.-3*w) * (1-Omega0_m)/Omega0_m * (1+zm)**(3*w) )**2 # 1611.07966 eq. (4.15) | ||
| cosmo["pk_lin"] *= Dq**2 / Dm**2 * ( 1 + (1+w)/(1.-3*w) * (1-Omega0_m)/Omega0_m * (1+zm)**(3*w) )**2 |
|
|
||
| z_arr = linspace(0., 20., 5000) | ||
| dz = z_arr[-1] - z_arr[-2] | ||
| idx = abs(z_arr - z).argmin().astype(int) |
Comment on lines
+24
to
+25
| """Show how many and what models are actually in the cache.""" | ||
| print(_get_cpj_model.cache_info()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description:
This PR introduces two improvements to the cosmo module: