def test_cosmology_calculator_answers(): """ Test cosmology calculator functions against previously calculated values. """ fn = os.path.join(local_dir, 'cosmology_answers.yml') data = yaml.load(open(fn, 'r'), Loader=yaml.FullLoader) cosmologies = data['cosmologies'] functions = data['functions'] for cname, copars in cosmologies.items(): omega_curvature = 1 - copars['omega_matter'] - \ copars['omega_lambda'] - copars['omega_radiation'] cosmology = Cosmology(omega_curvature=omega_curvature, **copars) for fname, finfo in functions.items(): func = getattr(cosmology, fname) args = finfo.get('args', []) val = func(*args) units = finfo.get('units') if units is not None: val.convert_to_units(units) val = float(val) err_msg = '%s answer has changed for %s cosmology, old: %f, new: %f.' % \ (fname, cname, finfo['answers'][cname], val) assert_almost_equal( val, finfo['answers'][cname], 10, err_msg=err_msg)
def test_cosmology_calculator_answers(): """ Test cosmology calculator functions against previously calculated values. """ fn = os.path.join(local_dir, "cosmology_answers.yml") with open(fn) as fh: data = yaml.load(fh, Loader=yaml.FullLoader) cosmologies = data["cosmologies"] functions = data["functions"] for cname, copars in cosmologies.items(): omega_curvature = ( 1 - copars["omega_matter"] - copars["omega_lambda"] - copars["omega_radiation"] ) cosmology = Cosmology(omega_curvature=omega_curvature, **copars) for fname, finfo in functions.items(): func = getattr(cosmology, fname) args = finfo.get("args", []) val = func(*args) units = finfo.get("units") if units is not None: val.convert_to_units(units) val = float(val) err_msg = ( "{} answer has changed for {} cosmology, old: {:f}, new: {:f}.".format( fname, cname, finfo["answers"][cname], val, ) ) assert_almost_equal(val, finfo["answers"][cname], 10, err_msg=err_msg)