Cosmic Linear Anisotropy Solving System with Machine Learning Accelerated and Accurate CMB, LSS, and Halo Model Observables Computations.
Class_sz is compatible with Jax and now allows for automatic differentiation on some of its output, see here for an example on the matter power spectrum. The code can now be used in Hamiltonian Monte Carlo and Simulation Based Inference pipelines.
Check our evolving documentation.
To install the code, run:
pip install classy_sz
By default, the neural nets emulators (~1GB of files) will be installed in your home directory. If you're working on a computing cluster or prefer to store the data elsewhere, you can specify a custom directory.
To specify where you want to store the neural nets data, run the following command in your terminal before importing the package:
export PATH_TO_CLASS_SZ_DATA=/path/to/store/class_sz/data
mkdir -p $PATH_TO_CLASS_SZ_DATA/class_sz_data_directory
This command sets the PATH_TO_CLASS_SZ_DATA
variable for the current session.
To ensure this variable is set every time you open a terminal, you can add this line to your ~/.bashrc
or ~/.bash_profile
file automatically using the echo
command.
For ~/.bashrc
(common for most Linux systems), type in your terminal:
echo -e "\n# Set path for CLASS-SZ data\nexport PATH_TO_CLASS_SZ_DATA=/path/to/store/class_sz/data" >> ~/.bashrc
echo -e "\n# Create directory for CLASS-SZ data\nmkdir -p \$PATH_TO_CLASS_SZ_DATA/class_sz_data_directory" >> ~/.bashrc
To apply the changes immediately:
source ~/.bashrc
(If you use macOS, use .bash_profile
instead of bashrc
, replace accordingly above.)
Now, every time you open a terminal, the PATH_TO_CLASS_SZ_DATA
environment variable will automatically be set to your specified directory, ensuring the neural nets emulators are always stored in the correct location.
(You may also take a loook at our legacy example notebooks, although these are no longer maintained as we move the material to the docs.)
Have a look at the notebooks, there are loads of examples. Because the code is still evolving, we apologize that some of the examples may not run fully smoothly. We shall update our notebook toolbox by the end of 2025.
The idea is:
from classy_sz import Class as Class_sz
class_sz = Class_sz()
class_sz.set({'output':'tSZ_1h'}) # ask for cross-correlations, tsz, etc.
class_sz.compute()
To run the machine learning accelerated computations, the idea is to change:
class_sz.compute()
to:
class_sz.compute_class_szfast()
In a bit more details, say you are interested in CMB
from classy_sz import Class as Class_sz
cosmo_params = {
'omega_b': 0.02242,
'omega_cdm': 0.11933,
'H0': 67.66,
'tau_reio': 0.0561,
'ln10^{10}A_s': 3.047,
'n_s': 0.9665,
'N_ncdm': 1,
'N_ur': 2.0328,
'm_ncdm': 0.06
}
class_sz = Class_sz()
class_sz.set(cosmo_params)
class_sz.set({
'output':'tCl,lCl,pCl',
'skip_background_and_thermo': 1, # do you want exact solution for background? yes: 1, no: 0 (if "no" you can access exact background quantities via emulators).
})
class_sz.compute_class_szfast()
lensed_cls = class_sz.lensed_cl()
l_fast = lensed_cls['ell']
cl_tt_fast = lensed_cls['tt']
cl_ee_fast = lensed_cls['ee']
cl_te_fast = lensed_cls['te']
cl_pp_fast = lensed_cls['pp']
CLASS_SZ is as fast as it gets, with full parallelization, implementation of high-accuracy neural network emulators, and Fast Fourier Transforms.
CLASS_SZ has been built as an extension of Julien Lesgourgues's CLASS code, therefore the halo model and LSS calculations (essentially based on distances and matter clustering) are always consistent with the cosmological model computed by CLASS. We are doing our best to keep up with CLASS version updates. We are currently working on updating to CLASS v3.
CLASS_SZ is initially based on Eiichiro Komatsu’s Fortran code SZFAST.
CLASS_SZ's outputs are regularly cross-checked with other CMBxLSS codes, such as:
The class_sz code is public.
If you use it, please cite:
- CLASS_SZ: I Overview (Bolliet et al. 2024)
- Projected-field kinetic Sunyaev-Zel'dovich Cross-correlations: halo model and forecasts (Bolliet et al. 2023)
If you use accelerated computations, please cite:
- High-accuracy emulators for observables in LCDM, Neff+LCDM, Mnu+LCDM and wCDM cosmologies (Bolliet et al. 2023)
- COSMOPOWER: emulating cosmological power spectra for accelerated Bayesian inference from next-generation surveys (Spurio Mancini et al. 2021)
If you use thermal SZ power spectrum and cluster counts calculations, please cite:
- Including massive neutrinos in thermal Sunyaev Zeldovich power spectrum and cluster counts analyses (Bolliet et al. 2020)
- Dark Energy from the Thermal Sunyaev Zeldovich Power Spectrum (Bolliet et al. 2017)
- The Sunyaev-Zel'dovich angular power spectrum as a probe of cosmological parameters (Komatsu and Seljak, 2002)
In all these cases, please also cite the original CLASS papers:
- CLASS I: Overview (Lesgourgues, 2011)
- CLASS II: Approximation schemes (Blas, Lesgourgues, Tram, 2011)
As well as other references listed here: http://class-code.net
If you are a developer, you may need to modify the C code and the python wrapper to implement your own functions, or modify existing ones.
CLASS_SZ functionalities are located in the files:
- source/class_sz.c for the main CLASS_SZ functions,
- tools/class_sz_tools.c for other useful routines,
- source/class_sz_clustercounts.c for tSZ cluster counts. Since March 2024, CLASS_SZ cluster counts calculations are superseded by cosmocnc (Zubeldia & Bolliet 2024).
And importantly, in the python and cython files:
- python/classy.pyx for the Python wrapper,
- classy_szfast/classy_szfast.py for the Python wrapper for the emulators,
- classy_szfast/classy_sz.py for the Python wrapper for cobaya,
- classy_szfast/cosmosis_classy_szfast_interface.py for the Python wrapper for cosmosis.
To install the C executable, so you can run the C code, you should install from source and compile:
Clean up and compile:
$ git clone https://github.com/CLASS-SZ/class_sz
$ git clone https://github.com/CLASS-SZ/get_cosmopower_emus.git
$ cd get_cosmopower_emus
$ pip install -e .
$ cd ..
$ git clone https://github.com/CLASS-SZ/class_sz_data.git
$ cd class_sz_data
$ pip install -e .
$ cd ..
$ cd class_sz/class-sz/python
$ git clone https://github.com/CLASS-SZ/classy_szfast
$ cd ..
$ chmod +x select_makefile.sh
$ ./select_makefile.sh
$ chmod +x download_emulators.sh
$ ./download_emulators.sh
$ make clean
$ make -j
$ export PYTHONPATH=$(pwd)/python/classy_szfast:$PYTHONPATH
The -j
flag speeds up the compilation process by using multiple cores.
If it installs, run the C code with many power spectra output:
$ ./class_sz class_sz_test.ini
The .ini
files are the parameter files.
If you want to run CLASS and not do the class_sz part, you can! For example:
$ ./class_sz explanatory.ini
This will just run the standard CLASS code and its calculations. All depends on what output you request: if you request a class_sz observable or not.
It is often the case that some libraries are not found. In general, setting the following paths appropriately should solve your issues:
export LIBRARY_PATH=/path/to/your/libs:path/to/gsl:path/to/fftw:$LIBRARY_PATH
export C_INCLUDE_PATH=/path/to/your/includes:path/to/gsl:path/to/fftw:$C_INCLUDE_PATH
export DYLD_LIBRARY_PATH="/path/to/your/libs:$DYLD_LIBRARY_PATH" # (Mac M1 users only)
To ensure these paths are set every time you open a terminal, you can add these lines to your ~/.bashrc
or ~/.bash_profile
file automatically using the echo
command.
For ~/.bashrc
(common for most Linux systems):
echo -e "\n# Set library paths for class_sz\nexport LIBRARY_PATH=/path/to/your/libs:path/to/gsl/:path/to/fftw/:\$LIBRARY_PATH\nexport C_INCLUDE_PATH=/path/to/your/includes:path/to/gsl:path/to/fftw:\$C_INCLUDE_PATH\nexport DYLD_LIBRARY_PATH=\"/path/to/your/libs:\$DYLD_LIBRARY_PATH\" # (Mac M1 users only)" >> ~/.bashrc
To apply the changes immediately:
source ~/.bashrc
For ~/.bash_profile
(common for macOS):
echo -e "\n# Set library paths for class_sz\nexport LIBRARY_PATH=/path/to/your/libraries:path/to/gsl/:path/to/fftw/:\$LIBRARY_PATH\nexport C_INCLUDE_PATH=path/to/gsl/:path/to/fftw/:\$C_INCLUDE_PATH\nexport DYLD_LIBRARY_PATH=\"/path/to/your/libraries:\$DYLD_LIBRARY_PATH\" # (Mac M1 users only)" >> ~/.bash_profile
To apply the changes immediately:
source ~/.bash_profile
Use module load, module show to get GSL and FFTW. At NERSC/Cori/Perlmutter, the code works with gsl/2.7. (There seems to be a problematic behavior during job submission with gsl/2.5.)
For Monte Carlo analyses, we also recall that Mpi4py needs to be correctly installed. Follow: Cobaya MPI Installation Guide.
At NERSC, these commands may help for mpi4py:
MPICC="cc -shared" pip install --force-reinstall --no-cache-dir --no-binary=mpi4py mpi4py
To install the new version of CLASS_SZ, you will need TensorFlow (needed for the Cosmopower emulators). On M1/M2, make sure you have the arm64 version of conda (if not, you need to remove your entire conda and install the arm64 version for Apple Silicon).
This video might be helpful: Installing TensorFlow on M1 Mac.
Then you can follow the standard TensorFlow installation recipe for M1, e.g., Medium Article or the Apple Developer Forums.
The following line should fix most issues:
$ conda install -c apple tensorflow-deps