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
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
Refactor kcorrect absolute and apparent magnitude calculations
  • Loading branch information
rrjbca committed Dec 22, 2020
commit 79b5efe78c72c9a003a75692fd03907214db2141
125 changes: 69 additions & 56 deletions skypy/galaxy/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
'dirichlet_coefficients',
'load_spectral_data',
'mag_ab',
'magnitudes_from_templates',
'kcorrect',
'kcorrect_absolute_magnitudes',
'kcorrect_apparent_magnitudes',
]

try:
Expand Down Expand Up @@ -256,33 +256,73 @@ def mag_ab(wavelength, spectrum, filters, *, redshift=None, coefficients=None,
return m


@spectral_data_input(templates=units.Jy)
def magnitudes_from_templates(coefficients, templates, filters, redshift=None,
resolution=1000, stellar_mass=None, distance_modulus=None):
r'''Compute AB magnitudes from template spectra.
def kcorrect_absolute_magnitudes(coefficients, filters, stellar_mass=None):
'''Galaxy AB absolute magnitudes from kcorrect template spectra.

This function calculates photometric AB absolute magnitudes for galaxies
whose spectra are modelled as a linear combination of the kcorrect template
spectra from [1]_ and [2]_.

Parameters
----------
coefficients : (ng, nt) array_like
Array of spectrum coefficients.
filters : str or list of str
Bandpass filter specification for `~speclite.filters.load_filters`.
stellar_mass : (ng,) array_like, optional
Optional array of stellar masses for each galaxy in template units.

Returns
-------
mag_ab : (ng, nf) array_like
The absolute AB magnitude of each object in each filter, where ``nf``
is the number of loaded filters.

References
----------
.. [1] M. R. Blanton et al., 2003, AJ, 125, 2348
.. [2] M. R. Blanton and S. Roweis, 2007, AJ, 125, 2348
'''

# kcorrect data
filename = resource_filename('skypy', 'data/kcorrect/k_nmf_derived.default.fits')
with fits.open(filename) as hdul:
spec = hdul[1].data * units.Unit('erg s-1 cm-2 angstrom-1')
lambda_ = hdul[11].data * units.Unit('angstrom')

# Magnitudes from templates
magnitudes = mag_ab(lambda_, spec, filters, coefficients=coefficients)

mass_modulus = 0 if stellar_mass is None else -2.5 * np.log10(stellar_mass)

return magnitudes + mass_modulus


This function calculates photometric AB magnitudes for objects whose
spectra are modelled as a linear combination of template spectra following
[1]_ and [2]_.
def kcorrect_apparent_magnitudes(coefficients, redshifts, filters, cosmology,
*, stellar_mass=None, resolution=1000):
'''Galaxy AB apparent magnitudes from kcorrect template spectra.

This function calculates photometric AB apparent magnitudes for galaxies
whose spectra are modelled as a linear combination of the kcorrect template
spectra from [1]_ and [2]_.

Parameters
----------
coefficients : (ng, nt) array_like
Array of spectrum coefficients.
templates : spectral_data
Template spectra.
redshifts : (ng,) array_like
Array of redshifts for each galaxy used to calculte the distance
modulus and k-correction.
filters : str or list of str
Bandpass filter specification for `~speclite.filters.load_filters`.
redshift : (ng,) array_like, optional
Optional array of values for redshifting the source spectrum.
cosmology : Cosmology
Astropy Cosmology object to calculate distance modulus.
stellar_mass : (ng,) array_like, optional
Optional array of stellar masses for each galaxy in template units.
resolution : integer, optional
Redshift resolution for intepolating magnitudes. Default is 1000. If
the number of objects is less than resolution their magnitudes are
calculated directly without interpolation.
stellar_mass : (ng,) array_like, optional
Optional array of stellar masses for each galaxy in template units.
distance_modulus : (ng,) array_like, optional
Optional array of distance moduli for each galaxy.

Returns
-------
Expand All @@ -296,16 +336,20 @@ def magnitudes_from_templates(coefficients, templates, filters, redshift=None,
.. [2] M. R. Blanton and S. Roweis, 2007, AJ, 125, 2348
'''

# combine coefficients and stellar masses, if given
if stellar_mass is not None:
coefficients = (coefficients.T * stellar_mass).T
# kcorrect data
filename = resource_filename('skypy', 'data/kcorrect/k_nmf_derived.default.fits')
with fits.open(filename) as hdul:
spec = hdul[1].data * units.Unit('erg s-1 cm-2 angstrom-1')
lambda_ = hdul[11].data * units.Unit('angstrom')

# Magnitudes from templates
magnitudes = mag_ab(lambda_, spec, filters, redshift=redshifts,
coefficients=coefficients, interpolate=resolution)

# compute AB magnitudes
magnitudes = mag_ab(templates.wavelength, templates.flux, filters,
redshift=redshift, coefficients=coefficients,
distmod=distance_modulus, interpolate=resolution)
distance_modulus = cosmology.distmod(redshifts).value
mass_modulus = 0 if stellar_mass is None else -2.5 * np.log10(stellar_mass)

return magnitudes
return magnitudes + distance_modulus + mass_modulus


@spectral_data_input(templates=units.Jy)
Expand Down Expand Up @@ -403,34 +447,3 @@ def load_spectral_data(name):

# run the loader
return loader(*args, *groups)


def kcorrect(coefficients, filters, redshifts, cosmology, reference_magnitudes=None,
reference_filter=None, stellar_mass=None, resolution=1000):
'''Galaxy photometry from kcorrect spectrum templates.

'''

# kcorrect data
filename = resource_filename('skypy', 'data/kcorrect/k_nmf_derived.default.fits')
with fits.open(filename) as hdul:
spec = hdul[1].data
lambda_ = hdul[11].data

# Magnitudes from templates
m = mag_ab(lambda_, spec, filters, redshift=redshifts, interpolate=resolution)

# Distance Modulus
distance_modulus = cosmology.distmod(redshifts) if cosmology else 0

# Mass modulus from reference magnitude or stellar_mass
if stellar_mass is None and reference_magnitudes is not None and reference_filter is not None:
mass_modulus = reference_magnitudes
mass_modulus -= mag_ab(lambda_, spec, filters, coefficients=coefficients, interpolate=resolution)
elif stellar_mass is not None and reference_magnitudes is not None and reference_filter is not None:
mass_modulus = -2.5 * np.log10(stellar_mass)
else:
mass_modulus = 0

# Sum over templates
return np.sum(coefficients * m, axis=1) + distance_modulus + mass_modulus