Exemple #1
0
def fisher_single(par,v) :
    # v -> seen pixels
    nb=len(par.bins)-1
    npix=hp.nside2npix(par.nside)
    npix_seen=len(par.ip_seen)
    lmax=3*par.nside-1
    larr=np.arange(lmax+1)
    fisher=np.zeros([nb,nb])
    pixsize=4*np.pi/hp.nside2npix(par.nside)
    
    v_map=np.zeros(npix); v_map[par.ip_seen]=v
    vcm1=invert_covar(par,v_map)
    v_lm=hp.map2alm(v_map,iter=0)
    vcm1_lm=hp.map2alm(vcm1,iter=0)
    for iba in np.arange(nb) :
#        print " Row %d"%iba
        transfer=np.zeros(lmax+1); transfer[par.bins[iba]:par.bins[iba+1]]=1.
        v_map2=hp.alm2map(hp.almxfl(v_lm,transfer),par.nside,verbose=False)/pixsize #Q_a * v
        v_map2cm1=invert_covar(par,v_map2) #C^-1 * Q_a * v
        va_lm=hp.map2alm(v_map2cm1,iter=0)
        cl_vcm1_va=(2*larr+1)*hp.alm2cl(vcm1_lm,alms2=va_lm)
        for ibb in np.arange(nb-iba)+iba :
            fisher[iba,ibb]=np.sum(cl_vcm1_va[par.bins[ibb]:par.bins[ibb+1]])/pixsize**2
            if iba!=ibb :
                fisher[ibb,iba]=fisher[iba,ibb]

    return fisher
def estimate_alm(sky_map, lmax, binary_mask=None, pol=False):
    sky_map_masked = mask_map(sky_map, binary_mask, pol=pol)
    if pol:
        alm = hp.map2alm((sky_map_masked[0].filled(), sky_map_masked[1].filled(), sky_map_masked[2].filled()), lmax=lmax)
    else:
        alm = hp.map2alm(sky_map_masked.filled(), lmax=lmax)

    return alm
Exemple #3
0
def map2vlm( m, lmax, iter=0 ):
    alm_re = hp.map2alm( m.real.copy(), lmax=lmax, iter=iter, regression=False )
    alm_im = hp.map2alm( m.imag.copy(), lmax=lmax, iter=iter, regression=False )

    ret = np.zeros( (lmax+1)**2, dtype=np.complex )
    for l in xrange(0, lmax+1):
        ms = np.arange(1,l+1)
        ret[l*l+l]    = alm_re[l] + 1.j * alm_im[l]
        ret[l*l+l+ms] = alm_re[ms * (2*lmax+1-ms)/2 + l] + 1.j * alm_im[ms * (2*lmax+1-ms)/2 + l]
        ret[l*l+l-ms] = (-1)**ms * ( np.conj( alm_re[ms * (2*lmax+1-ms)/2 + l] ) + 1.j * np.conj( alm_im[ms * (2*lmax+1-ms)/2 + l] ) )
    return ret
Exemple #4
0
def sphtrans_real_pol(hpmaps, lmax=None, lside=None):
    """Spherical Harmonic transform of polarisation functions on the sky.

    Accepts real T, Q, U and optionally V like maps, and returns :math:`a^T_{lm}`,
    :math:`a^E_{lm}`, :math:`a^B_{lm}` and :math:`a^V_{lm}`.

    Parameters
    ----------
    hpmaps : np.ndarray[npol, npix]
        An array of Healpix maps, assumed to be T, Q, U and potentially V.
    lmax : scalar, optional
        The maximum l to calculate. If `None` (default), calculate up to 3*nside
        - 1.

    Returns
    -------
    alms[npol, lmax+1, lmax+1] : np.ndarray
        A 3d array of alms, packed as alm[pol, l, m].

    Notes
    -----
    This only includes m >= 0. As these are the transforms of a real field:

    .. math:: a_{l -m} = (-1)^m a_{lm}^*
    """
    if lmax is None:
        lmax = 3*healpy.npix2nside(hpmaps[0].size) - 1

    if lside is None or lside < lmax:
        lside = lmax

    npol = len(hpmaps)

    alms = np.zeros([npol, lside+1, lside+1], dtype=np.complex128)
    hpmaps = np.ascontiguousarray(hpmaps)

    tlms = healpy.map2alm([hpmap for hpmap in hpmaps[:3]],
                          lmax=lmax, use_weights=_weight, iter=_iter)

    for i in range(3):
        alms[i][np.triu_indices(lmax+1)] = tlms[i]

    # Transform Stokes-V
    if npol == 4:
        alms[3][np.triu_indices(lmax+1)] = healpy.map2alm(np.ascontiguousarray(hpmaps[3]),
                                                          lmax=lmax, use_weights=_weight, iter=_iter)

    return alms.transpose((0, 2, 1))
def _interpolate_jones_freq(J_in, freqs, multiway=True, interp_type='spline'):
    """
    A scheme to interpolate the spherical harmonic components of jones matrix elements.
    Does not seem to work well, and is unused.
    """
    nfreq_in = len(freqs)

    if multiway == True:
        J_flat = np.zeros((nfreq_in, npix, 8), dtype='float64')
        for i in range(nfreq_in):
            J_flat[i] = irf.flatten_jones(J_in[i])
        J_in = J_flat

    lmax = 3 * nside -1
    nlm = hp.Alm.getsize(lmax)
    Jlm_in = np.zeros(nfreq_in, nlm, 8)
    for i in range(nfreq_in):
        sht = lambda m: hp.map2alm(m, lmax=lmax)
        Jlm_in[i,:,:] = (np.asarray(map(sht, J_in.T))).T

    Jlm_out = np.zeros(p.nfreq, nlm, 8)
    for lm in range(nlm):
        for j in range(8):
            Jlmj_re = np.real(Jlm_in[:,lm,j])
            Jlmj_im = np.imag(Jlm_in[:,lm,j])

            a = interpolate_pixel(Jlmj_re, freqs, p.nu_axis, interp_type=p.interp_type) # note! interpolate_pixel function no longer exists
            b = interpolate_pixel(Jlmj_im, freqs, p.nu_axis, interp_type=p.interp_type)
            Jlm_out[:, lm, j] = a + 1j*b
Exemple #6
0
def get_Tlm(filename='test_tlm.npy',
            Imap=None, mask=None,
            add_beam=None,div_beam=None,
            healpy_format=False,
            lmax=100, recalc=False,
            filtermap=False, l0=None,
            save=False):
    """computes and saves 2d alms from a given
    map and mask, corrected for sqrt(fsky), beams added or divided by choice.
    """
    if not recalc and os.path.exists(data_path + filename):
        Tlm2d = np.load(data_path + filename)
        return Tlm2d
    fsky = mask.sum() / len(mask)
    Tlm = hp.map2alm(Imap * mask, lmax=lmax) / np.sqrt(fsky)
    if add_beam is not None:
        hp.sphtfunc.almxfl(Tlm, add_beam, inplace=True)
    if div_beam is not None:
        hp.sphtfunc.almxfl(Tlm, 1./div_beam, inplace=True)

    if not healpy_format:
        ls, ms = hp.sphtfunc.Alm.getlm(lmax, np.arange(len(Tlm)))
        Tlm = make2d_alm_square(Tlm, lmax, ls, ms)
    if save:
        np.save(data_path + filename, Tlm)
    return Tlm
Exemple #7
0
def taylor_interpol_iter(m, pos, order=3, verbose=False, lmax=None):
        """Given a healpix map m[npix], and a set of positions                                       
        pos[{theta,phi},...], evaluate the values at those positions                                 
        using harmonic Taylor interpolation to the given order (3 by                                 
        default). Successively yields values for each cumulative order                               
        up to the specified one. If verbose is specified, it will print                              
        progress information to stderr."""
        nside = hp.npix2nside(m.size)
        if lmax is None: lmax = 3*nside
        # Find the healpix pixel centers closest to pos,                                             
        # and our deviation from these pixel centers.                                                
        ipos = hp.ang2pix(nside, pos[0], pos[1])
        pos0 = np.array(hp.pix2ang(nside, ipos))
        dpos = pos[:2]-pos0
        # Take wrapping into account                                                                 
        bad = dpos[1]>np.pi
        dpos[1,bad] = dpos[1,bad]-2*np.pi
        bad = dpos[1]<-np.pi
        dpos[1,bad] = dpos[1,bad]+2*np.pi

        # Since healpix' dphi actually returns dphi/sintheta, we choose                              
        # to expand in terms of dphi*sintheta instead.                                               
        dpos[1] *= np.sin(pos0[0])
        del pos0

        # We will now Taylor expand our healpix field to                                             
        # get approximations for the values at our chosen                                            
        # locations. The structure of this section is                                                
        # somewhat complicated by the fact that alm2map_der1 returns                                 
        # two different derivatives at the same time.                                                
        derivs = [[m]]
        res = m[ipos]
        yield res
        for o in range(1,order+1):
                # Compute our derivatives                                                            
                derivs2 = [None for i in range(o+1)]
                used    = [False for i in range(o+1)]
                # Loop through previous level in steps of two (except last)                          
                if verbose: tprint("order %d" % o)
                for i in range(o):
                        # Each alm2map_der1 provides two derivatives, so avoid                       
                        # doing double work.                                                         
                        if i < o-1 and i % 2 == 1:
                                continue
                        a = hp.map2alm(derivs[i], use_weights=True, lmax=lmax, iter=0)
                        derivs[i] = None
                        dtheta, dphi = hp.alm2map_der1(a, nside, lmax=lmax)[-2:]
                        derivs2[i:i+2] = [dtheta,dphi]
                        del a, dtheta, dphi
                        # Use these to compute the next level                                        
                        for j in range(i,min(i+2,o+1)):
                                if used[j]: continue
                                N = comb(o,j)/factorial(o)
                                res += N * derivs2[j][ipos] * dpos[0]**(o-j) * dpos[1]**j
                                used[j] = True
                                # If we are at the last order, we don't need to waste memory         
                                # storing the derivatives any more                                   
                                if o == order: derivs2[j] = None
                derivs = derivs2
                yield res
Exemple #8
0
 def test_map2alm_pol_gal_cut(self):
     tmp = [np.empty(o.size * 2) for o in self.mapiqu]
     for t, o in zip(tmp, self.mapiqu):
         t[::2] = o
     maps = [
         self.mapiqu,
         [o.astype(np.float32) for o in self.mapiqu],
         [t[::2] for t in tmp],
     ]
     for use_weights in [False, True]:
         for input in maps:
             gal_cut = 30
             nside = hp.get_nside(input)
             npix = hp.nside2npix(nside)
             gal_mask = (
                 np.abs(hp.pix2ang(nside, np.arange(npix), lonlat=True)[1]) < gal_cut
             )
             alm = hp.map2alm(
                 input, iter=10, use_weights=use_weights, gal_cut=gal_cut
             )
             output = hp.alm2map(alm, 32)
             for i, o in zip(input, output):
                 # Testing requires low tolerances because of the
                 # mask boundary
                 i[gal_mask] = 0
                 np.testing.assert_allclose(i, o, atol=1e-2)
def getReconstructionsAligned(idShell, Ilm_na, Lgrid, Lmax=7, vmin = 1e-4, nside=128):

    Ir = np.zeros((hp.nside2npix(nside), np.size(idShell)))
    for id, Ilm in enumerate(Ilm_na):
        Ilm = np.ascontiguousarray(Ilm)

        if id == 0:
            '''align the first shell with theoretically calculated intensity'''
            Ith = np.load('first_shell_to_start_alignment.npy')

            almth = hp.map2alm(Ith, Lmax-1)

            almr = alignShells(almth, Ilm, Lmax-1, Lgrid)

            rI = hp.alm2map(almr, nside)
            neg = np.where(rI < 0)
            rI[neg] = vmin
            Ir[:, 0] = rI

        else:
            ''' Align the succesive shells with respect to each preceding shell
            '''

            almr = alignShells(almr, Ilm, Lmax-1, Lgrid)
            rI = hp.alm2map(almr, nside)
            neg = np.where(rI < 0)
            rI[neg] = vmin
            Ir[:, id] = rI

    return Ir
Exemple #10
0
	def VisM( self ) : 
		'''
		Use BeamLM and TmapLM to calculate V(m)/vism
		vism is the FFT of vis

		return:
			self.vism = [V(m), V(-m)]  # NOT conj
			self.vism.shape = (2, Npoint, 1, Nv, lmax+1)
		                                 Nfreq=1
		'''
		if (self.verbose) : print 'Tmap2Vis.VisM \n'
		lmax, shape = self.lmax, self.beamlm.shape
		Np, Nv, Nl = shape[1], shape[2], shape[4]
		self.vism = np.zeros([2, Np, 1, Nv, Nl], complex)
		almT = hp.map2alm(self.Tmap, lmax)
		#--------------------------------------------------
		l = np.arange(lmax+1)
		m = l.copy()
		# For Tmap ('Alm')
		idxT = jp.LM2Index('Alm', lmax, l, m, '2D')[0]
		idxT = jp.Invalid(idxT , 0).astype(int).T  # ml
		#--------------------------------------------------
		self.vism[0,:,0,:,:] = (self.beamlm[0] * almT[idxT][None,None,:,:]).sum(-1)
		self.vism[1,:,0,:,:] = np.conj( (self.beamlm[1] * almT[idxT][None,None,:,:]).sum(-1) )
		return self.vism
Exemple #11
0
def sphtrans_real(hpmap, lmax=None, lside=None):
    """Spherical Harmonic transform of a real map.

    Parameters
    ----------
    hpmap : np.ndarray
        A Healpix map.
    lmax : scalar, optional
        The maximum l to calculate. If `None` (default), calculate up
        to 3*nside - 1.

    Returns
    -------
    alm : np.ndarray
        A 2d array of alms, packed as alm[l,m].

    Notes
    -----
    This only includes m > 0. As this is the transform of a real field:

    .. math:: a_{l -m} = (-1)^m a_{lm}^*
    """
    if lmax == None:
        lmax = 3*healpy.npix2nside(hpmap.size) - 1

    if lside == None or lside < lmax:
        lside = lmax

    alm = np.zeros([lside+1, lside+1], dtype=np.complex128)

    tlm = healpy.map2alm(np.ascontiguousarray(hpmap), lmax=lmax, use_weights=_weight, iter=_iter)

    alm[np.triu_indices(lmax+1)] = tlm

    return alm.T
Exemple #12
0
def rotate_map_to_axis(m, ra, dec, nest=False, method="direct"):
    """Rotate a sky map to place a given line of sight on the +z axis.

    Parameters
    ----------

    m : np.ndarray
        The input HEALPix array.

    ra : float
        Right ascension of axis in radians.

        To specify the axis in geocentric coordinates, supply ra=(lon + gmst),
        where lon is the geocentric longitude and gmst is the Greenwich mean
        sidereal time in radians.

    dec : float
        Declination of axis in radians.

        To specify the axis in geocentric coordinates, supply dec=lat,
        where lat is the geocentric latitude in radians.

    nest : bool, default=False
        Indicates whether the input sky map is in nested rather than
        ring-indexed HEALPix coordinates (default: ring).

    method : 'direct' or 'fft'
        Select whether to use spherical harmonic transformation ('fft') or
        direct coordinate transformation ('direct')

    Returns
    -------

    m_rotated : np.ndarray
        The rotated HEALPix array.
    """
    npix = len(m)
    nside = hp.npix2nside(npix)

    theta = 0.5 * np.pi - dec
    phi = ra

    if method == "fft":
        if nest:
            m = hp.reorder(m, n2r=True)
        alm = hp.map2alm(m)
        hp.rotate_alm(alm, -phi, -theta, 0.0)
        ret = hp.alm2map(alm, nside, verbose=False)
        if nest:
            ret = hp.reorder(ret, r2n=True)
    elif method == "direct":
        R = hp.Rotator(rot=np.asarray([0, theta, -phi]), deg=False, inv=False, eulertype="Y")
        theta, phi = hp.pix2ang(nside, np.arange(npix), nest=nest)
        ipix = hp.ang2pix(nside, *R(theta, phi), nest=nest)
        ret = m[ipix]
    else:
        raise ValueError("Unrecognized method: {0}".format(method))

    return ret
Exemple #13
0
 def harmonic_ud_grade(m, nside_in, nside_out):
     """
     Decompose a map at a resolution nside_in into spherical harmonic components
     and then resynthesize the map at nside_out.
     """
     lmax = 3 * nside_in - 1
     alm = hp.map2alm(m, lmax=lmax)
     return hp.alm2map(alm, nside_out, lmax=lmax, verbose=False)
Exemple #14
0
 def test_rotate_alm(self):
     almigc = hp.map2alm(self.mapiqu)
     alms = [almigc[0], almigc[0:2], almigc, np.vstack(almigc)]
     for i in alms:
         o = deepcopy(i)
         hp.rotate_alm(o, 0.1, 0.2, 0.3)
         hp.rotate_alm(o, -0.3, -0.2, -0.1)
         np.testing.assert_allclose(i, o, rtol=1e-6)
def A_matrix_func(data):
    """
    cf eq 25 of eriksen 2004
    """
    Chalf = np.sqrt(data.cl_th[: data.lmax + 1]) * data.beam[: data.lmax + 1]
    map2 = hp.alm2map(hp.almxfl(real2complex_alm(data.alm), Chalf), data.nside) * data.invvar
    alm2 = hp.almxfl(hp.map2alm(map2, data.lmax, use_weights=False) * hp.nside2npix(data.nside) / 4.0 / np.pi, Chalf)
    return data.alm + complex2real_alm(alm2)
Exemple #16
0
 def direct(self, input, output):
     if input.ndim == 1:
         input = input[:, None]
         output = output[:, None]
     for i, o in zip(input.T, output.T):
         ialm = hp.map2alm(i)
         alm_smoothed = hp.almxfl(ialm, self.fl)
         o[...] = hp.alm2map(alm_smoothed, hp.npix2nside(len(i)))# * \
def rs_w1_matrix_func(data, w1):
    """
    cf eq 25 of eriksen 2004
    """
    Chalf = np.sqrt(data.cl_th[: data.lmax]) * data.beam[: data.lmax]
    map2 = w1 * np.sqrt(data.invvar)
    # map2 = hp.alm2map(real2complex_alm(data.alm),data.nside)*np.sqrt(data.invvar)
    alm2 = hp.almxfl(hp.map2alm(map2, data.lmax, use_weights=False) * hp.nside2npix(data.nside) / 4.0 / np.pi, Chalf)
    return complex2real_alm(alm2)
Exemple #18
0
def correlation2pt(map1, map2=None, npoints=100):

    if map2 is None:
        map2 = map1
    alm1 = hp.map2alm(map1)
    alm2 = hp.map2alm(map2)
    clcross = hp.alm2cl(alm1, alm2)

    
    thetas = np.linspace(0., np.pi, npoints)
    corr = np.zeros(npoints)
    
    for i,theta in enumerate(thetas):
        for l,cl in enumerate(clcross):
            lfactor = (2*l + 1.)/(4.*np.pi)  * scipy.special.lpmv(0, l, np.cos(theta))
            corr[i] += lfactor * cl

    return thetas, corr
Exemple #19
0
def hires_healpixellize(map_data,theta,phi,nside_interp,nside_synth):
    
    map = healpixellize(map_data,theta,phi,nside_interp)
    
    lmax = 3*nside_interp-1
#    l,m=hp.Alm.getlm(lmax)
    alm = hp.map2alm(map,lmax=lmax)
    
    return hp.alm2map(alm,nside_synth,lmax=lmax)
Exemple #20
0
 def test_rotate_alm(self):
     almigc = hp.map2alm(self.mapiqu)
     alms = [almigc[0], almigc[0:2], almigc, np.vstack(almigc)]
     for i in alms:
         o = deepcopy(i)
         hp.rotate_alm(o, 0.1, 0.2, 0.3)
         hp.rotate_alm(o, -0.3, -0.2, -0.1)
         # FIXME: rtol=1e-6 works here, except on Debian with Python 3.4.
         np.testing.assert_allclose(i, o, rtol=1e-5)
def TQU_TO_TEB_maps(sky_TQU, lmax):
    nside = hp.get_nside(sky_TQU)

    alm_in = hp.map2alm(sky_TQU, lmax, pol=True)

    sky_TEB = np.empty((3, 12*nside**2))
    for i in range(3):
        sky_TEB[i] = hp.alm2map(alm_in[i], nside, pol=False)

    return sky_TEB
Exemple #22
0
	def BeamLmax( self, verbose=None ) : 
		'''
		Decompose BeamMapComplex (with phase) to lm with lmax

		return:
			self.beamlmax.shape = (Npointing, Nv, (lmax+1)**2)
		'''
		if (self.verbose) : print 'Tmap2Vis.BeamLmax'
		if (verbose is None) : verbose = self.verbose
		lmax = self.lmax
		l = np.arange(lmax+1)
		# Negetive
		mn = np.arange(-l.max(), 0)
		idxdsn, morder = jp.LM2Index('almds', lmax, l,  mn)[:2]
		idxmn          = jp.LM2Index('alm'  , lmax, l, -mn)[0]
		# Positive
		mp = np.arange(0, l.max()+1)
		idxdsp = jp.LM2Index('almds', lmax, l, mp)[0]
		idxmp  = jp.LM2Index('alm'  , lmax, l, mp)[0]
		#--------------------------------------------------
		self.beamlmax = []
		for i in xrange(self.pointing.size) : 
			verbose0 = self.verbose
			self.verbose = verbose
			Bmap = self.BeamMap(self.pointing[i], self.fwhm)
			Bmap = self.BeamMapComplex(0, Bmap, self.freq)[0]
			self.verbose = verbose0
			beamlmtmp = []
			#--------------------------------------------------
			for j in xrange(Bmap.shape[0]) : 
				almR = hp.map2alm(Bmap[j].real, lmax)
				almI = hp.map2alm(Bmap[j].imag, lmax)
				almds = np.zeros((lmax+1)**2, complex)  # =beamlm
				#--------------------------------------------------
				almds[idxdsn] = (-1)**morder * (np.conj(almR[idxmn]) + 1j*np.conj(almI[idxmn]))
				almds[idxdsp] = almR[idxmp] + 1j*almI[idxmp]
				#--------------------------------------------------
				beamlmtmp.append( almds )
			self.beamlmax.append(np.array(beamlmtmp)) # (Nv, lmax)
		self.beamlmax = np.array(self.beamlmax)
		if (self.verbose) : print
		return self.beamlmax  # (Npoint, Nv, (lmax+1)**2)
Exemple #23
0
def harmonic_ud_grade(hmap, nside):
    """
    Decompose a map at a resolution nside_in into spherical harmonic components
    and then resynthesize the map at nside_out.
    """
    npix_in = len(hmap)
    nside_in = hp.npix2nside(npix_in)
    lmax_in = 3 * nside_in - 1
    hmap_out = hp.alm2map(hp.map2alm(hmap, lmax=lmax_in), nside, verbose=False)

    return hmap_out
Exemple #24
0
    def apply_ivf(self, det, tmap, pmap):
        mask_t = qcinv.util.load_map(self.mask_t)

        bl     = self.bl
        pxw    = hp.pixwin(self.nside)[0:self.lmax+1]

        tlm = hp.map2alm( tmap * mask_t, lmax=self.lmax, iter=0, regression=False )

        hp.almxfl( tlm, self.ftl / bl / pxw, inplace=True )
    
        return tlm
Exemple #25
0
def remove_low_l(map1, mask1):
    map1m=hp.ma(map1)
    map1m.mask=np.logical_not(mask1)
    map2=np.ma.masked_values(map1, UNSEEN)
    alm2=hp.map2alm(map2, lmax=LMAX)
    fac2=np.array([1.]*(LMAX+1))
    fac2[0:lREM]=0.
    alm2n=hp.sphtfunc.almxfl(alm2, fac2, inplace=False)
    map2new=hp.ma(hp.alm2map(alm2n, 2048))
    map2new.mask=np.logical_not(mask1)
    return map2new
Exemple #26
0
 def test_map2alm_pol(self):
     tmp = [np.empty(o.size*2) for o in self.mapiqu]
     for t, o in zip(tmp, self.mapiqu):
         t[::2] = o
     maps = [self.mapiqu, [o.astype(np.float32) for o in self.mapiqu],
             [t[::2] for t in tmp]]
     for input in maps:
         alm = hp.map2alm(input, iter=10)
         output = hp.alm2map(alm, 32)
         for i, o in zip(input, output):
             np.testing.assert_allclose(i, o, atol=1e-4)
def TEB_to_TQU_maps(sky_TEB, lmax):
    nside = hp.get_nside(sky_TEB)

    alm = np.empty((3, (lmax + 1)**2))
    
    for i in range(3):
        alm[i] = hp.map2alm(sky_TEB[i], lmax, pol=False)

    sky_TQU = hp.alm2map(alm, nside, lmax, pol=True)

    return sky_TQU
Exemple #28
0
def calc_IP2_equil(Imap, Qmap, Umap,
                   lmax=100):

    Tlm = hp.map2alm( Imap )
    Elm,Blm = hp.map2alm_spin( (Qmap,Umap), 2 )
    
    TEE = hp.alm2cl( Tlm, Elm**2 )
    TBB = hp.alm2cl( Tlm, Blm**2 )
    TEB = hp.alm2cl( Tlm, Elm*Blm )

    ls = np.arange( len(TEE) )
    return ls, TEE, TBB, TEB
Exemple #29
0
def calc_prep(map, s_cls, n_inv_filt):
    tmap = np.copy(map)
    n_inv_filt.apply_map(tmap)

    lmax  = len(n_inv_filt.b_transf) - 1
    npix  = len(map)

    alm  = hp.map2alm(tmap, lmax=lmax, iter=0, regression=False)
    alm *= npix / (4.*np.pi)

    hp.almxfl( alm, n_inv_filt.b_transf, inplace=True )
    return alm
Exemple #30
0
    def apply_ivf(self, det, tmap):
        mask_t = qcinv.util.load_map(self.mask_t)

        tlm = hp.map2alm( tmap * mask_t, lmax=self.lmax, iter=0, regression=False )

        bl  = dmc.get_bl(self.sim_lib.year, det)[0:self.lmax+1]
        pxw = hp.pixwin( self.sim_lib.nside )[0:self.lmax+1]

        ftl = self.get_ftl(det)

        hp.almxfl( tlm, ftl / bl / pxw, inplace=True )

        return tlm
def iqu2teb(IQU, nside, lmax=None):
    alms = hp.map2alm(IQU, lmax=lmax, pol=True)
    return hp.alm2map(alms, nside=nside, lmax=lmax, pol=False)
Exemple #32
0
sigma_0 = np.array([1.429, 1.466, 2.188, 3.131, 6.544])  # unit: mk
N_obs = [
    hp.read_map(data_path + 'wmap_band_smth_imap_r9_3yr_' + band + '_v2.fits',
                field=(1)) for band in band_name
]
sigma = sigma_0.reshape(5, -1) / np.sqrt(N_obs)
n_realization = 100
noise = 0
len_pix = len(sigma[0])
lmax = 1024
nside = 512
fwhm = [0.88, 0.66, 0.51, 0.35, 0.22]  # degree
n_band = len(band_name)
alm_comb_n_smoothed = []

alm_comb_n = np.array([hp.map2alm(np.random.normal(nu,sigma[i],\
    (n_realization,len_pix)),lmax=lmax,pol=False) for i in np.arange(n_band)])
np.save('alm_comb_noise', alm_comb_n)


def beam_func(fwhm):
    '''
    return a gaussian beam with specific fwhm and lmax
    '''
    return hp.gauss_beam(fwhm=np.radians(fwhm), lmax=lmax)


#one can also try hp.almxfl function to compute alm times B_ell
def main():
    '''
    smoothing noise maps to the same angular resolution (1 degree)
    '''
Exemple #33
0
 def _get_sim_a_p(self, idx, joint=False, swapped=False):
     """Polarization rotation estimator. """
     Q1, U1 = self.f2map1.get_irespmap(idx) if not swapped else self.f2map2.get_irespmap(idx)
     Q2, U2 = (self.f2map2.get_pmap(idx, joint=joint) if not swapped else self.f2map1.get_pmap(idx, joint=joint))
     return -4. * hp.map2alm(Q1 * U2 -  U1 * Q2 , lmax=self.get_lmax_qlm('P'), iter=0)
Exemple #34
0
import matplotlib.pyplot as plt
import fitsio as fs
import gc

Np = hp.nside2npix(2048)
freqs = np.array([100, 143, 217, 353, 545, 857])
h = np.zeros((10, 8192))
maps = np.zeros((6, Np))
maps_filtered = np.zeros((6, Np))
alm = np.zeros((6, 33558528), dtype=complex)

localpath = '/home/yanza15/research/Ghinshaw/ILC/'

for j in range(6):
    freq = freqs[j]
    maps[j] = pf.open('/data/yanza15/sky_map/SkyMap_' + str(freqs[j]) +
                      '_2048_full_i_smth10.fits')[1].data['I'].ravel()
    alm[j] = hp.map2alm(maps[j])
del maps
gc.collect()

for i in range(10):
    h[i] = np.loadtxt(localpath + 'results/needlet_' + str(i) + '.txt')[0:6142]
    for j in range(6):
        alm_filtered = hp.almxfl(alm[j], h[i])
        maps_filtered[j] = hp.alm2map(alm_filtered, 2048)
        hp.write_map(
            localpath + 'maps/Planck_maps/Sky_maps/HFI_SkyMap_' +
            str(freqs[j]) + '_2048_R2.02_full_i_smth10_needlet' + str(i) +
            '.fits', maps_filtered[j])
Exemple #35
0
def taylor_interpol_iter(m, pos, order=3, verbose=False, lmax=None):
    """Given a healpix map m[npix], and a set of positions
    pos[{theta,phi},...], evaluate the values at those positions using
    harmonic Taylor interpolation to the given order (3 by
    default). Successively yields values for each cumulative order up
    to the specified one. If verbose is specified, it will print
    progress information to stderr.

    """
    nside = hp.npix2nside(m.size)
    if lmax is None:
        lmax = 3 * nside
    # Find the healpix pixel centers closest to pos,
    # and our deviation from these pixel centers.
    ipos = hp.ang2pix(nside, pos[0], pos[1])
    pos0 = np.array(hp.pix2ang(nside, ipos))
    dpos = pos[:2] - pos0
    # Take wrapping into account
    bad = dpos[1] > np.pi
    dpos[1, bad] = dpos[1, bad] - 2 * np.pi
    bad = dpos[1] < -np.pi
    dpos[1, bad] = dpos[1, bad] + 2 * np.pi

    # Since healpix' dphi actually returns dphi/sintheta, we choose
    # to expand in terms of dphi*sintheta instead.
    dpos[1] *= np.sin(pos0[0])
    del pos0

    # We will now Taylor expand our healpix field to
    # get approximations for the values at our chosen
    # locations. The structure of this section is
    # somewhat complicated by the fact that alm2map_der1 returns
    # two different derivatives at the same time.
    derivs = [[m]]
    res = m[ipos]
    yield res
    for o in range(1, order + 1):
        # Compute our derivatives
        derivs2 = [None for i in range(o + 1)]
        used = [False for i in range(o + 1)]
        # Loop through previous level in steps of two (except last)
        if verbose:
            print("order %d" % o)
        for i in range(o):
            # Each alm2map_der1 provides two derivatives, so avoid
            # doing double work.
            if i < o - 1 and i % 2 == 1:
                continue
            a = hp.map2alm(derivs[i], use_weights=True, lmax=lmax, iter=0)
            derivs[i] = None
            dtheta, dphi = hp.alm2map_der1(a, nside, lmax=lmax)[-2:]
            derivs2[i:i + 2] = [dtheta, dphi]
            del a, dtheta, dphi
            # Use these to compute the next level
            for j in range(i, min(i + 2, o + 1)):
                if used[j]:
                    continue
                N = comb(o, j) / factorial(o)
                res += N * derivs2[j][ipos] * dpos[0]**(o - j) * dpos[1]**j
                used[j] = True
                # If we are at the last order, we don't need to waste memory
                # storing the derivatives any more
                if o == order:
                    derivs2[j] = None
        derivs = derivs2
        yield res
Exemple #36
0
def udgrade(x,nside_out):
    return hp.alm2map(hp.map2alm(x),nside_out, verbose=False)
Exemple #37
0
def plotCl(healpixMapFileName,
           figureTitleNamePre,
           saveNamePre,
           pointCount,
           SIGNAL_DATA,
           MY_DOUBLE,
           spannedAngle,
           fignum,
           plotting=True,
           **kwargs):
    """ plot the angular power spectrum coeff

    SIGNAL_DATA, MY_DOUBLE no usage

    Parameters:
    --------
    healpixMapFileName: str
        name of the data file

    figureTitleNamePre: str
        prefix of the title
        
    saveNamePre: str
        prefix of the to be saved figures
    
    return:
        dictionary, {'Nside', 'cl','pointsCount'}
    """
    global figX, figY
    if (plotting == True):
        corrName = "spanned:%.4f" % (spannedAngle) + " points:" + str(
            pointCount)

        additionName = ""
        titleAddition = ""
        count = 0
        for key, value in kwargs.items():
            additionName += key + ":" + str(value)
            titleAddition += key + ":" + str(value) + ", "
            count += 1
            if (count % 3 == 0):
                titleAddition += "\n"

        figureName = figureTitleNamePre + ' ' + corrName + "\n" + titleAddition
        saveName = saveNamePre + corrName + additionName
    hp1 = []
    for line in open(healpixMapFileName, 'r'):
        # if(SIGNAL_DATA == False or MY_DOUBLE == False):
        #     values = [float(s) for s in line.split()]
        # else:
        values = [float(s) for s in line.split()]
        hp1.append(values[0])

    hpOrder = int(hp1[0])
    hpPointCount = int(hp1[1])
    pixVal = hp1[2:]
    pixValLen = len(pixVal)
    Nside = 2**hpOrder
    Npixel = hp.nside2npix(Nside)
    pixVal.extend([0] * (int(Npixel) - pixValLen))
    pixVal = np.array(pixVal)
    if (hpPointCount <= 1):
        return None
    LMAX = Nside * 2
    cl = hp.anafast(pixVal, lmax=LMAX)
    alm = hp.map2alm(pixVal, lmax=LMAX)
    ell = np.arange(len(cl))
    coeff = (cl * (2 * ell + 1)) / (
        (hpPointCount)**2) * 4 * np.pi - 1 / (hpPointCount)

    coeff2 = (cl * (2 * ell + 1)) / ((hpPointCount)**2)

    if (plotting == True):
        xMax = LMAX

        plt.figure(figsize=(figX, figY))
        fig1, ax1 = plt.subplots(num=fignum + 1)
        plt.xlim(0, LMAX)

        ax1.xaxis.set_major_locator(
            plt.MultipleLocator(xMax / CL_MAJOR_TICKS_NUM))
        ax1.xaxis.set_minor_locator(
            plt.MultipleLocator(xMax / (CL_MAJOR_TICKS_NUM * 2)))
        ax1.plot(ell, ell * (ell + 1) * cl)
        plt.xlabel("$\ell$")
        plt.ylabel("$\ell(\ell+1)C_{\ell}$")
        plt.grid()
        saveName1 = saveName + "LMAX:" + str(LMAX) + "CLcoeff_llp1.png"
        figureName1 = figureName + "LMAX:" + str(LMAX) + " l(l+1)_normalized"
        plt.title(figureName1, fontsize=8, y=0.99)
        plt.savefig(saveName1)
        # print(alm[0])

        plt.figure(figsize=(figX, figY))
        fig2, ax2 = plt.subplots(num=fignum + 2)
        ax2.xaxis.set_major_locator(
            plt.MultipleLocator(xMax / CL_MAJOR_TICKS_NUM))
        ax2.xaxis.set_minor_locator(
            plt.MultipleLocator(xMax / (CL_MAJOR_TICKS_NUM * 2)))

        normedCl = (2 * ell + 1) * cl
        ax2.plot(ell, normedCl)
        plt.xlabel("$\ell$")
        plt.ylabel("$(2\ell+1)C_{\ell}$")
        plt.grid()
        saveName2 = saveName + "LMAX:" + str(LMAX) + "CLcoeff_2lp1.png"
        figureName2 = figureName + "LMAX:" + str(LMAX) + " (2l+1)_normalized"
        plt.title(figureName2, fontsize=8, y=0.99)

        #find local max
        indexX = localMax(ell, normedCl, ax2)
        maxXVal = ell[indexX]
        maxYVal = normedCl[indexX]
        plt.stem(maxXVal, maxYVal, linefmt='b--', markerfmt='ro', basefmt=' ')
        plt.savefig(saveName2)

        plt.figure(figsize=(figX, figY))
        fig3, ax3 = plt.subplots(num=fignum + 3)
        ax3.xaxis.set_major_locator(
            plt.MultipleLocator(xMax / CL_MAJOR_TICKS_NUM))
        ax3.xaxis.set_minor_locator(
            plt.MultipleLocator(xMax / (CL_MAJOR_TICKS_NUM * 2)))
        ax3.plot(ell, coeff)
        plt.xlabel("$\ell$")
        plt.ylabel("$\widehat{c_{\ell}}$")
        plt.grid()
        saveName3 = saveName + "LMAX:" + str(LMAX) + "CLcoeff_coeff.png"
        figureName3 = figureName + "LMAX:" + str(LMAX) + " legendre coeff"
        plt.title(figureName3, fontsize=8, y=0.99)
        plt.savefig(saveName3)

        plt.figure(figsize=(figX, figY))
        fig4, ax4 = plt.subplots(num=fignum + 4)
        ax4.xaxis.set_major_locator(
            plt.MultipleLocator(xMax / CL_MAJOR_TICKS_NUM))
        ax4.xaxis.set_minor_locator(
            plt.MultipleLocator(xMax / (CL_MAJOR_TICKS_NUM * 2)))
        ax4.plot(ell, cl)
        plt.xlabel("$\ell$")
        plt.ylabel("$C_{\ell}$")
        plt.grid()
        saveName4 = saveName + "LMAX:" + str(LMAX) + "CLcoeff_rawCL.png"
        figureName4 = figureName + "LMAX:" + str(LMAX) + " Cl"
        plt.title(figureName4, fontsize=8, y=0.99)
        plt.savefig(saveName4)
    else:
        return {'Nside': Nside, 'cl': cl, 'pointsCount': hpPointCount}
Exemple #38
0
def map2alm_spin(maps, spin, lmax=None, mmax=None):
    assert spin >= 0, spin
    if spin > 0:
        return hp.map2alm_spin(maps, spin, lmax=lmax, mmax=mmax)
    else:
        return -hp.map2alm(maps[0], lmax=lmax, mmax=mmax, iter=0), 0.
Exemple #39
0
allmaps = sky.signal()(nu)

if verbose:
    print('Microwave sky simulated')

T = np.array(allmaps[0], dtype=float) * mapFactor
Q = np.array(allmaps[1], dtype=float) * mapFactor
U = np.array(allmaps[2], dtype=float) * mapFactor
#smoothing with gaussian psf
t = hp.smoothing(map_in=T, fwhm=fwhmrad, verbose=False)
q = hp.smoothing(map_in=Q, fwhm=fwhmrad, verbose=False)
u = hp.smoothing(map_in=U, fwhm=fwhmrad, verbose=False)
if verbose:
    print('Maps smoothed')

alms = hp.map2alm([t, q, u])
emap = hp.alm2map(alms[1], nside=nside, verbose=False)
bmap = hp.alm2map(alms[2], nside=nside, verbose=False)


def vec2pix(x, y, z):
    """
    Auxiliar function for projection.
    """
    return hp.vec2pix(nside, x, y, z)


epatches = []
bpatches = []
for h in np.arange(0, len(pos), 1, dtype=int):
    #coordinate limits that define each patch
 def dot(self, tmap):
     npix = len(tmap)
     return alm_to_xyz(hp.map2alm(tmap, lmax=1, iter=0)) * npix / 3.
def rotate_map_to_axis(m, ra, dec, nest=False, method='direct'):
    """Rotate a sky map to place a given line of sight on the +z axis.

    Parameters
    ----------

    m : np.ndarray
        The input HEALPix array.

    ra : float
        Right ascension of axis in radians.

        To specify the axis in geocentric coordinates, supply ra=(lon + gmst),
        where lon is the geocentric longitude and gmst is the Greenwich mean
        sidereal time in radians.

    dec : float
        Declination of axis in radians.

        To specify the axis in geocentric coordinates, supply dec=lat,
        where lat is the geocentric latitude in radians.

    nest : bool, default=False
        Indicates whether the input sky map is in nested rather than
        ring-indexed HEALPix coordinates (default: ring).

    method : 'direct' or 'fft'
        Select whether to use spherical harmonic transformation ('fft') or
        direct coordinate transformation ('direct')

    Returns
    -------

    m_rotated : np.ndarray
        The rotated HEALPix array.
    """
    npix = len(m)
    nside = hp.npix2nside(npix)

    theta = 0.5 * np.pi - dec
    phi = ra

    if method == 'fft':
        if nest:
            m = hp.reorder(m, n2r=True)
        alm = hp.map2alm(m)
        hp.rotate_alm(alm, -phi, -theta, 0.0)
        ret = hp.alm2map(alm, nside, verbose=False)
        if nest:
            ret = hp.reorder(ret, r2n=True)
    elif method == 'direct':
        R = hp.Rotator(
            rot=np.asarray([0, theta, -phi]),
            deg=False, inv=False, eulertype='Y')
        theta, phi = hp.pix2ang(nside, np.arange(npix), nest=nest)
        ipix = hp.ang2pix(nside, *R(theta, phi), nest=nest)
        ret = m[ipix]
    else:
        raise ValueError('Unrecognized method: {0}'.format(method))

    return ret
Exemple #42
0
def lens_tlm(tlm,dlm,lmaxout = None,verbose = True,nside = 2048,nband = 16,facres = 0,iter = 0):
    # FIXME : suppress the stupid intermediate step
    lenmap = tlm2lensmap(nside,tlm,dlm,verbose=verbose,nband=nband,facres=facres)
    return hp.map2alm(lenmap,lmax = (lmaxout or hp.Alm.getlmax(tlm.size)),iter = iter)
def make_ijones_spectrum(parameters_dict, verbose=False):

    p = Parameters(parameters_dict)
    """
    nu_axis: frequency in Hz
    """
    fmax = int(p.nu_axis[-1] / 1e6)
    fmin = int(p.nu_axis[0] / 1e6)
    nfreq = len(p.nu_axis)

    nnodes = fmax - fmin + 1

    nu_nodes = np.array([fmin + x for x in range(nnodes)])

    lmax = 3 * p.nside - 1
    nlm = hp.Alm.getsize(lmax)
    joneslm = np.zeros((nnodes, nlm, 2, 2, 2), dtype=np.complex128)

    sht = lambda x: hp.map2alm(x, lmax=lmax)

    comp = [np.real, np.imag]
    u = [1, 1j]

    if verbose == True:
        print "Freq. min/max:", fmin, fmax
        print "nnodes: ", nnodes
        print "len(nu_nodes): ", len(nu_nodes)

    # synthesize maps at the nside to be used in the simulation for each frequency node
    # This is necessary because the basis transformation is done at nside 1024 to minimize
    # the topological error at the center of the beam. But for the frequency interpolation
    # this resolution would probably use too much memory.
    # note that the output of jones_f() is a beam with zenith at -31 deg latitude
    for n in range(nnodes):
        if verbose == True:
            print "Loading jones node ", n
        jones_node = jones_f(nu_nodes[n], p.nside)
        for i in range(2):
            for j in range(2):
                for k in range(2):
                    joneslm[n, :, i, j, k] = sht(comp[k](jones_node[:, i, j]))

    joneslm_re = joneslm.real
    joneslm_im = joneslm.imag

    if verbose == True:
        print joneslm_re.shape
        print joneslm_im.shape
        print nu_nodes.shape

    interpolant_re = interpolate.interp1d(nu_nodes,
                                          joneslm_re,
                                          kind='cubic',
                                          axis=0)
    interpolant_im = interpolate.interp1d(nu_nodes,
                                          joneslm_im,
                                          kind='cubic',
                                          axis=0)

    freqs_out = p.nu_axis / 1e6

    joneslm_re_int = interpolant_re(freqs_out)
    joneslm_im_int = interpolant_im(freqs_out)

    joneslm_int = joneslm_re_int + 1j * joneslm_im_int

    # now we just need to resynthesize at each frequency and we're done
    isht = lambda x: hp.alm2map(
        np.ascontiguousarray(x), p.nside, verbose=False)

    z0_cza = np.radians(120.7215)

    ijones = np.zeros((p.nfreq, p.npix, 2, 2), dtype=np.complex128)
    for n in range(p.nfreq):
        for i in range(2):
            for j in range(2):
                ijones[n, :, i, j] = isht(
                    joneslm_int[n, :, i, j,
                                0]) + 1j * isht(joneslm_int[n, :, i, j, 1])

        If = abs(ijones[n, :, 0, 0])**2. + abs(ijones[n, :, 0, 1])**2. + abs(
            ijones[n, :, 0, 1])**2. + abs(ijones[n, :, 1, 0])**2
        norm = np.sqrt(np.amax(If))
        ijones[n] /= norm

        ijones[n] *= horizon_mask(ijones[n].squeeze(), z0_cza)

        if verbose == True:
            print "norm is:", norm

    return ijones
def teb2iqu(TEB, nside, lmax=None):
    alms = hp.map2alm(TEB, lmax=lmax, pol=False)
    return hp.alm2map(alms, nside=nside, lmax=lmax, pol=True)
Exemple #45
0
def plotDiffCl(healpixMapFileName, randomMapFileName, figureTitleNamePre,
               saveNamePre, pointCount, SIGNAL_DATA, MY_DOUBLE, spannedAngle,
               fignum, **kwargs):
    """ plot the angular power spectrum coeff subtracted from a randomly generated map

    Parameters:
    --------
    healpixMapFileName: str
        name of the data file

    randomMapFileName: str
        name of the random Map file

    figureTitleNamePre: str
        prefix of the title
        
    saveNamePre: str
        prefix of the to be saved figures
    """
    global figX, figY
    corrName = "spanned:%.4f" % (spannedAngle) + " points:" + str(pointCount)

    additionName = ""
    titleAddition = ""
    count = 0
    for key, value in kwargs.items():
        additionName += key + ":" + str(value)
        titleAddition += key + ":" + str(value) + ", "
        count += 1
        if (count % 3 == 0):
            titleAddition += "\n"

    figureName = figureTitleNamePre + ' ' + corrName + "\n" + titleAddition
    saveName = saveNamePre + corrName + additionName
    hp1 = []
    for line in open(healpixMapFileName, 'r'):
        if (SIGNAL_DATA == False or MY_DOUBLE == False):
            values = [float(s) for s in line.split()]
        else:
            values = [float(s) for s in line.split()]
        if (len(values) >= 1):
            hp1.append(values[0])

    hpOrder = int(hp1[0])
    hpPointCount = int(hp1[1])
    if (hpPointCount <= 1):
        print("healpix map empty, fileName: " + healpixMapFileName)
        return
    pixVal = hp1[2:]
    pixValLen = len(pixVal)
    Nside = 2**hpOrder
    Npixel = hp.nside2npix(Nside)
    pixVal.extend([0] * (int(Npixel) - pixValLen))
    pixVal = np.array(pixVal)

    LMAX = Nside * 2

    # read in random Map
    hp2 = []
    for line in open(randomMapFileName, 'r'):
        values = [float(s) for s in line.split()]
        if (len(values) >= 1):
            hp2.append(values[0])
    hp2Order = int(hp2[0])
    if (not hpOrder == hp2Order):
        print("two maps not of the same order")
        return
    hp2PointCount = int(hp2[1])
    if (hp2PointCount <= 1):
        print("random map empty, fileName: " + randomMapFileName)
        return
    pix2Val = hp2[2:]
    pix2ValLen = len(pix2Val)
    pix2Val.extend([0] * (int(Npixel) - pix2ValLen))
    pix2Val = np.array(pix2Val)

    cl = hp.anafast(pixVal, lmax=LMAX)
    alm = hp.map2alm(pixVal, lmax=LMAX)
    ell = np.arange(len(cl))
    # coeff = (cl * (2 * ell + 1))/((hpPointCount)**2) * 4 * np.pi - 1/( hpPointCount)

    # coeff2 = (cl * (2 * ell + 1)) / ((hpPointCount)**2)

    cl2 = hp.anafast(pix2Val, lmax=LMAX)
    # ell2 = np.arange(len(cl2))
    # coeffRandom = (cl * (2*ell + 1))/((hp2PointCount)**2)*
    if (len(cl) != len(cl2)):
        print("len(cl) !=  len(cl2)")
        return

    clFinal = cl - cl2
    xMax = LMAX

    plt.figure(figsize=(figX, figY))
    fig1, ax1 = plt.subplots(num=fignum + 1)
    ax1.xaxis.set_major_locator(plt.MultipleLocator(xMax / CL_MAJOR_TICKS_NUM))
    ax1.xaxis.set_minor_locator(
        plt.MultipleLocator(xMax / (CL_MAJOR_TICKS_NUM * 2)))
    ax1.plot(ell, ell * (ell + 1) * clFinal)
    plt.xlabel("$\ell$")
    plt.ylabel("$\ell(\ell+1)C_{\ell}$")
    plt.grid()

    annot_max(ell, ell * (ell + 1) * clFinal, ax1)
    saveName1 = saveName + "Diff_LMAX:" + str(LMAX) + "CLcoeff_llp1.png"
    figureName1 = figureName + "LMAX:" + str(
        LMAX) + " l(l+1)_normalized\nrandom Map offset"
    plt.title(figureName1, fontsize=8, y=0.99)
    plt.savefig(saveName1)
    # print(alm[0])

    plt.figure(figsize=(figX, figY))
    fig2, ax2 = plt.subplots(num=fignum + 2)
    ax2.xaxis.set_major_locator(plt.MultipleLocator(xMax / CL_MAJOR_TICKS_NUM))
    ax2.xaxis.set_minor_locator(
        plt.MultipleLocator(xMax / (CL_MAJOR_TICKS_NUM * 2)))
    ax2.plot(ell, (2 * ell + 1) * clFinal)
    normedCl = (2 * ell + 1) * clFinal

    plt.xlabel("$\ell$")
    plt.ylabel("$(2\ell+1)C_{\ell}$")
    annot_max(ell, (2 * ell + 1) * clFinal, ax2)
    plt.grid()
    saveName2 = saveName + "Diff_LMAX:" + str(LMAX) + "CLcoeff_2lp1.png"
    figureName2 = figureName + "LMAX:" + str(
        LMAX) + " (2l+1)_normalized\nrandom Map offset"
    plt.title(figureName2, fontsize=8, y=0.99)

    #find local max
    indexX = localMax(ell, normedCl, ax2)
    maxXVal = ell[indexX]
    maxYVal = normedCl[indexX]
    plt.stem(maxXVal, maxYVal, linefmt='b--', markerfmt='ro', basefmt=' ')
    plt.savefig(saveName2)

    plt.savefig(saveName2)

    # plt.figure(figsize= (figX, figY))
    # plt.plot(ell, coeff)
    # plt.xlabel("$\ell$")
    # plt.ylabel("$\widehat{c_{\ell}}$")
    # plt.grid()
    # saveName3= saveName + "LMAX:" + str(LMAX) + "CLcoeff_coeff.png"
    # figureName3 = figureName + "LMAX:" + str(LMAX) +  " legendre coeff"
    # plt.title(figureName3,fontsize = 8, y = 0.99)
    # plt.savefig(saveName3)

    plt.figure(figsize=(figX, figY))
    fig3, ax3 = plt.subplots(num=fignum + 3)
    ax3.xaxis.set_major_locator(plt.MultipleLocator(xMax / CL_MAJOR_TICKS_NUM))
    ax3.xaxis.set_minor_locator(
        plt.MultipleLocator(xMax / (CL_MAJOR_TICKS_NUM * 2)))
    ax3.plot(ell, clFinal)
    plt.xlabel("$\ell$")
    plt.ylabel("$C_{\ell}$")

    annot_max(ell, clFinal, ax3)
    plt.grid()
    saveName4 = saveName + "Diff_LMAX:" + str(LMAX) + "CLcoeff_rawCL.png"
    figureName4 = figureName + "LMAX:" + str(LMAX) + " Cl\nrandom Map offset"
    plt.title(figureName4, fontsize=8, y=0.99)
    plt.savefig(saveName4)
Exemple #46
0
lmax = 1024
maps = []
alms = []
mk2uk = 1000
map_path = r'/home/wudl/project/ILC/fits_files/wmap/'

def beam_func(fwhm):
    return hp.gauss_beam(fwhm=np.radians(fwhm), lmax=lmax)

for i in np.arange(5):
    '''
    load 5 maps, note that all the maps are smoothed to 1 degree.
    '''
    T_map = hp.read_map(map_path+'wmap_band_smth_imap_r9_3yr_{0}_v2.fits'.format(bandname[i]))
    maps.append(T_map)
    alms.append(hp.map2alm(T_map))
    hp.mollview(T_map,sub=(2,3,i+1),unit=r'mK$_\mathrm{CMB}$',norm='hist',min=-0.4,max=0.4,title= bandname[i] + "-band")

map_cov = np.cov(maps)
print('map_cov:>>> ',map_cov)
weight = []
map_cov_inv = np.linalg.inv(map_cov)
for i in range(5):
    weight.append(np.sum(map_cov_inv[i])/np.sum(map_cov_inv))

#print weight
print(np.array(maps).shape) # shape of the data array
print(weight,np.sum(weight))

# get clean map
map_clean = np.sum(np.array(weight).reshape(5,-1)*maps,axis=0)
Exemple #47
0
def psd(x):
    """Compute the Power Spectral Density for heaply maps."""
    if len(x.shape) == 2 and x.shape[1] > 1:
        return np.stack([psd(x[ind, ]) for ind in range(len(x))])
    hatx = hp.map2alm(hp.reorder(x, n2r=True))
    return hp.alm2cl(hatx)
Exemple #48
0
def check_cl_sims(
        nmaps=1,
        lmax=1000,
        nside=2048,
        read_file=False,
        filename='testsky.fits',
        frequency=100,
        beam=None,
        beamP=None,
        smear=True,
        nonoise=False,
        cl_file='bf_base_cmbonly_plikHMv18_TT_lowTEB_lmax4000.minimum.theory_cl'
):

    if read_file and os.path.exists(data_path + filename):
        Tmap, Qmap, Umap = hp.read_map(data_path + filename, field=(0, 1, 2))
    else:
        if nonoise:
            Tmap, Qmap, Umap = simulate_cmb(nside=nside,
                                            lmax=lmax,
                                            save=False,
                                            smear=smear,
                                            beam=beam,
                                            beamP=beamP,
                                            cl_file=cl_file)
        else:
            Tmap, Qmap, Umap = observe_cmb_sky(save=False,
                                               nside=nside,
                                               npix=None,
                                               lmax=3000,
                                               frequency=frequency,
                                               beam=beam,
                                               beamP=beamP,
                                               cl_file=cl_file)

    Tlm = hp.map2alm(Tmap, lmax=lmax)
    Elm, Blm = hp.map2alm_spin((Qmap, Umap), 2, lmax=lmax)

    if smear:
        if (beam is None) or (beamP is None):
            hdulist = fits.open(data_path + 'HFI_RIMO_Beams-100pc_R2.00.fits')
            beam = hdulist[beam_index['{}'.format(
                frequency)]].data.NOMINAL[0][:lmax + 1]
            beamP = hdulist[beam_index['{}P'.format(
                frequency)]].data.NOMINAL[0][:lmax + 1]
        hp.sphtfunc.almxfl(Tlm, 1. / beam, inplace=True)
        hp.sphtfunc.almxfl(Elm, 1. / beamP, inplace=True)
        hp.sphtfunc.almxfl(Blm, 1. / beamP, inplace=True)

    ls = np.arange(lmax + 1)
    factor = ls * (ls + 1.) / (2. * np.pi)

    cltt = hp.alm2cl(Tlm) * factor
    clee = hp.alm2cl(Elm) * factor
    clbb = hp.alm2cl(Blm) * factor
    clte = hp.alm2cl(Tlm, Elm) * factor

    cl = get_theory_cmb()
    ls_theory = cl[0][:lmax + 1]
    factor_theory = ls_theory * (ls_theory + 1.) / (2. * np.pi)

    cltt_theory = cl[1][:lmax + 1] * factor_theory
    clee_theory = cl[2][:lmax + 1] * factor_theory
    clbb_theory = cl[3][:lmax + 1] * factor_theory
    clte_theory = cl[4][:lmax + 1] * factor_theory

    plt.figure()
    plt.plot(ls, cltt, label='sims')
    plt.plot(ls_theory, cltt_theory, label='theory TT')
    plt.legend()

    plt.figure()
    plt.plot(ls, clte, label='sims')
    plt.plot(ls_theory, clte_theory, label='theory TE')
    plt.legend()

    plt.figure()
    plt.plot(ls, clee, label='sims')
    plt.plot(ls_theory, clee_theory, label='theory EE')
    plt.legend()

    plt.figure()
    plt.plot(ls, clbb, label='sims')
    plt.plot(ls_theory, clbb_theory, label='theory BB')
    plt.legend()
Exemple #49
0
def mkconstrained(corr, constraints, nside):
    """Construct a set of Healpix maps, satisfying given constraints
    on specified frequency slices, by using the lowest eigenmodes.

    Parameters
    ----------
    corr : np.ndarray (lmax+1, numz, numz)
        The correlation matrix :math:`C_l(z, z')`.
    constrains : list
        A list of constraints packed as [[frequency_index, healpix map], ...]
    nside : integer
        The resolution of the Healpix maps.

    Returns
    -------
    hpmaps : np.ndarray (numz, npix)
        The Healpix maps. hpmaps[i] is the i'th map.
    """

    numz = corr.shape[1]
    maxl = corr.shape[0] - 1
    larr, marr = healpy.Alm.getlm(maxl)
    matshape = larr.shape + (numz, )

    # The number of constraints
    nmodes = len(constraints)

    # The frequency slices that are constrained.
    f_ind = zip(*constraints)[0]

    if corr.shape[2] != numz:
        raise Exception("Correlation matrix is incorrect shape.")

    trans = np.zeros((corr.shape[0], nmodes, corr.shape[2]))
    tmat = np.zeros((corr.shape[0], nmodes, nmodes))
    cmap = np.zeros(larr.shape + (nmodes, ), dtype=np.complex128)
    cv = np.zeros((numz, ) + larr.shape, dtype=np.complex128)

    # Find eigenmodes, extract the largest nmodes (enough to satisfy
    # constraints), and then pull out coefficients for each constrained
    # frequency.
    for i in range(maxl + 1):
        trans[i] = la.eigh(corr[i])[1][:, -nmodes:].T
        tmat[i] = trans[i][:, f_ind]

    # Switch constraint maps into harmonic space
    for i, cons in enumerate(constraints):
        cmap[:, i] = healpy.map2alm(cons[1], lmax=maxl)

    # Solve for the eigenmode amplitudes to satisfy constraints, and project
    # each mode across the whole frequency range.
    for i, l in enumerate(larr):
        if l == 0:
            cv[:, i] = 0.0
        else:
            cv[:, i] = np.dot(trans[l].T, la.solve(tmat[l].T, cmap[i]))

    hpmaps = np.empty((numz, healpy.nside2npix(nside)))

    for i in range(numz):
        hpmaps[i] = healpy.alm2map(cv[i], nside, verbose=False)

    return hpmaps
Exemple #50
0
 def _get_sim_f_p(self, idx, joint=False, swapped=False):
     """Modulation estimator, polarization only. """
     Q1, U1 = self.f2map1.get_irespmap(idx) if not swapped else self.f2map2.get_irespmap(idx)
     Q2, U2 = (self.f2map2.get_pmap(idx, joint=joint) if not swapped else self.f2map1.get_pmap(idx, joint=joint))
     return -2 * hp.map2alm(Q1 * Q2 + U1 * U2 , lmax=self.get_lmax_qlm('P'), iter=0)
Exemple #51
0
sim_tau = hp.read_map(sim_tau_file)
print('...done...')

# CMB maps
print('...loading CMB maps...')
sim_tt1 = hp.read_map(sim_tt_file)
sim_tt1 = np.exp(-sim_tau) * sim_tt1
sim_tt1 = hp.smoothing(sim_tt1, fwhm=np.radians(
    fwhm / 60.))  # Apply beam to signal-only map
if wnoise:
    sim_ntt1 = hp.read_map(sim_ntt1_file)
    sim_ntt2 = hp.read_map(sim_ntt2_file)
    sim_tt1 = sim_tt1 + sim_ntt1
    sim_tt2 = sim_tt1 + sim_ntt2

sim_tt1_alm = hp.map2alm(sim_tt1, lmax=lmax)
sim_tt1 = hp.alm2map(hp.almxfl(sim_tt1_alm, filt),
                     hp.npix2nside(len(sim_tt1)),
                     lmax=lmax)
if wnoise:
    sim_tt2_alm = hp.map2alm(sim_tt2, lmax=lmax)
    sim_tt2 = hp.alm2map(hp.almxfl(sim_tt2_alm, filt),
                         hp.npix2nside(len(sim_tt2)),
                         lmax=lmax)
print('...done...')

# Mask
print('...loading CMB masks...')
mask_gal = hp.reorder(fits.getdata(mask_gal_file, hdu=1)['GAL080'],
                      inp='NESTED',
                      out='RING')
Exemple #52
0
def rotate_map_g_c(map_in, c2g=False):
    ns = hp.npix2nside(len(map_in))
    alm_in = hp.map2alm(map_in)
    alm_out = rotate_alm_g_c(alm_in, c2g=False)
    return hp.alm2map(alm_out, ns, verbose=False)
def udgrade(x, nside_out):
    """
    Spatial interpolation of a healpix map x using spherical harmonic decomposition
    and then synthesis at resolution nside_out.
    """
    return hp.alm2map(hp.map2alm(x), nside_out, verbose=False)
Exemple #54
0
	N_NU = 30
	# get the spetrum of frequenies covered in units of MHz
	(bn,nu_bot,nu_top,z_bot,z_top) = np.loadtxt("./sim_info/nuTable.txt").T
	nu_arr = ((nu_bot + nu_top)/2.)[:-1]
	nu_arr = np.array([np.mean(i,axis=0) for i in np.split(nu_arr,N_NU)])

	pca = np.load("sim_data/x_val_nepochs100.npy")
	cosmo = np.load("sim_data/y_val_nepochs100.npy")
	pred = np.load("sim_data/y_pred_nepochs100.npy")
	rearr = np.array(np.load("rearr.npy"),dtype=int)

	for i in range(N_NU):
		# Get Cls for PCA spectrum
		pca0 = (pca.T[i].T).flatten()
		pca0 = pca0[rearr]
		alm_pca = hp.map2alm(pca0)
		Cl_pca = hp.alm2cl(alm_pca)

		# Get Cls for COSMO spectrum
		cosmo0 = (cosmo.T[i].T).flatten()
		cosmo0 = cosmo0[rearr]
		alm_cosmo = hp.map2alm(cosmo0)
		Cl_cosmo = hp.alm2cl(alm_cosmo)

		# Get Cls for PCA spectrum
		pred0 = (pred.T[i].T).flatten()
		pred0 = pred0[rearr]
		alm_pred = hp.map2alm(pred0)
		Cl_pred = hp.alm2cl(alm_pred)
		fig = plt.figure()
		plt.plot(((Cl_pca-Cl_cosmo)/Cl_cosmo)[1:],c=plt.cm.bwr(.9),label="PCA")
Exemple #55
0
 def _get_sim_stt(self, idx, swapped=False):
     """Point source estimator """
     tmap1 = self.f2map1.get_irestmap(idx) if not swapped else self.f2map2.get_irestmap(idx)  # healpy map
     tmap1 *= (self.f2map2.get_irestmap(idx) if not swapped else self.f2map1.get_irestmap(idx))  # healpy map
     return -0.5 * hp.map2alm(tmap1, lmax=self.get_lmax_qlm('PS'), iter=0)
Exemple #56
0
                ".dat", "r")))

    norm2 = np.array(
        pickle.load(
            open(foldername + "norm2_32_360_iteration" + str(Nstart) + ".dat",
                 "r")))
    Emap2 = np.array(
        pickle.load(
            open(
                foldername + "exposure2_32_360_iteration" + str(Nstart) +
                ".dat", "r")))
    namefits = foldername + 'CR_32_360_iteration' + str(Nstart) + '.fits'
    diffCRmap = H.read_map(namefits)

    LMAX = 180
    alms3 = H.map2alm(diffCRmap, lmax=3)
    #fitmap3 = H.alm2map(alms3, nside,lmax=LMAX)
    fitmap3 = H.alm2map(alms3, nside)
    diffCRmap = CRmap - fitmap3

    # substract l<=3
    subLMAX = 3
    redLMAX = 3
    out = H.anafast(tempmap, alm=True, lmax=subLMAX)
    outr = H.anafast(tempmap, alm=True, lmax=redLMAX)
    submap = H.alm2map(out[1], nside, lmax=subLMAX)

    diffCRmap = diffCRmap - submap

    # calculate new norm
Exemple #57
0
 def _get_sim_ntt(self, idx, swapped=False):
     """ Noise inhomogeneity estimator (same as point-source estimator but acting on beam-deconvolved maps) """
     f1 = self.f2map1 if not swapped else self.f2map2
     f2 = self.f2map2 if not swapped else self.f2map1
     tmap1 = f1.get_wirestmap(idx, f1.ivfs.get_tal('t')[:]) * f2.get_wirestmap(idx, f2.ivfs.get_tal('t')[:])
     return -0.5 * hp.map2alm(tmap1, lmax=self.get_lmax_qlm('T'), iter=0)
Exemple #58
0
 def _get_sim_ftt(self, idx, joint=False, swapped=False):
     """Modulation estimator, temperature only."""
     tmap1 = self.f2map1.get_irestmap(idx) if not swapped else self.f2map2.get_irestmap(idx)  # healpy map
     tmap1 *= (self.f2map2.get_tmap(idx, joint=joint) if not swapped else self.f2map1.get_tmap(idx, joint=joint))  # healpy map
     return -hp.map2alm(tmap1, lmax=self.get_lmax_qlm('T'), iter=0)
Exemple #59
0
def xForecast(components,
              instrument,
              d_fgs,
              lmin,
              lmax,
              Alens=1.0,
              r=0.001,
              make_figure=False,
              **minimize_kwargs):
    """ xForecast

    Run XForcast (Stompor et al, 2016) using the provided instrumental
    specifications and input foregrounds maps. If the foreground maps match the
    components provided (constant spectral indices are assumed), it reduces to
    CMB4cast (Errard et al, 2011). Currently, only polarization is considered
    fot component separation and only the BB power spectrum for cosmological
    analysis.

    Parameters
    ----------
    components: list
         `Components` of the mixing matrix
    instrument:
        Object that provides the following as a key or an attribute.

        - **frequency**
        - **depth_p** (optional, frequencies are inverse-noise
          weighted according to these noise levels)
        - **fwhm** (optional)

        They can be anything that is convertible to a float numpy array.
    d_fgs: ndarray
        The foreground maps. No CMB. Shape `(n_freq, n_stokes, n_pix)`.
        If some pixels have to be masked, set them to zero.
        Since (cross-)spectra of the maps will be computed, you might want to
        apodize your mask (use the same apodization for all the frequency).
    lmin: int
        minimum multipole entering the likelihood computation
    lmax: int
        maximum multipole entering the likelihood computation
    Alens: float
        Amplitude of the lensing B-modes entering the likelihood on r
    r: float
        tensor-to-scalar ratio assumed in the likelihood on r
    minimize_kwargs: dict
        Keyword arguments to be passed to `scipy.optimize.minimize` during
        the fitting of the spectral parameters.
        A good choice for most cases is
        `minimize_kwargs = {'tol': 1, options: {'disp': True}}`. `tol` depends
        on both the solver and your signal to noise: it should ensure that the
        difference between the best fit -logL and and the minimum is well less
        then 1, without exagereting (a difference of 1e-4 is useless).
        `disp` also triggers a verbose callback that monitors the convergence.

    Returns
    -------
    xFres: dict
        xForecast result. It includes

        - the fitted spectral parameters
        - noise-averaged post-component separation CMB power spectrum

          - noise spectrum
          - statistical residuals spectrum
          - systematic residuals spectrum

        - noise-averaged cosmological likelihood

    """
    # Preliminaries
    instrument = standardize_instrument(instrument)
    nside = hp.npix2nside(d_fgs.shape[-1])
    n_stokes = d_fgs.shape[1]
    n_freqs = d_fgs.shape[0]
    invN = np.diag(hp.nside2resol(nside, arcmin=True) /
                   (instrument.depth_p))**2
    mask = d_fgs[0, 0, :] != 0.
    fsky = mask.astype(float).sum() / mask.size
    ell = np.arange(lmin, lmax + 1)
    print('fsky = ', fsky)

    ############################################################################
    # 1. Component separation using the noise-free foregrounds templare
    # grab the max-L spectra parameters with the associated error bars
    print('======= ESTIMATION OF SPECTRAL PARAMETERS =======')
    A = MixingMatrix(*components)
    A_ev = A.evaluator(instrument.frequency)
    A_dB_ev = A.diff_evaluator(instrument.frequency)

    x0 = np.array([x for c in components for x in c.defaults])
    if n_stokes == 3:  # if T and P were provided, extract P
        d_comp_sep = d_fgs[:, 1:, :]
    else:
        d_comp_sep = d_fgs

    res = comp_sep(A_ev, d_comp_sep.T, invN, A_dB_ev, A.comp_of_dB, x0,
                   **minimize_kwargs)

    res.params = A.params
    res.s = res.s.T
    A_maxL = A_ev(res.x)
    A_dB_maxL = A_dB_ev(res.x)
    A_dBdB_maxL = A.diff_diff_evaluator(instrument.frequency)(res.x)

    print('res.x = ', res.x)

    ############################################################################
    # 2. Estimate noise after component separation
    ### A^T N_ell^-1 A
    print('======= ESTIMATION OF NOISE AFTER COMP SEP =======')
    i_cmb = A.components.index('CMB')
    Cl_noise = _get_Cl_noise(instrument, A_maxL, lmax)[i_cmb, i_cmb, lmin:]

    ############################################################################
    # 3. Compute spectra of the input foregrounds maps
    ### TO DO: which size for Cl_fgs??? N_spec != 1 ?
    print('======= COMPUTATION OF CL_FGS =======')
    if n_stokes == 3:
        d_spectra = d_fgs
    else:  # Only P is provided, add T for map2alm
        d_spectra = np.zeros((n_freqs, 3, d_fgs.shape[2]), dtype=d_fgs.dtype)
        d_spectra[:, 1:] = d_fgs

    # Compute cross-spectra
    almBs = [
        hp.map2alm(freq_map, lmax=lmax, iter=10)[2] for freq_map in d_spectra
    ]
    Cl_fgs = np.zeros((n_freqs, n_freqs, lmax + 1), dtype=d_fgs.dtype)
    for f1 in range(n_freqs):
        for f2 in range(n_freqs):
            if f1 > f2:
                Cl_fgs[f1, f2] = Cl_fgs[f2, f1]
            else:
                Cl_fgs[f1, f2] = hp.alm2cl(almBs[f1], almBs[f2], lmax=lmax)

    Cl_fgs = Cl_fgs[..., lmin:] / fsky

    ############################################################################
    # 4. Estimate the statistical and systematic foregrounds residuals
    print('======= ESTIMATION OF STAT AND SYS RESIDUALS =======')

    W_maxL = W(A_maxL, invN=invN)[i_cmb, :]
    W_dB_maxL = W_dB(A_maxL, A_dB_maxL, A.comp_of_dB, invN=invN)[:, i_cmb]
    W_dBdB_maxL = W_dBdB(A_maxL,
                         A_dB_maxL,
                         A_dBdB_maxL,
                         A.comp_of_dB,
                         invN=invN)[:, :, i_cmb]
    V_maxL = np.einsum('ij,ij...->...', res.Sigma, W_dBdB_maxL)

    # Check dimentions
    assert ((n_freqs, ) == W_maxL.shape == W_dB_maxL.shape[1:] ==
            W_dBdB_maxL.shape[2:] == V_maxL.shape)
    assert (len(res.params) == W_dB_maxL.shape[0] == W_dBdB_maxL.shape[0] ==
            W_dBdB_maxL.shape[1])

    # elementary quantities defined in Stompor, Errard, Poletti (2016)
    Cl_xF = {}
    Cl_xF['yy'] = _utmv(W_maxL, Cl_fgs.T, W_maxL)  # (ell,)
    Cl_xF['YY'] = _mmm(W_dB_maxL, Cl_fgs.T, W_dB_maxL.T)  # (ell, param, param)
    Cl_xF['yz'] = _utmv(W_maxL, Cl_fgs.T, V_maxL)  # (ell,)
    Cl_xF['Yy'] = _mmv(W_dB_maxL, Cl_fgs.T, W_maxL)  # (ell, param)
    Cl_xF['Yz'] = _mmv(W_dB_maxL, Cl_fgs.T, V_maxL)  # (ell, param)

    # bias and statistical foregrounds residuals
    res.noise = Cl_noise
    res.bias = Cl_xF['yy'] + 2 * Cl_xF['yz']  # S16, Eq 23
    res.stat = np.einsum('ij, lij -> l', res.Sigma, Cl_xF['YY'])  # E11, Eq. 12
    res.var = res.stat**2 + 2 * np.einsum(
        'li, ij, lj -> l',  # S16, Eq. 28
        Cl_xF['Yy'],
        res.Sigma,
        Cl_xF['Yy'])

    ###############################################################################
    # 5. Plug into the cosmological likelihood
    print('======= OPTIMIZATION OF COSMO LIKELIHOOD =======')
    Cl_fid = {}
    Cl_fid['BB'] = _get_Cl_cmb(Alens=Alens, r=r)[2][lmin:lmax + 1]
    Cl_fid['BuBu'] = _get_Cl_cmb(Alens=0.0, r=1.0)[2][lmin:lmax + 1]
    Cl_fid['BlBl'] = _get_Cl_cmb(Alens=1.0, r=0.0)[2][lmin:lmax + 1]

    res.BB = Cl_fid['BB'] * 1.0
    res.BuBu = Cl_fid['BuBu'] * 1.0
    res.BlBl = Cl_fid['BlBl'] * 1.0
    res.ell = ell
    if make_figure:
        fig = pl.figure(figsize=(14, 12), facecolor='w', edgecolor='k')
        ax = pl.gca()
        left, bottom, width, height = [0.2, 0.2, 0.15, 0.2]
        ax0 = fig.add_axes([left, bottom, width, height])
        ax0.set_title(r'$\ell_{\min}=$'+str(lmin)+\
            r'$ \rightarrow \ell_{\max}=$'+str(lmax), fontsize=16)

        ax.loglog(ell,
                  Cl_fid['BB'],
                  color='DarkGray',
                  linestyle='-',
                  label='BB tot',
                  linewidth=2.0)
        ax.loglog(ell,
                  Cl_fid['BuBu'] * r,
                  color='DarkGray',
                  linestyle='--',
                  label='primordial BB for r=' + str(r),
                  linewidth=2.0)
        ax.loglog(ell,
                  res.stat,
                  'DarkOrange',
                  label='statistical residuals',
                  linewidth=2.0)
        ax.loglog(ell,
                  res.bias,
                  'DarkOrange',
                  linestyle='--',
                  label='systematic residuals',
                  linewidth=2.0)
        ax.loglog(ell,
                  res.noise,
                  'DarkBlue',
                  linestyle='--',
                  label='noise after component separation',
                  linewidth=2.0)
        ax.legend()
        ax.set_xlabel('$\ell$', fontsize=20)
        ax.set_ylabel('$C_\ell$ [$\mu$K-arcmin]', fontsize=20)
        ax.set_xlim(lmin, lmax)

    ## 5.1. data
    Cl_obs = Cl_fid['BB'] + Cl_noise
    dof = (2 * ell + 1) * fsky
    YY = Cl_xF['YY']
    tr_SigmaYY = np.einsum('ij, lji -> l', res.Sigma, YY)

    ## 5.2. modeling
    def cosmo_likelihood(r_):
        # S16, Appendix C
        Cl_model = Cl_fid['BlBl'] * Alens + Cl_fid['BuBu'] * r_ + Cl_noise
        dof_over_Cl = dof / Cl_model
        ## Eq. C3
        U = np.linalg.inv(res.Sigma_inv + np.dot(YY.T, dof_over_Cl))

        ## Eq. C9
        first_row = np.sum(
            dof_over_Cl *
            (Cl_obs *
             (1 - np.einsum('ij, lji -> l', U, YY) / Cl_model) + tr_SigmaYY))
        second_row = -np.einsum('l, m, ij, mjk, kf, lfi', dof_over_Cl,
                                dof_over_Cl, U, YY, res.Sigma, YY)
        trCinvC = first_row + second_row

        ## Eq. C10
        first_row = np.sum(dof_over_Cl * (Cl_xF['yy'] + 2 * Cl_xF['yz']))
        ### Cyclicity + traspose of scalar + grouping terms -> trace becomes
        ### Yy_ell^T U (Yy + 2 Yz)_ell'
        trace = np.einsum('li, ij, mj -> lm', Cl_xF['Yy'], U,
                          Cl_xF['Yy'] + 2 * Cl_xF['Yz'])
        second_row = -_utmv(dof_over_Cl, trace, dof_over_Cl)
        trECinvC = first_row + second_row

        ## Eq. C12
        logdetC = np.sum(dof * np.log(Cl_model)) - np.log(np.linalg.det(U))

        # Cl_hat = Cl_obs + tr_SigmaYY

        ## Bringing things together
        return trCinvC + trECinvC + logdetC

    # Likelihood maximization
    r_grid = np.logspace(-5, 0, num=500)
    logL = np.array([cosmo_likelihood(r_loc) for r_loc in r_grid])
    ind_r_min = np.argmin(logL)
    r0 = r_grid[ind_r_min]
    if ind_r_min == 0:
        bound_0 = 0.0
        bound_1 = r_grid[1]
        # pl.figure()
        # pl.semilogx(r_grid, logL, 'r-')
        # pl.show()
    elif ind_r_min == len(r_grid) - 1:
        bound_0 = r_grid[-2]
        bound_1 = 1.0
        # pl.figure()
        # pl.semilogx(r_grid, logL, 'r-')
        # pl.show()
    else:
        bound_0 = r_grid[ind_r_min - 1]
        bound_1 = r_grid[ind_r_min + 1]
    print('bounds on r = ', bound_0, ' / ', bound_1)
    print('starting point = ', r0)
    res_Lr = sp.optimize.minimize(cosmo_likelihood, [r0],
                                  bounds=[(bound_0, bound_1)],
                                  **minimize_kwargs)
    print('    ===>> fitted r = ', res_Lr['x'])

    print('======= ESTIMATION OF SIGMA(R) =======')

    def sigma_r_computation_from_logL(r_loc):
        THRESHOLD = 1.00
        # THRESHOLD = 2.30 when two fitted parameters
        delta = np.abs(cosmo_likelihood(r_loc) - res_Lr['fun'] - THRESHOLD)
        # print r_loc, cosmo_likelihood(r_loc),  res_Lr['fun']
        return delta

    if res_Lr['x'] != 0.0:
        sr_grid = np.logspace(np.log10(res_Lr['x']), 0, num=25)
    else:
        sr_grid = np.logspace(-5, 0, num=25)

    slogL = np.array(
        [sigma_r_computation_from_logL(sr_loc) for sr_loc in sr_grid])
    ind_sr_min = np.argmin(slogL)
    sr0 = sr_grid[ind_sr_min]
    print('ind_sr_min = ', ind_sr_min)
    print('sr_grid[ind_sr_min-1] = ', sr_grid[ind_sr_min - 1])
    print('sr_grid[ind_sr_min+1] = ', sr_grid[ind_sr_min + 1])
    print('sr_grid = ', sr_grid)
    if ind_sr_min == 0:
        print('case # 1')
        bound_0 = res_Lr['x']
        bound_1 = sr_grid[1]
    elif ind_sr_min == len(sr_grid) - 1:
        print('case # 2')
        bound_0 = sr_grid[-2]
        bound_1 = 1.0
    else:
        print('case # 3')
        bound_0 = sr_grid[ind_sr_min - 1]
        bound_1 = sr_grid[ind_sr_min + 1]
    print('bounds on sigma(r) = ', bound_0, ' / ', bound_1)
    print('starting point = ', sr0)
    res_sr = sp.optimize.minimize(
        sigma_r_computation_from_logL,
        sr0,
        bounds=[(bound_0.item(), bound_1.item())],
        # item required for test to pass but reason unclear. sr_grid has
        # extra dimension?
        **minimize_kwargs)
    print('    ===>> sigma(r) = ', res_sr['x'] - res_Lr['x'])
    res.cosmo_params = {}
    res.cosmo_params['r'] = (res_Lr['x'], res_sr['x'] - res_Lr['x'])

    ###############################################################################
    # 6. Produce figures
    if make_figure:
        print('======= GRIDDING COSMO LIKELIHOOD =======')
        r_grid = np.logspace(-4, -1, num=500)
        logL = np.array([cosmo_likelihood(r_loc) for r_loc in r_grid])
        chi2 = logL - np.min(logL)
        ax0.semilogx(r_grid,
                     np.exp(-chi2),
                     color='DarkOrange',
                     linestyle='-',
                     linewidth=2.0,
                     alpha=0.8)
        ax0.axvline(x=r, color='k', linestyle='--')
        ax0.set_ylabel(r'$\mathcal{L}(r)$', fontsize=20)
        ax0.set_xlabel(r'tensor-to-scalar ratio $r$', fontsize=20)
        pl.show()

    return res
def read_kappa_input(nside=256):
    kappa_input = fits.open('kappa_input_gaussian.fits')[1].data.I.ravel()
    #kappa_input = fits.open('kappa_input_noiseless.fits')[1].data.I.ravel()
    alm_input = hp.map2alm(kappa_input, lmax=3*nside-1)
    kappa_input = hp.alm2map(alm_input, nside=nside)
    return kappa_input