Skip to content

Commit

Permalink
Accept both pre-v0.9 and current ScopeSim versions
Browse files Browse the repository at this point in the history
  • Loading branch information
teutoburg committed Aug 30, 2024
1 parent c979f48 commit 57d067d
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 61 deletions.
46 changes: 24 additions & 22 deletions scopesim_templates/extragalactic/galaxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,35 +272,37 @@ def spiral_two_component(extent=60*u.arcsec, fluxes=(0, 0), offset=(0, 0)):

filename = "spiral_two_component.fits.bz2"
path = pathlib.Path(__file__).parent.absolute() / filename
hdulist = fits.open(path)
img_ext = hdulist[0].header["IMG_EXT"]
spec_ext = hdulist[0].header["SPEC_EXT"]

src = Source()
src.fields = hdulist[img_ext:spec_ext]
src.spectra = [hdu_to_synphot(hdu) for hdu in hdulist[spec_ext:]]

for field in src.fields:
w, h = field.data.shape
field.header["CRPIX1"] = (w + 1) / 2,
field.header["CRPIX2"] = (h + 1) / 2,
field.header["CRVAL1"] = 0
field.header["CRVAL2"] = 0
field.header["CDELT1"] = extent / w
field.header["CDELT2"] = extent / w
field.header["CUNIT1"] = "deg"
field.header["CUNIT2"] = "deg"
field.header["CTYPE1"] = "RA---TAN"
field.header["CTYPE2"] = "DEC--TAN"
field.header["SPEC_REF"] = field.header["SPEC_EXT"] - spec_ext
with fits.open(path) as hdulist:
img_ext = hdulist[0].header["IMG_EXT"]
spec_ext = hdulist[0].header["SPEC_EXT"]

srcs = []
for i in range(img_ext, spec_ext):
w, h = hdulist[i].data.shape
hdulist[i].header["CRPIX1"] = (w + 1) / 2,
hdulist[i].header["CRPIX2"] = (h + 1) / 2,
hdulist[i].header["CRVAL1"] = 0
hdulist[i].header["CRVAL2"] = 0
hdulist[i].header["CDELT1"] = extent / w
hdulist[i].header["CDELT2"] = extent / w
hdulist[i].header["CUNIT1"] = "deg"
hdulist[i].header["CUNIT2"] = "deg"
hdulist[i].header["CTYPE1"] = "RA---TAN"
hdulist[i].header["CTYPE2"] = "DEC--TAN"
# hdulist[i].header["SPEC_REF"] = hdulist[i].header["SPEC_EXT"] - spec_ext

i_spec_ext = spec_ext - img_ext + i
srcs.append(Source(image_hdu=hdulist[i],
spectra=[hdu_to_synphot(hdulist[i_spec_ext])]))
src = sum(srcs[1:], start=srcs[0])

# TODO: scale image plane according to fluxes
# TODO: shift header values according to offset

# src.meta.update(params)
# Ensure the number of _meta_dicts is the same as the number of _fields.
# TODO: check if this still works with the new decorator...
src._meta_dicts += [{}] * (len(src.fields) - len(src._meta_dicts))
# src._meta_dicts += [{}] * (len(src.fields) - len(src._meta_dicts))

return src

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ def test_empty_sky_returns_source_object(self):
sky = calibration.calibration.empty_sky()
assert isinstance(sky, Source)
assert isinstance(sky.spectra[0], SourceSpectrum)
assert isinstance(sky.fields[0], Table)
assert (isinstance(getattr(sky.fields[0], "field", sky.fields[0]), Table)
or isinstance(sky.fields[0], Table))
assert sky.fields[0]["ref"][0] == 0
3 changes: 2 additions & 1 deletion scopesim_templates/tests/test_micado/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ def test_cluster_returns_source_object(self):
sky = cluster()
assert isinstance(sky, Source)
assert isinstance(sky.spectra[0], SourceSpectrum)
assert isinstance(sky.fields[0], Table)
assert (isinstance(getattr(sky.fields[0], "field", sky.fields[0]), Table)
or isinstance(sky.fields[0], Table))
3 changes: 2 additions & 1 deletion scopesim_templates/tests/test_micado/test_darkness.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ def test_darkness_returns_source_object(self):
sky = darkness()
assert isinstance(sky, Source)
assert isinstance(sky.spectra[0], SourceSpectrum)
assert isinstance(sky.fields[0], Table)
assert (isinstance(getattr(sky.fields[0], "field", sky.fields[0]), Table)
or isinstance(sky.fields[0], Table))
assert sky.fields[0]["ref"][0] == 0
2 changes: 1 addition & 1 deletion scopesim_templates/tests/test_micado/test_flatlamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ def test_flatlamp_returns_source_object(self):
lamp = flatlamp()
assert isinstance(lamp, Source)
assert isinstance(lamp.spectra[0], SourceSpectrum)
assert isinstance(lamp.fields[0], ImageHDU)
assert isinstance(getattr(lamp.fields[0], "field", lamp.fields[0]), ImageHDU)
4 changes: 3 additions & 1 deletion scopesim_templates/tests/test_micado/test_pinhole_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def test_pinhole_mask_returns_source_object(self):

