Cosmology

class astropy.cosmology.Cosmology(*args, **kwargs)[source]

Bases: object

Base-class for all Cosmologies.

Parameters
*args

Arguments into the cosmology; used by subclasses, not this base class.

namestr or None (optional, keyword-only)

The name of the cosmology.

metadict or None (optional, keyword-only)

Metadata for the cosmology, e.g., a reference.

**kwargs

Arguments into the cosmology; used by subclasses, not this base class.

Notes

Class instances are static – you cannot (and should not) change the values of the parameters. That is, all of the above attributes (except meta) are read only.

For details on how to create performant custom subclasses, see the documentation on Speeding up Integrals in Custom Cosmologies.

Attributes Summary

from_format

Transform object to a Cosmology.

meta

name

The name of the Cosmology instance.

read

Read and parse data to a Cosmology.

to_format

Transform this Cosmology to another format.

write

Write this Cosmology object out in the specified format.

Methods Summary

clone(*[, meta])

Returns a copy of this object with updated parameters, as specified.

is_equivalent(other)

Check equivalence between Cosmologies.

Attributes Documentation

from_format

Transform object to a Cosmology.

This function provides the Cosmology interface to the Astropy unified I/O layer. This allows easily parsing supported data formats using syntax such as:

>>> from astropy.cosmology import Cosmology
>>> cosmo1 = Cosmology.from_format(cosmo_mapping, format='mapping')

When the from_format method is called from a subclass the subclass will provide a keyword argument cosmology=<class> to the registered parser. The method uses this cosmology class, regardless of the class indicated in the data, and sets parameters’ default values from the class’ signature.

Get help on the available readers using the help() method:

>>> Cosmology.from_format.help()  # Get help and list supported formats
>>> Cosmology.from_format.help('<format>')  # Get detailed help on a format
>>> Cosmology.from_format.list_formats()  # Print list of available formats

See also: https://docs.astropy.org/en/stable/io/unified.html

Note

from_format() and read() currently access the same registry. This will be deprecated and formats intended for read should not be used here. Use Cosmology.to_format.help() to confirm that the format may be used to convert to a Cosmology.

Parameters
objobject

The object to parse according to ‘format’

*args

Positional arguments passed through to data parser.

formatstr (optional, keyword-only)

Object format specifier.

**kwargs

Keyword arguments passed through to data parser.

Returns
outCosmology subclass instance

Cosmology corresponding to obj contents.

meta
name

The name of the Cosmology instance.

read

Read and parse data to a Cosmology.

This function provides the Cosmology interface to the Astropy unified I/O layer. This allows easily reading a file in supported data formats using syntax such as:

>>> from astropy.cosmology import Cosmology
>>> cosmo1 = Cosmology.read('<file name>')

When the read method is called from a subclass the subclass will provide a keyword argument cosmology=<class> to the registered read method. The method uses this cosmology class, regardless of the class indicated in the file, and sets parameters’ default values from the class’ signature.

Get help on the available readers using the help() method:

>>> Cosmology.read.help()  # Get help reading and list supported formats
>>> Cosmology.read.help(format='<format>')  # Get detailed help on a format
>>> Cosmology.read.list_formats()  # Print list of available formats

See also: https://docs.astropy.org/en/stable/io/unified.html

Note

read() and from_format() currently access the same registry. This will be deprecated and formats intended for from_format should not be used here. Use Cosmology.read.help() to confirm that the format may be used to read a file.

Parameters
*args

Positional arguments passed through to data reader. If supplied the first argument is typically the input filename.

formatstr (optional, keyword-only)

File format specifier.

**kwargs

Keyword arguments passed through to data reader.

Returns
outCosmology subclass instance

Cosmology corresponding to file contents.

to_format

Transform this Cosmology to another format.

This function provides the Cosmology interface to the astropy unified I/O layer. This allows easily transforming to supported data formats using syntax such as:

>>> from astropy.cosmology import Planck18
>>> Planck18.to_format("mapping")
{'cosmology': astropy.cosmology.core.FlatLambdaCDM,
 'name': 'Planck18',
 'H0': <Quantity 67.66 km / (Mpc s)>,
 'Om0': 0.30966,
 ...

Get help on the available representations for Cosmology using the help() method:

>>> Cosmology.to_format.help()  # Get help and list supported formats
>>> Cosmology.to_format.help('<format>')  # Get detailed help on format
>>> Cosmology.to_format.list_formats()  # Print list of available formats

Note

to_format() and write() currently access the same registry. This will be deprecated and formats intended for write should not be used here. Use Cosmology.to_format.help() to confirm that the format may be used to convert a Cosmology.

Parameters
formatstr

Format specifier.

*args

Positional arguments passed through to data writer. If supplied the first argument is the output filename.

**kwargs

Keyword arguments passed through to data writer.

write

Write this Cosmology object out in the specified format.

This function provides the Cosmology interface to the astropy unified I/O layer. This allows easily writing a file in supported data formats using syntax such as:

>>> from astropy.cosmology import Planck18
>>> Planck18.write('<file name>')

Get help on the available writers for Cosmology using the help() method:

>>> Cosmology.write.help()  # Get help writing and list supported formats
>>> Cosmology.write.help(format='<format>')  # Get detailed help on format
>>> Cosmology.write.list_formats()  # Print list of available formats

Note

write() and to_format() currently access the same registry. This will be deprecated and formats intended for to_format should not be used here. Use Cosmology.write.help() to confirm that the format may be used to write to a file.

Parameters
*args

Positional arguments passed through to data writer. If supplied the first argument is the output filename.

formatstr (optional, keyword-only)

File format specifier.

**kwargs

Keyword arguments passed through to data writer.

Methods Documentation

clone(*, meta=None, **kwargs)[source]

Returns a copy of this object with updated parameters, as specified.

This cannot be used to change the type of the cosmology, so clone() cannot be used to change between flat and non-flat cosmologies.

Parameters
metamapping or None (optional, keyword-only)

Metadata that will update the current metadata.

**kwargs

Cosmology parameter (and name) modifications. If any parameter is changed and a new name is not given, the name will be set to “[old name] (modified)”.

Returns
newcosmoCosmology subclass instance

A new instance of this class with updated parameters as specified. If no modifications are requested, then a reference to this object is returned instead of copy.

Examples

To make a copy of the Planck13 cosmology with a different matter density (Om0), and a new name:

>>> from astropy.cosmology import Planck13
>>> newcosmo = Planck13.clone(name="Modified Planck 2013", Om0=0.35)

If no name is specified, the new name will note the modification.

>>> Planck13.clone(Om0=0.35).name
'Planck13 (modified)'
is_equivalent(other)[source]

Check equivalence between Cosmologies.

Two cosmologies may be equivalent even if not the same class. For example, an instance of LambdaCDM might have \(\Omega_0=1\) and \(\Omega_k=0\) and therefore be flat, like FlatLambdaCDM.

Parameters
otherCosmology subclass instance

The object in which to compare.

Returns
bool

True if cosmologies are equivalent, False otherwise.