Skip to content

cosmo: add CPJ neural network cache and refactor helper functions - #17

Open
MarcoOriani00 wants to merge 1 commit into
pierrexyz:masterfrom
MarcoOriani00:cache-cpj-networks
Open

cosmo: add CPJ neural network cache and refactor helper functions#17
MarcoOriani00 wants to merge 1 commit into
pierrexyz:masterfrom
MarcoOriani00:cache-cpj-networks

Conversation

@MarcoOriani00

Copy link
Copy Markdown

Description:
This PR introduces two improvements to the cosmo module:

  • CPJ network caching: neural networks are now loaded once and cached, avoiding redundant reloading on repeated calls to get_cosmo, improving performance.
  • Helper function refactoring: helper functions have been moved outside of get_cosmo to avoid redundant re-definitions on every call, which is particularly relevant in MCMC runs where get_cosmo is called thousands of times.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_custom helpers and a models-directory resolver.
  • Hoist to_Mpc_per_h_jax out of set_cosmo's CPJ/CPJ_custom branches; comment out the now-unused get_smooth_wiggle_resc block.
  • Add a models_dir=None kwarg to set_cosmo (currently unused) and switch CPJ construction from filename=<relative> to filepath=<absolute>.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pybird/cosmo.py
Comment on lines +34 to +35
ilogpk_ = interp1d(log(_kk), log(_pk), fill_value='extrapolate')
return exp(ilogpk_(log(_kk*h))) * h**3
Comment thread pybird/cosmo.py
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 thread pybird/cosmo.py
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)
Comment thread pybird/cosmo.py
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'))
Comment thread pybird/cosmo.py
# ──────────────────── 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
Comment thread pybird/cosmo.py
import numpy as np
import jax

# ──────────────────── cache modelli ────────────────────
Comment thread pybird/cosmo.py
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 thread pybird/cosmo.py
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
Comment thread pybird/cosmo.py

z_arr = linspace(0., 20., 5000)
dz = z_arr[-1] - z_arr[-2]
idx = abs(z_arr - z).argmin().astype(int)
Comment thread pybird/cosmo.py
Comment on lines +24 to +25
"""Show how many and what models are actually in the cache."""
print(_get_cpj_model.cache_info())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants