Skip to content

Commit

Permalink
add kwargs option to Cosmology.read_yaml and tests (#829)
Browse files Browse the repository at this point in the history
* add kwargs option to Cosmology.read_yaml and tests

* appease flake8

Co-authored-by: c-d-leonard <[email protected]>
  • Loading branch information
joezuntz and c-d-leonard authored Jan 11, 2021
1 parent 641ba87 commit be756a1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pyccl/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,12 @@ def write_yaml(self, filename):
raise IOError("Unable to write YAML file {}".format(filename))

@classmethod
def read_yaml(cls, filename):
def read_yaml(cls, filename, **kwargs):
"""Read the parameters from a YAML file.
Args:
filename (:obj:`str`) Filename to read parameters from.
**kwargs (dict) Additional keywords that supersede file contents
"""
with open(filename, 'r') as fp:
params = yaml.load(fp, Loader=yaml.Loader)
Expand Down Expand Up @@ -256,6 +257,8 @@ def read_yaml(cls, filename):
inits['m_nu'] = params['m_nu']
inits['m_nu_type'] = 'list'

inits.update(kwargs)

return cls(**inits)

def _build_config(
Expand Down
12 changes: 12 additions & 0 deletions pyccl/tests/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,18 @@ def test_parameters_read_write():
assert_almost_equal(params['Neff'], params2['Neff'])
assert_almost_equal(params['sum_nu_masses'], params2['sum_nu_masses'])

# check overriding parameters with kwargs
params3 = ccl.Cosmology.read_yaml(temp_file_name,
matter_power_spectrum='emu',
n_s=1.1)
# check unmodified parameters are the same
assert_almost_equal(params['Omega_c'], params3['Omega_c'])
assert_almost_equal(params['Neff'], params3['Neff'])
# check new parameters and config correctly updated
assert_almost_equal(1.1, params3['n_s'])
assert_almost_equal(params['sum_nu_masses'], params3['sum_nu_masses'])
assert params3._config_init_kwargs['matter_power_spectrum'] == 'emu'

# Now make a file that will be deleted so it does not exist
# and check the right error is raise
with tempfile.NamedTemporaryFile(delete=True) as tmpfile:
Expand Down

0 comments on commit be756a1

Please sign in to comment.