Skip to content

Commit

Permalink
General bugfixes & enhancements. (#925)
Browse files Browse the repository at this point in the history
* wrong Pk2D type should return TypeError

* cosmetics

* x_out in GNFW profile should trigger interp recomputation

* enable flake for constants.py

* comprehensive imports & specified __all__ for star-imports

* E402 is all over CCL and we silence it every time; get rid of it

* fixed typos

* document x_out behavior

* removed spurious import

* remove huge mamba banner from installation output

* temporary bugfix for rho0 in HMCalculator

* enable func_from_name --> Func.from_name

* implemented MassDef.from_name

* HMCalculator from strings

* removed  anti-patterns

* clear all temporary vars in Cosmology

* combined type checking for

* tests for all new features & few fixes

* don't even store fastpt; reload time is negligible

* more useful error when cosmicemu fails

* improved coverage

* Address Issue 805.

* needs a copy of ccl_params stored in cosmo

* removed repeated code

* added new func to __all__

* forgot parens

* flake

* ImportError --> ModuleNotFoundError

* make mass_def a required argument

* make mass_def a required argument (tests)

* reverted integ_interp in HaloProfileGNFW

* make mass_def a required argument (more funcs)

* bugfix in profiles.py

* make sure changes don't break API

* flake explicit exception

* flake explicit exception

* move rho_x calculation one indentation inward

* remove checks for Gaussian and PowerLaw profiles

Co-authored-by: David Alonso <dam.phys@gmail.com>
  • Loading branch information
nikfilippas and damonge committed Aug 1, 2022
1 parent 77624d2 commit 96d7b11
Show file tree
Hide file tree
Showing 30 changed files with 503 additions and 252 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jobs:
- name: lint
run: |
export MAMBA_NO_BANNER=1
mamba install flake8
flake8 pyccl
flake8 --exclude=data benchmarks
Expand Down
191 changes: 148 additions & 43 deletions pyccl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,178 @@
# flake8: noqa
# flake8: noqa E402
from pkg_resources import get_distribution, DistributionNotFound
try:
__version__ = get_distribution(__name__).version
except DistributionNotFound:
# package is not installed
pass
del get_distribution, DistributionNotFound

# Sets the environment variable for default config path if it does not
# exist yet
# Set the environment variable for default config path
from os import environ, path
if environ.get("CLASS_PARAM_DIR") is None:
environ["CLASS_PARAM_DIR"] = path.dirname(path.abspath(__file__))
del environ, path

# SWIG-generated
from . import ccllib as lib
from . import core, constants, background, power, halomodel, pk2d, tk3d, haloprofile, halos, massfunction, nl_pt

# Errors
from .errors import (
CCLError,
CCLWarning,
)

# Core data structures
from .core import Cosmology, CosmologyVanillaLCDM, CosmologyCalculator
from .core import (
Cosmology,
CosmologyVanillaLCDM,
CosmologyCalculator,
)

# Background cosmology functions and growth functions
from .background import growth_factor, growth_factor_unnorm, \
growth_rate, comoving_radial_distance, angular_diameter_distance, comoving_angular_distance, \
h_over_h0, luminosity_distance, distance_modulus, scale_factor_of_chi, \
omega_x, rho_x
from .background import (
growth_factor,
growth_factor_unnorm,
growth_rate,
comoving_radial_distance,
angular_diameter_distance,
comoving_angular_distance,
luminosity_distance,
distance_modulus,
h_over_h0,
scale_factor_of_chi,
omega_x,
rho_x,
sigma_critical,
)

# Boltzmann solvers
from .boltzmann import get_camb_pk_lin, get_isitgr_pk_lin, get_class_pk_lin
from .boltzmann import (
get_camb_pk_lin,
get_isitgr_pk_lin,
get_class_pk_lin,
)

# Generalized power spectra
from .pk2d import Pk2D, parse_pk2d
from .pk2d import (
Pk2D,
parse_pk2d,
)

# Generalized connected trispectra
from .tk3d import Tk3D

# Power spectrum calculations, sigma8 and kNL
from .power import linear_power, nonlin_power, linear_matter_power, nonlin_matter_power, \
sigmaR, sigmaV, sigma8, sigmaM, kNL

# BCM stuff
from .bcm import bcm_model_fka, bcm_correct_pk2d

# Old halo mass function
from .massfunction import massfunc, halo_bias, massfunc_m2r

# Cl's and tracers
from .tracers import Tracer, NumberCountsTracer, WeakLensingTracer, CMBLensingTracer, \
tSZTracer, CIBTracer, ISWTracer, get_density_kernel, get_kappa_kernel, get_lensing_kernel
from .power import (
linear_power,
nonlin_power,
linear_matter_power,
nonlin_matter_power,
sigmaR,
sigmaV,
sigma8,
sigmaM,
kNL,
)

# Baryons & Neutrinos
from .bcm import (
bcm_model_fka,
bcm_correct_pk2d,
)

from .neutrinos import (
Omeganuh2,
nu_masses,
)

# Cells & Tracers
from .cls import angular_cl
from .covariances import angular_cl_cov_cNG, angular_cl_cov_SSC, sigma2_B_disc, sigma2_B_from_mask

# Useful constants and unit conversions
physical_constants = lib.cvar.constants

from .tracers import (
Tracer,
NumberCountsTracer,
WeakLensingTracer,
CMBLensingTracer,
tSZTracer,
CIBTracer,
ISWTracer,
get_density_kernel,
get_kappa_kernel,
get_lensing_kernel,
)

# Correlations & Covariances
from .correlations import (
correlation, correlation_3d, correlation_multipole, correlation_3dRsd,
correlation_3dRsd_avgmu, correlation_pi_sigma)

# Properties of haloes
from .halomodel import (
halomodel_matter_power, halo_concentration,
onehalo_matter_power, twohalo_matter_power)

# Halo density profiles
from .haloprofile import nfw_profile_3d, einasto_profile_3d, hernquist_profile_3d, nfw_profile_2d

# Specific to massive neutrinos
from .neutrinos import Omeganuh2, nu_masses
correlation,
correlation_3d,
correlation_multipole,
correlation_3dRsd,
correlation_3dRsd_avgmu,
correlation_pi_sigma,
)

