Skip to content

Commit

Permalink
Merge pull request #387 from LSSTDESC/fix_missing_config_file_error
Browse files Browse the repository at this point in the history
Fix for ungraceful behaviour in python for missing config files
  • Loading branch information
tilmantroester committed May 31, 2018
2 parents 2e460fe + 3191e7b commit 4ba33e6
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 33 deletions.
1 change: 1 addition & 0 deletions include/ccl_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern "C" {
#define CCL_ERROR_NOT_IMPLEMENTED 1040
#define CCL_ERROR_MNU_UNPHYSICAL 1041
#define CCL_ERROR_ANGPOW 1042
#define CCL_ERROR_MISSING_CONFIG_FILE 1043

typedef enum {
CCL_ERROR_POLICY_EXIT = 0,
Expand Down
1 change: 1 addition & 0 deletions pyccl/ccl_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -25163,6 +25163,7 @@ SWIG_init(void) {
SWIG_Python_SetConstant(d, "CCL_ERROR_NOT_IMPLEMENTED",SWIG_From_int((int)(1040)));
SWIG_Python_SetConstant(d, "CCL_ERROR_MNU_UNPHYSICAL",SWIG_From_int((int)(1041)));
SWIG_Python_SetConstant(d, "CCL_ERROR_ANGPOW",SWIG_From_int((int)(1042)));
SWIG_Python_SetConstant(d, "CCL_ERROR_MISSING_CONFIG_FILE",SWIG_From_int((int)(1043)));
SWIG_Python_SetConstant(d, "CCL_ERROR_POLICY_EXIT",SWIG_From_int((int)(CCL_ERROR_POLICY_EXIT)));
SWIG_Python_SetConstant(d, "CCL_ERROR_POLICY_CONTINUE",SWIG_From_int((int)(CCL_ERROR_POLICY_CONTINUE)));
SWIG_Python_SetConstant(d, "CCL_DEBUG_MODE_OFF",SWIG_From_int((int)(CCL_DEBUG_MODE_OFF)));
Expand Down
1 change: 1 addition & 0 deletions pyccl/ccllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,7 @@ def __init__(self):
CCL_ERROR_NOT_IMPLEMENTED = _ccllib.CCL_ERROR_NOT_IMPLEMENTED
CCL_ERROR_MNU_UNPHYSICAL = _ccllib.CCL_ERROR_MNU_UNPHYSICAL
CCL_ERROR_ANGPOW = _ccllib.CCL_ERROR_ANGPOW
CCL_ERROR_MISSING_CONFIG_FILE = _ccllib.CCL_ERROR_MISSING_CONFIG_FILE
CCL_ERROR_POLICY_EXIT = _ccllib.CCL_ERROR_POLICY_EXIT
CCL_ERROR_POLICY_CONTINUE = _ccllib.CCL_ERROR_POLICY_CONTINUE
CCL_DEBUG_MODE_OFF = _ccllib.CCL_DEBUG_MODE_OFF
Expand Down
36 changes: 18 additions & 18 deletions pyccl/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,21 @@

# Error types
error_types = {
lib.CCL_ERROR_MEMORY: 'CCL_ERROR_MEMORY',
lib.CCL_ERROR_LINSPACE: 'CCL_ERROR_LINSPACE',
lib.CCL_ERROR_INCONSISTENT: 'CCL_ERROR_INCONSISTENT',
lib.CCL_ERROR_SPLINE: 'CCL_ERROR_SPLINE',
lib.CCL_ERROR_SPLINE_EV: 'CCL_ERROR_SPLINE_EV',
lib.CCL_ERROR_INTEG: 'CCL_ERROR_INTEG',
lib.CCL_ERROR_ROOT: 'CCL_ERROR_ROOT',
lib.CCL_ERROR_CLASS: 'CCL_ERROR_CLASS',
lib.CCL_ERROR_COMPUTECHI: 'CCL_ERROR_COMPUTECHI',
lib.CCL_ERROR_MF: 'CCL_ERROR_MF',
lib.CCL_ERROR_HMF_INTERP: 'CCL_ERROR_HMF_INTERP',
lib.CCL_ERROR_PARAMETERS: 'CCL_ERROR_PARAMETERS',
lib.CCL_ERROR_NU_INT: 'CCL_ERROR_NU_INT',
lib.CCL_ERROR_EMULATOR_BOUND: 'CCL_ERROR_EMULATOR_BOUND'
lib.CCL_ERROR_MEMORY: 'CCL_ERROR_MEMORY',
lib.CCL_ERROR_LINSPACE: 'CCL_ERROR_LINSPACE',
lib.CCL_ERROR_INCONSISTENT: 'CCL_ERROR_INCONSISTENT',
lib.CCL_ERROR_SPLINE: 'CCL_ERROR_SPLINE',
lib.CCL_ERROR_SPLINE_EV: 'CCL_ERROR_SPLINE_EV',
lib.CCL_ERROR_INTEG: 'CCL_ERROR_INTEG',
lib.CCL_ERROR_ROOT: 'CCL_ERROR_ROOT',
lib.CCL_ERROR_CLASS: 'CCL_ERROR_CLASS',
lib.CCL_ERROR_COMPUTECHI: 'CCL_ERROR_COMPUTECHI',
lib.CCL_ERROR_MF: 'CCL_ERROR_MF',
lib.CCL_ERROR_HMF_INTERP: 'CCL_ERROR_HMF_INTERP',
lib.CCL_ERROR_PARAMETERS: 'CCL_ERROR_PARAMETERS',
lib.CCL_ERROR_NU_INT: 'CCL_ERROR_NU_INT',
lib.CCL_ERROR_EMULATOR_BOUND: 'CCL_ERROR_EMULATOR_BOUND',
lib.CCL_ERROR_MISSING_CONFIG_FILE: 'CCL_ERROR_MISSING_CONFIG_FILE',
}

class Parameters(object):
Expand Down Expand Up @@ -433,10 +434,9 @@ def __del__(self):
"""Free the ccl_cosmology instance that this Cosmology object is managing.
"""
try:
lib.cosmology_free(self.cosmo)
except:
pass
if hasattr(self, "cosmo"):
if self.cosmo is not None:
lib.cosmology_free(self.cosmo)

def __str__(self):
"""Output the cosmological parameters that were set, and their values,
Expand Down
30 changes: 16 additions & 14 deletions src/ccl_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ void ccl_cosmology_read_config(void)
}

if ((fconfig=fopen(param_file, "r")) == NULL) {
ccl_raise_exception(EXIT_FAILURE, "ccl_core.c: Failed to open config file");
char msg[256];
snprintf(msg, 256, "ccl_core.c: Failed to open config file: %s", param_file);
ccl_raise_exception(CCL_ERROR_MISSING_CONFIG_FILE, msg);
return;
}

Expand Down Expand Up @@ -356,19 +358,7 @@ ccl_parameters ccl_parameters_create(
#endif

ccl_parameters params;

/* Check whether ccl_splines and ccl_gsl exist. If either is not set yet, load
parameters from the config file. */
if(ccl_splines==NULL || ccl_gsl==NULL) {
ccl_cosmology_read_config();
}
/* Exit gracefully if config file can't be opened. */
if(ccl_splines==NULL || ccl_gsl==NULL) {
ccl_raise_exception(EXIT_FAILURE, "ccl_core.c: Failed to read config file.");
*status = EXIT_FAILURE;
return params;
}

// Initialize params
params.mnu = NULL;
params.z_mgrowth=NULL;
params.df_mgrowth=NULL;
Expand All @@ -384,6 +374,18 @@ ccl_parameters ccl_parameters_create(
double mnusum = *mnu;
double *mnu_in = NULL;

/* Check whether ccl_splines and ccl_gsl exist. If either is not set yet, load
parameters from the config file. */
if(ccl_splines==NULL || ccl_gsl==NULL) {
ccl_cosmology_read_config();
}
/* Exit gracefully if config file can't be opened. */
if(ccl_splines==NULL || ccl_gsl==NULL) {
ccl_raise_exception(CCL_ERROR_MISSING_CONFIG_FILE, "ccl_core.c: Failed to read config file.");
*status = CCL_ERROR_MISSING_CONFIG_FILE;
return params;
}

// Decide how to split sum of neutrino masses between 3 neutrinos. See the
// CCL note for how we get these expressions for the neutrino masses in
// normal and inverted hierarchy.
Expand Down
16 changes: 15 additions & 1 deletion tests/ccl_test_pyccl_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,19 @@ def check_neutrinos():
assert_( all_finite(ccl.nu_masses(OmNuh2, 'equal', TCMB)) )
assert_( all_finite(ccl.nu_masses(OmNuh2, 'sum', TCMB)) )

# Check that the right exceptions are raised
assert_raises(ValueError, ccl.Cosmology, Omega_c=0.27, Omega_b=0.045,
h=0.67, A_s=1e-10, n_s=0.96,
m_nu=[0.1, 0.2, 0.3, 0.4])
assert_raises(ValueError, ccl.Cosmology, Omega_c=0.27, Omega_b=0.045,
h=0.67, A_s=1e-10, n_s=0.96,
m_nu=[0.1, 0.2, 0.3],
mnu_type="sum")
assert_raises(ValueError, ccl.Cosmology, Omega_c=0.27, Omega_b=0.045,
h=0.67, A_s=1e-10, n_s=0.96,
m_nu=42)


def check_lsst_specs(cosmo):
"""
Check that lsst_specs functions can be run.
Expand Down Expand Up @@ -707,7 +720,7 @@ def check_corr_3d(cosmo):
assert_( all_finite(corr2))
assert_( all_finite(corr3))



def test_valid_transfer_combos():
"""
Expand Down Expand Up @@ -806,6 +819,7 @@ def test_debug_mode():
ccl.debug_mode(True)
ccl.debug_mode(False)



if __name__ == '__main__':
run_module_suite()

0 comments on commit 4ba33e6

Please sign in to comment.