def test_mag_ab_multi(): from astropy import units from skypy.galaxy.spectrum import mag_ab # 5 redshifts z = np.linspace(0, 1, 5) # 2 Gaussian bandpasses bp_lam = np.logspace(0, 4, 1000) * units.AA bp_mean = np.array([[1000], [2000]]) * units.AA bp_width = np.array([[100], [10]]) * units.AA bp_tx = np.exp(-( (bp_lam - bp_mean) / bp_width)**2) * units.dimensionless_unscaled bp = specutils.Spectrum1D(spectral_axis=bp_lam, flux=bp_tx) # 3 Flat Spectra lam = np.logspace(0, 4, 1000) * units.AA A = np.array([[2], [3], [4]]) flam = A * 0.10884806248538730623 * units.Unit('erg s-1 cm-2 AA') / lam**2 spec = specutils.Spectrum1D(spectral_axis=lam, flux=flam) # Compare calculated magnitudes with truth magnitudes = mag_ab(spec, bp, z) truth = -2.5 * np.log10(A * (1 + z)).T[:, np.newaxis, :] assert magnitudes.shape == (5, 2, 3) np.testing.assert_allclose(*np.broadcast_arrays(magnitudes, truth))
def test_mag_ab_redshift_dependence(): from astropy import units from skypy.galaxy.spectrum import mag_ab # make a wide tophat bandpass bp_lam = np.logspace(-10, 10, 3) * units.AA bp_tx = np.ones(3) * units.dimensionless_unscaled bp = specutils.Spectrum1D(spectral_axis=bp_lam, flux=bp_tx) # create a narrow gaussian source lam = np.logspace(0, 3, 1000) * units.AA flam = np.exp(-((lam - 100 * units.AA) / (10 * units.AA))**2) * units.Unit('erg s-1 cm-2 AA-1') spec = specutils.Spectrum1D(spectral_axis=lam, flux=flam) # array of redshifts z = np.linspace(0, 1, 11) # compute the AB magnitude at different redshifts m = mag_ab(spec, bp, z) # compare with expected redshift dependence np.testing.assert_allclose(m, m[0] - 2.5 * np.log10(1 + z))
def test_template_spectra(): from astropy import units from skypy.galaxy.spectrum import mag_ab, magnitudes_from_templates from astropy.cosmology import Planck15 # 3 Flat Templates lam = np.logspace(0, 4, 1000) * units.AA A = np.array([[2], [3], [4]]) flam = A * 0.10884806248538730623 * units.Unit('erg s-1 cm-2 AA') / lam**2 spec = specutils.Spectrum1D(spectral_axis=lam, flux=flam) # Gaussian bandpass bp_lam = np.logspace(0, 4, 1000) * units.AA bp_tx = np.exp(-((bp_lam - 1000 * units.AA) / (100 * units.AA))**2) * units.dimensionless_unscaled bp = specutils.Spectrum1D(spectral_axis=bp_lam, flux=bp_tx) # Each test galaxy is exactly one of the templates coefficients = np.diag(np.ones(3)) mt = magnitudes_from_templates(coefficients, spec, bp) m = mag_ab(spec, bp) np.testing.assert_allclose(mt, m) # Test distance modulus redshift = np.array([0, 1, 2]) dm = Planck15.distmod(redshift).value mt = magnitudes_from_templates(coefficients, spec, bp, distance_modulus=dm) np.testing.assert_allclose(mt, m + dm) # Test stellar mass sm = np.array([1, 2, 3]) mt = magnitudes_from_templates(coefficients, spec, bp, stellar_mass=sm) np.testing.assert_allclose(mt, m - 2.5 * np.log10(sm)) # Redshift interpolation test; linear interpolation sufficient over a small # redshift range at low relative tolerance z = np.linspace(0, 0.1, 3) m_true = magnitudes_from_templates(coefficients, spec, bp, redshift=z, resolution=4) m_interp = magnitudes_from_templates(coefficients, spec, bp, redshift=z, resolution=2) np.testing.assert_allclose(m_true, m_interp, rtol=1e-2) with pytest.raises(AssertionError): np.testing.assert_allclose(m_true, m_interp, rtol=1e-5)
def test_mag_ab_standard_source(): from astropy import units from specutils import Spectrum1D from skypy.galaxy.spectrum import mag_ab # create a bandpass bp_lam = np.logspace(0, 4, 1000)*units.AA bp_tx = np.exp(-((bp_lam - 1000*units.AA)/(100*units.AA))**2)*units.dimensionless_unscaled bp = Spectrum1D(spectral_axis=bp_lam, flux=bp_tx) # test that the AB standard source has zero magnitude lam = np.logspace(0, 4, 1000)*units.AA flam = 0.10884806248538730623*units.Unit('erg s-1 cm-2 AA')/lam**2 spec = Spectrum1D(spectral_axis=lam, flux=flam) m = mag_ab(spec, bp) assert np.isclose(m, 0)
def test_stellar_mass_from_reference_band(): from astropy import units from skypy.galaxy.spectrum import mag_ab, stellar_mass_from_reference_band from specutils import Spectrum1D # Gaussian bandpass bp_lam = np.logspace(0, 4, 1000) * units.AA bp_mean = 1000 * units.AA bp_width = 100 * units.AA bp_tx = np.exp(-((bp_lam-bp_mean)/bp_width)**2)*units.dimensionless_unscaled band = Spectrum1D(spectral_axis=bp_lam, flux=bp_tx) # 3 Flat template spectra lam = np.logspace(0, 4, 1000)*units.AA A = np.array([[2], [3], [4]]) flam = A * 0.10884806248538730623*units.Unit('erg s-1 cm-2 AA')/lam**2 templates = Spectrum1D(spectral_axis=lam, flux=flam) # Absolute magnitudes for each template Mt = mag_ab(templates, band) # Using the identity matrix for the coefficients yields trivial test cases coeff = np.diag(np.ones(3)) # Using the absolute magnitudes of the templates as reference magnitudes # should return one solar mass for each template. stellar_mass = stellar_mass_from_reference_band(coeff, templates, Mt, band) truth = 1 np.testing.assert_allclose(stellar_mass, truth) # Solution for given magnitudes without template mixing Mb = np.array([10, 20, 30]) stellar_mass = stellar_mass_from_reference_band(coeff, templates, Mb, band) truth = np.power(10, -0.4*(Mb-Mt)) np.testing.assert_allclose(stellar_mass, truth)