from .covariances import (
angular_cl_cov_cNG,
angular_cl_cov_SSC,
sigma2_B_disc,
sigma2_B_from_mask,
)

# Parameters
physical_constants = lib.cvar.constants

# Expose function to toggle debug mode
# Miscellaneous
from .pyutils import debug_mode, resample_array

from .errors import CCLError, CCLWarning
# Deprecated & Renamed modules
from .halomodel import (
halomodel_matter_power,
halo_concentration,
onehalo_matter_power,
twohalo_matter_power,
)

from .massfunction import (
massfunc,
halo_bias,
massfunc_m2r,
)

from .haloprofile import (
nfw_profile_3d,
einasto_profile_3d,
hernquist_profile_3d,
nfw_profile_2d,
)


__all__ = (
'lib',
'physical_constants',
'CCLError', 'CCLWarning',
'Cosmology', 'CosmologyVanillaLCDM', 'CosmologyCalculator',
'growth_factor', 'growth_factor_unnorm', 'growth_rate',
'comoving_radial_distance', 'angular_diameter_distance',
'comoving_angular_distance', 'luminosity_distance', 'distance_modulus',
'h_over_h0', 'scale_factor_of_chi', 'omega_x', 'rho_x', 'sigma_critical',
'get_camb_pk_lin', 'get_isitgr_pk_lin', 'get_class_pk_lin',
'Pk2D', 'parse_pk2d', 'Tk3D',
'linear_power', 'nonlin_power',
'linear_matter_power', 'nonlin_matter_power',
'sigmaR', 'sigmaV', 'sigma8', 'sigmaM', 'kNL',
'bcm_model_fka', 'bcm_correct_pk2d',
'Omeganuh2', 'nu_masses',
'angular_cl',
'Tracer', 'NumberCountsTracer', 'WeakLensingTracer', 'CMBLensingTracer',
'tSZTracer', 'CIBTracer', 'ISWTracer',
'get_density_kernel', 'get_kappa_kernel', 'get_lensing_kernel',
'correlation', 'correlation_3d', 'correlation_multipole',
'correlation_3dRsd', 'correlation_3dRsd_avgmu', 'correlation_pi_sigma',
'angular_cl_cov_cNG', 'angular_cl_cov_SSC',
'sigma2_B_disc', 'sigma2_B_from_mask',
'debug_mode', 'resample_array',
'halomodel_matter_power', 'halo_concentration',
'onehalo_matter_power', 'twohalo_matter_power',
'massfunc', 'halo_bias', 'massfunc_m2r', 'nfw_profile_3d',
'einasto_profile_3d', 'hernquist_profile_3d', 'nfw_profile_2d',
)
2 changes: 1 addition & 1 deletion pyccl/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def angular_diameter_distance(cosmo, a1, a2=None):
lib.angular_diameter_distance_vec,
cosmo, a1, a2)
else:
if(isinstance(a1, float) or isinstance(a1, int)):
if isinstance(a1, (int, float)):
return _vectorize_fn5(lib.angular_diameter_distance,
lib.angular_diameter_distance_vec,
cosmo, 1., a1)
Expand Down
2 changes: 1 addition & 1 deletion pyccl/bcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def bcm_correct_pk2d(cosmo, pk2d):
pk2d (:class:`~pyccl.pk2d.Pk2D`): power spectrum.
"""
if not isinstance(pk2d, Pk2D):
raise ValueError("pk2d must be a Pk2D object")
raise TypeError("pk2d must be a Pk2D object")
status = 0
status = lib.bcm_correct(cosmo.cosmo, pk2d.psp, status)
check(status, cosmo)
40 changes: 6 additions & 34 deletions pyccl/boltzmann.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
import numpy as np

try:
import classy
HAVE_CLASS = True
except ImportError:
HAVE_CLASS = False

try:
import camb
import camb.model
HAVE_CAMB = True
except ImportError:
HAVE_CAMB = False

try:
import isitgr # noqa: F401
except ImportError:
except ModuleNotFoundError:
pass # prevent nans from isitgr

from . import ccllib as lib
Expand All @@ -39,12 +26,8 @@ def get_camb_pk_lin(cosmo, nonlin=False):
spectrum. If ``nonlin=True``, returns a tuple \
``(pk_lin, pk_nonlin)``.
"""

