Skip to content

Commit 03b8dc0

Browse files
authored
BUG: fix set_cosmology (#1005)
* BUG: fix set_cosmology * TST: add test for getting cosmology after global is set * TST: use fixture to ensure cosmology is reset
1 parent da78508 commit 03b8dc0

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

bilby/gw/cosmology.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ def set_cosmology(cosmology=None):
106106
"""
107107
from ..core.utils.meta_data import global_meta_data
108108
cosmology = get_cosmology(cosmology)
109+
global COSMOLOGY, DEFAULT_COSMOLOGY
110+
DEFAULT_COSMOLOGY = cosmology
109111
COSMOLOGY[0] = cosmology
110112
if cosmology.name is not None:
111113
COSMOLOGY[1] = cosmology.name

test/gw/cosmology_test.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
import unittest
22

3-
from astropy.cosmology import WMAP9, Planck15
3+
from astropy.cosmology import WMAP9
44
from bilby.gw import cosmology
55
import lal
6+
import pytest
7+
8+
9+
@pytest.fixture(autouse=True)
10+
def reset_cosmology():
11+
# Reset cosmology before each test
12+
cosmology.DEFAULT_COSMOLOGY = None
13+
cosmology.COSMOLOGY = [None, str(None)]
14+
try:
15+
yield
16+
finally:
17+
# Reset cosmology after each test
18+
cosmology.DEFAULT_COSMOLOGY = None
19+
cosmology.COSMOLOGY = [None, str(None)]
620

721

822
class TestSetCosmology(unittest.TestCase):
@@ -12,12 +26,11 @@ def setUp(self):
1226
def test_setting_cosmology_with_string(self):
1327
cosmology.set_cosmology("WMAP9")
1428
self.assertEqual(cosmology.COSMOLOGY[1], "WMAP9")
15-
cosmology.set_cosmology("Planck15")
29+
self.assertEqual(cosmology.DEFAULT_COSMOLOGY.name, "WMAP9")
1630

1731
def test_setting_cosmology_with_astropy_object(self):
1832
cosmology.set_cosmology(WMAP9)
1933
self.assertEqual(cosmology.COSMOLOGY[1], "WMAP9")
20-
cosmology.set_cosmology(Planck15)
2134

2235
def test_setting_cosmology_with_default(self):
2336
cosmology.set_cosmology()
@@ -38,6 +51,15 @@ def test_setting_cosmology_with_w_cdm_dict(self):
3851
cosmology.set_cosmology(cosmo_dict)
3952
self.assertEqual(cosmology.COSMOLOGY[1][:4], "wCDM")
4053

54+
def test_repeated_setting_cosmology(self):
55+
cosmology.set_cosmology("WMAP9")
56+
self.assertEqual(cosmology.COSMOLOGY[1], "WMAP9")
57+
self.assertEqual(cosmology.DEFAULT_COSMOLOGY.name, "WMAP9")
58+
cosmology.set_cosmology("Planck18")
59+
self.assertEqual(cosmology.COSMOLOGY[1], "Planck18")
60+
self.assertEqual(cosmology.DEFAULT_COSMOLOGY.name, "Planck18")
61+
cosmology.set_cosmology("Planck15")
62+
4163

4264
class TestGetCosmology(unittest.TestCase):
4365
def setUp(self):
@@ -49,6 +71,10 @@ def test_getting_cosmology_with_string(self):
4971
def test_getting_cosmology_with_default(self):
5072
self.assertEqual(cosmology.get_cosmology().name, "Planck15")
5173

74+
def test_getting_cosmology_non_standard_default(self):
75+
cosmology.set_cosmology("WMAP9")
76+
self.assertEqual(cosmology.get_cosmology().name, "WMAP9")
77+
5278

5379
class TestPlanck15LALCosmology(unittest.TestCase):
5480

0 commit comments

Comments
 (0)