Skip to content
@CLASS-SZ

CLASS-SZ

GitHub stars Documentation Status PyPI version

CLASS_SZ

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.

Documentation

Check our evolving documentation.

Installation

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.)

Computing

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()

Accelerated Computations

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 $C_\ell$'s:

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']

Some basic info

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:

Using the Code

The class_sz code is public.

If you use it, please cite:

If you use accelerated computations, please cite:

If you use thermal SZ power spectrum and cluster counts calculations, please cite:

In all these cases, please also cite the original CLASS papers:

As well as other references listed here: http://class-code.net

For developers

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:

And importantly, in the python and cython files:

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.

Library and Include Path Configuration

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

Some Tips to Run on Computer Clusters

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

TensorFlow on Mac M1

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

Pinned Loading

  1. notebooks notebooks Public

    Tutorial notebooks for the class_sz code

    Jupyter Notebook 4 1

  2. class_sz class_sz Public

    Cosmic Linear Anisotropy Solving System with Machine Learning Accelerated CMB, LSS and Halo Model Observables Computations

    C 13 4

Repositories

Showing 6 of 6 repositories
  • class_sz Public

    Cosmic Linear Anisotropy Solving System with Machine Learning Accelerated CMB, LSS and Halo Model Observables Computations

    CLASS-SZ/class_sz’s past year of commit activity
    C 13 MIT 4 1 0 Updated Nov 19, 2024
  • CLASS-SZ/classy_szfast’s past year of commit activity
    Python 0 1 0 0 Updated Nov 11, 2024
  • get_cosmopower_emus Public

    utility package to automatically download emulators

    CLASS-SZ/get_cosmopower_emus’s past year of commit activity
    Python 0 0 0 0 Updated Nov 11, 2024
  • class_sz_data Public

    data for the class_sz code

    CLASS-SZ/class_sz_data’s past year of commit activity
    Python 0 0 0 0 Updated Nov 11, 2024
  • .github Public
    CLASS-SZ/.github’s past year of commit activity
    0 0 0 0 Updated Nov 10, 2024
  • notebooks Public

    Tutorial notebooks for the class_sz code

    CLASS-SZ/notebooks’s past year of commit activity
    Jupyter Notebook 4 1 0 0 Updated Sep 27, 2024

Top languages

Loading…

Most used topics

Loading…