# Comment from Jarvis: TODO clean up this and other assert
# anti-patterns in this file
assert HAVE_CAMB, (
"You must have the `camb` python package "
"installed to run CCL with CAMB!")
import camb
import camb.model

# Get extra CAMB parameters that were specified
extra_camb_params = {}
Expand Down Expand Up @@ -256,16 +239,8 @@ def get_isitgr_pk_lin(cosmo):
:class:`~pyccl.pk2d.Pk2D`: Power spectrum \
object. The linear power spectrum.
"""

try:
import isitgr # noqa: F811
import isitgr.model
except ImportError as e:
e.args = (
"You must have the `isitgr` python package "
"installed to run CCL with ISiTGR-CAMB!",
*e.args)
raise
import isitgr # noqa: F811
import isitgr.model

# Get extra CAMB parameters that were specified
extra_camb_params = {}
Expand Down Expand Up @@ -442,10 +417,7 @@ def get_class_pk_lin(cosmo):
:class:`~pyccl.pk2d.Pk2D`: Power spectrum object.\
The linear power spectrum.
"""

assert HAVE_CLASS, (
"You must have the python wrapper for CLASS "
"installed to run CCL with CLASS!")
import classy

params = {
"output": "mPk",
Expand Down
6 changes: 3 additions & 3 deletions pyccl/ccl_core.i
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/* put additional #include here */
%}

// SWIG black magic. Change the behaviour of setting A_SPLINE_MAX to thowing an
// SWIG black magic. Change the behaviour of setting A_SPLINE_MAX to throwing an
// error.
%typemap(memberin) double A_SPLINE_MAX {
if($input) {
Expand Down Expand Up @@ -46,7 +46,7 @@ ccl_parameters parameters_create_nu(
return ccl_parameters_create(
Omega_c, Omega_b, Omega_k, Neff, m_nu, n_m,
w0, wa, h, norm_pk, n_s, bcm_log10Mc, bcm_etab,
bcm_ks, mu_0, sigma_0, c1_mg, c2_mg, lambda_mg,
bcm_ks, mu_0, sigma_0, c1_mg, c2_mg, lambda_mg,
-1, NULL, NULL, status );
}

Expand All @@ -72,7 +72,7 @@ ccl_parameters parameters_create_nu_vec(
return ccl_parameters_create(
Omega_c, Omega_b, Omega_k, Neff, m_nu, n_m,
w0, wa, h, norm_pk, n_s, bcm_log10Mc, bcm_etab, bcm_ks,
mu_0, sigma_0, c1_mg, c2_mg, lambda_mg,
mu_0, sigma_0, c1_mg, c2_mg, lambda_mg,
nz, zarr, dfarr, status);
}

Expand Down
9 changes: 7 additions & 2 deletions pyccl/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
"""This file exposes constants present in CCL."""
# flake8: noqa
from .ccllib import (
CCL_ERROR_CLASS, CCL_ERROR_INCONSISTENT, CCL_ERROR_INTEG,
CCL_ERROR_LINSPACE, CCL_ERROR_MEMORY, CCL_ERROR_ROOT, CCL_ERROR_SPLINE,
CCL_ERROR_SPLINE_EV, CCL_CORR_FFTLOG, CCL_CORR_BESSEL, CCL_CORR_LGNDRE,
CCL_CORR_GG, CCL_CORR_GL, CCL_CORR_LP, CCL_CORR_LM)


__all__ = ('CCL_ERROR_CLASS', 'CCL_ERROR_INCONSISTENT', 'CCL_ERROR_INTEG',
'CCL_ERROR_LINSPACE', 'CCL_ERROR_MEMORY', 'CCL_ERROR_ROOT',
'CCL_ERROR_SPLINE', 'CCL_ERROR_SPLINE_EV', 'CCL_CORR_FFTLOG',
'CCL_CORR_BESSEL', 'CCL_CORR_LGNDRE', 'CCL_CORR_GG', 'CCL_CORR_GL',
'CCL_CORR_LP', 'CCL_CORR_LM')

0 comments on commit 96d7b11

Please sign in to comment.