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 @@
- 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 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
- 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)
......
......@@ -8,7 +8,7 @@
from functools import lru_cache
from astropy import log
from astropy.cosmology import WMAP7 as cosmo
from ...utils.cosmology import luminosity_distance
import numpy as np
from scipy import optimize
from scipy.constants import parsec
......@@ -48,11 +48,7 @@ def compute_corr_dz(model_z, obs):
Object containing the distance and redshift of an object
"""
if model_z == 0.:
mod_distance = 10. * parsec
else:
mod_distance = cosmo.luminosity_distance(model_z).value * 1e6 * parsec
return (obs.distance / mod_distance) ** 2. * \
return (obs.distance / luminosity_distance(model_z)) ** 2. * \
(1. + model_z) / (1. + obs.redshift)
......
......@@ -3,11 +3,11 @@
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
# Author: Médéric Boquien
from astropy.cosmology import WMAP7 as cosmo
from astropy.table import Column
import numpy as np
from scipy.constants import parsec
from ..utils.cosmology import luminosity_distance
from ..utils.io import read_table
from .utils import get_info
......@@ -299,11 +299,8 @@ class Observation(object):
if 'distance' in row.colnames and np.isfinite(row['distance']):
self.distance = row['distance'] * parsec * 1e6
else:
if self.redshift == 0.:
self.distance = 10. * parsec
elif self.redshift > 0.:
self.distance = cosmo.luminosity_distance(self.redshift).value \
* 1e6 * parsec
if self.redshift >= 0.:
self.distance = luminosity_distance(self.redshift)
else:
self.distance = np.nan
self.flux = {k: row[k] for k in cls.bands
......
......@@ -23,7 +23,7 @@ from collections import OrderedDict
import numpy as np
from scipy.constants import parsec
from scipy.special import factorial
from astropy.cosmology import WMAP7 as cosmology
from ..utils.cosmology import age, luminosity_distance
from . import SedModule
......@@ -163,13 +163,8 @@ class Redshifting(SedModule):
raise Exception(f"The redshift provided is negative "
f"({self.redshift}).")
self.universe_age = cosmology.age(self.redshift).value * 1000.
if self.redshift == 0.:
self.luminosity_distance = 10. * parsec
else:
self.luminosity_distance = (
cosmology.luminosity_distance(self.redshift).value * 1e6 *
parsec)
self.universe_age = age(self.redshift)
self.luminosity_distance = luminosity_distance(self.redshift)
# We do not define the values of the IGM attenuation component yet.
# This is because we need the wavelength grid for that first. This
# 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