From 1928d7cd106a1bd8b335fd6f579fbcfbca0e1dbf Mon Sep 17 00:00:00 2001 From: RyoTerasawa Date: Tue, 3 Dec 2024 03:21:04 +0000 Subject: [PATCH] lint --- benchmarks/test_pkresponse.py | 7 +++-- pyccl/pkresponse.py | 50 +++++++++++++++-------------- pyccl/tests/test_pkresponse.py | 57 ++++++++++++++++++++++------------ 3 files changed, 70 insertions(+), 44 deletions(-) diff --git a/benchmarks/test_pkresponse.py b/benchmarks/test_pkresponse.py index 087268e10..61b8515dd 100644 --- a/benchmarks/test_pkresponse.py +++ b/benchmarks/test_pkresponse.py @@ -32,7 +32,7 @@ Pgm_resp_data = pkresponse_data["Pgm_resp"] / h**3 Pgm_resp_err_data = pkresponse_data["Pgm_resp_err"] / h**3 Pgg_resp_data = pkresponse_data["Pgg_resp"] / h**3 -Pgg_resp_err_data = pkresponse_data["Pgg_resp_err"]/ h**3 +Pgg_resp_err_data = pkresponse_data["Pgg_resp_err"] / h**3 # HOD parameters @@ -108,6 +108,7 @@ def test_pmm_resp(): atol=10 * Pmm_resp_err_data[0, indx_mm], ) + def test_pmm_resp2(): assert np.allclose( Pmm_resp_data[1:, indx_mm], @@ -123,6 +124,7 @@ def test_pgm_resp(): atol=15 * Pgm_resp_err_data[:2, indx], ) + def test_pgm_resp2(): assert np.allclose( Pgm_resp_data[2:, indx], @@ -138,9 +140,10 @@ def test_pgg_resp(): atol=4 * Pgg_resp_err_data[:2, indx], ) + def test_pgg_resp2(): assert np.allclose( Pgg_resp_data[2:, indx], generated_Pgg_resp[2:], atol=15 * Pgg_resp_err_data[2:, indx], - ) \ No newline at end of file + ) diff --git a/pyccl/pkresponse.py b/pyccl/pkresponse.py index 790b75902..675c89488 100755 --- a/pyccl/pkresponse.py +++ b/pyccl/pkresponse.py @@ -75,7 +75,9 @@ def Pmm_resp( k_use = np.exp(lk_arr) # set h-modified cosmology to take finite differencing - cosmo_hp, cosmo_hm = _set_hmodified_cosmology(cosmo, deltah, extra_parameters) + cosmo_hp, cosmo_hm = _set_hmodified_cosmology( + cosmo, deltah, extra_parameters + ) # Growth factor Dp = cosmo_hp.growth_factor_unnorm(a_arr) @@ -113,9 +115,7 @@ def Pmm_resp( # Eq. 11 ((hp-hm) term is cancelled out) T_h[k_use > kmin] = ( np.log(pk_hp[k_use > kmin]) - np.log(pk_hm[k_use > kmin]) - ) / ( - 2 * (np.log(Dp[ia]) - np.log(Dm[ia])) - ) + ) / (2 * (np.log(Dp[ia]) - np.log(Dm[ia]))) dpk[k_use <= kmin] = dpklin[k_use <= kmin] dpk[k_use > kmin] = dpknl[k_use > kmin] @@ -262,10 +262,10 @@ def darkemu_Pgm_resp( dprof_dlogM = (prof_Mp - prof_Mm) / (2 * dlogM) nth_mat = np.tile(nths, (len(k_use), 1)).transpose() - + # Eq. 18 ng = integrate.romb(dndlog10m_emu(logM) * Ng, dx=dlogM, axis=0) - + # Eq. 17 bgE = ( integrate.romb( @@ -537,7 +537,7 @@ def darkemu_Pgg_resp( / ng ) - #Eq. 19 + # Eq. 19 bgE2 = ( integrate.romb( dndlog10m_emu(logM) * Ng * _b2H17(b1), dx=dlogM, axis=0 @@ -680,7 +680,7 @@ def darkemu_Pgg_resp( def _mass_to_dens(dndlog10m, cosmo, mass_thre): - """Converts mass threshold to the cumulative number density + """Converts mass threshold to the cumulative number density for the current cosmological model at redshift z. """ logM1 = np.linspace( @@ -691,9 +691,11 @@ def _mass_to_dens(dndlog10m, cosmo, mass_thre): return dens + def _get_phh_massthreshold_mass(emu, k_emu, dens1, Mbin, redshift): - """Compute the halo-halo power spectrum between mass bin halo sample - and mass threshold halo sample specified by the corresponding cumulative number density. + """Compute the halo-halo power spectrum between + mass bin halo sample and mass threshold halo sample + specified by the corresponding cumulative number density. """ M2p = Mbin * 1.01 M2m = Mbin * 0.99 @@ -723,14 +725,13 @@ def _b2H17(b1): def _darkemu_set_cosmology(emu, cosmo): - """Input cosmology and initiallize the base class of DarkEmulator. - """ + """Input cosmology and initiallize the base class of DarkEmulator.""" h = cosmo["h"] n_s = cosmo["n_s"] A_s = cosmo["A_s"] if np.isnan(A_s): raise ValueError("A_s must be provided to use the Dark Emulator") - + omega_c = cosmo["Omega_c"] * h**2 omega_b = cosmo["Omega_b"] * h**2 omega_nu = 0.00064 # we fix this value (Nishimichi et al. 2019) @@ -742,8 +743,10 @@ def _darkemu_set_cosmology(emu, cosmo): [omega_b, omega_c, Omega_L, np.log(10**10 * A_s), n_s, -1.0] ) if darkemu.cosmo_util.test_cosm_range(cparam): - raise ValueError(('cosmological parameter out of supported range of DarkEmulator')) - + raise ValueError( + ("cosmological parameter out of supported range of DarkEmulator") + ) + emu.set_cosmology(cparam) @@ -756,7 +759,7 @@ def _darkemu_set_cosmology_forAsresp(emu, cosmo, deltalnAs): A_s = cosmo["A_s"] if np.isnan(A_s): raise ValueError("A_s must be provided to use the Dark Emulator") - + omega_c = cosmo["Omega_c"] * h**2 omega_b = cosmo["Omega_b"] * h**2 omega_nu = 0.00064 # we fix this value (Nishimichi et al. 2019) @@ -775,7 +778,9 @@ def _darkemu_set_cosmology_forAsresp(emu, cosmo, deltalnAs): ] ) if darkemu.cosmo_util.test_cosm_range(cparam): - raise ValueError(('cosmological parameter out of supported range of DarkEmulator')) + raise ValueError( + ("cosmological parameter out of supported range of DarkEmulator") + ) emu.set_cosmology(cparam) @@ -783,26 +788,25 @@ def _darkemu_set_cosmology_forAsresp(emu, cosmo, deltalnAs): def _set_hmodified_cosmology(cosmo, deltah, extra_parameters=None): - """Create the Cosmology objects with modified Hubble parameter h. - """ + """Create the Cosmology objects with modified Hubble parameter h.""" Omega_c = cosmo["Omega_c"] Omega_b = cosmo["Omega_b"] h = cosmo["h"] - + cosmo_hmodified = [] for i in [+1, -1]: hp = h + i * deltah - + # \Omega_c h^2, \Omega_b h^2 is fixed Omega_c_p = np.power((h / hp), 2) * Omega_c Omega_b_p = np.power((h / hp), 2) * Omega_b - + cosmo_hp_dict = cosmo.to_dict() cosmo_hp_dict["h"] = hp cosmo_hp_dict["Omega_c"] = Omega_c_p cosmo_hp_dict["Omega_b"] = Omega_b_p cosmo_hp_dict["extra_parameters"] = extra_parameters cosmo_hp = cosmology.Cosmology(**cosmo_hp_dict) - cosmo_hmodified.append(cosmo_hp) + cosmo_hmodified.append(cosmo_hp) return cosmo_hmodified[0], cosmo_hmodified[1] diff --git a/pyccl/tests/test_pkresponse.py b/pyccl/tests/test_pkresponse.py index d7ba50a62..3065debd7 100644 --- a/pyccl/tests/test_pkresponse.py +++ b/pyccl/tests/test_pkresponse.py @@ -1,8 +1,18 @@ import numpy as np import pyccl as ccl import pytest -from pyccl.pkresponse import * -from pyccl.pkresponse import _mass_to_dens, _get_phh_massthreshold_mass , _b2H17, _darkemu_set_cosmology, _darkemu_set_cosmology_forAsresp, _set_hmodified_cosmology +from dark_emulator import darkemu +from pyccl.pkresponse import ( + Pmm_resp, + darkemu_Pgm_resp, + darkemu_Pgg_resp, + _mass_to_dens, + _get_phh_massthreshold_mass, + _b2H17, + _darkemu_set_cosmology, + _darkemu_set_cosmology_forAsresp, + _set_hmodified_cosmology, +) from .test_cclobject import check_eq_repr_hash # Set cosmology @@ -29,7 +39,7 @@ deltalnAs = 0.03 lk_arr = np.log(np.geomspace(1e-3, 1e1, 100)) k_use = np.exp(lk_arr) -k_emu = k_use / h # [h/Mpc] +k_emu = k_use / h # [h/Mpc] a_arr = np.array([1.0]) @@ -70,7 +80,7 @@ def test_Pmm_resp(): response = Pmm_resp(cosmo, deltah=deltah, lk_arr=lk_arr, a_arr=a_arr) valid = (k_emu > 1e-2) & (k_emu < 4) - assert np.all(response[0][valid]>0) + assert np.all(response[0][valid] > 0) def test_Pgm_resp(): @@ -79,7 +89,7 @@ def test_Pgm_resp(): ) valid = (k_emu > 1e-2) & (k_emu < 4) - assert np.all(response[0][valid]>0) + assert np.all(response[0][valid] > 0) def test_Pgg_resp(): @@ -88,63 +98,72 @@ def test_Pgg_resp(): ) valid = (k_emu > 1e-2) & (k_emu < 4) - assert np.all(response[0][valid]<0) + assert np.all(response[0][valid] < 0) + # Tests for the utility functions def test_mass_to_dens(): def dndlog10m(logM): return np.ones_like(logM) - + mass_thre = 1e13 dens = _mass_to_dens(dndlog10m, cosmo, mass_thre) assert dens > 0 + def test_get_phh_massthreshold_mass(): # set cosmology for dark emulator _darkemu_set_cosmology(emu, cosmo) dens1 = 1e-3 Mbin = 1e13 - redshift = 0.0 + redshift = 0.0 phh = _get_phh_massthreshold_mass(emu, k_emu, dens1, Mbin, redshift) - pklin = pk2dlin(k_use, 1.0, cosmo)*h**3 + pklin = pk2dlin(k_use, 1.0, cosmo) * h**3 valid = (k_emu > 1e-3) & (k_emu < 1e-2) # phh is power spectrum of biased tracer assert np.all(phh[valid] > pklin[valid]) + def test_b2H17(): b2 = _b2H17(0.0) assert b2 == 0.77 + def test_darkemu_set_cosmology(): # Cosmo parameters out of bounds - cosmo_wr = ccl.Cosmology(Omega_c=0.25, Omega_b=0.05, h=0.67, - A_s=2.2e-9, n_s=2.0) + cosmo_wr = ccl.Cosmology( + Omega_c=0.25, Omega_b=0.05, h=0.67, A_s=2.2e-9, n_s=2.0 + ) with pytest.raises(ValueError): _darkemu_set_cosmology(emu, cosmo_wr) + def test_darkemu_set_cosmology_forAsresp(): # Cosmo parameters out of bounds - cosmo_wr = ccl.Cosmology(Omega_c=0.25, Omega_b=0.05, h=0.67, - A_s=2.2e-9, n_s=2.0) + cosmo_wr = ccl.Cosmology( + Omega_c=0.25, Omega_b=0.05, h=0.67, A_s=2.2e-9, n_s=2.0 + ) with pytest.raises(ValueError): _darkemu_set_cosmology_forAsresp(emu, cosmo_wr, deltalnAs=100.0) def test_set_hmodified_cosmology(): - cosmo_hp, cosmo_hm = _set_hmodified_cosmology(cosmo, deltah, extra_parameters=None) - + cosmo_hp, cosmo_hm = _set_hmodified_cosmology( + cosmo, deltah, extra_parameters=None + ) + for cosmo_test in [cosmo_hp, cosmo_hm]: cosmo_dict = cosmo_test.to_dict() cosmo_dict["h"] = cosmo["h"] cosmo_dict["Omega_c"] = cosmo["Omega_c"] cosmo_dict["Omega_b"] = cosmo["Omega_b"] cosmo_dict["extra_parameters"] = cosmo["extra_parameters"] - cosmo_new = cosmology.Cosmology(**cosmo_dict) - - # make sure the output cosmologies are exactly the same as the input one, except for the modified parameters. + cosmo_new = ccl.Cosmology(**cosmo_dict) + + # make sure the output cosmologies are exactly the same as + # the input one, except for the modified parameters. assert check_eq_repr_hash(cosmo, cosmo_new) -