Skip to content

Commit 4a852cb

Browse files
authored
Changing fsky to sky_area (#314)
1 parent 50ee934 commit 4a852cb

File tree

7 files changed

+55
-34
lines changed

7 files changed

+55
-34
lines changed

docs/pipeline/examples/lightcone.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ tables:
1010
phi_star: 0.0017
1111
alpha: -0.5
1212
m_lim: 25
13-
fsky: 1.0e-6
13+
sky_area: 0.05 deg2

examples/gravitational_wave_rates.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cosmology: !astropy.cosmology.default_cosmology.get
22
z_range: !numpy.arange [0, 2.01, 0.1]
33
mag_lim: 30
4-
fsky: 0.0000242407
4+
sky_area: 1 deg2
55
M_star_red: !astropy.modeling.models.Linear1D [-0.70798041, -20.37196157]
66
phi_star_red: !astropy.modeling.models.Exponential1D [0.0035097, -1.41649]
77
alpha_red: -0.5
@@ -16,7 +16,7 @@ tables:
1616
phi_star: $phi_star_red
1717
alpha: $alpha_red
1818
m_lim: $mag_lim
19-
fsky: $fsky
19+
sky_area: $sky_area
2020
magnitude: !skypy.galaxy.luminosity.schechter_lf_magnitude
2121
redshift: $merger_rates.redshift
2222
M_star: $M_star_red

examples/mccl_galaxies.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cosmology: !astropy.cosmology.FlatLambdaCDM
33
Om0: 0.307
44
z_range: !numpy.arange [0, 2.01, 0.1]
55
mag_lim: 30
6-
fsky: 0.0000242407
6+
sky_area: 1 deg2
77
M_star_red: !astropy.modeling.models.Linear1D [-0.70798041, -20.37196157]
88
phi_star_red: !astropy.modeling.models.Exponential1D [0.0035097, -1.41649]
99
alpha_red: -0.5
@@ -20,7 +20,7 @@ tables:
2020
phi_star: $phi_star_red
2121
alpha: $alpha_red
2222
m_lim: $mag_lim
23-
fsky: $fsky
23+
sky_area: $sky_area
2424
spectral_coefficients: !skypy.galaxy.spectrum.dirichlet_coefficients
2525
alpha0: [2.461, 2.358, 2.568, 2.268, 2.402]
2626
alpha1: [2.410, 2.340, 2.200, 2.540, 2.464]
@@ -47,7 +47,7 @@ tables:
4747
phi_star: $phi_star_blue
4848
alpha: $alpha_blue
4949
m_lim: $mag_lim
50-
fsky: $fsky
50+
sky_area: $sky_area
5151
spectral_coefficients: !skypy.galaxy.spectrum.dirichlet_coefficients
5252
alpha0: [2.079, 3.524, 1.917, 1.992, 2.536]
5353
alpha1: [2.265, 3.862, 1.921, 1.685, 2.480]

skypy/galaxy/_schechter.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@
55
from ..utils import uses_default_cosmology
66
from .redshift import schechter_lf_redshift
77
from .luminosity import schechter_lf_magnitude
8-
8+
from astropy import units
99

1010
__all__ = [
1111
'schechter_lf',
1212
]
1313

1414

1515
@uses_default_cosmology
16-
def schechter_lf(redshift, M_star, phi_star, alpha, m_lim, fsky, cosmology, noise=True):
16+
@units.quantity_input(sky_area=units.sr)
17+
def schechter_lf(redshift, M_star, phi_star, alpha, m_lim, sky_area, cosmology, noise=True):
1718
r'''Sample redshifts and magnitudes from a Schechter luminosity function.
1819
1920
Sample the redshifts and magnitudes of galaxies following a Schechter
2021
luminosity function with potentially redshift-dependent parameters, limited
21-
by an apparent magnitude `m_lim`, for a fraction `fsky` of the sky.
22+
by an apparent magnitude `m_lim`, for a sky area `sky_area`.
2223
2324
Parameters
2425
----------
@@ -37,8 +38,8 @@ def schechter_lf(redshift, M_star, phi_star, alpha, m_lim, fsky, cosmology, nois
3738
values for each `redshift`, or a function of redshift.
3839
m_lim : float
3940
Limiting apparent magnitude.
40-
fsky : array_like
41-
Sky fraction over which galaxies are sampled.
41+
sky_area : `~astropy.units.Quantity`
42+
Sky area over which galaxies are sampled. Must be in units of solid angle.
4243
cosmology : Cosmology, optional
4344
Cosmology object to convert apparent to absolute magnitudes. If not
4445
given, the default cosmology is used.
@@ -61,7 +62,7 @@ def schechter_lf(redshift, M_star, phi_star, alpha, m_lim, fsky, cosmology, nois
6162
'''
6263

6364
# sample galaxy redshifts
64-
z = schechter_lf_redshift(redshift, M_star, phi_star, alpha, m_lim, fsky, cosmology, noise)
65+
z = schechter_lf_redshift(redshift, M_star, phi_star, alpha, m_lim, sky_area, cosmology, noise)
6566

6667
# if a function is NOT given for M_star, phi_star, alpha, interpolate to z
6768
if not callable(M_star) and np.ndim(M_star) > 0:

skypy/galaxy/redshift.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import numpy as np
88
import scipy.integrate
99
import scipy.special
10+
from astropy import units
1011

1112
from ..utils import uses_default_cosmology, broadcast_arguments, dependent_argument
1213

@@ -77,12 +78,14 @@ def smail(z_median, alpha, beta, size=None):
7778
@dependent_argument('phi_star', 'redshift')
7879
@dependent_argument('alpha', 'redshift')
7980
@broadcast_arguments('redshift', 'M_star', 'phi_star', 'alpha')
80-
def schechter_lf_redshift(redshift, M_star, phi_star, alpha, m_lim, fsky, cosmology, noise=True):
81+
@units.quantity_input(sky_area=units.sr)
82+
def schechter_lf_redshift(redshift, M_star, phi_star, alpha, m_lim, sky_area,
83+
cosmology, noise=True):
8184
r'''Sample redshifts from Schechter luminosity function.
8285
8386
Sample the redshifts of galaxies following a Schechter luminosity function
8487
with potentially redshift-dependent parameters, limited by an apparent
85-
magnitude `m_lim`, for a fraction `fsky` of the sky.
88+
magnitude `m_lim`, for a sky area `sky_area`.
8689
8790
Parameters
8891
----------
@@ -101,8 +104,8 @@ def schechter_lf_redshift(redshift, M_star, phi_star, alpha, m_lim, fsky, cosmol
101104
values for each `redshift`, or a function of redshift.
102105
m_lim : float
103106
Limiting apparent magnitude.
104-
fsky : array_like
105-
Sky fraction over which galaxies are sampled.
107+
sky_area : `~astropy.units.Quantity`
108+
Sky area over which galaxies are sampled. Must be in units of solid angle.
106109
cosmology : Cosmology, optional
107110
Cosmology object to convert apparent to absolute magnitudes. If not
108111
given, the default cosmology is used.
@@ -123,11 +126,13 @@ def schechter_lf_redshift(redshift, M_star, phi_star, alpha, m_lim, fsky, cosmol
123126
the sky.
124127
125128
>>> from skypy.galaxy.redshift import schechter_lf_redshift
129+
>>> from astropy import units
126130
>>> z = [0., 5.]
127131
>>> M_star = -20.5
128132
>>> phi_star = 3.5e-3
129133
>>> alpha = -1.3
130-
>>> z_gal = schechter_lf_redshift(z, M_star, phi_star, alpha, 22, 1/41253)
134+
>>> sky_area = 1*units.deg**2
135+
>>> z_gal = schechter_lf_redshift(z, M_star, phi_star, alpha, 22, sky_area)
131136
132137
'''
133138

@@ -150,16 +155,17 @@ def f(lnx, a):
150155

151156
# sample redshifts from the comoving density
152157
return redshifts_from_comoving_density(redshift=redshift, density=density,
153-
fsky=fsky, cosmology=cosmology, noise=noise)
158+
sky_area=sky_area, cosmology=cosmology, noise=noise)
154159

155160

156161
@uses_default_cosmology
157-
def redshifts_from_comoving_density(redshift, density, fsky, cosmology, noise=True):
162+
@units.quantity_input(sky_area=units.sr)
163+
def redshifts_from_comoving_density(redshift, density, sky_area, cosmology, noise=True):
158164
r'''Sample redshifts from a comoving density function.
159165
160166
Sample galaxy redshifts such that the resulting distribution matches a past
161167
lightcone with comoving galaxy number density `density` at redshifts
162-
`redshift`. The comoving volume sampled corresponds to a sky fraction `fsky`
168+
`redshift`. The comoving volume sampled corresponds to a sky area `sky_area`
163169
and transverse comoving distance given by the cosmology `cosmology`.
164170
165171
If the `noise` parameter is set to true, the number of galaxies has Poisson
@@ -171,8 +177,8 @@ def redshifts_from_comoving_density(redshift, density, fsky, cosmology, noise=Tr
171177
Redshifts at which comoving number densities are provided.
172178
density : array_like
173179
Comoving galaxy number density at each redshift in Mpc-3.
174-
fsky : array_like
175-
Sky fraction over which galaxies are sampled.
180+
sky_area : `~astropy.units.Quantity`
181+
Sky area over which galaxies are sampled. Must be in units of solid angle.
176182
cosmology : Cosmology, optional
177183
Cosmology object for conversion to comoving volume. If not given, the
178184
default cosmology is used.
@@ -191,15 +197,16 @@ def redshifts_from_comoving_density(redshift, density, fsky, cosmology, noise=Tr
191197
redshift 1 for a survey of 1 square degree = 1/41253 of the sky.
192198
193199
>>> from skypy.galaxy.redshift import redshifts_from_comoving_density
200+
>>> from astropy import units
194201
>>> z_range = np.arange(0, 1.01, 0.1)
195-
>>> z_gal = redshifts_from_comoving_density(z_range, 1e-3, 1/41253)
202+
>>> sky_area = 1*units.deg**2
203+
>>> z_gal = redshifts_from_comoving_density(z_range, 1e-3, sky_area)
196204
197205
'''
198206

199207
# redshift number density
200-
dN_dz = cosmology.differential_comoving_volume(redshift).to_value('Mpc3/sr')
208+
dN_dz = (cosmology.differential_comoving_volume(redshift) * sky_area).to_value('Mpc3')
201209
dN_dz *= density
202-
dN_dz *= 4*np.pi*fsky
203210

204211
# integrate density to get expected number of galaxies
205212
N = np.trapz(dN_dz, redshift)

skypy/galaxy/tests/test_redshift.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
@pytest.mark.flaky
77
def test_schechter_lf_redshift():
88

9-
from skypy.galaxy.redshift import schechter_lf_redshift, redshifts_from_comoving_density
9+
from skypy.galaxy.redshift import schechter_lf_redshift
1010
from astropy.cosmology import FlatLambdaCDM
11+
from astropy import units
1112
from scipy.special import gamma, gammaincc
1213

1314
# fix this cosmology
@@ -19,10 +20,10 @@ def test_schechter_lf_redshift():
1920
phi_star = 1e-3
2021
alpha = -0.5
2122
m_lim = 30.
22-
fsky = 1/41253
23+
sky_area = 1.0 * units.deg**2
2324

2425
# sample redshifts
25-
z_gal = schechter_lf_redshift(z, M_star, phi_star, alpha, m_lim, fsky, cosmo, noise=False)
26+
z_gal = schechter_lf_redshift(z, M_star, phi_star, alpha, m_lim, sky_area, cosmo, noise=False)
2627

