def make_spectra(settings = None):
    if settings is None:
        from custom_settings import settings
    
    ell = np.arange(2, settings.lmax + 1)
    spectra = np.zeros((4,ell.size))
    if settings.cosmo_params['source'] is "pycamb_default":
        spectra = pycamb.camb(settings.lmax)
    elif settings.cosmo_params['source'] is "planck_latest":
        spectra = pycamb.camb(settings.lmax, settings.cosmo_params)
    else:
        spectra = pycamb.camb(settings.lmax, settings.cosmo_params)

    if settings.plot_spectra:
        plt.figure()
        make_subplot(1, 'TT', spectra[0], ell, '', r'$\mu K^2$')    
        make_subplot(2, 'EE', spectra[1], ell, '', '') 
        make_subplot(3, 'BB', spectra[2], ell, 'l', r'$\mu K^2$')    
        make_subplot(4, 'TE', spectra[3], ell, 'l', '')    
        plt.show()


    if settings.normalise_spectra is False:
        norm_fact = ell*(ell + 1)/(2*np.pi)
        spectra = spectra/norm_fact

    if settings.write_spectra:
        np.save(settings.spectra_file, spectra)
    
    if settings.return_spectra:
        return spectra
Example #2
0
def get_dl(rvalue, lmaxcamb=1000):
    params = {'H0':H0,'omegab':Omegab,'omegac':Omegac,'omegak':0,'scalar_index':0.968,
         'reion__use_optical_depth':True,'reion__optical_depth':0.066,
         'tensor_ratio':rvalue,'WantTensors':True,'scalar_amp':scalar_amp,'DoLensing':True}
    T,E,B,X = pycamb.camb(lmaxcamb+1+150,**params)
    params_nl = {'H0':H0,'omegab':Omegab,'omegac':Omegac,'omegak':0,'scalar_index':0.968,
         'reion__use_optical_depth':True,'reion__optical_depth':0.066,
         'tensor_ratio':rvalue,'WantTensors':True,'scalar_amp':scalar_amp,'DoLensing':False}
    Tnl,Enl,Bnl,Xnl = pycamb.camb(lmaxcamb+1+150,**params_nl)
    lll = np.arange(1,lmaxcamb+1)
    return lll,T[:lmaxcamb],E[:lmaxcamb],B[:lmaxcamb],X[:lmaxcamb], Tnl[:lmaxcamb], Enl[:lmaxcamb], Bnl[:lmaxcamb], Xnl[:lmaxcamb]
Example #3
0
def get_spectrum_bins(pars, x, stuff, spectra=None):
	#### Parameters
	params = stuff[0]
	lmax = stuff[1]
	fsky = stuff[2]
	mukarcmin = stuff[3]
	fwhmdeg = stuff[4]
	deltal = stuff[5]
	xmin = stuff[6]
	xmax = stuff[7]
	consistency = stuff[8]
	parkeys = stuff[9]

	thepars = params.copy()
	for i in np.arange(len(parkeys)):
		thepars[parkeys[i]] = pars[i]
	#if consistency:
	#	thepars['tensor_index'] = -thepars['tensor_ratio']/thepars['consistency']


	if spectra is None:
		#### Camb for primordial B
		print("")
		print("Calling Camb for Primordial B-modes with lmax = {0:.0f}".format(lmax+200))
		for i in np.arange(len(parkeys)):
			print(parkeys[i], thepars[parkeys[i]])
		amplens = thepars['lensing_amplitude']
		Tprim,Eprim,Bprim,Xprim = pycamb.camb(np.max(lmax)+200,**thepars)
		Bprim = Bprim[0:np.max(lmax)]
		#### Camb for lensing B
		thepars = params.copy()
		thepars['tensor_ratio'] = 0
		thepars['tensor_index'] = 0
		thepars['DoLensing'] = True
		print("Calling Camb for Lensing B-modeswith lmax = {0:.0f}".format(lmax+200))
		Tl,El,Bl,Xl = pycamb.camb(np.max(lmax)+200,**thepars)
		Bl = Bl[0:np.max(lmax)]	
		#### Lensing B
		Blensed = amplens * Bl
		##### Make linear combination
		totBB = Bprim + Blensed
	else:
		Bprim = spectra[0]
		Blensed = thepars['lensing_amplitude'] * spectra[1]
		totBB = Bprim + Blensed
	##### Bin the spectra
	ell = np.arange(len(totBB))+1
	clprim_binned = binspec(ell, Bprim, x, xmin, xmax)
	cllens_binned = binspec(ell, Blensed, x, xmin, xmax)
	btot_binned = clprim_binned + cllens_binned
	##### Corresponding error bars
	dclsb, dclnb, dclb = th_errors_bins(x, ell, totBB/(ell*(ell+1)/(2*np.pi)), fsky, mukarcmin, fwhmdeg, deltal=deltal)
	return btot_binned, clprim_binned, cllens_binned, dclsb, dclnb, dclb, Bprim, Blensed, totBB 
Example #4
0
def getbbsum(pars, x):
	thepars = params.copy()
	thepars['tensor_ratio'] = pars[0]
	Tprim,Eprim,Bprim,Xprim = pycamb.camb(lmax+1,**thepars)
	Bprim = Bprim[0:599]
	bla = np.interp(x, ell, Bprim) + pars[1] * np.interp(x, ell, Bpl)
	return bla
Example #5
0
def getbbsum_samplevar(pars, x):
	thepars = params.copy()
	thepars['tensor_ratio'] = pars[0]
	Tprim,Eprim,Bprim,Xprim = pycamb.camb(lmax+1,**thepars)
	Bprim = Bprim[0:599]
	totBB = Bprim + pars[1] * Bpl
	dclsb, dclnb, dclb = th_errors_bins(x, ell, totBB/(ell*(ell+1)/(2*np.pi)), fsky, mukarcmin, fwhmdeg, deltal=deltal)
	bla = np.interp(x, ell, Bprim) + pars[1] * np.interp(x, ell, Bpl)
	return bla, dclsb, dclnb, dclb
Example #6
0
def get_camb_spectra(cambparams, maxell):
	#### Call CAMB: first without lensing and second with lensing
	print("")
	print("Calling Camb (Unlensed) with lmax = {0:.0f}".format(maxell+200))
	thepars = cambparams.copy()
	thepars['DoLensing'] = False
	Tnl,Enl,Bnl,Xnl = pycamb.camb(np.max(maxell)+200,**thepars)
	Bprim = Bnl[0:np.max(maxell)]
	#### Camb for lensing B
	thepars = cambparams.copy()
	thepars['DoLensing'] = True
	print("Calling Camb (Lensed) with lmax = {0:.0f}".format(maxell+200))
	Tl,El,Bl,Xl = pycamb.camb(np.max(maxell)+200,**thepars)
	Tl = Tl[0:np.max(maxell)]	
	El = El[0:np.max(maxell)]	
	Xl = Xl[0:np.max(maxell)]	
	Bl = Bl[0:np.max(maxell)]	
	#### Get Pure lensing B modes
	Bl = Bl - Bprim
	#### build spectra list
	x = np.arange(len(Bl)) + 1
	basic_spectra = [x, Tl, El, Xl, Bprim, Bl]
	return basic_spectra
Example #7
0
def camb_spectra(lmax, **params):
    """
    Wrapper around `pycamb.camb`. Return Cl_TT, Cl_EE, Cl_BB, Cl_EB
    non-weighted by l(l+1)/2pi, starting from l=0.

    """
    import pycamb
    T, E, B, X = pycamb.camb(lmax + 1, **params)
    ell_camb = np.arange(0, lmax + 1)
    fact_camb = 2 * np.pi / (ell_camb * (ell_camb + 1))
    T = np.r_[0, T * fact_camb[1:]]
    E = np.r_[0, E * fact_camb[1:]]
    B = np.r_[0, B * fact_camb[1:]]
    X = np.r_[0, X * fact_camb[1:]]
    return [T, E, B, X]