assert isinstance(ph_mask, Source)
assert isinstance(ph_mask.spectra[0], SourceSpectrum)
assert isinstance(ph_mask.fields[0], Table)
assert (isinstance(getattr(ph_mask.fields[0], "field",
ph_mask.fields[0]), Table)
or isinstance(ph_mask.fields[0], Table))

def test_pinhole_with_basic_instrument(self):
dr = np.arange(-25, 26, 5) # [arcsec]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_returns_source_object_with_everything_as_needed(self):
plt.show()

assert isinstance(src.spectra[0], SourceSpectrum)
assert isinstance(src.fields[0], fits.ImageHDU)
assert isinstance(getattr(src.fields[0], "field", src.fields[0]), fits.ImageHDU)

def test_returns_flux_scaled_spectrum(self):
src1 = mic_sc.line_list()
Expand Down
28 changes: 21 additions & 7 deletions scopesim_templates/tests/test_stellar/test_clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,36 @@ def test_it_works(self, basic_cluster):

@pytest.mark.usefixtures("basic_cluster")
def test_spectra_are_correct_type(self, basic_cluster):
assert all(isinstance(spec, Spextrum)
for spec in basic_cluster.spectra)
if (hasattr(basic_cluster.fields[0], "field") and
not isinstance(basic_cluster.fields[0], Table)):
assert all(isinstance(spec, Spextrum)
for spec in basic_cluster.spectra.values())
else:
assert all(isinstance(spec, Spextrum)
for spec in basic_cluster.spectra)

@pytest.mark.usefixtures("basic_cluster")
def test_spectra_waverange(self, basic_cluster):
# TODO: don't know if it makes sense to test like this
wvrng_min = [s.waverange.min().to(u.um).value
for s in basic_cluster.spectra]
wvrng_max = [s.waverange.max().to(u.um).value
for s in basic_cluster.spectra]
if (hasattr(basic_cluster.fields[0], "field") and
not isinstance(basic_cluster.fields[0], Table)):
wvrng_min = [s.waverange.min().to(u.um).value
for s in basic_cluster.spectra.values()]
wvrng_max = [s.waverange.max().to(u.um).value
for s in basic_cluster.spectra.values()]
else:
wvrng_min = [s.waverange.min().to(u.um).value
for s in basic_cluster.spectra]
wvrng_max = [s.waverange.max().to(u.um).value
for s in basic_cluster.spectra]
assert all(wave == approx(0.115) for wave in wvrng_min)
assert all(wave == approx(2.5) for wave in wvrng_max)

@pytest.mark.usefixtures("basic_cluster")
def test_field_is_correct_type(self, basic_cluster):
assert isinstance(basic_cluster.fields[0], Table)
assert (isinstance(getattr(basic_cluster.fields[0], "field",
basic_cluster.fields[0]), Table)
or isinstance(basic_cluster.fields[0], Table))
assert len(basic_cluster.fields[0]) > 0

@pytest.mark.usefixtures("basic_cluster")
Expand Down
11 changes: 8 additions & 3 deletions scopesim_templates/tests/test_stellar/test_stars.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
def source_eq(source_lhs: Source, source_rhs: Source):
"""hacky way to ensure two source"""
eq = len(source_lhs.spectra) == len(source_rhs.spectra)
for spectrum_lhs, spectrum_rhs in zip(source_lhs.spectra,
source_rhs.spectra):
eq = eq and all(spectrum_lhs.waveset == spectrum_rhs.waveset)
if isinstance(source_lhs.spectra, list):
for spectrum_lhs, spectrum_rhs in zip(source_lhs.spectra,
source_rhs.spectra):
eq = eq and all(spectrum_lhs.waveset == spectrum_rhs.waveset)
elif isinstance(source_lhs.spectra, dict):
for spectrum_lhs, spectrum_rhs in zip(source_lhs.spectra.values(),
source_rhs.spectra.values()):
eq = eq and all(spectrum_lhs.waveset == spectrum_rhs.waveset)
return eq


