Exemplo n.º 1
0
    def _apply_signal(self, _type, alms, use_Pool=0):
        assert alms.shape == self._datalms_shape(_type), (alms.shape, self._datalms_shape(_type))

        t = timer(_timed, prefix=__name__, suffix=' _apply signal')
        t.checkpoint("just started")

        tempalms = np.empty(self._skyalms_shape(_type), dtype=complex)
        ret = np.empty_like(alms)
        for _i in range(len(_type)): tempalms[_i] = self._mask_alm(alms[_i], forward=False)
        tempalms = self._apply_beams(_type, tempalms)
        t.checkpoint(("masked alms and applied beams"))

        if use_Pool <= -100:
            import mllens_GPU.apply_GPU as apply_GPU
            f = self.f
            f_inv = self.f_inv
            apply_GPU.apply_FDxiDtFt_GPU_inplace(_type, self.lib_skyalm, self.lib_skyalm, tempalms, f, f_inv,
                                                 self.cls_unl)
            tempalms = self._apply_beams(_type, tempalms)
            for _i in range(len(_type)):
                ret[_i] = self._mask_alm(tempalms[_i], forward=True)
            return ret + self.apply_noise(_type, alms)

        for _i in range(len(_type)):  # Lens with inverse and mult with determinant magnification.
            tempalms[_i] = self.f_inv.lens_alm(self.lib_skyalm, tempalms[_i],
                                               mult_magn=True, use_Pool=use_Pool)
        # NB : 7 new full sky alms for TQU in this routine - > 4 GB total for full sky lmax_sky =  6000.
        t.checkpoint("backward lens + det magn")

        skyalms = np.zeros_like(tempalms)
        for j in range(len(_type)):
            for i in range(len(_type)):
                skyalms[i] += get_unlPmat_ij(_type, self.lib_skyalm, self.cls_unl, i, j) * tempalms[j]
        del tempalms
        t.checkpoint("mult with Punl mat ")

        for i in range(len(_type)):  # Lens with forward displacement
            skyalms[i] = self.f.lens_alm(self.lib_skyalm, skyalms[i], use_Pool=use_Pool)
        t.checkpoint("Forward lensing mat ")

        skyalms = self._apply_beams(_type, skyalms)
        t.checkpoint("Beams")
        for _i in range(len(_type)):
            ret[_i] = self._mask_alm(skyalms[_i], forward=True)
        t.checkpoint("masking")

        return ret
Exemplo n.º 2
0
    def apply_cond3(self, _type, alms, use_Pool=0):
        """
        (DBxiB ^ tD ^ t + N) ^ -1 \sim D ^ -t(BxiBt + N) ^ -1 D ^ -1
        :param alms:
        :return:
        """
        assert alms.shape == self._datalms_shape(_type), (alms.shape, self._datalms_shape(_type))
        t = timer(_timed)
        t.checkpoint("  cond3::just started")
        tempalms = np.empty(self._skyalms_shape(_type), dtype=complex)
        for _i in range(len(_type)): tempalms[_i] = self._mask_alm(alms[_i], forward=False)
        t.checkpoint("  cond3::masked alms")

        if use_Pool <= -100:
            # Try entire evaluation on GPU :
            from fs.gpu.apply_cond3_GPU import apply_cond3_GPU_inplace as c3GPU
            c3GPU(_type, self.lib_skyalm, tempalms, self.f, self.f_inv, self.cls_unl, self.cl_transf, self.cls_noise)
            ret = np.empty_like(alms)
            for _i in range(len(_type)):
                ret[_i] = self._mask_alm(tempalms[_i], forward=True)
            return ret
        temp = np.empty_like(alms)
        for i in range(len(_type)):  # D^{-1}
            temp[i] = self._deg(self.f_inv.lens_alm(self.lib_skyalm, tempalms[i], use_Pool=use_Pool))

        t.checkpoint("  cond3::Lensing with inverse")

        ret = np.zeros_like(alms)  # (B xi B^t + N)^{-1}
        for i in range(len(_type)):
            for j in range(len(_type)):
                ret[i] += self.get_Pmatinv(_type, i, j) * temp[j]
        del temp
        t.checkpoint("  cond3::Mult. w. inv Pmat")

        for i in range(len(_type)):  # D^{-t}
            tempalms[i] = self.f.lens_alm(self.lib_skyalm, self._upg(ret[i]), use_Pool=use_Pool, mult_magn=True)

        t.checkpoint("  cond3::Lens w. forward and det magn.")
        for _i in range(len(_type)):
            ret[_i] = self._mask_alm(tempalms[_i], forward=True)
        return ret
Exemplo n.º 3
0
def lens_glm_sym_timed(spin,
                       dlm,
                       glm,
                       nside,
                       nband=32,
                       facres=0,
                       clm=None,
                       rotpol=True):
    """
    Same as lens_alm but lens simultnously a North and South colatitude band,
    to make profit of the symmetries of the spherical harmonics.
    """
    assert spin >= 0, spin
    t = timer(True, suffix=' ' + __name__)
    target_nt = 3**1 * 2**(11 + facres)  # on one hemisphere
    times = {}

    #co-latitudes
    th1s = np.arange(nband) * (np.pi * 0.5 / nband)
    th2s = np.concatenate((th1s[1:], [np.pi * 0.5]))
    ret = np.zeros(hp.nside2npix(nside), dtype=float if spin == 0 else complex)
    Nt_perband = target_nt / nband
    t0 = time.time()
    Redtot, Imdtot = hp.alm2map_spin([dlm, np.zeros_like(dlm)], nside, 1,
                                     hp.Alm.getlmax(dlm.size))
    times['dx,dy (full sky)'] = time.time() - t0
    times['dx,dy band split'] = 0.
    times['pol. rot.'] = 0.
    t.checkpoint('healpy Spin 1 transform for displacement (full %s map)' %
                 nside)
    _Npix = 0  # Total number of pixels used for interpolation

    def coadd_times(tim):
        for _k, _t in tim.iteritems():
            if _k not in times:
                times[_k] = _t
            else:
                times[_k] += _t

    for ib, th1, th2 in zip(range(nband), th1s, th2s):
        print "BAND %s in %s :" % (ib, nband)
        t0 = time.time()
        pixN = hp.query_strip(nside, th1, th2, inclusive=True)
        pixS = hp.query_strip(nside, np.pi - th2, np.pi - th1, inclusive=True)
        tnewN, phinewN = _buildangles(hp.pix2ang(nside, pixN), Redtot[pixN],
                                      Imdtot[pixN])
        tnewS, phinewS = _buildangles(hp.pix2ang(nside, pixS), Redtot[pixS],
                                      Imdtot[pixS])

        # Adding a 10 pixels buffer for new angles to be safely inside interval.
        # th1,th2 is mapped onto pi - th2,pi -th1 so we need to make sure to cover both buffers
        matnewN = np.max(tnewN)
        mitnewN = np.min(tnewN)
        matnewS = np.max(tnewS)
        mitnewS = np.min(tnewS)
        buffN = 10 * (matnewN - mitnewN) / (Nt_perband -
                                            1) / (1. - 2. * 10. /
                                                  (Nt_perband - 1))
        buffS = 10 * (matnewS - mitnewS) / (Nt_perband -
                                            1) / (1. - 2. * 10. /
                                                  (Nt_perband - 1))
        _thup = min(np.pi - (matnewS + buffS), mitnewN - buffN)
        _thdown = max(np.pi - (mitnewS - buffS), matnewN + buffN)

        #print "min max tnew (degrees) in the band %.3f %.3f "%(_th1 /np.pi * 180.,_th2 /np.pi * 180.)
        #==== these are the theta and limits. It is ok to go negative or > 180
        print 'input t1,t2 %.3f %.3f in degrees' % (_thup / np.pi * 180,
                                                    _thdown / np.pi * 180.)
        print 'North %.3f and South %.3f buffers in amin' % (
            buffN / np.pi * 180 * 60, buffS / np.pi * 180. * 60.)
        Nphi = get_Nphi(_thup, _thdown, facres=facres)
        dphi_patch = (2. * np.pi) / Nphi * max(np.sin(_thup), np.sin(_thdown))
        dth_patch = (_thdown - _thup) / (Nt_perband - 1)
        print "cell (theta,phi) in amin (%.3f,%.3f)" % (
            dth_patch / np.pi * 60. * 180, dphi_patch / np.pi * 60. * 180)
        times['dx,dy band split'] += time.time() - t0
        if spin == 0:
            lenN, lenS, tim = lens_band_sym_timed(glm,
                                                  _thup,
                                                  _thdown,
                                                  Nt_perband, (tnewN, phinewN),
                                                  (tnewS, phinewS),
                                                  Nphi=Nphi)
            ret[pixN] = lenN
            ret[pixS] = lenS
        else:
            lenNR, lenNI, lenSR, lenSI, tim = gclm2lensmap_symband_timed(
                spin,
                glm,
                _thup,
                _thdown,
                Nt_perband, (tnewN, phinewN), (tnewS, phinewS),
                Nphi=Nphi,
                clm=clm)
            ret[pixN] = lenNR + 1j * lenNI
            ret[pixS] = lenSR + 1j * lenSI
            t0 = time.time()
            if rotpol and spin > 0:
                ret[pixN] *= polrot(spin, ret[pixN],
                                    hp.pix2ang(nside, pixN)[0], Redtot[pixN],
                                    Imdtot[pixN])
                ret[pixS] *= polrot(spin, ret[pixS],
                                    hp.pix2ang(nside, pixS)[0], Redtot[pixS],
                                    Imdtot[pixS])
                times['pol. rot.'] += time.time() - t0
        coadd_times(tim)

        #coadd_times(tim)
        _Npix += 2 * Nt_perband * Nphi
    t.checkpoint('Total exec. time')

    print "STATS for lmax tlm %s lmax dlm %s" % (hp.Alm.getlmax(
        glm.size), hp.Alm.getlmax(dlm.size))
    tot = 0.
    for _k, _t in times.iteritems():
        print '%20s: %.2f' % (_k, _t)
        tot += _t
    print "%20s: %2.f sec." % ('tot', tot)
    print "%20s: %2.f sec." % ('excl. defl. angle calc.',
                               tot - times['dx,dy (full sky)'] -
                               times['dx,dy band split'])
    print '%20s: %s = %.1f^2' % ('Tot. int. pix.', int(_Npix),
                                 np.sqrt(_Npix * 1.))

    return ret