From c3d4dff2deb1277edfce607923fa907e706df5ec Mon Sep 17 00:00:00 2001 From: teutoburg Date: Sat, 16 Nov 2024 20:51:53 +0100 Subject: [PATCH] Remove patchy getattrs now that ScopeSim v0.9.0 is here This was just a temporary workaround anyway, now there's no longer a need for it. --- .../test_calibration/test_calibration.py | 9 ++---- .../tests/test_micado/test_cluster.py | 3 +- .../tests/test_micado/test_darkness.py | 3 +- .../tests/test_micado/test_flatlamp.py | 2 +- .../tests/test_micado/test_pinhole_mask.py | 4 +-- .../test_micado/test_spectral_calibrations.py | 2 +- .../tests/test_stellar/test_clusters.py | 28 +++++------------ .../tests/test_stellar/test_stars.py | 11 ++----- .../tests/test_utils/test_validate_content.py | 30 ++++++------------- 9 files changed, 27 insertions(+), 65 deletions(-) diff --git a/scopesim_templates/tests/test_calibration/test_calibration.py b/scopesim_templates/tests/test_calibration/test_calibration.py index ade6f48..6d292bc 100644 --- a/scopesim_templates/tests/test_calibration/test_calibration.py +++ b/scopesim_templates/tests/test_calibration/test_calibration.py @@ -13,8 +13,7 @@ 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(getattr(sky.fields[0], "field", sky.fields[0]), Table) - or isinstance(sky.fields[0], Table)) + assert isinstance(sky.fields[0].field, Table) assert sky.fields[0]["ref"][0] == 0 @@ -23,8 +22,7 @@ def test_flat_field_returns_source_object(self): flatfield = calibration.calibration.flat_field() assert isinstance(flatfield, Source) assert isinstance(flatfield.spectra[0], SourceSpectrum) - assert (isinstance(getattr(flatfield.fields[0], "field", flatfield.fields[0]), ImageHDU) - or isinstance(flatfield.fields[0], ImageHDU)) + assert isinstance(flatfield.fields[0].field, ImageHDU) class TestLamp: @@ -40,5 +38,4 @@ def test_lamp_returns_source_object(self): ) assert isinstance(lamp, Source) assert isinstance(lamp.spectra[0], SourceSpectrum) - assert (isinstance(getattr(lamp.fields[0], "field", lamp.fields[0]), ImageHDU) - or isinstance(lamp.fields[0], ImageHDU)) + assert isinstance(lamp.fields[0].field, ImageHDU) diff --git a/scopesim_templates/tests/test_micado/test_cluster.py b/scopesim_templates/tests/test_micado/test_cluster.py index 9093e74..3d59a6d 100644 --- a/scopesim_templates/tests/test_micado/test_cluster.py +++ b/scopesim_templates/tests/test_micado/test_cluster.py @@ -15,5 +15,4 @@ def test_cluster_returns_source_object(self): sky = cluster() assert isinstance(sky, Source) assert isinstance(sky.spectra[0], SourceSpectrum) - assert (isinstance(getattr(sky.fields[0], "field", sky.fields[0]), Table) - or isinstance(sky.fields[0], Table)) + assert isinstance(sky.fields[0].field, Table) diff --git a/scopesim_templates/tests/test_micado/test_darkness.py b/scopesim_templates/tests/test_micado/test_darkness.py index 29a9b0a..72e40aa 100644 --- a/scopesim_templates/tests/test_micado/test_darkness.py +++ b/scopesim_templates/tests/test_micado/test_darkness.py @@ -15,6 +15,5 @@ def test_darkness_returns_source_object(self): sky = darkness() assert isinstance(sky, Source) assert isinstance(sky.spectra[0], SourceSpectrum) - assert (isinstance(getattr(sky.fields[0], "field", sky.fields[0]), Table) - or isinstance(sky.fields[0], Table)) + assert isinstance(sky.fields[0].field, Table) assert sky.fields[0]["ref"][0] == 0 diff --git a/scopesim_templates/tests/test_micado/test_flatlamp.py b/scopesim_templates/tests/test_micado/test_flatlamp.py index 85a20c5..d2b59f4 100644 --- a/scopesim_templates/tests/test_micado/test_flatlamp.py +++ b/scopesim_templates/tests/test_micado/test_flatlamp.py @@ -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(getattr(lamp.fields[0], "field", lamp.fields[0]), ImageHDU) + assert isinstance(lamp.fields[0].field, ImageHDU) diff --git a/scopesim_templates/tests/test_micado/test_pinhole_mask.py b/scopesim_templates/tests/test_micado/test_pinhole_mask.py index 019846d..5590d63 100644 --- a/scopesim_templates/tests/test_micado/test_pinhole_mask.py +++ b/scopesim_templates/tests/test_micado/test_pinhole_mask.py @@ -24,9 +24,7 @@ def test_pinhole_mask_returns_source_object(self): assert isinstance(ph_mask, Source) assert isinstance(ph_mask.spectra[0], SourceSpectrum) - assert (isinstance(getattr(ph_mask.fields[0], "field", - ph_mask.fields[0]), Table) - or isinstance(ph_mask.fields[0], Table)) + assert isinstance(ph_mask.fields[0].field, Table) def test_pinhole_with_basic_instrument(self): dr = np.arange(-25, 26, 5) # [arcsec] diff --git a/scopesim_templates/tests/test_micado/test_spectral_calibrations.py b/scopesim_templates/tests/test_micado/test_spectral_calibrations.py index 3be1b11..3cb5a61 100644 --- a/scopesim_templates/tests/test_micado/test_spectral_calibrations.py +++ b/scopesim_templates/tests/test_micado/test_spectral_calibrations.py @@ -25,7 +25,7 @@ def test_returns_source_object_with_everything_as_needed(self): plt.show() assert isinstance(src.spectra[0], SourceSpectrum) - assert isinstance(getattr(src.fields[0], "field", src.fields[0]), fits.ImageHDU) + assert isinstance(src.fields[0].field, fits.ImageHDU) def test_returns_flux_scaled_spectrum(self): src1 = mic_sc.line_list() diff --git a/scopesim_templates/tests/test_stellar/test_clusters.py b/scopesim_templates/tests/test_stellar/test_clusters.py index 68a2623..08a7b9a 100644 --- a/scopesim_templates/tests/test_stellar/test_clusters.py +++ b/scopesim_templates/tests/test_stellar/test_clusters.py @@ -24,36 +24,22 @@ def test_it_works(self, basic_cluster): @pytest.mark.usefixtures("basic_cluster") def test_spectra_are_correct_type(self, basic_cluster): - 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) + assert all(isinstance(spec, Spextrum) + for spec in basic_cluster.spectra.values()) @pytest.mark.usefixtures("basic_cluster") def test_spectra_waverange(self, basic_cluster): # TODO: don't know if it makes sense to test like this - 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] + 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()] 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(getattr(basic_cluster.fields[0], "field", - basic_cluster.fields[0]), Table) - or isinstance(basic_cluster.fields[0], Table)) + assert isinstance(basic_cluster.fields[0].field, Table) assert len(basic_cluster.fields[0]) > 0 @pytest.mark.usefixtures("basic_cluster") diff --git a/scopesim_templates/tests/test_stellar/test_stars.py b/scopesim_templates/tests/test_stellar/test_stars.py index 4b27a6a..b6353b5 100644 --- a/scopesim_templates/tests/test_stellar/test_stars.py +++ b/scopesim_templates/tests/test_stellar/test_stars.py @@ -11,14 +11,9 @@ def source_eq(source_lhs: Source, source_rhs: Source): """hacky way to ensure two source""" eq = len(source_lhs.spectra) == len(source_rhs.spectra) - 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) + 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 diff --git a/scopesim_templates/tests/test_utils/test_validate_content.py b/scopesim_templates/tests/test_utils/test_validate_content.py index ec9e18d..033aa57 100644 --- a/scopesim_templates/tests/test_utils/test_validate_content.py +++ b/scopesim_templates/tests/test_utils/test_validate_content.py @@ -35,24 +35,18 @@ def test_is_fields_a_list(self, src): def test_fields_list_has_only_imagehdus_or_tables(self, src): for field in src.fields: if not isinstance(field, Table): - assert isinstance(getattr(field, "field", field), (Table, ImageHDU)) + assert isinstance(field.field, (Table, ImageHDU)) def test_any_tables_have_correct_column_names(self, src): for field in src.fields: - if isinstance(field, Table): - fld = field - else: - if isinstance(getattr(field, "field", field), Table): - fld = getattr(field, "field", field) - else: - continue - + if not isinstance(field, Table): + continue req_colnames = ["x", "y", "ref", "weight"] - assert all(col in fld.colnames for col in req_colnames) + assert all(col in field.field.colnames for col in req_colnames) def test_any_imagehdus_have_correct_header_keywords(self, src): for field in src.fields: - if isinstance(getattr(field, "field", field), ImageHDU): + if isinstance(field.field, ImageHDU): req_keys = ["SPEC_REF", "NAXIS", "NAXIS1", "NAXIS2", "CUNIT1", "CUNIT2", "CTYPE1", "CTYPE2", "CDELT1", "CDELT2", "CRVAL1", "CRVAL2", @@ -69,14 +63,8 @@ def test_is_spectra_a_list_or_dict(self, src): assert isinstance(src.spectra, (list, dict)) def test_spectra_list_has_only_synphot_sourcespectrum_objects(self, src): - 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) + for spectrum in src.spectra.values(): + assert isinstance(spectrum, SourceSpectrum) @pytest.mark.parametrize("src", SOURCE_LIST) @@ -85,12 +73,12 @@ class TestConnectionBetweenFieldsAndSpectra: def test_all_spectra_in_table_ref_column_exist(self, src): for field in src.fields: - if isinstance(getattr(field, "field", field), Table): + if isinstance(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(getattr(field, "field", field), ImageHDU): + if isinstance(field.field, ImageHDU): ref = field.header["SPEC_REF"] assert isinstance(src.spectra[ref], SourceSpectrum)