Skip to content

Commit

Permalink
Address more PR comments
Browse files Browse the repository at this point in the history
Fix the distance kind as "luminosity"
  • Loading branch information
jeremykubica committed Jul 15, 2024
1 parent bc4848a commit bd09398
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 18 deletions.
13 changes: 3 additions & 10 deletions src/tdastro/astro_utils/cosmology.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from tdastro.base_models import FunctionNode


def redshift_to_distance(redshift, cosmology, kind="comoving"):
def redshift_to_distance(redshift, cosmology):
"""Compute a source's luminosity distance given its redshift and a
specified cosmology using astropy's redshift_distance().
Expand All @@ -14,17 +14,14 @@ def redshift_to_distance(redshift, cosmology, kind="comoving"):
The redshift value.
cosmology : `astropy.cosmology`
The cosmology specification.
kind : `str`
The distance type for the Equivalency as defined by
astropy.cosmology.units.redshift_distance.
Returns
-------
distance : `float`
The luminosity distance (in pc)
"""
z = redshift * cu.redshift
distance = z.to(u.pc, cu.redshift_distance(cosmology, kind=kind))
distance = z.to(u.pc, cu.redshift_distance(cosmology, kind="luminosity"))
return distance.value


Expand All @@ -45,18 +42,14 @@ class RedshiftDistFunc(FunctionNode):
The function or constant providing the redshift value.
cosmology : `astropy.cosmology`
The cosmology specification.
kind : `str`
The distance type for the Equivalency as defined by
astropy.cosmology.units.redshift_distance.
"""

def __init__(self, redshift, cosmology, kind="comoving"):
def __init__(self, redshift, cosmology):
# Call the super class's constructor with the needed information.
super().__init__(
func=redshift_to_distance,
redshift=redshift,
cosmology=cosmology,
kind=kind,
)

def __str__(self):
Expand Down
11 changes: 5 additions & 6 deletions tests/tdastro/astro_utils/test_cosmology.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import pytest
from astropy.cosmology import WMAP9, Planck18
from tdastro.astro_utils.cosmology import redshift_to_distance


def test_redshift_to_distance():
"""Test that we can convert the redshift to a distance using a given cosmology."""
# Use the example from:
# https://docs.astropy.org/en/stable/api/astropy.cosmology.units.redshift_distance.html
assert redshift_to_distance(1100, cosmology=WMAP9) == pytest.approx(14004.03157418 * 1e6)
wmap9_val = redshift_to_distance(1100, cosmology=WMAP9)
planck18_val = redshift_to_distance(1100, cosmology=Planck18)

# Try the Planck18 cosmology.
assert redshift_to_distance(1100, cosmology=Planck18) == pytest.approx(13886.327957 * 1e6)
assert abs(planck18_val - wmap9_val) > 1000.0
assert 13.0 * 1e12 < wmap9_val < 16.0 * 1e12
assert 13.0 * 1e12 < planck18_val < 16.0 * 1e12
3 changes: 1 addition & 2 deletions tests/tdastro/sources/test_physical_models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pytest
from astropy.cosmology import Planck18
from tdastro.sources.physical_model import PhysicalModel

Expand All @@ -17,7 +16,7 @@ def test_physical_model():
assert model2.ra == 1.0
assert model2.dec == 2.0
assert model2.redshift == 1100.0
assert model2.distance == pytest.approx(13886.327957 * 1e6)
assert 13.0 * 1e12 < model2.distance < 16.0 * 1e12

# Neither distance nor redshift are specified.
model3 = PhysicalModel(ra=1.0, dec=2.0)
Expand Down

0 comments on commit bd09398

Please sign in to comment.