Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add kwargs option to Cosmology.read_yaml and tests #829

Merged
merged 4 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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