Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor galaxy photometry from kcorrect templates #386

Merged
merged 22 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c8f4435
Galaxy photometry using kcorrect spectrum templates
rrjbca Dec 1, 2020
bd01959
kcorrect template data
rrjbca Dec 1, 2020
ae3a246
kcorrect license
rrjbca Dec 1, 2020
04dc940
Fix typos
rrjbca Dec 3, 2020
79b5efe
Refactor kcorrect absolute and apparent magnitude calculations
rrjbca Dec 22, 2020
5069a20
Refactor stellar mass calculation for kcorrect templates
rrjbca Dec 23, 2020
414df2e
Refactor mag_ab to return fluxes of templates
rrjbca Dec 23, 2020
a93ffdd
Refactor unit tests for ab_maggies_redshift
rrjbca Jan 5, 2021
6947a9b
Update spectrum module function list in documentation
rrjbca Jan 5, 2021
578c060
Factor out generalised template magnitude functions
rrjbca Jan 12, 2021
e2e2092
Docstrings for template functions
rrjbca Jan 18, 2021
382d95f
Fix docstring for ab_maggies_redshift
rrjbca Jan 18, 2021
6354cec
Refactor test_kcorrect_stellar_mass
rrjbca Jan 18, 2021
eda94db
Merge branch 'master' into kcorrect
rrjbca Jan 18, 2021
eea828c
test_kcorrect_magnitudes
rrjbca Jan 18, 2021
175197d
Fix failing tests
rrjbca Jan 18, 2021
728d608
Test incorrect number of coefficients raises an exception
rrjbca Jan 18, 2021
fb561f9
Revert mag_ab; fix handling of coefficients
rrjbca Jan 18, 2021
2a74d6d
Fix stellar_mass parameter; add unit tests
rrjbca Jan 18, 2021
1a73608
GalaxyTemplateBase and kcorrect classes
rrjbca Jan 18, 2021
5b2881b
kcorrect is an instance of KCorrectTemplates class
rrjbca Jan 19, 2021
76a214d
Abstract method is NotImplemented for purpose of test coverage
rrjbca Jan 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Factor out generalised template magnitude functions
  • Loading branch information
