Summary

When providing the same input parameters and units, class and classy has the same outputs.

In [1]:
from classy import Class
/home/kolen/.conda/envs/all3-intel/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 216, got 192
  return f(*args, **kwds)
In [2]:
import os
import io
In [3]:
import pandas as pd
import numpy as np
In [4]:
import matplotlib.pyplot as plt
In [5]:
%matplotlib inline
In [6]:
def read_txt(path):
    with open(path, 'r') as f:
        # remove beginning `#`
        text = f.read()[1:]
    with io.StringIO(text) as f:
        # input has 6 sig. fig. which fits in float32
        return pd.read_csv(f, delim_whitespace=True, index_col=0, dtype=np.float32)
In [7]:
def read_class_txt(path, camb=False):
    # read once
    with open(path, 'r') as f:
        text = f.read()

    # get last comment line
    comment = None
    for line in text.split('\n'):
        if line.startswith('#'):
            comment = line
    # remove beginning '#'
    comment = comment[1:]
    # parse comment line: get name after ':'
    names = [name.split(':')[1] for name in comment.strip().split()]

    with io.StringIO(text) as f:
        df = pd.read_csv(f, delim_whitespace=True, index_col=0, comment='#', header=None, names=names)
        return df if camb else df * 1.e12
In [8]:
def plot_compare(df1, df2, keys, log=False, relative=False):
    '''plot to compare columns from `df1` and `df2`,
    using columns from `df1`.

    `keys`: tuple of str of names of `df1` and `df2`.
    `relative`: if True, plot the relative error w.r.t. `df1` instead.
    '''
    for col in df1.columns:
#         pd.merge(df1[col], df2[col], left_index=True, right_index=True).plot()
        df_temp = pd.concat((df1[col], df2[col]), axis=1, join='inner', keys=(' '.join((key, col)) for key in keys))
        if relative:
            temp = df_temp.values
            if log:
                plt.loglog(df_temp.index, np.abs((temp[:, 0] - temp[:, 1]) / temp[:, 0]), label=col)
            else:
                plt.plot(df_temp.index, np.abs((temp[:, 0] - temp[:, 1]) / temp[:, 0]), label=col)
            plt.legend()
            plt.show()
        else:
            if log:
                df_temp.plot(logx=True, logy=True)
            else:
                df_temp.plot()

Class result from command line

In [9]:
path = os.path.expanduser('~/git/fork/class/output/base_2018_plikHM_TTTEEE_lowl_lowE_lensing_cl_lensed.dat')
path = os.path.expanduser('~/git/fork/class/output/explanatory00_cl_lensed.dat')
In [10]:
!head -n 20 $path
# dimensionless total lensed [l(l+1)/2pi] C_l's
# for l=2 to 2500, i.e. number of multipoles equal to 2499
#
# -> if you prefer output in CAMB/HealPix/LensPix units/order, set 'format' to 'camb' in input file
# -> if you don't want to see such a header, set 'headers' to 'no' in input file
# -> for CMB lensing (phi), these are C_l^phi-phi for the lensing potential.
#    Remember the conversion factors:
#    C_l^dd (deflection) = l(l+1) C_l^phi-phi
#    C_l^gg (shear/convergence) = 1/4 (l(l+1))^2 C_l^phi-phi
#
# 1:l     2:TT                     3:EE                     4:TE                     5:BB                     6:phiphi                 7:TPhi                   8:Ephi               
    2       1.367931912660e-10       4.159484692652e-15       3.526335207715e-13       2.408172234822e-19       8.134961894325e-09       4.445106340070e-10      -1.842798604913e-12 
    3       1.295945251171e-10       5.349733740796e-15       3.964503659821e-13       4.817284989080e-19       5.054137452615e-09       3.156333034557e-10      -1.397319388602e-12 
    4       1.227547058989e-10       4.643087023570e-15       3.707843637428e-13       8.030879489435e-19       3.519416644922e-09       2.389401792561e-10      -9.173079895417e-13 
    5       1.176290584767e-10       3.108763757226e-15       3.158736441164e-13       1.205015779352e-18       2.626727120201e-09       1.886559194388e-10      -5.125390348965e-13 
    6       1.141210253281e-10       1.743340826985e-15       2.544586051760e-13       1.687657901702e-18       2.049971355020e-09       1.534175281304e-10      -2.134638992242e-13 
    7       1.118431825408e-10       9.419702220516e-16       1.997866879298e-13       2.251183231379e-18       1.652438879728e-09       1.275601555442e-10      -2.190942761396e-14 
    8       1.105289048194e-10       6.058279166538e-16       1.576742948264e-13       2.895780631231e-18       1.365523298466e-09       1.079786550797e-10       7.665300812357e-14 
    9       1.099378486606e-10       4.860149482924e-16       1.294121595380e-13       3.621655591190e-18       1.150341484836e-09       9.281644873522e-11       1.051490753598e-13 
   10       1.098383621606e-10       4.157364997235e-16       1.137366001401e-13       4.429026740620e-18       9.837059836283e-10       8.082175560691e-11       8.912619866438e-14 
In [11]:
df_class = read_class_txt(path, camb=True)
df_class.head()
Out[11]:
TT EE TE BB phiphi TPhi Ephi
l
2 1.367932e-10 4.159485e-15 3.526335e-13 2.408172e-19 8.134962e-09 4.445106e-10 -1.842799e-12
3 1.295945e-10 5.349734e-15 3.964504e-13 4.817285e-19 5.054137e-09 3.156333e-10 -1.397319e-12
4 1.227547e-10 4.643087e-15 3.707844e-13 8.030879e-19 3.519417e-09 2.389402e-10 -9.173080e-13
5 1.176291e-10 3.108764e-15 3.158736e-13 1.205016e-18 2.626727e-09 1.886559e-10 -5.125390e-13
6 1.141210e-10 1.743341e-15 2.544586e-13 1.687658e-18 2.049971e-09 1.534175e-10 -2.134639e-13

Class

In [13]:
LambdaCDM = Class()
In [43]:
# optional: clear content of LambdaCDM (to reuse it for another model)
LambdaCDM.struct_cleanup()
# optional: reset parameters to default
LambdaCDM.empty()
In [51]:
kwargs = {
    # background parameters
    'H0': 67.32117,
    'omega_b': 0.02238280,
    'N_ur': 2.03066666667,
    'omega_cdm': 0.1201075,
    'N_ncdm': 1,
    'omega_ncdm': 0.0006451439,

    'YHe': 0.2454006,
    'tau_reio': 0.05430842,

    'n_s': 0.9660499,
    'A_s': 2.100549e-09,

    'non linear': 'halofit',

    'output': 'tCl,pCl,lCl,mPk',
    'lensing': 'yes',
#     'P_k_max_1/Mpc': 3.,
#     'l_max_scalars': 3000,
}
LambdaCDM.set(kwargs)
Out[51]:
True
In [52]:
LambdaCDM.compute()
In [53]:
df = pd.DataFrame(LambdaCDM.lensed_cl())
df.set_index('ell', inplace=True)
ell = df.index.values.astype(np.int32)
df *= ((ell * (ell + 1)) * 0.5 / np.pi)[:, None]
df.columns = ['TT', 'EE', 'TE', 'BB', 'phiphi', 'TPhi']
# df *= 1.e12
In [54]:
df.head()
Out[54]:
TT EE TE BB phiphi TPhi
ell
0 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
1 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
2 1.367932e-10 4.159485e-15 3.526335e-13 2.408172e-19 8.134962e-09 4.445106e-10
3 1.295945e-10 5.349734e-15 3.964504e-13 4.817285e-19 5.054137e-09 3.156333e-10
4 1.227547e-10 4.643087e-15 3.707844e-13 8.030879e-19 3.519417e-09 2.389402e-10
In [55]:
plot_compare(df, df_class, ('classy', 'class'), relative=True)