{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Reading and writing Cosmology objects\n", "\n", "In this notebook, we show a few usage examples for how to read and write `Cosmology` objects to disk." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from pyccl import Cosmology" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A cosmology including parameters and associated data.\n", "\n", " .. note:: Although some arguments default to `None`, they will raise a\n", " ValueError inside this function if not specified, so they are not\n", " optional.\n", "\n", " .. note:: The parameter Omega_g can be used to set the radiation density\n", " (not including relativistic neutrinos) to zero. Doing this will\n", " give you a model that is physically inconsistent since the\n", " temperature of the CMB will still be non-zero. Note however\n", " that this approximation is common for late-time LSS computations.\n", "\n", " .. note:: BCM stands for the \"baryonic correction model\" of Schneider &\n", " Teyssier (2015; https://arxiv.org/abs/1510.06034). See the\n", " `DESC Note `_\n", " for details.\n", "\n", " Args:\n", " Omega_c (:obj:`float`): Cold dark matter density fraction.\n", " Omega_b (:obj:`float`): Baryonic matter density fraction.\n", " h (:obj:`float`): Hubble constant divided by 100 km/s/Mpc; unitless.\n", " A_s (:obj:`float`): Power spectrum normalization. Exactly one of A_s\n", " and sigma_8 is required.\n", " sigma8 (:obj:`float`): Variance of matter density perturbations at\n", " an 8 Mpc/h scale. Exactly one of A_s and sigma_8 is required.\n", " n_s (:obj:`float`): Primordial scalar perturbation spectral index.\n", " Omega_k (:obj:`float`, optional): Curvature density fraction.\n", " Defaults to 0.\n", " Omega_g (:obj:`float`, optional): Density in relativistic species\n", " except massless neutrinos. The default of `None` corresponds\n", " to setting this from the CMB temperature. Note that if a non-`None`\n", " value is given, this may result in a physically inconsistent model\n", " because the CMB temperature will still be non-zero in the\n", " parameters.\n", " Neff (:obj:`float`, optional): Effective number of massless\n", " neutrinos present. Defaults to 3.046.\n", " m_nu (:obj:`float`, optional): Total mass in eV of the massive\n", " neutrinos present. Defaults to 0.\n", " mnu_type (:obj:`str`, optional): The type of massive neutrinos.\n", " w0 (:obj:`float`, optional): First order term of dark energy equation\n", " of state. Defaults to -1.\n", " wa (:obj:`float`, optional): Second order term of dark energy equation\n", " of state. Defaults to 0.\n", " bcm_log10Mc (:obj:`float`, optional): One of the parameters of the\n", " BCM model. Defaults to `np.log10(1.2e14)`.\n", " bcm_etab (:obj:`float`, optional): One of the parameters of the BCM\n", " model. Defaults to 0.5.\n", " bcm_ks (:obj:`float`, optional): One of the parameters of the BCM\n", " model. Defaults to 55.0.\n", " df_mg (array_like, optional): Perturbations to the GR growth rate as\n", " a function of redshift :math:`\\Delta f`. Used to implement simple\n", " modified growth scenarios.\n", " z_mg (array_like, optional): Array of redshifts corresponding to df_mg.\n", " transfer_function (:obj:`str`, optional): The transfer function to\n", " use. Defaults to 'boltzmann_class'.\n", " matter_power_spectrum (:obj:`str`, optional): The matter power\n", " spectrum to use. Defaults to 'halofit'.\n", " baryons_power_spectrum (:obj:`str`, optional): The correction from\n", " baryonic effects to be implemented. Defaults to 'nobaryons'.\n", " mass_function (:obj:`str`, optional): The mass function to use.\n", " Defaults to 'tinker10' (2010).\n", " halo_concentration (:obj:`str`, optional): The halo concentration\n", " relation to use. Defaults to Duffy et al. (2008) 'duffy2008'.\n", " emulator_neutrinos: `str`, optional): If using the emulator for\n", " the power spectrum, specified treatment of unequal neutrinos.\n", " Options are 'strict', which will raise an error and quit if the\n", " user fails to pass either a set of three equal masses or a sum with\n", " mnu_type = 'sum_equal', and 'equalize', which will redistribute\n", " masses to be equal right before calling the emualtor but results in\n", " internal inconsistencies. Defaults to 'strict'.\n", " \n" ] } ], "source": [ "cosmo = Cosmology(Omega_c=0.25, Omega_b=0.05, sigma8=0.8, h=0.7, n_s=0.96)\n", "print(cosmo.__doc__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Serialization (writing parameters to disk)\n", "\n", "Cosmology objects can be saved to a YAML format using the `write_yaml` method. This format is not currently very robust -- the exact order of the parameters must be maintained or the object cannot be read back in." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Omega_c: 2.500000e-01\r\n", "Omega_b: 5.000000e-02\r\n", "Omega_m: 3.000000e-01\r\n", "Omega_k: 0.000000e+00\r\n", "k_sign: 0\r\n", "w0: -1.000000e+00\r\n", "wa: 0.000000e+00\r\n", "H0: 7.000000e+01\r\n", "h: 7.000000e-01\r\n", "Neff: 3.046000e+00\r\n", "N_nu_mass: 0\r\n", "N_nu_rel: 3.046000e+00\r\n", "sum_nu_masses: 0.000000e+00\r\n", "Omega_n_mass: 0.000000e+00\r\n", "Omega_n_rel: 3.488600e-05\r\n", "A_s: nan\r\n", "n_s: 9.600000e-01\r\n", "Omega_g: 5.043013e-05\r\n", "T_CMB: 2.725000e+00\r\n", "bcm_log10Mc: 1.407918e+01\r\n", "bcm_etab: 5.000000e-01\r\n", "bcm_ks: 5.500000e+01\r\n", "sigma8: 8.000000e-01\r\n", "Omega_l: 6.999147e-01\r\n", "z_star: nan\r\n", "has_mgrowth: 0\r\n", "nz_mgrowth: 0\r\n" ] } ], "source": [ "cosmo.write_yaml('example_params.yaml')\n", "!cat example_params.yaml" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Deserialization (reading parameters from disk)\n", "\n", "The parameters can be read back in using the `read_yaml` *class method*. Note that this must be called on the `Cosmology` class itself, as shown below, and not an instance of the class." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "cosmo2 = Cosmology.read_yaml(\"example_params.yaml\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pyccl.Cosmology(wa=0.0, n_s=0.96, Neff=3.046, sigma8=0.8, w0=-1.0, bcm_etab=0.5, bcm_log10Mc=14.07918, A_s=None, h=0.7, Omega_k=0.0, bcm_ks=55.0, Omega_c=0.25, Omega_b=0.05, Omega_g=None, m_nu=0.0, mnu_type=None, z_mg=None, df_mg=None, matter_power_spectrum='halofit', baryons_power_spectrum='nobaryons', halo_concentration='duffy2008', transfer_function='boltzmann_class', mass_function='tinker10', emulator_neutrinos='strict')\n" ] } ], "source": [ "print(cosmo2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This `Cosmology` object can then be used to obtain cosmological predictions. See the other examples in this directory, for example *Distance Calculations Example.ipynb* or the more comprehensive demo *SLAC Feb2018 Demo.ipynb*." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Using Python Pickle\n", "\n", "`Cosmology` objects are also pickle-able, to make them easy to store on disk and to pass around in MPI environments." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "import pickle\n", "\n", "with open('cosmo.pkl', 'wb') as fp:\n", " pickle.dump(cosmo2, fp)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pyccl.Cosmology(Neff=3.046, A_s=None, wa=0.0, h=0.7, Omega_b=0.05, n_s=0.96, Omega_k=0.0, sigma8=0.8, w0=-1.0, Omega_c=0.25, bcm_etab=0.5, Omega_g=None, bcm_log10Mc=14.07918, bcm_ks=55.0, m_nu=0.0, mnu_type=None, z_mg=None, df_mg=None, matter_power_spectrum='halofit', halo_concentration='duffy2008', mass_function='tinker10', emulator_neutrinos='strict', baryons_power_spectrum='nobaryons', transfer_function='boltzmann_class')\n" ] } ], "source": [ "with open('cosmo.pkl', 'rb') as fp:\n", " cosmo3 = pickle.load(fp)\n", "\n", "print(cosmo3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.15rc1" } }, "nbformat": 4, "nbformat_minor": 2 }