Commit 027a891c authored by Médéric Boquien's avatar Médéric Boquien

The computation of all the cosmology-dependent quantities has been...

The computation of all the cosmology-dependent quantities has been consolidated in pcigale/utils/cosmology.py and optimised. This leads to a slightly faster startup, in particular when there are many objects to fit, and it makes it easier to change the cosmology.
parent 9fb807a3
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
- Some lines have been made slightly thicker in SED plots so the different components are more visible. (Médéric Boquien) - Some lines have been made slightly thicker in SED plots so the different components are more visible. (Médéric Boquien)
- The colours in the SED plots have been tweaked for aesthetic reasons. (Médéric Boquien) - The colours in the SED plots have been tweaked for aesthetic reasons. (Médéric Boquien)
- The markers for the observed fluxes in the SED plots have been tweaked to improve readability. (Médéric Boquien) - The markers for the observed fluxes in the SED plots have been tweaked to improve readability. (Médéric Boquien)
- The computation of all the cosmology-dependent quantities has been consolidated in pcigale/utils/cosmology.py and optimised. This leads to a slightly faster startup, in particular when there are many objects to fit, and it makes it easier to change the cosmology. (Médéric Boquien)
### Fixed ### Fixed
- Make sure we can plot the PDF of equivalent widths. (Médéric Boquien) - Make sure we can plot the PDF of equivalent widths. (Médéric Boquien)
- Fix a crash when generating a mock catalogue containing intensive properties. (Médéric Boquien) - Fix a crash when generating a mock catalogue containing intensive properties. (Médéric Boquien)
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
from functools import lru_cache from functools import lru_cache
from astropy import log from astropy import log
from astropy.cosmology import WMAP7 as cosmo from ...utils.cosmology import luminosity_distance
import numpy as np import numpy as np
from scipy import optimize from scipy import optimize
from scipy.constants import parsec from scipy.constants import parsec
...@@ -48,11 +48,7 @@ def compute_corr_dz(model_z, obs): ...@@ -48,11 +48,7 @@ def compute_corr_dz(model_z, obs):
Object containing the distance and redshift of an object Object containing the distance and redshift of an object
""" """
if model_z == 0.: return (obs.distance / luminosity_distance(model_z)) ** 2. * \
mod_distance = 10. * parsec
else:
mod_distance = cosmo.luminosity_distance(model_z).value * 1e6 * parsec
return (obs.distance / mod_distance) ** 2. * \
(1. + model_z) / (1. + obs.redshift) (1. + model_z) / (1. + obs.redshift)
......
...@@ -3,11 +3,11 @@ ...@@ -3,11 +3,11 @@
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt # Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
# Author: Médéric Boquien # Author: Médéric Boquien
from astropy.cosmology import WMAP7 as cosmo
from astropy.table import Column from astropy.table import Column
import numpy as np import numpy as np
from scipy.constants import parsec from scipy.constants import parsec
from ..utils.cosmology import luminosity_distance
from ..utils.io import read_table from ..utils.io import read_table
from .utils import get_info from .utils import get_info
...@@ -299,11 +299,8 @@ class Observation(object): ...@@ -299,11 +299,8 @@ class Observation(object):
if 'distance' in row.colnames and np.isfinite(row['distance']): if 'distance' in row.colnames and np.isfinite(row['distance']):
self.distance = row['distance'] * parsec * 1e6 self.distance = row['distance'] * parsec * 1e6
else: else:
if self.redshift == 0.: if self.redshift >= 0.:
self.distance = 10. * parsec self.distance = luminosity_distance(self.redshift)
elif self.redshift > 0.:
self.distance = cosmo.luminosity_distance(self.redshift).value \
* 1e6 * parsec
else: else:
self.distance = np.nan self.distance = np.nan
self.flux = {k: row[k] for k in cls.bands self.flux = {k: row[k] for k in cls.bands
......
...@@ -23,7 +23,7 @@ from collections import OrderedDict ...@@ -23,7 +23,7 @@ from collections import OrderedDict
import numpy as np import numpy as np
from scipy.constants import parsec from scipy.constants import parsec
from scipy.special import factorial from scipy.special import factorial
from astropy.cosmology import WMAP7 as cosmology from ..utils.cosmology import age, luminosity_distance
from . import SedModule from . import SedModule
...@@ -163,13 +163,8 @@ class Redshifting(SedModule): ...@@ -163,13 +163,8 @@ class Redshifting(SedModule):
raise Exception(f"The redshift provided is negative " raise Exception(f"The redshift provided is negative "
f"({self.redshift}).") f"({self.redshift}).")
self.universe_age = cosmology.age(self.redshift).value * 1000. self.universe_age = age(self.redshift)
if self.redshift == 0.: self.luminosity_distance = luminosity_distance(self.redshift)
self.luminosity_distance = 10. * parsec
else:
self.luminosity_distance = (
cosmology.luminosity_distance(self.redshift).value * 1e6 *
parsec)
# We do not define the values of the IGM attenuation component yet. # We do not define the values of the IGM attenuation component yet.
# This is because we need the wavelength grid for that first. This # This is because we need the wavelength grid for that first. This
# will be assigned on the first call. # will be assigned on the first call.
......
# -*- coding: utf-8 -*-
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
from functools import lru_cache
from astropy.cosmology import WMAP7 as _cosmo
from scipy.constants import parsec
@lru_cache(maxsize=None)
def luminosity_distance(redshift):
"""Computes the luminosity distance in m for a given redshift. If the
redshift is 0, then we assume a distance of 10 pc.
Parameter
---------
redshift: scalar
Redshift of the object
Returns
-------
luminosity_distance: scalar
Luminosity distance to the object in m
"""
if redshift > 0:
return _cosmo.luminosity_distance(redshift).value * 1e6 * parsec
return 10. * parsec
@lru_cache(maxsize=None)
def age(redshift):
"""Computes the age of the universe in Myr at a given redshift.
Parameter
---------
redshift: scalar
Redshift of the object
Returns
-------
age: float
Age of the universe in Myr at the corresponding redshift
"""
return _cosmo.age(redshift).value * 1000.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment