Package: cosmopower
Overview•Documentation•Installation•Getting Started•Training•Trained Models•Likelihoods•Support•CitationOverviewCosmoPoweris a library for Machine Learning - accelerated Bayesian inference. While the emphasis is on building algorithms to accelerate Bayesian inference incosmology, the interdisciplinary nature of the methodologies implemented in the package allows for their application across a wide range of scientific fields. The ultimate goal ofCosmoPoweris to solveinverseproblems in science, by developing Bayesian inference pipelines that leverage the computational power of Machine Learning to accelerate the inference process. This approach represents a principled application of Machine Learning to scientific research, with the Machine Learning component embedded within a rigorous framework for uncertainty quantification.In cosmology,CosmoPoweraims to become a fullydifferentiablelibrary for cosmological analyses. Currently,CosmoPowerprovides neural network emulators of matter and Cosmic Microwave Background power spectra. These emulators can be used to replace Boltzmann codes such asCAMBorCLASSin cosmological inference pipelines, to source the power spectra needed for two-point statistics analyses. This provides orders-of-magnitude acceleration to the inference pipeline and integrates naturally with efficient techniques for sampling very high-dimensional parameter spaces. The power spectra emulators implemented inCosmoPower, and first presented in itsrelease paper, have been applied to the analysis of real cosmological data from  experiments, as well as having been tested against the accuracy requirements for the analysis of next-generation cosmological surveys.CosmoPoweris written entirely inPython. Neural networks are implemented using theTensorFlowlibrary.DocumentationComprehensive documentation is availablehere.InstallationWe recommend installingCosmoPowerwithin aCondavirtual environment.
For example, to create and activate an environment calledcp_env, use:conda create -n cp_env python=3.7 pip && conda activate cp_envOnce inside the environment, you can installCosmoPower:from PyPIpip install cosmopowerTo test the installation, you can usepython3 -c 'import cosmopower as cp'If you do not have a GPU on your machine, you will see a warning message about it which you can safely ignore.from sourcegit clone https://github.com/alessiospuriomancini/cosmopower
  cd cosmopower
  pip install .To test the installation, you can usepytestGetting StartedCosmoPower currently provides two ways to emulate power spectra, implemented in the classescosmopower_NNandcosmopower_PCAplusNN:cosmopower_NNcosmopower_PCAplusNNa neural network mapping cosmological parameters directly to (log)-power spectraa neural network mapping cosmological parameters to coefficients of a Principal Component Analysis (PCA) of the (log)-power spectraBelow you can find minimal working examples that useCosmoPowerpre-trained models from thecode release paper, shared in thetrained_modelsfolder (see theTrained modelssection for details) to predict power spectra for a given set of input parameters. You need to clone the repository and replace/path/to/cosmopowerwith the location of the cloned repository to make these examples work. Further examples are available as demo notebooks in thegetting_started_notebooksfolder, for bothcosmopower_NN() andcosmopower_PCAplusNN().Note that, whenever possible, we recommend working with models trained onlog-power spectra, to reduce the dynamic range. Bothcosmopower_NNandcosmopower_PCAplusNNhave methods to provide predictions (cf.cp_pca_nn.predictions_npin the example below) as well as "10^predictions" (cf.cp_nn.ten_to_predictions_npin the example below).Usingcosmopower_NNUsingcosmopower_PCAplusNNimportcosmopowerascp# load pre-trained NN model: maps cosmological parameters to CMB TT log-C_ellcp_nn=cp.cosmopower_NN(restore=True,restore_filename='/path/to/cosmopower'\+'/cosmopower/trained_models/CP_paper/CMB/cmb_TT_NN')# create a dict of cosmological parametersparams={'omega_b':[0.0225],'omega_cdm':[0.113],'h':[0.7],'tau_reio':[0.055],'n_s':[0.96],'ln10^{10}A_s':[3.07],}# predictions (= forward pass through the network) -> 10^predictionsspectra=cp_nn.ten_to_predictions_np(params)importcosmopowerascp# load pre-trained PCA+NN model: maps cosmological parameters to CMB TE C_ellcp_pca_nn=cp.cosmopower_PCAplusNN(restore=True,restore_filename='/path/to/cosmopower'\+'/cosmopower/trained_models/CP_paper/CMB/cmb_TE_PCAplusNN')# create a dict of cosmological parametersparams={'omega_b':[0.0225],'omega_cdm':[0.113],'h':[0.7],'tau_reio':[0.055],'n_s':[0.96],'ln10^{10}A_s':[3.07],}# predictions (= forward pass through the network)spectra=cp_pca_nn.predictions_np(params)Note that the suffix_npof thepredictions_npandten_to_predictions_npfunctions refer to their implementation usingNumPy. These functions are best suited to standard analysis pipelines fully implemented in normal Python, normally run on Central Processing Units. For pipelines built using theTensorFlowlibrary, highly optimised to run on Graphics Processing Units, we recommend the use of the corresponding_tffunctions (i.e.predictions_tfandten_to_predictions_tf) in bothcosmopower_NNandcosmopower_PCAplusNN(seeLikelihoodsfor further details and examples).TrainingThetraining_notebooksfolder contains examples of how to:traincosmopower_NN:traincosmopower_PCAplusNN:These notebooks implement emulation of CMB temperature (TT) and lensing potential ()
power spectra as practical examples - the procedure is completely analogous for the matter power spectrum.Trained ModelsTrained models are available in thetrained_modelsfolder. The folder contains all of the emulators used in theCosmoPower release paper; as new models are trained, they will be shared in this folder, along with a description and BibTex entry of the relevant paper to be cited when using these models. Please consider sharing your own model in this folder with a pull request!Please refer to theREADMEfile within thetrained_modelsfolder for all of the details on the models contained there.LikelihoodsThelikelihoodsfolder contains examples of likelihood codes sourcing power spectra fromCosmoPower. Some of these likelihoods are written in pureTensorFlow, hence they can be run with highly optimisedTensorFlow-based samplers, such as the ones fromTensorFlow Probability. Being written entirely inTensorFlow, these codes can be massively accelerated by running on Graphics or Tensor Processing Units. We recommend the use of thepredictions_tfandten_to_predictions_tffunctions within these pipelines, to compute (log)-power spectra predictions for input parameters. Thelikelihoods_notebooksfolder contains anexampleof how to run a pure-Tensorflow likelihood, the Planck-lite 2018 TTTEEE likelihood.Contributing, Support, CommunityFor bugs and feature requests consider using theissue tracker.Contributions to the code via pull requests are most welcome!For general support, please send an email toa dot spuriomancini at ucl dot ac dot uk, or post onGitHub discussions.Users ofCosmoPowerare strongly encouraged to join theGitHub discussionsforum to follow the latest news on the code as well as to discuss all things Machine Learning / Bayesian Inference in cosmology!CitationIf you useCosmoPowerat any point in your work please cite itsrelease paper:@article{SpurioMancini2022,
         title={CosmoPower: emulating cosmological power spectra for accelerated Bayesian inference from next-generation surveys},
         volume={511},
         ISSN={1365-2966},
         url={http://dx.doi.org/10.1093/mnras/stac064},
         DOI={10.1093/mnras/stac064},
         number={2},
         journal={Monthly Notices of the Royal Astronomical Society},
         publisher={Oxford University Press (OUP)},
         author={Spurio Mancini, Alessio and Piras, Davide and Alsing, Justin and Joachimi, Benjamin and Hobson, Michael P},
         year={2022},
         month={Jan},
         pages={1771–1788}
         }If you use a specific likelihood or trained model then in addition to therelease paperpleasealsocite their relevant papers (always listed in the corresponding directory).LicenseCosmoPoweris released under the GPL-3 license (seeLICENSE) subject to
the non-commercial use condition (seeLICENSE_EXT).CosmoPower
Copyright (C) 2021 A. Spurio Mancini & contributors

This program is released under the GPL-3 license (see LICENSE), 
subject to a non-commercial use condition (see LICENSE_EXT).

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.