2728
# the absolute magnitude limit as function of redshift
2829
M_lim = m_lim - cosmo.distmod(z).value
@@ -34,7 +35,7 @@ def test_schechter_lf_redshift():
3435
density = phi_star*gamma(alpha+1)*gammaincc(alpha+1, x_min)
3536

3637
# turn into galaxies/surface area
37-
density *= 4*np.pi*fsky*cosmo.differential_comoving_volume(z).to_value('Mpc3/sr')
38+
density *= (sky_area * cosmo.differential_comoving_volume(z)).to_value('Mpc3')
3839

3940
# integrate total number
4041
n_gal = np.trapz(density, z, axis=-1)
@@ -58,6 +59,7 @@ def test_redshifts_from_comoving_density():
5859

5960
from skypy.galaxy.redshift import redshifts_from_comoving_density
6061
from astropy.cosmology import LambdaCDM
62+
from astropy import units
6163

6264
# random cosmology
6365
H0 = np.random.uniform(50, 100)
@@ -69,10 +71,10 @@ def test_redshifts_from_comoving_density():
6971
Ngal = 1000
7072
redshift = np.arange(0.0, 2.001, 0.1)
7173
density = Ngal/cosmo.comoving_volume(redshift[-1]).to_value('Mpc3')
72-
fsky = 1.0
74+
sky_area = 4 * np.pi * units.steradian
7375

7476
# sample galaxies over full sky without Poisson noise
75-
z_gal = redshifts_from_comoving_density(redshift, density, fsky, cosmo, noise=False)
77+
z_gal = redshifts_from_comoving_density(redshift, density, sky_area, cosmo, noise=False)
7678

7779
# make sure number of galaxies is correct (no noise)
7880
assert np.isclose(len(z_gal), Ngal, atol=1, rtol=0)
@@ -82,6 +84,16 @@ def test_redshifts_from_comoving_density():
8284
D, p = kstest(z_gal, lambda z: cosmo.comoving_volume(z).to_value('Mpc3')/V_max)
8385
assert p > 0.01, 'D = {}, p = {}'.format(D, p)
8486

87+
# Test that an error is raised if sky_area has the wrong units
88+
sky_area = 1 * units.degree
89+
with pytest.raises(units.UnitsError):
90+
redshifts_from_comoving_density(redshift, density, sky_area, cosmo, noise=False)
91+
92+
# Test that an error is raised if sky_area has no units
93+
sky_area = 1
94+
with pytest.raises(TypeError):
95+
redshifts_from_comoving_density(redshift, density, sky_area, cosmo, noise=False)
96+
8597

8698
@pytest.mark.flaky
8799
def test_smail():

skypy/galaxy/tests/test_schechter.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def test_schechter_lf():
77

88
from pytest import raises
99
from skypy.galaxy import schechter_lf
10+
from astropy import units
1011

1112
# redshift and magnitude distributions are tested separately
1213
# only test that output is consistent here
@@ -17,10 +18,10 @@ def test_schechter_lf():
1718
phi_star = 1e-3
1819
alpha = -0.5
1920
m_lim = 30.
20-
fsky = 1/41253
21+
sky_area = 1.0 * units.deg**2
2122

2223
# sample redshifts and magnitudes
23-
z_gal, M_gal = schechter_lf(z, M_star, phi_star, alpha, m_lim, fsky)
24+
z_gal, M_gal = schechter_lf(z, M_star, phi_star, alpha, m_lim, sky_area)
2425

2526
# check length
2627
assert len(z_gal) == len(M_gal)
@@ -31,4 +32,4 @@ def test_schechter_lf():
3132
# sample s.t. arrays need to be interpolated
3233
# alpha array not yet supported
3334
with raises(NotImplementedError):
34-
z_gal, M_gal = schechter_lf(z, M_star, phi_star, alpha, m_lim, fsky)
35+
z_gal, M_gal = schechter_lf(z, M_star, phi_star, alpha, m_lim, sky_area)

0 commit comments

Comments
 (0)