from_mapping

astropy.cosmology.io.mapping.from_mapping(map, *, move_to_meta=False, cosmology=None)[source]

Load Cosmology from mapping object.

Parameters
mapmapping

Arguments into the class – like “name” or “meta”. If ‘cosmology’ is None, must have field “cosmology” which can be either the string name of the cosmology class (e.g. “FlatLambdaCDM”) or the class itself.

move_to_metabool (optional, keyword-only)

Whether to move keyword arguments that are not in the Cosmology class’ signature to the Cosmology’s metadata. This will only be applied if the Cosmology does NOT have a keyword-only argument (e.g. **kwargs). Arguments moved to the metadata will be merged with existing metadata, preferring specified metadata in the case of a merge conflict (e.g. for Cosmology(meta={'key':10}, key=42), the Cosmology.meta will be {'key': 10}).

cosmologystr, Cosmology class, or None (optional, keyword-only)

The cosmology class (or string name thereof) to use when constructing the cosmology instance. The class also provides default parameter values, filling in any non-mandatory arguments missing in ‘map’.

Returns
Cosmology subclass instance

Examples

To see loading a Cosmology from a dictionary with from_mapping, we will first make a mapping using to_format().

>>> from astropy.cosmology import Cosmology, Planck18
>>> cm = Planck18.to_format('mapping')
>>> cm
{'cosmology': <class 'astropy.cosmology.flrw.FlatLambdaCDM'>,
 'name': 'Planck18', 'H0': <Quantity 67.66 km / (Mpc s)>, 'Om0': 0.30966,
 'Tcmb0': <Quantity 2.7255 K>, 'Neff': 3.046,
 'm_nu': <Quantity [0. , 0. , 0.06] eV>, 'Ob0': 0.04897,
 'meta': ...

Now this dict can be used to load a new cosmological instance identical to the Planck18 cosmology from which it was generated.

>>> cosmo = Cosmology.from_format(cm, format="mapping")
>>> cosmo
FlatLambdaCDM(name="Planck18", H0=67.7 km / (Mpc s), Om0=0.31,
              Tcmb0=2.725 K, Neff=3.05, m_nu=[0. 0. 0.06] eV, Ob0=0.049)

Specific cosmology classes can be used to parse the data. The class’ default parameter values are used to fill in any information missing in the data.

>>> from astropy.cosmology import FlatLambdaCDM
>>> del cm["Tcmb0"]  # show FlatLambdaCDM provides default
>>> FlatLambdaCDM.from_format(cm)
FlatLambdaCDM(name="Planck18", H0=67.7 km / (Mpc s), Om0=0.31,
              Tcmb0=0 K, Neff=3.05, m_nu=None, Ob0=0.049)