Skip to content

Commit 3f36e55

Browse files
author
David Turner
committed
Individual test .py files have been altered to use 'get_test_source', rather than fetching declared sources directly from the init. CAUTION THIS IS LIKELY NOT FUNCTIONING RIGHT QUITE YET.
Signed-off-by: David Turner <djturner@umbc.edu>
1 parent 847fffa commit 3f36e55

25 files changed

Lines changed: 481 additions & 307 deletions

tests/__init__.py

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# This code is a part of X-ray: Generate and Analyse (XGA), a module designed for the XMM Cluster Survey (XCS).
2-
# Last modified by David J Turner (turne540@msu.edu) 20/02/2023, 14:04. Copyright (c) The Contributors
1+
# This code is part of X-ray: Generate and Analyse (XGA), a module designed for the XMM Cluster Survey (XCS).
2+
# Last modified by David J Turner (djturner@umbc.edu) 4/24/26, 1:14 PM. Copyright (c) The Contributors.
33
from astropy.units import Quantity
44
import numpy as np
55
import pandas as pd
@@ -31,30 +31,47 @@
3131
CLUSTER_SMP = pd.DataFrame(data=cluster_data, columns=column_names)
3232
CLUSTER_SMP[['ra', 'dec', 'z', 'r500']] = CLUSTER_SMP[['ra', 'dec', 'z', 'r500']].astype(float)
3333

34-
SRC_ALL_TELS = GalaxyCluster(SRC_INFO['ra'],
35-
SRC_INFO['dec'],
36-
SRC_INFO['z'],
37-
r500=Quantity(500, 'kpc'),
38-
name=SRC_INFO['name'],
39-
use_peak=False,
40-
search_distance={'erosita': Quantity(3.6, 'deg')},
41-
load_profiles=False)
42-
43-
SRC_XMM = GalaxyCluster(SRC_INFO['ra'],
44-
SRC_INFO['dec'],
45-
SRC_INFO['z'],
46-
r500=Quantity(500, 'kpc'),
47-
name=SRC_INFO['name'],
48-
use_peak=False,
49-
telescope='xmm',
50-
load_profiles=False)
51-
52-
SRC_ERO = GalaxyCluster(SRC_INFO['ra'],
53-
SRC_INFO['dec'],
54-
SRC_INFO['z'],
55-
r500=Quantity(500, 'kpc'),
56-
name=SRC_INFO['name'],
57-
use_peak=False,
58-
telescope='erosita',
59-
search_distance={'erosita': Quantity(3.6, 'deg')},
60-
load_profiles=False)
34+
35+
# We use a factory pattern to provide the test sources, this is because they are expensive to instantiate
36+
# and we don't want to do it at import time, as the configuration might not be set up yet.
37+
_CACHED_SOURCES = {}
38+
39+
def get_test_source(telescope: str = 'all', shared: bool = True) -> GalaxyCluster:
40+
"""
41+
A factory function to provide test sources. This is used to avoid instantiating them at import time.
42+
43+
:param str telescope: The telescope for which we want a source. Options are 'all', 'xmm', 'erosita'.
44+
:param bool shared: Whether to return a shared (cached) instance or a fresh one.
45+
:return: The requested source.
46+
:rtype: GalaxyCluster
47+
"""
48+
global _CACHED_SOURCES
49+
50+
# If a shared instance is requested, and we have one cached, return it
51+
if shared and telescope in _CACHED_SOURCES:
52+
return _CACHED_SOURCES[telescope]
53+
54+
# Otherwise we set up the requested source instance for testing.
55+
if telescope == 'all':
56+
src = GalaxyCluster(SRC_INFO['ra'], SRC_INFO['dec'], SRC_INFO['z'], r500=Quantity(500, 'kpc'),
57+
name=SRC_INFO['name'], use_peak=False,
58+
search_distance={'erosita': Quantity(3.6, 'deg')},
59+
load_profiles=False)
60+
elif telescope == 'xmm':
61+
src = GalaxyCluster(SRC_INFO['ra'], SRC_INFO['dec'], SRC_INFO['z'], r500=Quantity(500, 'kpc'),
62+
name=SRC_INFO['name'], use_peak=False,
63+
telescope='xmm', load_profiles=False)
64+
elif telescope == 'erosita':
65+
src = GalaxyCluster(SRC_INFO['ra'], SRC_INFO['dec'], SRC_INFO['z'], r500=Quantity(500, 'kpc'),
66+
name=SRC_INFO['name'], use_peak=False,
67+
telescope='erosita',
68+
search_distance={'erosita': Quantity(3.6, 'deg')},
69+
load_profiles=False)
70+
else:
71+
raise ValueError(f"Unknown mission name: {telescope}")
72+
73+
# If a shared instance was requested, cache it
74+
if shared:
75+
_CACHED_SOURCES[telescope] = src
76+
77+
return src

tests/test_generate/test_ciao_phot.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
# This code is part of X-ray: Generate and Analyse (XGA), a module designed for the XMM Cluster Survey (XCS).
2+
# Last modified by David J Turner (djturner@umbc.edu) 4/27/26, 10:27 AM. Copyright (c) The Contributors.
3+
14
import unittest
5+
from ..utils import require_ciao
26
import sys
37
import os
48

@@ -14,31 +18,37 @@
1418
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
1519

1620

17-
from .. import SRC_ALL_TELS, SROC_ERO
21+
from .. import get_test_source
1822

1923

2024
class TestCiaoPhotFuncs(unittest.TestCase):
25+
@classmethod
26+
def setUpClass(cls):
27+
cls.src = get_test_source('all')
28+
29+
@require_ciao
2130
def test_chandra_image_expmap_no_tel_error(self):
2231
"""
2332
Testing that TelescopeNotAssociatedError is raised when Chandra isn't associated.
2433
"""
2534
with self.assertRaises(TelescopeNotAssociatedError):
26-
chandra_image_expmap(SRC_ERO)
35+
chandra_image_expmap(self.src)
2736

37+
@require_ciao
2838
def test_chandra_image_expmap_if_available(self):
2939
"""
3040
Test chandra_image_expmap if Chandra data is available. Will skip if no Chandra observations.
3141
"""
3242
# Check if Chandra is actually available for this source
33-
if 'chandra' not in SRC_ALL_TELS.obs_ids or len(SRC_ALL_TELS.obs_ids.get('chandra', [])) == 0:
43+
if 'chandra' not in self.src.obs_ids or len(self.src.obs_ids.get('chandra', [])) == 0:
3444
self.skipTest("No Chandra observations available for test source")
3545

3646
# If we get here, Chandra data exists - try to generate products
37-
chandra_image_expmap(SRC_ALL_TELS, Quantity(0.5, 'keV'), Quantity(2.0, 'keV'))
47+
chandra_image_expmap(self.src, Quantity(0.5, 'keV'), Quantity(2.0, 'keV'))
3848