rrjbca committed Jan 12, 2021
commit 578c06047436218b0b7bee5ac6899556fa945c81
36 changes: 25 additions & 11 deletions skypy/galaxy/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
'dirichlet_coefficients',
'load_spectral_data',
'ab_maggies_redshift',
'template_absolute_magnitudes',
'template_apparent_magnitudes',
'kcorrect_absolute_magnitudes',
'kcorrect_apparent_magnitudes',
'kcorrect_stellar_mass',
Expand Down Expand Up @@ -219,6 +221,26 @@ def ab_maggies_redshift(wavelength, spectrum, filters, redshift, interpolate=100
return m * np.reshape((1 + redshift), np.shape(redshift) + (1,)*(nd_s+nd_f))


def template_absolute_magnitudes(lambda_, spec, coefficients, filters, stellar_mass=None):

flux = ab_maggies_redshift(lambda_, spec, filters, 0)
flux = np.sum((coefficients.T * flux.T).T, axis=1)
mass_modulus = 0 if stellar_mass is None else -2.5 * np.log10(stellar_mass)

return (-2.5*np.log10(flux).T + mass_modulus).T


def template_apparent_magnitudes(lambda_, spec, coefficients, redshift, filters, cosmology,
*, stellar_mass=None, resolution=1000):

flux = ab_maggies_redshift(lambda_, spec, filters, redshift, resolution)
flux = np.sum((coefficients.T * flux.T).T, axis=1)
distance_modulus = cosmology.distmod(redshift).value
mass_modulus = 0 if stellar_mass is None else -2.5 * np.log10(stellar_mass)

return (-2.5*np.log10(flux).T + distance_modulus + mass_modulus).T


def kcorrect_absolute_magnitudes(coefficients, filters, stellar_mass=None):
'''Galaxy AB absolute magnitudes from kcorrect template spectra.

Expand Down Expand Up @@ -253,11 +275,7 @@ def kcorrect_absolute_magnitudes(coefficients, filters, stellar_mass=None):
spec = hdul[1].data * units.Unit('erg s-1 cm-2 angstrom-1')
lambda_ = hdul[11].data * units.Unit('angstrom')

flux = ab_maggies_redshift(lambda_, spec, filters, 0)
flux = np.sum((coefficients.T * flux.T).T, axis=1)
mass_modulus = 0 if stellar_mass is None else -2.5 * np.log10(stellar_mass)

return (-2.5*np.log10(flux).T + mass_modulus).T
return template_absolute_magnitudes(lambda_, spec, coefficients, filters, stellar_mass)


def kcorrect_apparent_magnitudes(coefficients, redshift, filters, cosmology,
Expand Down Expand Up @@ -304,12 +322,8 @@ def kcorrect_apparent_magnitudes(coefficients, redshift, filters, cosmology,
spec = hdul[1].data * units.Unit('erg s-1 cm-2 angstrom-1')
lambda_ = hdul[11].data * units.Unit('angstrom')

flux = ab_maggies_redshift(lambda_, spec, filters, redshift, resolution)
flux = np.sum((coefficients.T * flux.T).T, axis=1)
distance_modulus = cosmology.distmod(redshift).value
mass_modulus = 0 if stellar_mass is None else -2.5 * np.log10(stellar_mass)

return (-2.5*np.log10(flux).T + distance_modulus + mass_modulus).T
return template_apparent_magnitudes(lambda_, spec, coefficients, redshift, filters,
cosmology, stellar_mass, resolution)


def kcorrect_stellar_mass(coefficients, magnitudes, filter):
Expand Down
32 changes: 16 additions & 16 deletions skypy/galaxy/tests/test_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,47 +156,47 @@ def test_ab_maggies_redshift_multi():
def test_template_spectra():

from astropy import units
from skypy.galaxy.spectrum import mag_ab, magnitudes_from_templates
from skypy.galaxy.spectrum import ab_maggies_redshift, template_absolute_magnitudes, template_apparent_magnitudes
from astropy.cosmology import Planck15
from specutils import Spectrum1D
from speclite.filters import FilterResponse

# 3 Flat Templates
lam = np.logspace(-1, 4, 1000)*units.AA
A = np.array([[2], [3], [4]])
flam = A * 0.10885464149979998*units.Unit('erg s-1 cm-2 AA')/lam**2
spec = Spectrum1D(spectral_axis=lam, flux=flam)

# Gaussian bandpass
filt_lam = np.logspace(0, 4, 1000)*units.AA
filt_tx = np.exp(-((filt_lam - 1000*units.AA)/(100*units.AA))**2)
filt_tx[[0, -1]] = 0
filt = FilterResponse(wavelength=filt_lam, response=filt_tx,
meta=dict(group_name='test', band_name='filt'))
FilterResponse(wavelength=filt_lam, response=filt_tx,
meta=dict(group_name='test', band_name='filt'))

# Each test galaxy is exactly one of the templates
coefficients = np.eye(3)
mt = magnitudes_from_templates(coefficients, spec, 'test-filt')
m = mag_ab(lam, flam, 'test-filt')
np.testing.assert_allclose(mt, m)

# Test distance modulus
# Absolute Magnitudes
mt = ab_maggies_redshift(lam, flam, 'test-filt', 0)
m = template_absolute_magnitudes(lam, flam, coefficients, 'test-filt')
np.testing.assert_allclose(-2.5*np.log10(mt), m)

# Apparent Magnitudes
redshift = np.array([1, 2, 3])
dm = Planck15.distmod(redshift).value
mt = magnitudes_from_templates(coefficients, spec, 'test-filt', distance_modulus=dm)
np.testing.assert_allclose(mt, m + dm)
mt = template_apparent_magnitudes(lam, flam, coefficients, redshift, 'test-filt', Planck15)
np.testing.assert_allclose(mt, m - 2.5*np.log10(1+redshift) + dm)

# Test stellar mass
sm = np.array([1, 2, 3])
mt = magnitudes_from_templates(coefficients, spec, 'test-filt', stellar_mass=sm)
mt = template_absolute_magnitudes(lam, flam, coefficients, 'test-filt', 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, 'test-filt', redshift=z, resolution=4)
m_interp = magnitudes_from_templates(coefficients, spec, 'test-filt', redshift=z, resolution=2)
np.testing.assert_allclose(m_true, m_interp, rtol=1e-2)
z = np.linspace(0.1, 0.2, 3)
m_true = template_apparent_magnitudes(lam, flam, coefficients, z, 'test-filt', Planck15, resolution=4)
m_interp = template_apparent_magnitudes(lam, flam, coefficients, z, 'test-filt', Planck15, resolution=2)
np.testing.assert_allclose(m_true, m_interp, rtol=1e-7)
assert not np.all(m_true == m_interp)


Expand Down