Expand Down
58 changes: 35 additions & 23 deletions scopesim_templates/tests/test_utils/test_validate_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,58 +27,70 @@
# Run all tests on each source indivdually
@pytest.mark.parametrize("src", SOURCE_LIST)
class TestContentOfFields:
"""
Test the <Source>.fields attribute to check that the content is correct
"""
"""Test the <Source>.fields attribute to check that the content is correct."""

def test_is_fields_a_list(self, src):
assert isinstance(src.fields, list)

def test_fields_list_has_only_imagehdus_or_tables(self, src):
for field in src.fields:
assert isinstance(field, (Table, ImageHDU))
if not isinstance(field, Table):
assert isinstance(getattr(field, "field", field), (Table, ImageHDU))

def test_any_tables_have_correct_column_names(self, src):
for field in src.fields:
if isinstance(field, Table):
req_colnames = ["x", "y", "ref", "weight"]
assert all([col in field.colnames for col in req_colnames])
fld = field
else:
if isinstance(getattr(field, "field", field), Table):
fld = getattr(field, "field", field)
else:
continue

req_colnames = ["x", "y", "ref", "weight"]
assert all(col in fld.colnames for col in req_colnames)

def test_any_imagehdus_have_correct_header_keywords(self, src):
for field in src.fields:
if isinstance(field, ImageHDU):
if isinstance(getattr(field, "field", field), ImageHDU):
req_keys = ["SPEC_REF", "NAXIS", "NAXIS1", "NAXIS2",
"CUNIT1", "CUNIT2", "CTYPE1", "CTYPE2", "CDELT1",
"CDELT2", "CRVAL1", "CRVAL2", "CRPIX1", "CRPIX2",
]
assert all([key in field.header for key in req_keys])
"CUNIT1", "CUNIT2", "CTYPE1", "CTYPE2",
"CDELT1", "CDELT2", "CRVAL1", "CRVAL2",
"CRPIX1", "CRPIX2"]
assert all(key in field.header for key in req_keys)


@pytest.mark.parametrize("src", SOURCE_LIST)
class TestContentOfSpectra:
"""
Test the <Source>.spectra attribute to check that all spectra are useful
"""
def test_is_spectra_a_list(self, src):
assert isinstance(src.spectra, list)
"""Test the <Source>.spectra attribute to check that all spectra are useful."""

def test_is_spectra_a_list_or_dict(self, src):
# ScopeSim pre-0.9 is a list, afterwards dict. Allow both here.
assert isinstance(src.spectra, (list, dict))

def test_spectra_list_has_only_synphot_sourcespectrum_objects(self, src):
for spectrum in src.spectra:
assert isinstance(spectrum, SourceSpectrum)
if isinstance(src.spectra, list):
for spectrum in src.spectra:
assert isinstance(spectrum, SourceSpectrum)
elif isinstance(src.spectra, dict):
for spectrum in src.spectra.values():
assert isinstance(spectrum, SourceSpectrum)
else:
raise TypeError(src.spectra)


@pytest.mark.parametrize("src", SOURCE_LIST)
class TestConnectionBetweenFieldsAndSpectra:
"""
Test that all spectra referenced by .fields are in .spectra
"""
"""Test that all spectra referenced by .fields are in .spectra."""

def test_all_spectra_in_table_ref_column_exist(self, src):
for field in src.fields:
if isinstance(field, Table):
if isinstance(getattr(field, "field", field), Table):
for ref in field["ref"]:
assert isinstance(src.spectra[ref], SourceSpectrum)

def test_all_spectra_refereced_in_imagehdu_header_exist(self, src):
for field in src.fields:
if isinstance(field, ImageHDU):
if isinstance(getattr(field, "field", field), ImageHDU):
ref = field.header["SPEC_REF"]
assert isinstance(src.spectra[ref], SourceSpectrum)

0 comments on commit 57d067d

Please sign in to comment.