Coverage for pesummary/tests/cosmology_test.py: 100.0%

46 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-05-02 08:42 +0000

1# Licensed under an MIT style license -- see LICENSE.md 

2 

3from pesummary.gw.cosmology import get_cosmology, available_cosmologies 

4import pytest 

5 

6__author__ = ["Charlie Hoy <charlie.hoy@ligo.org>"] 

7 

8 

9class TestCosmology(object): 

10 """Test the get_cosmology function as part of the `pesummary.gw.cosmology` 

11 package 

12 """ 

13 def test_invalid_string(self): 

14 """Test that a ValueError is passed when an invalid string is passed 

15 """ 

16 with pytest.raises(ValueError): 

17 cosmology = get_cosmology(cosmology="random") 

18 

19 def test_lal_cosmology(self): 

20 """Test that the correct values are stored for the lal cosmology 

21 """ 

22 lal_values = dict(H0=67.90, Om0=0.3065, Ode0=0.6935) 

23 cosmology = get_cosmology(cosmology="planck15_lal") 

24 for param, value in lal_values.items(): 

25 if param == "H0": 

26 assert lal_values[param] == getattr(cosmology, param).value 

27 else: 

28 assert lal_values[param] == getattr(cosmology, param) 

29 

30 def test_astropy_cosmology(self): 

31 """Test that the astropy cosmology is correct 

32 """ 

33 from astropy import cosmology 

34 from astropy.cosmology import parameters 

35 

36 for cosmo in parameters.available: 

37 _cosmo = get_cosmology(cosmology=cosmo) 

38 astropy_cosmology = getattr(cosmology, cosmo) 

39 for key, value in vars(_cosmo).items(): 

40 try: 

41 assert vars(astropy_cosmology)[key] == value 

42 except ValueError: 

43 assert all( 

44 vars(astropy_cosmology)[key][_] == value[_] for _ 

45 in range(len(vars(astropy_cosmology)[key])) 

46 ) 

47 

48 def test_Riess2019_H0(self): 

49 """Test that the Riess2019 H0 cosmology is correct 

50 """ 

51 riess_H0 = 74.03 

52 for cosmo in available_cosmologies: 

53 if "riess" not in cosmo: 

54 continue 

55 _cosmo = get_cosmology(cosmology=cosmo) 

56 base_cosmo = cosmo.split("_with_riess2019_h0")[0] 

57 _base_cosmo = get_cosmology(cosmology=base_cosmo) 

58 assert _cosmo.H0.value == riess_H0 

59 for key in ["Om0", "Ode0"]: 

60 assert getattr(_base_cosmo, key) == getattr(_cosmo, key) 

61 

62 def test_upper_lower_case(self): 

63 """Test that `get_cosmology` works with random upper and lower cases 

64 """ 

65 from astropy import cosmology 

66 for cosmo in ["Planck18", "PLANCK18", "planck18", "PlAnCk18"]: 

67 _cosmo = get_cosmology(cosmology=cosmo) 

68 astropy_cosmology = cosmology.Planck18 

69 for key, value in vars(_cosmo).items(): 

70 try: 

71 assert vars(astropy_cosmology)[key] == value 

72 except ValueError: 

73 assert all( 

74 vars(astropy_cosmology)[key][_] == value[_] for _ 

75 in range(len(vars(astropy_cosmology)[key])) 

76 )