Summary

Comparing spectra from

  • POLARBEAR's Planck 2015 CAMB input,
  • theory spectra from Planck 2018 release
  • class command line output
  • class Python interface output

class' agrees with first 2 when setting format as camb.

class without setting this format, and the result from the Python interface doesn't agree. Why?

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 [30]:
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()

Planck 2018 release

c.f.

In [9]:
path = os.path.expanduser('~/temp/COM_PowerSpect_CMB-base-plikHM_TTTEEE-lowl-lowE-lensing-minimum-theory_R3.01.txt')
In [10]:
!head -n 20 $path
#    L    TT             TE             EE             BB             PP
     2   0.101673E+04   0.261753E+01   0.308827E-01   0.181847E-05   0.501352E-07
     3   0.963727E+03   0.293806E+01   0.396903E-01   0.363743E-05   0.609943E-07
     4   0.912608E+03   0.275866E+01   0.344962E-01   0.606345E-05   0.702592E-07
     5   0.874477E+03   0.235185E+01   0.230941E-01   0.909717E-05   0.782921E-07
     6   0.848509E+03   0.189605E+01   0.129512E-01   0.127394E-04   0.853020E-07
     7   0.832082E+03   0.149063E+01   0.700022E-02   0.169910E-04   0.914734E-07
     8   0.822295E+03   0.117940E+01   0.450136E-02   0.218531E-04   0.969341E-07
     9   0.817803E+03   0.967988E+00   0.360317E-02   0.273268E-04   0.101794E-06
    10   0.817174E+03   0.848454E+00   0.307459E-02   0.334135E-04   0.106122E-06
    11   0.819034E+03   0.803426E+00   0.259588E-02   0.401146E-04   0.110013E-06
    12   0.824031E+03   0.813834E+00   0.239725E-02   0.474315E-04   0.113488E-06
    13   0.830534E+03   0.856342E+00   0.219551E-02   0.553659E-04   0.116594E-06
    14   0.837657E+03   0.908861E+00   0.224915E-02   0.639195E-04   0.119385E-06
    15   0.845805E+03   0.966082E+00   0.247194E-02   0.730940E-04   0.121889E-06
    16   0.855503E+03   0.102738E+01   0.270996E-02   0.828911E-04   0.124134E-06
    17   0.866739E+03   0.109094E+01   0.310371E-02   0.933126E-04   0.126148E-06
    18   0.878618E+03   0.115646E+01   0.365104E-02   0.104360E-03   0.127957E-06
    19   0.891980E+03   0.122429E+01   0.433324E-02   0.116036E-03   0.129570E-06
    20   0.905157E+03   0.129250E+01   0.517714E-02   0.128342E-03   0.130999E-06
In [11]:
df_planck = read_txt(path)
In [12]:
df_planck.head()
Out[12]:
TT TE EE BB PP
L
2.0 1016.729980 2.61753 0.030883 0.000002 5.013520e-08
3.0 963.726990 2.93806 0.039690 0.000004 6.099430e-08
4.0 912.607971 2.75866 0.034496 0.000006 7.025920e-08
5.0 874.476990 2.35185 0.023094 0.000009 7.829210e-08
6.0 848.508972 1.89605 0.012951 0.000013 8.530200e-08

from CAMB

In [13]:
df_camb = pd.read_hdf('/scratch/data/largepatch_high/Planck2015_base_plikHM_TT_lowTEB_lensing_r0p025_lensedtotCls.hdf5')
In [14]:
df_camb.head()
Out[14]:
TT EE BB TE
L
2 1056.199951 0.038731 0.000470 2.9027
3 995.500000 0.055588 0.000499 3.4016
4 938.789978 0.054410 0.000453 3.3215
5 896.020020 0.041343 0.000370 2.9371
6 866.200012 0.025925 0.000282 2.4456
In [15]:
plot_compare(df_camb, df_planck, ('CAMB', 'Planck'))
In [31]:
plot_compare(df_camb, df_planck, ('CAMB', 'Planck'), relative=True, log=True)

Class result from command line