3949
# Try to retrieve the generated products
4050
try:
41-
im = SRC_ALL_TELS.get_images(lo_en=Quantity(0.5, 'keV'), hi_en=Quantity(2.0, 'keV'),
51+
im = self.src.get_images(lo_en=Quantity(0.5, 'keV'), hi_en=Quantity(2.0, 'keV'),
4252
telescope='chandra')
4353
if isinstance(im, list):
4454
for i in im:
@@ -58,3 +68,6 @@ def test_chandra_image_expmap_if_available(self):
5868

5969
if __name__ == "__main__":
6070
unittest.main()
71+
72+
73+

tests/test_generate/test_ciao_spec.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
# This code is part of X-ray: Generate and Analyse (XGA), a module designed for the XMM Cluster Survey (XCS).
2+
# Last modified by David J Turner (djturner@umbc.edu) 4/27/26, 10:24 AM. Copyright (c) The Contributors.
3+
14
import unittest
5+
from ..utils import require_ciao
26
import sys
37
import os
48

@@ -14,31 +18,37 @@
1418
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
1519

1620

17-
from .. import SRC_ALL_TELS, SRC_ERO
21+
from .. import get_test_source
1822

1923

2024
class TestCiaoSpecFuncs(unittest.TestCase):
25+
@classmethod
26+
def setUpClass(cls):
27+
cls.src = get_test_source('all')
28+
29+
@require_ciao
2130
def test_specextract_spectrum_no_tel_error(self):
2231
"""
2332
Testing that TelescopeNotAssociatedError is raised when Chandra isn't associated.
2433
"""
2534
with self.assertRaises(TelescopeNotAssociatedError):
26-
specextract_spectrum(SRC_ERO, 'r500')
35+
specextract_spectrum(self.src, 'r500')
2736

37+
@require_ciao
2838
def test_specextract_spectrum_if_available(self):
2939
"""
3040
Test specextract_spectrum if Chandra data is available. Will skip if no Chandra observations.
3141
"""
3242
# Check if Chandra is actually available for this source
33-
if 'chandra' not in SRC_ALL_TELS.obs_ids or len(SRC_ALL_TELS.obs_ids.get('chandra', [])) == 0:
43+
if 'chandra' not in self.src.obs_ids or len(self.src.obs_ids.get('chandra', [])) == 0:
3444
self.skipTest("No Chandra observations available for test source")
3545

3646
# If we get here, Chandra data exists - try to generate spectra
37-
specextract_spectrum(SRC_ALL_TELS, 'r500')
47+
specextract_spectrum(self.src, 'r500')
3848

3949
# Try to retrieve the generated products
4050
try:
41-
spec = SRC_ALL_TELS.get_spectra('r500', telescope='chandra')
51+
spec = self.src.get_spectra('r500', telescope='chandra')
4252
if isinstance(spec, list):
4353
for s in spec:
4454
assert s.telescope == 'chandra'
@@ -53,3 +63,6 @@ def test_specextract_spectrum_if_available(self):
5363

5464
if __name__ == "__main__":
5565
unittest.main()
66+
67+
68+
Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
import unittest
1+
# This code is part of X-ray: Generate and Analyse (XGA), a module designed for the XMM Cluster Survey (XCS).
2+
# Last modified by David J Turner (djturner@umbc.edu) 4/27/26, 10:28 AM. Copyright (c) The Contributors.
23

3-
from astropy.units import Quantity
4-
import os
4+
import os
55
import sys
6+
import unittest
67

7-
import xga
8-
from xga.sources import GalaxyCluster
98
from xga.generate.esass.lightcurve import srctool_lightcurve
109
from xga.products.lightcurve import LightCurve
11-
10+
from ..utils import require_esass
1211

1312
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
1413

15-
from .. import SRC_ALL_TELS
14+
from .. import get_test_source
15+
16+
class TestEsassLcFuncs(unittest.TestCase):
17+
@classmethod
18+
def setUpClass(cls):
19+
cls.src = get_test_source('erosita')
1620

17-
class TestEsassLcFuncs(unittest.TestCase):
21+
@require_esass
1822
def test_srctool_lightcurve_combine_insts_f_combine_obs_f(self):
1923
"""
2024
Testing srctool_lightcurve with the arguments combine_insts=False and combine_obs=False
2125
"""
22-
srctool_lightcurve(SRC_ALL_TELS, 'r500', combine_tm=False, combine_obs=False)
26+
srctool_lightcurve(self.src, 'r500', combine_tm=False, combine_obs=False)
2327

24-
lc = SRC_ALL_TELS.get_lightcurves('r500', telescope='erosita', inst='tm1')
28+
lc = self.src.get_lightcurves('r500', telescope='erosita', inst='tm1')
2529

2630
if isinstance(lc, list):
2731
for l in lc:
@@ -35,13 +39,14 @@ def test_srctool_lightcurve_combine_insts_f_combine_obs_f(self):
3539
assert lc.obs_id != 'combined'
3640
assert lc.instrument == 'tm1'
3741

42+
@require_esass
3843
def test_srctool_lightcurve_combine_insts_t_combine_obs_f(self):
3944
"""
4045
Testing srctool_lightcurve with the arguments combine_insts=True and combine_obs=False
4146
"""
42-
srctool_lightcurve(SRC_ALL_TELS, 'r500', combine_tm=True, combine_obs=False)
47+
srctool_lightcurve(self.src, 'r500', combine_tm=True, combine_obs=False)
4348

44-
lc = SRC_ALL_TELS.get_lightcurves('r500', telescope='erosita', inst='combined')
49+
lc = self.src.get_lightcurves('r500', telescope='erosita', inst='combined')
4550

4651
if isinstance(lc, list):
4752
for l in lc:
@@ -56,26 +61,28 @@ def test_srctool_lightcurve_combine_insts_t_combine_obs_f(self):
5661
assert lc.instrument == 'combined'
5762

5863

64+
@require_esass
5965
def test_srctool_lightcurve_combine_insts_f_combine_obs_t(self):
6066
"""
6167
Testing srctool_lightcurve with the arguments combine_insts=False and combine_obs=True
6268
"""
63-
srctool_lightcurve(SRC_ALL_TELS, 'r500', combine_tm=False, combine_obs=True)
69+
srctool_lightcurve(self.src, 'r500', combine_tm=False, combine_obs=True)
6470

65-
lc = SRC_ALL_TELS.get_combined_lightcurves('r500', telescope='erosita', inst='tm1')
71+
lc = self.src.get_combined_lightcurves('r500', telescope='erosita', inst='tm1')
6672

6773
assert isinstance(lc, LightCurve)
6874
assert lc.telescope == 'erosita'
6975
assert lc.obs_id == 'combined'
7076
assert lc.instrument == 'tm1'
7177

78+
@require_esass
7279
def test_srctool_lightcurve_combine_insts_t_combine_obs_t(self):
7380
"""
7481
Testing srctool_lightcurve with the arguments combine_insts=True and combine_obs=True
7582
"""
76-
srctool_lightcurve(SRC_ALL_TELS, 'r500', combine_tm=True, combine_obs=True)
83+
srctool_lightcurve(self.src, 'r500', combine_tm=True, combine_obs=True)
7784

78-
lc = SRC_ALL_TELS.get_combined_lightcurves('r500', telescope='erosita', inst='combined')
85+
lc = self.src.get_combined_lightcurves('r500', telescope='erosita', inst='combined')
7986

8087
if isinstance(lc, list):
8188
for l in lc:
@@ -87,4 +94,6 @@ def test_srctool_lightcurve_combine_insts_t_combine_obs_t(self):
8794
assert isinstance(lc, LightCurve)
8895
assert lc.telescope == 'erosita'
8996
assert lc.obs_id == 'combined'
90-
assert lc.instrument == 'combined'
97+
assert lc.instrument == 'combined'
98+
99+
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1-
import unittest
1+
# This code is part of X-ray: Generate and Analyse (XGA), a module designed for the XMM Cluster Survey (XCS).
2+
# Last modified by David J Turner (djturner@umbc.edu) 4/24/26, 1:36 PM. Copyright (c) The Contributors.
23

3-
from astropy.units import Quantity
4-
import sys
54
import os
5+
import sys
6+
import unittest
67

7-
import xga
8-
from xga.sources import GalaxyCluster
98
from xga.generate.esass.misc import evtool_combine_evts
10-
9+
from ..utils import require_esass
1110

1211
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
1312

14-
from ..import SRC_ALL_TELS
13+
from .. import get_test_source
1514

1615
class TestEsassMiscFuncs(unittest.TestCase):
16+
@classmethod
17+
def setUpClass(cls):
18+
cls.src = get_test_source('erosita')
19+
20+
@require_esass
1721
def test_evtool_combine_evts(self):
18-
evtool_combine_evts(SRC_ALL_TELS)
22+
evtool_combine_evts(self.src)
1923

20-
evtlist = SRC_ALL_TELS.get_products("combined_events", just_obj=False, telescope='erosita')[0][-1]
24+
evtlist = self.src.get_products("combined_events", just_obj=False, telescope='erosita')[0][-1]
2125
assert evtlist.telescope == 'erosita'
22-
assert set(evtlist.obs_ids) == set(SRC_ALL_TELS.obs_ids['erosita'])
26+
assert set(evtlist.obs_ids) == set(self.src.obs_ids['erosita'])
2327

2428
if __name__ == "__main__":
25-
unittest.main()
29+
unittest.main()

0 commit comments

Comments
 (0)