Example #8
0
def camb_spectra(lmax, **params):
    """
    Wrapper around `pycamb.camb`. Return Cl_TT, Cl_EE, Cl_BB, Cl_EB
    non-weighted by l(l+1)/2pi, starting from l=0.

    """
    import pycamb
    T, E, B, X = pycamb.camb(lmax + 1, **params)
    ell_camb = np.arange(0, lmax + 1)
    fact_camb = 2 * np.pi / (ell_camb * (ell_camb + 1))
    T = np.r_[0, T * fact_camb[1:]]
    E = np.r_[0, E * fact_camb[1:]]
    B = np.r_[0, B * fact_camb[1:]]
    X = np.r_[0, X * fact_camb[1:]]
    return [T, E, B, X]
Example #9
0
def rcamblib(rvalues,lmaxcamb):
	lll = np.arange(lmaxcamb+1)
	fact = (lll*(lll+1))/(2*np.pi)
	spec = np.zeros((lmaxcamb+1,len(rvalues)))
	i=0
	for r in rvalues:	
		print('i = {0:5.0f} over {1:5.0f}: Calling CAMB with r={2:10.8f}'.format(i,len(rvalues),r))	
		params = {'H0':H0,'omegab':Omegab,'omegac':Omegac,'omegak':0,'scalar_index':0.9624,'reion__use_optical_depth':True,'reion__optical_depth':0.0925,'tensor_ratio':r,'WantTensors':True,'scalar_amp':scalar_amp,'DoLensing':True}
		T,E,B,X = pycamb.camb(lmaxcamb+1+150,**params)
		B=B[:lmaxcamb+1]/fact
		B[~np.isfinite(B)]=0
		spec[:,i] = B
		i = i + 1

	return [lll, rvalues, spec]
    def __call__(self, ctx):
        p1 = ctx.getParams()[0:len(self.mapping)]
        
        try:
            params = self.constants.copy()
            for k,v in self.mapping.items():
                params[k] = p1[v]
            
            self._transform(params)

            cl_tt,cl_ee,cl_bb,cl_te = pycamb.camb(self.lmax, **params)
            ctx.add(CL_TT_KEY, cl_tt)
            ctx.add(CL_TE_KEY, cl_te)
            ctx.add(CL_EE_KEY, cl_ee)
            ctx.add(CL_BB_KEY, cl_bb)
        except RuntimeError:
            getLogger().warn("Runtime error catched from the camb so. Used params [%s]"%( ", ".join([str(i) for i in p1]) ) )
            raise LikelihoodComputationException()
    def __call__(self, ctx):
        p1 = ctx.getParams()[0:len(self.mapping)]

        try:
            params = self.constants.copy()
            for k, v in self.mapping.items():
                params[k] = p1[v]

            self._transform(params)

            cl_tt, cl_ee, cl_bb, cl_te = pycamb.camb(self.lmax, **params)
            ctx.add(CL_TT_KEY, cl_tt)
            ctx.add(CL_TE_KEY, cl_te)
            ctx.add(CL_EE_KEY, cl_ee)
            ctx.add(CL_BB_KEY, cl_bb)
        except RuntimeError:
            getLogger().warn(
                "Runtime error catched from the camb so. Used params [%s]" %
                (", ".join([str(i) for i in p1])))
            raise LikelihoodComputationException()
Example #12
0
r = 0.05
H0 = 67.04
omegab = 0.022032
omegac = 0.12038
h2 = (H0/100.)**2
scalar_amp = np.exp(3.098)/1.E10
omegav = h2 - omegab - omegac
Omegab = omegab/h2
Omegac = omegac/h2
print 'Omegab = ',omegab/h2,'Omegam = ',(omegac+omegab)/h2,'Omegav = ',omegav/h2

params = {'H0':H0,'omegab':Omegab,'omegac':Omegac,'omegak':0,'scalar_index':0.9624,
         'reion__use_optical_depth':True,'reion__optical_depth':0.0925,
         'tensor_ratio':r,'WantTensors':True,'scalar_amp':scalar_amp,'DoLensing':True}
lmaxcamb = 1000
T,E,B,X = pycamb.camb(lmaxcamb+1+150,**params)
lll = np.arange(1,lmaxcamb+1)
T=T[:lmaxcamb]
E=E[:lmaxcamb]
B=B[:lmaxcamb]
X=X[:lmaxcamb]
fact = (lll*(lll+1))/(2*np.pi)
spectra = [lll, T/fact, E/fact, B/fact, X/fact]

params_nl = {'H0':H0,'omegab':Omegab,'omegac':Omegac,'omegak':0,'scalar_index':0.9624,
         'reion__use_optical_depth':True,'reion__optical_depth':0.0925,
         'tensor_ratio':r,'WantTensors':True,'scalar_amp':scalar_amp,'DoLensing':False}
lmaxcamb = 1000
Tnl,Enl,Bnl,Xnl = pycamb.camb(lmaxcamb+1+150,**params_nl)
lll = np.arange(1,lmaxcamb+1)
Tnl=Tnl[:lmaxcamb]
Example #13
0
omegav = h2 - omegab - omegac
Omegab = omegab/h2
Omegac = omegac/h2
rvalue = 0.2
print 'Omegab = ',omegab/h2,'Omegam = ',(omegac+omegab)/h2,'Omegav = ',omegav/h2
params = {'H0':H0,'omegab':Omegab,'omegac':Omegac,'omegak':0,'scalar_index':0.9624,
         'reion__use_optical_depth':True,'reion__optical_depth':0.0925,
         'tensor_ratio':rvalue,'WantTensors':True,'scalar_amp':scalar_amp,'DoLensing':False,
         'tensor_index':-rvalue/8}
params_nl = {'H0':H0,'omegab':Omegab,'omegac':Omegac,'omegak':0,'scalar_index':0.9624,
         'reion__use_optical_depth':True,'reion__optical_depth':0.0925,
         'tensor_ratio':rvalue,'WantTensors':True,'scalar_amp':scalar_amp,'DoLensing':True,
         'tensor_index':-rvalue/8}

lmax=1000
ctt,cee,cbbprim,cte = pycamb.camb(lmax,**params)
ctt,cee,cbbtot,cte = pycamb.camb(lmax,**params_nl)
cbblensing = cbbtot-cbbprim
ell = np.arange(lmax-1)+2


clf()
xlim(0,500)
a0,=plot(ell, cbbprim, 'b--', label='Primordial B-modes (r={0:.1g})'.format(rvalue),lw=3)
a1,=plot(ell, cbblensing, 'g--', label='Lensing B-modes',lw=3)
a2,=plot(ell, cbbtot, 'r', label='Total B-modes',lw=3)
a3,=plot(ell, cee, color='orange', label='E-modes',lw=3)
a4,=plot(ell, -cte, ':', color='purple',label='Total',lw=3)
a4,=plot(ell, cte, color='purple',label='TE correlation',lw=3)
a5,=plot(ell, ctt, 'k', label='Temperature',lw=3)
yscale('log')
Example #14
0
paramsnl3 = {
    "H0": H0,
    "omegab": Omegab,
    "omegac": Omegac,
    "omegak": 0,
    "scalar_index": 0.9624,
    "reion__use_optical_depth": True,
    "reion__optical_depth": 0.0925,
    "tensor_ratio": 0.001,
    "WantTensors": True,
    "scalar_amp": scalar_amp,
    "DoLensing": False,
}
lmax = 1200
ell = np.arange(1, lmax + 1)
Tnl, Enl, Bnl, Xnl = pc.camb(lmax + 1, **paramsnl)
Tnl2, Enl2, Bnl2, Xnl2 = pc.camb(lmax + 1, **paramsnl2)
Tnl3, Enl3, Bnl3, Xnl3 = pc.camb(lmax + 1, **paramsnl3)
T, E, B, X = pc.camb(lmax + 1, **params)
Blens = B - Bnl

clf()
lw = 3
plot(ell, np.sqrt(T), "k-", label="$C_\ell^{TT}$", lw=lw)
plot(ell, np.sqrt(X), "g-", label="$C_\ell^{TE}$", lw=lw)
plot(ell, np.sqrt(-X), "g--", label="$-C_\ell^{TE}$", lw=lw)
plot(ell, np.sqrt(E), "b-", label="$C_\ell^{EE}$", lw=lw)
plot(ell, np.sqrt(Bnl), "r-", label="Primordial $C_\ell^{BB} (r=0.1)$", lw=lw)
plot(ell, np.sqrt(Bnl2), "-", label="Primordial $C_\ell^{BB} (r=0.01)$", lw=lw, color="firebrick")
plot(ell, np.sqrt(Bnl3), "-", label="Primordial $C_\ell^{BB} (r=0.001)$", lw=lw, color="darkred")
plot(ell, np.sqrt(Blens), "--", label="Lensing $C_\ell^{BB}$", lw=lw, color="darkviolet")
Example #15
0
params = {'H0':H0,'omegab':Omegab,'omegac':Omegac,'omegak':0,'scalar_index':0.9624,
         'reion__use_optical_depth':True,'reion__optical_depth':0.0925,
         'tensor_ratio':rvalue,'WantTensors':True,'scalar_amp':scalar_amp,'DoLensing':False,
         'tensor_index':-rvalue/8}

params_l = {'H0':H0,'omegab':Omegab,'omegac':Omegac,'omegak':0,'scalar_index':0.9624,
        	 'reion__use_optical_depth':True,'reion__optical_depth':0.0925,
        	 'tensor_ratio':rvalue,'WantTensors':True,'scalar_amp':scalar_amp,'DoLensing':True,
        	 'tensor_index':-rvalue/8}

lmax = 1000
ell = np.arange(1,600)


Tprim,Eprim,Bprim,Xprim = pycamb.camb(lmax+1,**params)
Tprim = Tprim[0:599]
Eprim = Eprim[0:599]
Bprim = Bprim[0:599]
Xprim = Xprim[0:599]
Tpl,Epl,Bpl,Xpl = pycamb.camb(lmax+1,**params_l)
Tpl = Tpl[0:599]
Epl = Epl[0:599]
Bpl = Bpl[0:599]
Xpl = Xpl[0:599]
Bl = Bpl - Bprim

clf()
plot(ell,Bprim,label='Primordial $C_\ell^{BB}$'+'$ (r={0:.2f})$'.format(rvalue))
plot(ell,Bl,label='Lensing $C_\ell^{BB}$')
plot(ell,Bpl,label='Total $C_\ell^{BB}$'+'$ (r={0:.2f})$'.format(rvalue))
Example #16
0
H0 = 67.04
omegab = 0.022032
omegac = 0.12038
h2 = (H0/100.)**2
scalar_amp = np.exp(3.098)/1.E10
omegav = h2 - omegab - omegac
Omegab = omegab/h2
Omegac = omegac/h2
print 'Omegab = ',omegab/h2,'Omegam = ',(omegac+omegab)/h2,'Omegav = ',omegav/h2

params = {'H0':H0,'omegab':Omegab,'omegac':Omegac,'omegak':0,'scalar_index':0.9624,
         'reion__use_optical_depth':True,'reion__optical_depth':0.0925,
         'tensor_ratio':0.,'WantTensors':True,'scalar_amp':scalar_amp,'DoLensing':False}

lmaxcamb = 3*512
T,E,B,X = pycamb.camb(lmaxcamb+1,**params)
lll = np.arange(1,lmaxcamb+1)
fact = (lll*(lll+1))/(2*np.pi)
spectra = [lll, T/fact, E/fact, B/fact, X/fact]
clf()
plot(lll,spectra[1]*(lll*(lll+1))/(2*np.pi),label='$C_\ell^{TT}$')
#yscale('log')
xlim(0,lmaxcamb+1)
#ylim(0.0001,100)
xlabel('$\ell$')
ylabel('$\ell(\ell+1)C_\ell/(2\pi)$'+'    '+'$[\mu K^2]$ ')
legend(loc='lower right',frameon=False)



Example #17
0
import pycamb
lmaxcamb = 3*nside

## parameters from Planck 2013 results XV. CMB Power spectra ..., table 8, Planck+WP
## Archiv 1303.5075
H0 = 67.04
omegab = 0.022032
omegac = 0.12038
h2 = (H0/100.)**2
scalar_amp = np.exp(3.098)/1.E10
omegav = h2 - omegab - omegac
Omegab = omegab/h2
Omegac = omegac/h2
print 'Omegab = ',omegab/h2,'Omegam = ',(omegac+omegab)/h2,'Omegav = ',omegav/h2
params = {'H0':H0,'omegab':Omegab,'omegac':Omegac,'omegak':0,'scalar_index':0.9624}
T,E,B,X = pycamb.camb(lmaxcamb+1,**params)
lll = np.arange(1,lmaxcamb+1)
fact = (lll*(lll+1))/(2*np.pi)
spectra = [lll, T/fact, E/fact, B/fact, X/fact]

## parameters from Planck 2013 results XV. CMB Power spectra ..., table 8, Planck+WP
## Archiv 1303.5075
H0 = 67.04
omegab = 0.022032
omegac = 0.12038
h2 = (H0/100.)**2
scalar_amp = np.exp(3.098)/1.E10
omegav = h2 - omegab - omegac
Omegab = omegab/h2
Omegac = omegac/h2
print 'Omegab = ',omegab/h2,'Omegam = ',(omegac+omegab)/h2,'Omegav = ',omegav/h2
Example #18
0
print 'Omegab = ',omegab/h2,'Omegam = ',(omegac+omegab)/h2,'Omegav = ',omegav/h2

params = {'H0':H0,'omegab':Omegab,'omegac':Omegac,'omegak':0,'scalar_index':0.9624,
         'reion__use_optical_depth':True,'reion__optical_depth':0.0925,
         'tensor_ratio':rvalue,'WantTensors':True,'scalar_amp':scalar_amp,'DoLensing':False,
         'tensor_index':-rvalue/8}

params_l = {'H0':H0,'omegab':Omegab,'omegac':Omegac,'omegak':0,'scalar_index':0.9624,
        	 'reion__use_optical_depth':True,'reion__optical_depth':0.0925,
        	 'tensor_ratio':0,'WantTensors':True,'scalar_amp':scalar_amp,'DoLensing':True}

lmax = 1000
ell = np.arange(1,600)


Tprim,Eprim,Bprim,Xprim = pycamb.camb(lmax+1,**params)
Tprim = Tprim[0:599]
Eprim = Eprim[0:599]
Bprim = Bprim[0:599]
Xprim = Xprim[0:599]
Tl,El,Bl,Xl = pycamb.camb(lmax+1,**params_l)
Tl = Tl[0:599]
El = El[0:599]
Bl = Bl[0:599]
Xl = Xl[0:599]
Btot = Bl + Bprim

clf()
plot(ell,Bprim,label='Primordial $C_\ell^{BB}$'+'$ (r={0:.2f})$'.format(rvalue))
plot(ell,Bl,label='Lensing $C_\ell^{BB}$')
plot(ell,Btot,label='Total $C_\ell^{BB}$'+'$ (r={0:.2f})$'.format(rvalue))