In [17]:
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 [18]:
!head -n 20 $path
# total lensed [l(l+1)/2pi] C_l's (units: [microK]^2)
# for l=2 to 2500, i.e. number of multipoles equal to 2499
#
# -> if you don't want to see such a header, set 'headers' to 'no' in input file
# -> for CMB lensing (d), these are C_l^dd for the deflection field.
#    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:BB                     5:TE                     6:dd                     7:dT                     8:dE                 
    2       1.015414535444e+03       3.084404607214e-02       1.825813559895e-06       2.617799563055e+00       5.014156505268e-08       2.987172106603e-03      -1.222420782582e-05 
    3       9.617693808245e+02       3.965120461004e-02       3.648864677072e-06       2.937632756411e+00       6.092903736881e-08       2.984078729482e-03      -1.312440562659e-05 
    4       9.108801769588e+02       3.444177498069e-02       6.078793060105e-06       2.752480256100e+00       7.017264593865e-08       2.911716800158e-03      -1.112030814762e-05 
    5       8.729454669531e+02       2.305561244267e-02       9.116966093196e-06       2.344338596510e+00       7.819478583098e-08       2.812348581471e-03      -7.585121996768e-06 
    6       8.469144993223e+02       1.292920478873e-02       1.276468104516e-05       1.889100818839e+00       8.519932168632e-08       2.703530655400e-03      -3.701920437267e-06 
    7       8.302095298436e+02       6.987060225013e-03       1.702292928723e-05       1.483720699260e+00       9.136118487882e-08       2.594088058813e-03      -3.771330089133e-07 
    8       8.205314505523e+02       4.495341953355e-03       2.189265380163e-05       1.171353813404e+00       9.681415090342e-08       2.488674674771e-03       1.842217063523e-06 
    9       8.160840452562e+02       3.607190293029e-03       2.737495662844e-05       9.618628720703e-01       1.016622022101e-07       2.389437800381e-03       2.786571729694e-06 
   10       8.155582825366e+02       3.085634881413e-03       3.347112210471e-05       8.453701331248e-01       1.059876061898e-07       2.296946505579e-03       2.612726234098e-06 
   11       8.179862061401e+02       2.606575955181e-03       4.018255198444e-05       8.022007202619e-01       1.098561507439e-07       2.210915418721e-03       1.694730508071e-06 
In [19]:
df_class = read_class_txt(path, camb=True)
df_class.head()
Out[19]:
TT EE BB TE dd dT dE
l
2 1015.414535 0.030844 0.000002 2.617800 5.014157e-08 0.002987 -0.000012
3 961.769381 0.039651 0.000004 2.937633 6.092904e-08 0.002984 -0.000013
4 910.880177 0.034442 0.000006 2.752480 7.017265e-08 0.002912 -0.000011
5 872.945467 0.023056 0.000009 2.344339 7.819479e-08 0.002812 -0.000008
6 846.914499 0.012929 0.000013 1.889101 8.519932e-08 0.002704 -0.000004
In [33]:
plot_compare(df_class[['TT', 'EE', 'BB', 'TE']], df_planck, ('CAMB', 'Planck'), relative=True, log=True)

Class

In [21]:
LambdaCDM = Class()
In [22]:
# optional: clear content of LambdaCDM (to reuse it for another model)
LambdaCDM.struct_cleanup()
# optional: reset parameters to default
LambdaCDM.empty()
In [23]:
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,

    'output': 'tCl,pCl,lCl,mPk',
    'lensing': 'yes',
    'P_k_max_1/Mpc': 3.,
    'l_max_scalars': 3000,
}
LambdaCDM.set(kwargs)
Out[23]:
True
LambdaCDM.set({'omega_b':0.022032,'omega_cdm':0.12038,'h':0.67556,'A_s':2.215e-9,'n_s':0.9619,'tau_reio':0.0925}) LambdaCDM.set({'output':'tCl,pCl,lCl,mPk','lensing':'yes','P_k_max_1/Mpc':3.0})
In [24]:
LambdaCDM.compute()
In [25]:
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 = df.columns.str.upper()
df *= 1.e12
In [26]:
df.head()
Out[26]:
TT EE TE BB PP TP
ell
0 0.000000 0.000000 0.000000 0.000000e+00 0.000000 0.000000
1 0.000000 0.000000 0.000000 0.000000e+00 0.000000 0.000000
2 136.793193 0.004159 0.352633 2.276109e-07 8158.816026 444.970132
3 129.594479 0.005350 0.396450 4.553686e-07 5071.398905 316.012294
4 122.754650 0.004643 0.370784 7.592689e-07 3532.219672 239.258185
In [27]:
df_planck.head()
Out[27]:
TT TE EE BB PP
L
2.0 1016.729980 2.61753 0.030883 0.000002 5.013520e-08
3.0 963.726990 2.93806 0.039690 0.000004 6.099430e-08
4.0 912.607971 2.75866 0.034496 0.000006 7.025920e-08
5.0 874.476990 2.35185 0.023094 0.000009 7.829210e-08
6.0 848.508972 1.89605 0.012951 0.000013 8.530200e-08
In [35]:
plot_compare(df_planck, df, ('planck', 'classy'), relative=True)
/home/kolen/.conda/envs/all3-intel/lib/python3.6/site-packages/ipykernel_launcher.py:16: RuntimeWarning: divide by zero encountered in true_divide
  app.launch_new_instance()
In [ ]: