Commit 58be8805 authored by Syl's avatar Syl
Browse files

few corrections

parent f1d0cc3b
...@@ -3,6 +3,7 @@ Test script for xQML ...@@ -3,6 +3,7 @@ Test script for xQML
Author: Vanneste Author: Vanneste
""" """
from __future__ import division from __future__ import division
import timeit import timeit
...@@ -11,107 +12,135 @@ import healpy as hp ...@@ -11,107 +12,135 @@ import healpy as hp
from pylab import * from pylab import *
import astropy.io.fits as fits import astropy.io.fits as fits
from xqml import xQML import xqml
from libcov import compute_ds_dcb import libcov
import simulation
from xqml_utils import progress_bar from xqml_utils import progress_bar
from simulation import getstokes, muKarcmin2var, GetBinningMatrix from simulation import muKarcmin2var, GetBinningMatrix
from simulation import extrapolpixwin from simulation import extrapolpixwin
ion()
if __name__ == "__main__": # if __name__ == "__main__":
nside = 4 nside = 4
lmax = 3 * nside - 1 lmax = 2 * nside - 1
Slmax = 3 * nside - 1 Slmax = 2 * nside - 1
deltal = 1 deltal = 1
nsimu = 500 nsimu = 10000
clth = np.array(hp.read_cl('planck_base_planck_2015_TTlowP.fits')) clth = np.array(hp.read_cl('planck_base_planck_2015_TTlowP.fits'))
lth = arange(2, lmax+1) Clthshape = zeros(((6,)+shape(clth)[1:]))
Clthshape[:4] = clth
ellbins = arange(2, lmax + 2, deltal) clth = Clthshape
ellbins[-1] = lmax + 1 EB = 0.5
clth[4] = EB*sqrt(clth[1]*clth[2])
P, Q, ell, ellval = GetBinningMatrix(ellbins, lmax) TB = 0.5
nbins = len(ellbins) - 1 clth[5] = TB*sqrt(clth[0]*clth[2])
# Create mask lth = arange(2, lmax+1)
t, p = hp.pix2ang(nside, range(hp.nside2npix(nside))) spec = ['EB', 'TE', 'TB']
mask = np.ones(hp.nside2npix(nside), bool) temp = True
mask[abs(90 - rad2deg(t)) < 10] = False polar = True
npix = sum(mask) corr = False
pixwin = False
fwhm = 0.5
bl = hp.gauss_beam(deg2rad(fwhm), lmax=Slmax + 1) ellbins = arange(2, lmax + 2, deltal)
ellbins[-1] = lmax + 1
allStoke, der, ind = getstokes(polar=True, temp=False, EBTB=False)
nder = len(der) P, Q, ell, ellval = GetBinningMatrix(ellbins, lmax)
nbins = len(ellbins) - 1
muKarcmin = 0.1
# Create mask
pixvar = muKarcmin2var(muKarcmin, nside) t, p = hp.pix2ang(nside, range(hp.nside2npix(nside)))
varmap = ones((2 * npix)) * pixvar mask = np.ones(hp.nside2npix(nside), bool)
NoiseVar = np.diag(varmap) mask[abs(90 - rad2deg(t)) < 60] = False
npix = sum(mask)
cmb = np.array(hp.synfast(
clth, nside, fwhm=deg2rad(fwhm), pixwin=True, new=True, verbose=False)) fwhm = 0.5
noise = (randn(len(varmap)) * varmap**0.5).reshape(2, -1) bl = hp.gauss_beam(deg2rad(fwhm), lmax=Slmax + 1)
# dm = cmb[1:, mask] + noise
dm = cmb[1:][:, mask] + noise stokes, spec, istokes, ispecs = simulation.getstokes(
spec=spec, polar=polar, temp=temp, corr=corr)
# ############## Compute ds_dcb ############### print(stokes, spec, istokes, ispecs)
ip = arange(hp.nside2npix(nside)) nspec = len(spec)
ipok = ip[mask] nstoke = len(stokes)
Pl, S = compute_ds_dcb( # ############## Compute ds_dcb ###############
ellbins, nside, ipok, bl, clth, Slmax, ip = arange(hp.nside2npix(nside))
polar=True, temp=False, EBTB=False, ipok = ip[mask]
pixwining=True, timing=True, MC=False)
# Pl = Pl.reshape((nder)*(np.shape(Pl)[1]), 2 * npix, 2 * npix) Pl, S = libcov.compute_ds_dcb(ellbins, nside, ipok, bl, clth, Slmax, spec=spec,
pixwin=pixwin, timing=True, MC=False)
# ############## Compute spectra ###############
esti = xQML(mask, ellbins, clth, Pl=Pl, S=S, fwhm=fwhm) # ############## Compute spectra ###############
esti.construct_esti(NoiseVar, NoiseVar)
cl = esti.get_spectra(dm, dm) muKarcmin = 1.0
V = esti.get_covariance() pixvar = muKarcmin2var(muKarcmin, nside)
varmap = ones((nstoke * npix)) * pixvar
# ############## Construct MC ############### NoiseVar = np.diag(varmap)
allcl = []
esti = xQML(mask, ellbins, clth, Pl=Pl, fwhm=fwhm) cmb = np.array(hp.synfast(clth, nside, fwhm=deg2rad(fwhm), pixwin=pixwin,
esti.construct_esti(NoiseVar, NoiseVar) new=True, verbose=False, lmax=Slmax))
fpixw = extrapolpixwin(nside, lmax+2, pixwining=True) noise = (randn(len(varmap)) * varmap**0.5).reshape(nstoke, -1)
start = timeit.default_timer() dm = cmb[istokes][:, mask] + noise
for n in np.arange(nsimu):
progress_bar(n, nsimu, timeit.default_timer() - start) esti = xqml.xQML(mask, ellbins, clth, Pl=Pl, S=S, fwhm=fwhm,
cmb = np.array(hp.synfast(clth[:, :len(fpixw)]*fpixw**2, nside, spec=spec, temp=temp, polar=polar, corr=corr)
fwhm=deg2rad(fwhm), new=True, verbose=False)) esti.construct_esti(NoiseVar, NoiseVar)
dm = cmb[1:, mask] + (randn(2 * npix) * sqrt(varmap)).reshape(2, npix) cl = esti.get_spectra(dm, dm)
allcl.append(esti.get_spectra(dm, dm)) V = esti.get_covariance()
figure() # ############## Construct MC ###############
subplot(3, 1, 1) allcl = []
plot(lth, clth.transpose()[lth, 1: 3], '--k') # allcmb = []
hcl = mean(allcl, 0).transpose() # esti = xqml.xQML(mask, ellbins, clth, Pl=Pl, fwhm=fwhm, spec=spec, temp=temp,
scl = std(allcl, 0).transpose() # polar=polar, corr=corr)
plot(ellval, hcl, 'b.') esti.construct_esti(NoiseVar, NoiseVar)
plot(ellval, hcl + scl, 'r--', label=r"$\pm 1\sigma$") fpixw = extrapolpixwin(nside, lmax+2, pixwin=pixwin)
plot(ellval, hcl - scl, 'r--') start = timeit.default_timer()
ylabel(r"$C_\ell$") for n in np.arange(nsimu):
semilogy() progress_bar(n, nsimu, timeit.default_timer() - start)
legend(loc=4) cmb = np.array(hp.synfast(clth[:, :len(fpixw)]*(fpixw*bl)**2, nside,
subplot(3, 1, 2) lmax=Slmax, fwhm=deg2rad(fwhm), new=True, verbose=False))
cosmic = sqrt(2./(2 * lth + 1)) / mean(mask) * clth[1: 3, lth] cmbm = cmb[istokes][:, mask]
plot(lth, cosmic.transpose(), '--k') dmA = cmbm + (randn(nstoke * npix) * sqrt(varmap)).reshape(nstoke, npix)
plot(ellval, scl, 'r-', label=r"$\sigma_{\rm MC}$") dmB = cmbm + (randn(nstoke * npix) * sqrt(varmap)).reshape(nstoke, npix)
plot(ellval, sqrt(diag(V)).reshape(nder, -1).transpose(), 'b.') # allcmb.append(cmbm)
ylabel(r"$\sigma(C_\ell)$") allcl.append(esti.get_spectra(dmA, dmB))
semilogy()
legend(loc=4) figure(figsize=[10, 8])
subplot(3, 1, 3) clf()
plot(ellval, (hcl-clth[1: 3, lth].T)/(scl/sqrt(nsimu)), 'o') subplot(3, 1, 1)
ylabel(r"$R[C_\ell]$") plot(lth, clth[ispecs][:, lth].T, '--k')
xlabel(r"$\ell$") hcl = mean(allcl, 0)
ylim(-3, 3) scl = std(allcl, 0)
show() [plot(ellval, hcl[s], 'o', color='C%i' % s, label=r"$%s$" % spec[s])
for s in np.arange(nspec)]
[fill_between(ellval, (hcl - scl/sqrt(nsimu))[s], (hcl + scl/sqrt(nsimu))[s],
color='C%i' % s, alpha=0.2) for s in np.arange(nspec)]
ylabel(r"$C_\ell$")
semilogy()
legend(loc=4)
subplot(3, 1, 2)
cosmic = sqrt(2./(2 * lth + 1)) / mean(mask) * clth[ispecs][:, lth]
# plot(lth, cosmic.transpose(), '--k')
[plot(ellval, scl[s], '--', color='C%i' % s, label=r"$\sigma^{%s}_{\rm MC}$" %
spec[s]) for s in np.arange(nspec)]
[plot(ellval, sqrt(diag(V)).reshape(nspec, -1)[s], 'o', color='C%i' % s)
for s in np.arange(nspec)]
ylabel(r"$\sigma(C_\ell)$")
semilogy()
legend(loc=4)
subplot(3, 1, 3)
[plot(ellval, (hcl[s]-clth[ispecs[s]][lth])/(scl[s]/sqrt(nsimu)), '--o',
color='C%i' % s) for s in np.arange(nspec)]
ylabel(r"$R[C_\ell]$")
xlabel(r"$\ell$")
ylim(-3, 3)
grid()
show()
# savefig("../Plots/Git/"+"test0.png")
if __name__ == "__main__": if __name__ == "__main__":
""" """
......
...@@ -42,7 +42,7 @@ def Pl(ds_dcb): ...@@ -42,7 +42,7 @@ def Pl(ds_dcb):
return np.copy(ds_dcb).reshape(2 * (np.shape(ds_dcb)[1]), nnpix, nnpix) return np.copy(ds_dcb).reshape(2 * (np.shape(ds_dcb)[1]), nnpix, nnpix)
def CorrelationMatrix(Clth, Pl, ellbins, polar=True, temp=False, EBTB=False): def CorrelationMatrix(Clth, Pl, ellbins, polar=True, temp=False, corr=False):
""" """
Compute correlation matrix S = sum_l Pl*Cl Compute correlation matrix S = sum_l Pl*Cl
...@@ -58,7 +58,7 @@ def CorrelationMatrix(Clth, Pl, ellbins, polar=True, temp=False, EBTB=False): ...@@ -58,7 +58,7 @@ def CorrelationMatrix(Clth, Pl, ellbins, polar=True, temp=False, EBTB=False):
If True, get Stokes parameters for polar (default: True) If True, get Stokes parameters for polar (default: True)
temp : bool temp : bool
If True, get Stokes parameters for temperature (default: False) If True, get Stokes parameters for temperature (default: False)
EBTB : bool corr : bool
If True, get Stokes parameters for EB and TB (default: False) If True, get Stokes parameters for EB and TB (default: False)
Returns Returns
...@@ -76,7 +76,7 @@ def CorrelationMatrix(Clth, Pl, ellbins, polar=True, temp=False, EBTB=False): ...@@ -76,7 +76,7 @@ def CorrelationMatrix(Clth, Pl, ellbins, polar=True, temp=False, EBTB=False):
[[ 0 280 560 840 1120] [[ 0 280 560 840 1120]
[1400 1680 1960 2240 2520]] [1400 1680 1960 2240 2520]]
""" """
if EBTB: if corr:
xx = ['TT', 'EE', 'BB', 'TE', 'EB', 'TB'] xx = ['TT', 'EE', 'BB', 'TE', 'EB', 'TB']
ind = [0, 1, 2, 3, 4, 5] ind = [0, 1, 2, 3, 4, 5]
else: else:
...@@ -85,7 +85,7 @@ def CorrelationMatrix(Clth, Pl, ellbins, polar=True, temp=False, EBTB=False): ...@@ -85,7 +85,7 @@ def CorrelationMatrix(Clth, Pl, ellbins, polar=True, temp=False, EBTB=False):
if not temp: if not temp:
allStoke = ['Q', 'U'] allStoke = ['Q', 'U']
if EBTB: if corr:
xx = ['EE', 'BB', 'EB'] xx = ['EE', 'BB', 'EB']
ind = [1, 2, 5] ind = [1, 2, 5]
else: else:
......
"""
Set of routines to ...
Author: Vanneste
"""
from __future__ import division
import numpy as np
def polrotangle(ri, rj):
"""
Computes cosine and sine of twice the angle between pixels i and j.
Parameters
----------
ri : 3D array of floats
Coordinates of vector corresponding to input pixels i following
healpy.pix2vec(nside,ipix) output
rj : 3D array of floats
Coordinates of vector corresponding to input pixels j following
healpy.pix2vec(nside,jpix) output
Returns
----------
cos2a : 1D array of floats
Cosine of twice the angle between pixels i and j
sin2a : 1D array of floats
Sine of twice the angle between pixels i and j
Example
----------
>>> cos2a, sin2a = polrotangle([0.1,0.2,0.3], [0.4,0.5,0.6])
>>> print(round(cos2a,5),round(sin2a,5))
(0.06667, 0.37333)
"""
z = np.array([0.0, 0.0, 1.0])
# Compute ri^rj : unit vector for the great circle connecting i and j
rij = np.cross(ri, rj)
norm = np.sqrt(np.dot(rij, np.transpose(rij)))
# case where pixels are identical or diametrically opposed on the sky
if norm <= 1e-15:
cos2a = 1.0
sin2a = 0.0
return cos2a, sin2a
rij = rij / norm
# Compute z^ri : unit vector for the meridian passing through pixel i
ris = np.cross(z, ri)
norm = np.sqrt(np.dot(ris, np.transpose(ris)))
# case where pixels is at the pole
if norm <= 1e-15:
cos2a = 1.0
sin2a = 0.0
return cos2a, sin2a
ris = ris / norm
# Now, the angle we want is that
# between these two great circles: defined by
cosa = np.dot(rij, np.transpose(ris))
# the sign is more subtle : see tegmark et de oliveira costa 2000 eq. A6
rijris = np.cross(rij, ris)
sina = np.dot(rijris, np.transpose(ri))
# so now we have directly cos2a and sin2a
cos2a = 2.0 * cosa * cosa - 1.0
sin2a = 2.0 * cosa * sina
return cos2a, sin2a
if __name__ == "__main__":
"""
Run the doctest using
python simulation.py
If the tests are OK, the script should exit gracefuly, otherwise the
failure(s) will be printed out.
"""
import doctest
if np.__version__ >= "1.14.0":
np.set_printoptions(legacy="1.13")
doctest.testmod()
This diff is collapsed.
""" """
Set of routines to generate basic simulations Set of routines to generate basic simulations
Author: Vanneste Author: Vanneste
""" """
...@@ -96,62 +96,81 @@ def getNl(pixvar, nside, nbins): ...@@ -96,62 +96,81 @@ def getNl(pixvar, nside, nbins):
return Nl return Nl
def getstokes(polar=True, temp=False, EBTB=False): def getstokes(spec=None, temp=False, polar=False, corr=False):
""" """
Get the Stokes parameters number and name(s) Get the Stokes parameters number and name(s)
Parameters Parameters
---------- ----------
spec : bool
If True, get Stokes parameters for polar (default: True)
polar : bool polar : bool
If True, get Stokes parameters for polar (default: True) If True, get Stokes parameters for polar (default: True)
temp : bool temp : bool
If True, get Stokes parameters for temperature (default: False) If True, get Stokes parameters for temperature (default: False)
EBTB : bool corr : bool
If True, get Stokes parameters for EB and TB (default: False) If True, get Stokes parameters for EB and TB (default: False)
Returns Returns
---------- ----------
allStoke : list of string stokes : list of string
Stokes variables names Stokes variables names
spec : int spec : int
Spectra names Spectra names
ind : list istokes : list
Indexes of power spectra Indexes of power spectra
Example Example
---------- ----------
>>> getstokes(polar=True, temp=False, EBTB=False) >>> getstokes(polar=True, temp=False, corr=False)
(['Q', 'U'], ['EE', 'BB'], [1, 2]) (['Q', 'U'], ['EE', 'BB'], [1, 2])
>>> getstokes(polar=True, temp=True, EBTB=False) >>> getstokes(polar=True, temp=True, corr=False)
(['I', 'Q', 'U'], ['TT', 'EE', 'BB', 'TE'], [0, 1, 2, 3]) (['I', 'Q', 'U'], ['TT', 'EE', 'BB', 'TE'], [0, 1, 2, 3])
>>> getstokes(polar=True, temp=True, EBTB=True) >>> getstokes(polar=True, temp=True, corr=True)
(['I', 'Q', 'U'], ['TT', 'EE', 'BB', 'TE', 'EB', 'TB'], [0, 1, 2, 3, 4, 5]) (['I', 'Q', 'U'], ['TT', 'EE', 'BB', 'TE', 'EB', 'TB'], [0, 1, 2, 3, 4, 5])
""" """
allStoke = ['I', 'Q', 'U'] if spec is not None:
if EBTB: _temp = "TT" in spec or "TE" in spec or "TB" in spec or temp
spec = ['TT', 'EE', 'BB', 'TE', 'EB', 'TB'] _polar = "EE" in spec or "BB" in spec or "TE" in spec or "TB" in \
ind = [0, 1, 2, 3, 4, 5] spec or "EB" in spec or polar
_corr = "TE" in spec or "TB" in spec or "EB" in spec or corr
if not _temp and not _polar and not _corr:
print("invalid spectra list")
else: else:
spec = ['TT', 'EE', 'BB', 'TE'] _temp = temp
ind = [0, 1, 2, 3] _polar = polar
if not temp: _corr = corr
allStoke = ['Q', 'U']
if EBTB: speclist = []
spec = ['EE', 'BB', 'EB'] if _temp or (spec is None and corr):
ind = [1, 2, 4] speclist.extend(["TT"])
else: if _polar:
spec = ['EE', 'BB'] speclist.extend(["EE", "BB"])
ind = [1, 2] if spec is not None and not corr:
if not polar: if 'TE' in spec:
allStoke = ['I'] speclist.extend(["TE"])
spec = ['TT'] if 'EB' in spec:
ind = [0] speclist.extend(["EB"])
return allStoke, spec, ind if 'TB' in spec:
speclist.extend(["TB"])
elif _corr:
speclist.extend(["TE", "EB", "TB"])
stokes = []
if _temp:
stokes.extend(["I"])
if _polar:
stokes.extend(["Q", "U"])
ispecs = [['TT', 'EE', 'BB', 'TE', 'EB', 'TB'].index(s) for s in speclist]
istokes = [['I', 'Q', 'U'].index(s) for s in stokes]
return stokes, speclist, istokes, ispecs
def GetBinningMatrix( def GetBinningMatrix(
ellbins, lmax, norm=False, polar=True, ellbins, lmax, norm=False, polar=True,
temp=False, EBTB=False): temp=False, corr=False):
""" """
Return P (m,n) and Q (n,m) binning matrices such that Return P (m,n) and Q (n,m) binning matrices such that
Cb = P.Cl and Vbb = P.Vll.Q with m the number of bins and Cb = P.Cl and Vbb = P.Vll.Q with m the number of bins and
...@@ -171,7 +190,7 @@ def GetBinningMatrix( ...@@ -171,7 +190,7 @@ def GetBinningMatrix(
If True, get Stokes parameters for polar (default: True) If True, get Stokes parameters for polar (default: True)
temp : bool temp : bool
If True, get Stokes parameters for temperature (default: False) If True, get Stokes parameters for temperature (default: False)
EBTB : bool corr : bool
If True, get Stokes parameters for EB and TB (default: False) If True, get Stokes parameters for EB and TB (default: False)
Returns Returns
...@@ -229,8 +248,7 @@ def GetBinningMatrix( ...@@ -229,8 +248,7 @@ def GetBinningMatrix(
[ 3. 7.] [ 3. 7.]
""" """
# ### define Stokes # ### define Stokes
allStoke, der, ind = getstokes(polar, temp, EBTB) nspec = 1
nder = len(der)
nbins = len(ellbins) - 1 nbins = len(ellbins) - 1
ellmin = np.array(ellbins[0: nbins]) ellmin = np.array(ellbins[0: nbins])
...@@ -248,10 +266,10 @@ def GetBinningMatrix( ...@@ -248,10 +266,10 @@ def GetBinningMatrix(
for i in np.arange(nbins): for i in np.arange(nbins):
masklm.append(((ell[:-1] >= minell[i]) & (ell[:-1] <= maxell[i]))) masklm.append(((ell[:-1] >= minell[i]) & (ell[:-1] <= maxell[i])))
allmasklm = nder*[list(masklm)] allmasklm = nspec*[list(masklm)]
masklM = np.array(sparse.block_diag(allmasklm[:]).toarray()) masklM = np.array(sparse.block_diag(allmasklm[:]).toarray())
binsnorm = np.array( binsnorm = np.array(
nder * [list(np.arange(minell[0], np.max(ellbins)))]).flatten() nspec * [list(np.arange(minell[0], np.max(ellbins)))]).flatten()
binsnorm = binsnorm*(binsnorm+1)/2./np.pi binsnorm = binsnorm*(binsnorm+1)/2./np.pi
P = np.array(masklM)*1. P = np.array(masklM)*1.
...@@ -263,7 +281,7 @@ def GetBinningMatrix( ...@@ -263,7 +281,7 @@ def GetBinningMatrix(
return P, Q, ell, ellval return P, Q, ell, ellval
def extrapolpixwin(nside, Slmax, pixwining=True): def extrapolpixwin(nside, Slmax, pixwin=True):
''' '''
Parameters Parameters
---------- ----------
...@@ -271,7 +289,7 @@ def extrapolpixwin(nside, Slmax, pixwining=True): ...@@ -271,7 +289,7 @@ def extrapolpixwin(nside, Slmax, pixwining=True):
Healpix map resolution Healpix map resolution
Slmax : int Slmax : int
Maximum multipole value computed for the pixel covariance pixel matrix Maximum multipole value computed for the pixel covariance pixel matrix
pixwining : bool pixwin : bool
If True, multiplies the beam window function by the pixel If True, multiplies the beam window function by the pixel
window function. Default: True window function. Default: True
...@@ -293,7 +311,7 @@ def extrapolpixwin(nside, Slmax, pixwining=True): ...@@ -293,7 +311,7 @@ def extrapolpixwin(nside, Slmax, pixwining=True):
[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
1. 1.] 1. 1.]
''' '''
if pixwining: if pixwin:
prepixwin = np.array(hp.pixwin(nside)) prepixwin = np.array(hp.pixwin(nside))
poly = np.polyfit(np.arange(len(prepixwin)), np.log(prepixwin), poly = np.polyfit(np.arange(len(prepixwin)), np.log(prepixwin),
deg=3, w=np.sqrt(prepixwin)) deg=3, w=np.sqrt(prepixwin))
...@@ -318,4 +336,4 @@ if __name__ == "__main__": ...@@ -318,4 +336,4 @@ if __name__ == "__main__":
import doctest import doctest
if np.__version__ >= "1.14.0": if np.__version__ >= "1.14.0":
np.set_printoptions(legacy="1.13") np.set_printoptions(legacy="1.13")
doctest.testmod() doctest.testmod()
\ No newline at end of file
"""
Set of routines to compute the pixel covariance matrix using
Legendre polynomials