def _build_sim_MVgclm(self, idx): """ MV. lensing potentials estimators """ G, C = self._get_sim_Pgclm(idx, 'p') if not self.f2map1.ivfs == self.f2map2.ivfs: _G, _C = self._get_sim_Pgclm(idx, 'p', swapped=True) G = 0.5 * (G + _G) del _G C = 0.5 * (C + _C) del _C GT, CT = self._get_sim_Tgclm(idx, 'p') if not self.f2map1.ivfs == self.f2map2.ivfs: _G, _C = self._get_sim_Tgclm(idx, 'p', swapped=True) GT = 0.5 * (GT + _G) del _G CT = 0.5 * (CT + _C) del _C hp.write_alm( os.path.join( self.lib_dir, 'sim_p_%04d.fits' % idx if idx != -1 else 'dat_p.fits'), G + GT) hp.write_alm( os.path.join( self.lib_dir, 'sim_x_%04d.fits' % idx if idx != -1 else 'dat_x.fits'), C + CT)
def gal2equ(in_file, out_file, smooth, eulers=None): e2g = np.array([[-0.054882486, -0.993821033, -0.096476249], [ 0.494116468, -0.110993846, 0.862281440], [-0.867661702, -0.000346354, 0.497154957]]) # intrinsic rotation g2e = np.linalg.inv(e2g) eps = 23.452294 - 0.0130125 - 1.63889E-6 + 5.02778E-7 eps = eps * np.pi / 180. e2q = np.array([[1., 0. , 0. ], [0., np.cos( eps ), -1. * np.sin( eps )], [0., np.sin( eps ), np.cos( eps ) ]]) g2q = np.dot(e2q , g2e) psi = np.arctan2(g2q[1,2],g2q[0,2]) theta = np.arccos(g2q[2,2]) phi = np.arctan2(g2q[2,1],-g2q[2,0]) # deduced from zyz rotation matrix fwhm = smooth*((2*np.pi)/360) alms = hp.read_alm(in_file) hp.smoothalm(alms, fwhm=fwhm) if eulers == None: hp.rotate_alm(alms, phi, theta, psi) # reverse rotation order -> extrinsic rotation print('Euler angles (zyz) = ', str(np.rad2deg(phi)), str(np.rad2deg(theta)), str(np.rad2deg(psi))) else: eulers = np.deg2rad(eulers) hp.rotate_alm(alms, eulers[0], eulers[1], eulers[2]) print('Euler angles (zyz) = ', str(np.rad2deg(eulers[0])), str(np.rad2deg(eulers[1])), str(np.rad2deg(eulers[2]))) print(e2q) hp.write_alm(out_file, alms)
def _cache_eblm(self, idx): elm = self.unlcmbs.get_sim_elm(idx) blm = None if 'b' not in self.fields else self.unlcmbs.get_sim_blm(idx) dlm = self.get_sim_plm(idx) assert 'o' not in self.fields, 'not implemented' lmaxd = hp.Alm.getlmax(dlm.size) hp.almxfl(dlm, np.sqrt( np.arange(lmaxd + 1, dtype=float) * np.arange(1, lmaxd + 2)), inplace=True) Qlen, Ulen = self.lens_module.alm2lenmap_spin([elm, blm], [dlm, None], self.nside_lens, 2, nband=self.nbands, facres=self.facres, verbose=self.verbose) elm, blm = hp.map2alm_spin([Qlen, Ulen], 2, lmax=self.lmax) del Qlen, Ulen hp.write_alm(os.path.join(self.lib_dir, 'sim_%04d_elm.fits' % idx), elm) del elm hp.write_alm(os.path.join(self.lib_dir, 'sim_%04d_blm.fits' % idx), blm)
def test_precomputed_alms(setup): alms, filename = setup nside = 64 # we assume the original `alms` are in `K_CMB` ref_freq = 40 test_map_K_CMB = hp.alm2map(alms, nside=nside) alms_K_RJ = alms * pysm.convert_units("K_CMB", "K_RJ", ref_freq) filename_K_RJ = filename.replace(".fits", "_RJ.fits") hp.write_alm(filename_K_RJ, alms_K_RJ) precomputed_alms = PrecomputedAlms( filename=filename_K_RJ, nside=nside, input_units="uK_RJ", input_reference_frequency_GHz=ref_freq, ) m = precomputed_alms.signal(23) np.testing.assert_allclose( m, test_map_K_CMB * pysm.convert_units("uK_CMB", "uK_RJ", 23)) freqs = np.array([1, 10, 100]) m_multifreq = precomputed_alms.signal(freqs) assert m_multifreq.shape == (3, 3, hp.nside2npix(64)) for freq, m in zip(freqs, m_multifreq): np.testing.assert_allclose( m, test_map_K_CMB * pysm.convert_units("uK_CMB", "uK_RJ", freq))
def test_precomputed_alms(setup): alms, filename = setup nside = 64 # we assume the original `alms` are in `K_CMB` ref_freq = 40 * u.GHz test_map_K_CMB = hp.alm2map(alms, nside=nside) << u.K_CMB alms_K_RJ = alms.to(u.K_RJ, equivalencies=u.cmb_equivalencies(ref_freq)) filename_K_RJ = filename.replace(".fits", "_RJ.fits") hp.write_alm(filename_K_RJ, alms_K_RJ) precomputed_alms = PrecomputedAlms( filename=filename_K_RJ, nside=nside, input_units="K_RJ", input_reference_frequency=ref_freq, ) m = precomputed_alms.get_emission(23 * u.GHz) assert_quantity_allclose( m, test_map_K_CMB.to(u.K_RJ, equivalencies=u.cmb_equivalencies(23 * u.GHz))) freqs = np.array([1, 10, 100]) * u.GHz for freq in freqs: np.testing.assert_allclose( precomputed_alms.get_emission(freq), test_map_K_CMB.to(u.K_RJ, equivalencies=u.cmb_equivalencies(freq)))
def _build_sim_ntt(self, idx): sLM = self._get_sim_ntt(idx) if not self.f2map1.ivfs == self.f2map2.ivfs: pass # No need to swap, this thing is symmetric anyways hp.write_alm( os.path.join( self.lib_dir, 'sim_ntt_%04d.fits' % idx if idx != -1 else 'dat_ntt.fits'), sLM)
def _build_sim_a_p(self, idx): fLM = self._get_sim_a_p(idx) if not self.f2map1.ivfs == self.f2map2.ivfs: _fLM = self._get_sim_f_p(idx, swapped=True) fLM = 0.5 * (fLM + _fLM) del _fLM hp.write_alm( os.path.join( self.lib_dir, 'sim_a_p_%04d.fits' % idx if idx != -1 else 'dat_a_p.fits'), fLM)
def setUp(self): # Create and a_lm expansion ell, _, cls = hp.sphtfunc.load_sample_spectra() self.lmax = len(ell) - 1 alms = hp.synalm(cls, lmax=self.lmax, new=True) self.fname_alm = 'test/alm.fits' hp.write_alm(self.fname_alm, alms, overwrite=True) self.fwhm = 0 self.nside = 16 self.pol = True self.comm = MPI.COMM_SELF self.refmap = hp.alm2map(alms[0], self.nside, verbose=False) self.freq = 0
def _build_sim_f(self, idx): """ MV. modulation estimators. """ G = self._get_sim_f_p(idx, joint=True) if not self.f2map1.ivfs == self.f2map2.ivfs: G = 0.5 * (G + self._get_sim_f_p(idx, joint=True, swapped=True)) GT = self._get_sim_ftt(idx, joint=True) if not self.f2map1.ivfs == self.f2map2.ivfs: GT = 0.5 * (GT + self._get_sim_ftt(idx, joint=True, swapped=True)) hp.write_alm( os.path.join( self.lib_dir, 'sim_f_%04d.fits' % idx if idx != -1 else 'dat_f.fits'), G + GT)
def setup(tmpdir): # tmpdir is a py.test feature to provide a temporary folder folder = tmpdir.mkdir("alms") np.random.seed(12) alm_size = hp.Alm.getsize(lmax=100) alms = 1j * np.random.normal(size=(3, alm_size)) alms += np.random.normal(size=(3, alm_size)) # str needed to support Python 3.5 filename = os.path.join(str(folder), "alms.fits") hp.write_alm(filename, alms) return alms, filename
def get_sim_tlm(self, idx): """Returns an inverse-filtered temperature simulation. Args: idx: simulation index Returns: inverse-filtered temperature healpy alm array """ tfname = os.path.join(self.lib_dir, 'sim_%04d_tlm.fits'%idx if idx >= 0 else 'dat_tlm.fits') if not os.path.exists(tfname): tlm = self._apply_ivf_t(self.sim_lib.get_sim_tmap(idx), soltn=None if self.soltn_lib is None else self.soltn_lib.get_sim_tmliklm(idx)) if self.cache: hp.write_alm(tfname, tlm) return tlm return hp.read_alm(tfname)
def _build_sim_Pgclm(self, idx): """ Pol. only lensing potentials estimators """ G, C = self._get_sim_Pgclm(idx, 'p_p') if not self.f2map1.ivfs == self.f2map2.ivfs: _G, _C = self._get_sim_Pgclm(idx, 'p_p', swapped=True) G = 0.5 * (G + _G) del _G C = 0.5 * (C + _C) del _C hp.write_alm( os.path.join( self.lib_dir, 'sim_p_p_%04d.fits' % idx if idx != -1 else 'dat_p_p.fits'), G) hp.write_alm( os.path.join( self.lib_dir, 'sim_x_p_%04d.fits' % idx if idx != -1 else 'dat_x_p.fits'), C)
def get_sim_qlm_mf(self, k, mc_sims, lmax=None): """Returns a QE mean-field estimate, by averaging QE estimates from a set simulations (caches the result). Args: k: quadratic estimator key mc_sims: simulation indices to use for the estimate. lmax: optionally reduces the lmax of the output healpy array. """ if lmax is None: lmax = self.get_lmax_qlm(k) assert lmax <= self.get_lmax_qlm(k) if k in ['p_tp', 'x_tp']: return (self.get_sim_qlm_mf('%stt' % k[0], mc_sims, lmax=lmax) + self.get_sim_qlm_mf('%s_p' % k[0], mc_sims, lmax=lmax)) if k in ['p_te', 'p_tb', 'p_eb', 'x_te', 'x_tb', 'x_eb']: return self.get_sim_qlm_mf(k[0] + k[2] + k[3], mc_sims, lmax=lmax) \ + self.get_sim_qlm_mf(k[0] + k[3] + k[2], mc_sims, lmax=lmax) if '_bh_' in k: # Bias-hardening assert self.resplib is not None, 'resplib arg necessary for this' kQE, ksource = k.split('_bh_') assert len(ksource) == 1 and ksource + kQE[1:] in self.keys, ( ksource, kQE) assert self.get_lmax_qlm(kQE) == self.get_lmax_qlm( ksource + kQE[1:]), 'fix this (easy)' lmax = self.get_lmax_qlm(kQE) wL = self.resplib.get_response(kQE, ksource) * ut.cli( self.resplib.get_response(ksource + kQE[1:], ksource)) ret = self.get_sim_qlm_mf(kQE, mc_sims, lmax=lmax) return ret - hp.almxfl( self.get_sim_qlm_mf(ksource + kQE[1:], mc_sims, lmax=lmax), wL) assert k in self.keys_fund, (k, self.keys_fund) fname = os.path.join(self.lib_dir, 'simMF_k1%s_%s.fits' % (k, ut.mchash(mc_sims))) if not os.path.exists(fname): MF = np.zeros(hp.Alm.getsize(lmax), dtype=complex) if len(mc_sims) == 0: return MF for i, idx in ut.enumerate_progress(mc_sims, label='calculating %s MF' % k): MF += self.get_sim_qlm(k, idx, lmax=lmax) MF /= len(mc_sims) hp.write_alm(fname, MF) print("Cached ", fname) return ut.alm_copy(hp.read_alm(fname), lmax=lmax)
def kap2phi(field_kappa, halo_kappa, unlensed_primary, phi_alm_file, writeMap=False, phi_map_file=None, lens_lmax=None): print "===============kappa2phi===============" nside = hp.get_nside(field_kappa) lmax = hp.Alm.getlmax(len(unlensed_primary)) if not lens_lmax: lens_lmax = lmax print "LMAX:", lmax #print "----Done loading maps." #combine print "----Combining field and halo kappa..." kappa_map = field_kappa + halo_kappa print "----Done combining." #convert to alm print "----Converting kappa map to alm..." kappa_lm = hp.map2alm(kappa_map, lmax=lens_lmax) print "----Done." #convert to phi (grav potential) print "----Converting kappa to phi..." l, m = hp.Alm.getlm(lens_lmax) phi_lm = kappa_lm * (2.0 / (l * (l + 1.0))) phi_lm[l == 0] = 0 print "----Writing phi alm to file..." hp.write_alm(phi_alm_file, phi_lm) print "----Done." if writeMap and phi_map_file: #convert to map print "----Converting phi to map..." phi_map = hp.alm2map(phi_lm, nside, lmax=lmax) print "----Writing phi map to file..." hp.write_map(phi_map_file, phi_map) print "----Done." print "=============kappa2phi end=============" return lmax
def get_sim_tlm(self, idx): fname = self.lib_dir + '/sim_%04d_tlm.fits' % idx if not os.path.exists(fname): tlm = self.unlcmbs.get_sim_tlm(idx) dlm = self.get_sim_plm(idx) lmaxd = hp.Alm.getlmax(dlm.size) hp.almxfl(dlm, np.sqrt( np.arange(lmaxd + 1, dtype=float) * np.arange(1, lmaxd + 2)), inplace=True) hp.write_alm( fname, lens.lens_tlm(tlm, dlm, nside=self.nside_lens, lmaxout=self.lmax)) return hp.read_alm(fname)
def _cache_eblm(self, idx): elm = self.unlcmbs.get_sim_elm(idx) blm = None if 'b' not in self.fields else self.unlcmbs.get_sim_blm(idx) dlm = self.get_sim_plm(idx) lmaxd = hp.Alm.getlmax(dlm.size) hp.almxfl(dlm, np.sqrt( np.arange(lmaxd + 1, dtype=float) * np.arange(1, lmaxd + 2)), inplace=True) elm, blm = lens.lens_eblm(elm, dlm, blm=blm, nside=self.nside_lens, lmaxout=self.lmax) hp.write_alm(self.lib_dir + '/sim_%04d_elm.fits' % idx, elm) del elm hp.write_alm(self.lib_dir + '/sim_%04d_blm.fits' % idx, blm)
def FilterMap(pixmap, freq, mask=None, lmax=None, pixelwindow=False): if mask is None: mask = np.ones_like(pixmap) else: assert hp.isnpixok(mask.size) Fl = GetFl(pixmap,freq, mask=mask, lmax=lmax) alm_fname='alm_%s.fits'%freq if not os.path.exists(alm_fname): print "computing alms" alm = hp.map2alm(pixmap, lmax=lmax) hp.write_alm(alm_fname,alm) else: print "reading alms",alm_fname alm = hp.read_alm(alm_fname) if pixelwindow: print "Correcting alms for pixelwindow" pl=hp.pixwin(hp.npix2nside(pixmap.size))[:lmax+1] else: pl=np.ones(len(Fl)) return hp.alm2map(hp.almxfl(alm, Fl/pl), hp.npix2nside(pixmap.size), lmax=lmax), Fl
def _build_sim_xfiltMVgclm(self, idx, k): """ Quick and dirty way to get the full set of estimators V X_1 W Y_2, or 1/2 (V X_1 W Y_2 + V Y_1 W X_2 ) if X_1 != Y_2; e.g. 1/2 (V E_1 W B_2 or V B_1 W E_2) """ assert k in [ 'ptt', 'pte', 'pet', 'ptb', 'pbt', 'pee', 'peb', 'pbe', 'pbb', 'xtt', 'xte', 'xet', 'xtb', 'xbt', 'xee', 'xeb', 'xbe', 'xbb' ] xfilt1 = {f: (k[-2] == f) * np.ones(10000) for f in ['t', 'e', 'b']} xfilt2 = {f: (k[-1] == f) * np.ones(10000) for f in ['t', 'e', 'b']} G, C = self._get_sim_Pgclm(idx, 'p', xfilt1=xfilt1, xfilt2=xfilt2) if not self.f2map1.ivfs == self.f2map2.ivfs or k[-1] != k[-2]: _G, _C = self._get_sim_Pgclm(idx, 'p', xfilt1=xfilt1, xfilt2=xfilt2, swapped=True) G = 0.5 * (G + _G) del _G C = 0.5 * (C + _C) del _C GT, CT = self._get_sim_Tgclm(idx, 'p', xfilt1=xfilt1, xfilt2=xfilt2) if not self.f2map1.ivfs == self.f2map2.ivfs or k[-1] != k[-2]: _G, _C = self._get_sim_Tgclm(idx, 'p', xfilt1=xfilt1, xfilt2=xfilt2, swapped=True) GT = 0.5 * (GT + _G) del _G CT = 0.5 * (CT + _C) del _C fnameG = os.path.join( self.lib_dir, 'sim_p%s_%04d.fits' % (k[1:], idx) if idx != -1 else 'dat_p%s.fits' % k[1:]) fnameC = os.path.join( self.lib_dir, 'sim_x%s_%04d.fits' % (k[1:], idx) if idx != -1 else 'dat_x%s.fits' % k[1:]) hp.write_alm(fnameG, G + GT) hp.write_alm(fnameC, C + CT)
def get_sim_tlm(self, idx): fname = os.path.join(self.lib_dir, 'sim_%04d_tlm.fits' % idx) if not os.path.exists(fname): tlm = self.unlcmbs.get_sim_tlm(idx) dlm = self.get_sim_plm(idx) assert 'o' not in self.fields, 'not implemented' lmaxd = hp.Alm.getlmax(dlm.size) hp.almxfl(dlm, np.sqrt( np.arange(lmaxd + 1, dtype=float) * np.arange(1, lmaxd + 2)), inplace=True) Tlen = self.lens_module.alm2lenmap(tlm, [dlm, None], self.nside_lens, facres=self.facres, nband=self.nbands, verbose=self.verbose) hp.write_alm(fname, hp.map2alm(Tlen, lmax=self.lmax, iter=0)) return hp.read_alm(fname)
def get_sim_blm(self, idx): """Returns an inverse-filtered B-polarization simulation. Args: idx: simulation index Returns: inverse-filtered B-polarization healpy alm array """ tfname = os.path.join(self.lib_dir, 'sim_%04d_blm.fits'%idx if idx >= 0 else 'dat_blm.fits') if not os.path.exists(tfname): if self.soltn_lib is None: soltn = None else: soltn = np.array([self.soltn_lib.get_sim_emliklm(idx), self.soltn_lib.get_sim_bmliklm(idx)]) elm, blm = self._apply_ivf_p(self.sim_lib.get_sim_pmap(idx), soltn=soltn) if self.cache: hp.write_alm(tfname, blm) hp.write_alm(os.path.join(self.lib_dir, 'sim_%04d_elm.fits'%idx if idx >= 0 else 'dat_elm.fits'), elm) return blm else: return hp.read_alm(tfname)
def get_sim_elm(self, idx): """Returns an inverse-filtered E-polarization simulation. Args: idx: simulation index Returns: inverse-filtered E-polarization healpy alm array """ tfname = os.path.join( self.lib_dir, 'sim_%04d_elm.fits' % idx if idx >= 0 else 'dat_elm.fits') if not os.path.exists(tfname): elm, blm = self._apply_ivf_p(self.sim_lib.get_sim_pmap(idx)) if self.cache: hp.write_alm(tfname, elm) hp.write_alm( os.path.join( self.lib_dir, 'sim_%04d_blm.fits' % idx if idx >= 0 else 'dat_blm.fits'), blm) return elm else: return hp.read_alm(tfname)
def _get_alms(self, a, idx): assert a in ['t', 'e', 'b'] tfname = os.path.join(self.lib_dir, 'sim_%04d_tlm.fits' % idx if idx >= 0 else 'dat_tlm.fits') fname = tfname.replace('tlm.fits', a + 'lm.fits') if not os.path.exists(fname): T = self.sim_lib.get_sim_tmap(idx) Q, U = self.sim_lib.get_sim_pmap(idx) if self.soltn_lib is None: soltn = None else: tlm = self.soltn_lib.get_sim_tmliklm(idx) elm = self.soltn_lib.get_sim_emliklm(idx) blm = self.soltn_lib.get_sim_bmliklm(idx) soltn = (tlm, elm, blm) tlm, elm, blm = self._apply_ivf([T, Q, U], soltn=soltn) if self.cache: hp.write_alm(tfname.replace('tlm.fits', 'tlm.fits'), tlm) hp.write_alm(tfname.replace('tlm.fits', 'elm.fits'), elm) hp.write_alm(tfname.replace('tlm.fits', 'blm.fits'), blm) return hp.read_alm(fname)
with io.nostdout(): s.get_stats() s.get_stacks() if rank==0: # Collect statistics and plot with io.nostdout(): acl = s.stats['acl']['mean'] xcl = s.stats['xcl']['mean'] icl = s.stats['icl']['mean'] if args.write_meanfield: mf_alm = s.stacks['rmf'] + 1j*s.stacks['imf'] hp.write_alm(f'{solenspipe.opath}/mf_{args.label}_{args.polcomb}_{isostr}_alm.fits',mf_alm,overwrite=True) ls = np.arange(xcl.size) Nl = maps.interp(Als['L'],Nl)(ls) pl = io.Plotter('CL',xyscale='loglog') pl.add(ls,acl,alpha=0.5,label='rr') if args.write_meanfield or args.read_meanfield: mf_cl = hp.alm2cl(mf_alm,mf_alm) / w4 pl.add(ls,mf_cl,alpha=0.5,label='mcmf cl') pl.add(ls,acl-mf_cl,label='rr - mf') pl.add(ls,xcl,label='ri') pl.add(ls,icl,color='k') pl.add(ls,icl+Nl,ls='--',label='ii + Nl') pl._ax.set_ylim(1e-10,1e-2) pl._ax.set_xlim(1,3100)
def s2let_ilc_dir_para(mapsextra): #mapsextra = (maps,scale_lmax,spin,n,j,i) print "\nRunning Directional S2LET ILC on wavelet scale", mapsextra[ 4], "/", jmax, "direction", mapsextra[3] + 1, "/", ndir, "\n" nrows = len(mapsextra[0]) #No. rows in covar. matrix smoothing_lmax = 2. * mapsextra[1] #=4.*nside(j) #Doubling lmax for input maps with zero-padding '''pool = mg.Pool(nprocess) mapsextra = [(maps[i],scale_lmax,smoothing_lmax,spin) for i in xrange(nrows)] del maps mapsdouble = np.array(pool.map(doubleworker,mapsextra)) del mapsextra''' #Serial version mapsdouble = np.zeros((nrows, ps.mw_size(smoothing_lmax)), dtype=np.complex128) #Pre-allocate array for i in xrange(nrows): mapsdouble[i, :] = doubleworker( (mapsextra[0][i], mapsextra[1], smoothing_lmax, mapsextra[2])) #mapsdouble = np.array(mapsdouble) #Calculating covariance matrix (at each pixel) #R = [None]*len(mapsdouble) R = np.zeros((len(mapsdouble), len(mapsdouble), len(mapsdouble[0])), dtype=np.complex128) #Pre-allocate array for i in xrange(len(mapsdouble)): R[i, :, :] = np.multiply(mapsdouble, np.roll(mapsdouble, -i, axis=0)) #R = np.array(R) #Calculate scale_fwhm & smoothing_lmax nsamp = 1200. npix = hp.nside2npix(0.5 * mapsextra[1]) #Equivalent number of HEALPix pixels scale_fwhm = 4. * mh.sqrt(nsamp / npix) #Smooth covariance matrices nindepelems = int( nrows * (nrows + 1) * .5) #No. independent elements in symmetric covariance matrix Rflat = np.reshape(R, (nrows * nrows, len(R[0, 0]))) #Flatten first two axes #del R #NEW!!! Rflatlen = len(Rflat) gausssmooth = hp.gauss_beam(scale_fwhm, smoothing_lmax - 1) #Testing zero-ing gaussian smoothing beam gauss_lmax = mapsextra[1] gausssmooth[gauss_lmax:] = 0. '''alms = [None]*nindepelems #alms_hp = [None]*nindepelems #alms_smooth = [None]*nindepelems Rsmoothflat = [None]*nindepelems #Only really need to pre-allocate this for i in xrange(nindepelems): #PARALLELISE print "Smoothing independent covariance element", i+1, "/", nindepelems alms[i] = ps.map2alm_mw(Rflat[i],scale_lmax,spin) #No pixwin correct. with MW sampling #alms_hp[i] = ps.lm2lm_hp(alms[i],smoothing_lmax) #Now in healpy ordering hp.almxfl(alms[i],gausssmooth,inplace=True) #Multiply by gaussian beam #alms_smooth[i] = ps.lm_hp2lm(alms_hp[i],smoothing_lmax) #Back in MW ordering Rsmoothflat[i] = ps.alm2map_mw(alms[i],scale_lmax,spin) #Smooth covariance in MW Rsmoothflat = np.array(Rsmoothflat)''' #Parallel version '''pool = mg.Pool(nprocess) Rflatextra = [(Rflat[i],smoothing_lmax,spin,gausssmooth,scale_lmax,en,i) for i in xrange(nindepelems)] del Rflat Rsmoothflat = np.array(pool.map(smoothworker,Rflatextra)) del Rflatextra''' #Serial version Rsmoothflat = np.zeros_like(Rflat) #Pre-allocate array for i in xrange(nindepelems): Rsmoothflat[i, :] = smoothworker( (Rflat[i], smoothing_lmax, mapsextra[2], gausssmooth, mapsextra[1], mapsextra[3], i, mapsextra[4])) del Rflat #Rsmoothflat = np.array(Rsmoothflat) #Rearranging and padding out elements of Rsmooth Rsmoothflat[: nrows] = 0.5 * Rsmoothflat[: nrows] #Multiply diag elements by half- not double-count Rsmoothflat = np.vstack( (Rsmoothflat, np.zeros( (Rflatlen - len(Rsmoothflat), len(Rsmoothflat[0]))))) #Zero-pad Rsmoothfat = np.reshape( Rsmoothflat, (nrows, nrows, len(Rsmoothflat[0]))) #Reshape Rsmooth as mat. del Rsmoothflat for i in xrange(1, len(Rsmoothfat[0])): Rsmoothfat[:, i, :] = np.roll(Rsmoothfat[:, i, :], i, axis=0) #Now in correct order-but with gaps Rsmoothfat = Rsmoothfat + np.transpose(Rsmoothfat, axes=(1, 0, 2)) #Gaps filled in #Compute inverse covariance matrices Rinv = np.linalg.inv(np.transpose( Rsmoothfat, axes=(2, 0, 1))) #Parallel vers. actually slower!? #del Rsmoothfat #Compute weights vectors (at each pixel) wknumer = np.sum(Rinv, axis=-1) del Rinv wkdenom = np.sum(wknumer, axis=-1) wk = wknumer / wkdenom[:, None] del wknumer, wkdenom #Dot weights with maps (at each small pixel) - at double l(j) finalmap = np.sum(np.multiply(wk, mapsdouble.T), axis=-1) del wk, mapsdouble #Downgrade resolution of MW maps print "Downgrading resolution of CMB wavelet map" finalmapalms = ps.map2alm_mw(finalmap, smoothing_lmax, mapsextra[2]) del finalmap #hp.write_alm('alms.fits',finalmapalms,lmax=mapsextra[1]-1,mmax=mapsextra[1]-1) alms_fname = 'alms_' + str(mapsextra[5]) + '.fits' hp.write_alm(alms_fname, finalmapalms, lmax=mapsextra[1] - 1, mmax=mapsextra[1] - 1) del finalmapalms finalmapalmstruncate = hp.read_alm(alms_fname) finalmaphalf = ps.alm2map_mw(finalmapalmstruncate, mapsextra[1], mapsextra[2]) del finalmapalmstruncate #Saving output map wav_outfits = wav_outfits_root + '_j' + str( mapsextra[4]) + '_n' + str(mapsextra[3] + 1) + '.npy' if mapsextra[4] == -1: wav_outfits = scal_outfits np.save(wav_outfits, finalmaphalf) del finalmaphalf return 0
def save_gaus_map(self, mapN): #print "for the gNL we will use the maps generated earlier, so no need to save" print ("writing "+str(mapN)) hp.write_alm(self.mapsdir+"gmap_"+str(mapN)+".fits", self.gausalm)
fieldKappaType = checkFitsType(fieldKappaFile) primaryType = checkFitsType(unlensedPrimaryFile) print "Loading field kappa..." fieldKappa = hp.read_map(fieldKappaFile) print "Loading halo kappa..." haloKappa = hp.read_map(haloKappaFile) if primaryType != 'alm': print "Primary is map - converting to alm..." unlensedPrimaryMap = hp.read_map(unlensedPrimaryFile) print "get filename" unlensedPrimaryFile = appendToFilename(unlensedPrimaryFile, "alm") print unlensedPrimaryFile print "convert" unlensedPrimary = hp.map2alm(unlensedPrimaryMap) print "write" hp.write_alm(unlensedPrimaryFile, unlensedPrimary) print "Saved new primary alm to", unlensedPrimaryFile else: print "Loading primary..." unlensedPrimary = hp.read_alm(unlensedPrimaryFile) unlenLmax = hp.Alm.getlmax(unlensedPrimary.shape[0]) #get lmax and create phi alm if not os.path.exists(phiAlmFile): print "phi doesn't exist" phiLmax = kap2phi(fieldKappa, haloKappa, unlensedPrimary, phiAlmFile) #lmax = kap2phi(fieldKappaFile, haloKappaFile, unlensedPrimaryFile, phiAlmFile) else: print "phi exists" phiAlm = hp.read_alm(phiAlmFile) phiLmax = hp.Alm.getlmax(phiAlm.shape[0])
""" from __future__ import print_function from __future__ import absolute_import import healpy as hp import numpy as np import os import pickle as pk import collections from plancklens import utils as ut, utils_qe as uqe from plancklens.helpers import mpi from plancklens import qresp _write_alm = lambda fn, alm: hp.write_alm(fn, alm, overwrite=True) def eval_qe(qe_key, lmax_ivf, cls_weight, get_alm, nside, lmax_qlm, verbose=True): """Evaluates a quadratic estimator gradient and curl terms. (see 'library' below for QE estimation coupled to CMB inverse-variance filtered simulation libraries, whose implementation can be faster for some estimators.) Args:
def mock_gen(nside=128, lmax=256, masked=False, beamed=False, anisotropic_noise=False, noise_amplitude=100, CAMB_Cov_S_fname="pol_data_boost_totCls.dat", Cov_S_provided=None, EB_only=False, maskname=None, beamname=None): """ Simulates a polarized CMB map IMPORTANT: Changing nside & lmax will respectively require rebuilding of mask & beam, respectively """ print("### Generating mock CMB data ###") npix = hp.nside2npix(nside) if Cov_S_provided is not None: Cov_S = np.load(Cov_S_provided)["Cov_S"] cls = np.zeros([6, lmax + 1]) for ell in range(lmax + 1): cls[0, ell] = Cov_S_provided[ell, 0, 0] cls[1, ell] = Cov_S_provided[ell, 1, 1] cls[2, ell] = Cov_S_provided[ell, 2, 2] cls[3, ell] = Cov_S_provided[ell, 0, 1] cls[3, ell] = Cov_S_provided[ell, 1, 0] else: # Read the cls from CAMB (.dat) # DKR -> usual format, GL -> more clever format cls, Cov_S = DKR_read_camb_cl(CAMB_Cov_S_fname, lmax, EB_only) # Generate a polarized CMB map from CAMB spectra alms = hp.synalm(tuple(cls), new=True) # "new" format for cls ordering # Manual generation below ''' #################### alm_T = np.zeros(hp.Alm.getsize(lmax), dtype='complex128') alm_E = np.zeros(hp.Alm.getsize(lmax), dtype='complex128') alm_B = np.zeros(hp.Alm.getsize(lmax), dtype='complex128') for l in range(2,lmax + 1): C = np.linalg.cholesky(Cov_S[l,:,:]) idx = hp.Alm.getidx(lmax, l, 0) vec = np.dot(C, np.random.randn(3)) alm_T[idx] = vec[0] alm_E[idx] = vec[1] alm_B[idx] = vec[2] for m in range(1, l+1): idx = hp.Alm.getidx(lmax, l, m) vec = np.dot(C, (np.random.randn(3) + 1.0j*np.random.randn(3))/np.sqrt(2.0)) alm_T[idx] = vec[0] alm_E[idx] = vec[1] alm_B[idx] = vec[2] alms = [alm_T, alm_E, alm_B] #################### ''' simulated_map = hp.alm2map(tuple(alms), nside, lmax, pol=True, verbose=False) print("### True polarized CMB map generated successfully ###") hp.write_map("true_map.fits", simulated_map, overwrite=True) alms = np.array(alms) if beamed: beam = _extract_beam(lmax, beamname) np.savez("beam_T_P.npz", beam=beam) print("### Beam saved successfully ###") alms = numba_almxfl_vec(alms, beam, lmax) beamed_simulated_map = hp.alm2map(tuple(alms), nside) print("### True beamed polarized CMB map generated successfully ###") hp.write_map("true_beamed_map.fits", beamed_simulated_map, overwrite=True) hp.write_alm("true_map_alms_T.fits", alms[0, :], overwrite=True) hp.write_alm("true_map_alms_E.fits", alms[1, :], overwrite=True) hp.write_alm("true_map_alms_B.fits", alms[2, :], overwrite=True) # Generate mock map and noise covariance if anisotropic_noise: print("### Generating anisotropic noise ###") noise_IQU, Cov_D_pix, Cov_C_harmonic = _mock_anisotropic_noise_covariance_generator( nside, lmax, noise_amplitude) # Save anisotropic noise covariance np.savez("anisotropic_noise_covariance.npz", Cov_D_pix=Cov_D_pix, Cov_C_harmonic=Cov_C_harmonic) else: print("### Generating white noise ###") noise_IQU, Cov_N = _mock_noise_covariance_generator( npix, noise_amplitude, EB_only) # Save white noise covariance np.savez("noise_covariance.npz", Cov_N=Cov_N) if beamed: mock_map = beamed_simulated_map + noise_IQU print("### Mock beamed polarized CMB map generated successfully ###") else: mock_map = simulated_map + noise_IQU print("### Mock polarized CMB map generated successfully ###") # Save simulated map hp.write_map("mock_map.fits", mock_map, overwrite=True) print("### Mock data saved successfully ###") ### We build & save the mask here but load it in the observations module if masked: mask = _build_mask(npix, nside, lmax, maskname) hp.write_map("mask_T_P.fits", mask, overwrite=True) print("### Mask saved successfully ###")
from pixell import enmap, powspec, curvedsky import numpy as np import os, sys from mapsims import cmb import healpy as hp seed = 10 lmax = 150 iteration_num = 0 cmb_set = 0 lensed = False aberrated = False nside = 32 theory_filename = "mapsims/data/test_scalCls.dat" save_name = "mapsims/data/test_map.fits" output_directory = "mapsims/data" output_file = cmb._get_cmb_map_string(output_directory, iteration_num, cmb_set, lensed, aberrated) ps = powspec.read_spectrum(theory_filename) alms = curvedsky.rand_alm_healpy(ps, lmax=lmax, seed=seed, dtype=np.complex64) hp.write_alm(output_file, alms, overwrite=True) imap = cmb.get_cmb_sky( iteration_num=0, nside=nside, cmb_dir=output_directory, lensed=False, aberrated=False, ) hp.write_map(save_name, imap, overwrite=True)
''' make_alms_with_fnl.py Create alms using the Elsner maps (http://gavo.mpe.mpg.de/pub/Elsner/) for alm_G and alm_NG to create: alm = alm_G + fnl * alm_NG where the input parameter to this program is fnl. ''' import healpy as hp import numpy as np nsims = 1000 fnl = 0 for i in range(1,nsims+1): fn_alm = 'alm_fnl_%i_sim_%04d.fits' % (fnl, i) fn_cl = 'cl_fnl_%i_sim_%04d.fits' % (fnl, i) print 'making %s...' % fn_alm fn_alm_l = 'alm_l_%04d_v3.fits' % (i,) fn_alm_nl = 'alm_nl_%04d_v3.fits' % (i,) alm_g = hp.read_alm(fn_alm_l) alm_ng = hp.read_alm(fn_alm_nl) alm = alm_g + fnl * alm_ng hp.write_alm(fn_alm, alm) print 'making %s...' % fn_cl cl = hp.alm2cl(alm) np.savetxt(fn_cl, cl)
def setUpClass(cls): ''' Create .npy and .fits blm arrays. ''' blm_name = opj(test_data_dir, 'blm_test.npy') cls.blm_name = blm_name blm_cross_name = opj(test_data_dir, 'blm_cross_test.npy') cls.blm_cross_name = blm_cross_name # .fits versions cls.blm_name_fits = cls.blm_name.replace('.npy', '.fits') cls.blm_cross_name_fits = cls.blm_cross_name.replace('.npy', '.fits') # .fits versions that are truncated in m cls.blm_name_mmax_fits = opj(test_data_dir, 'blm_test_mmax.fits') cls.blm_cross_name_mmax_fits = opj(test_data_dir, 'blm_cross_test_mmax.fits') beam_opts = dict(az=10, el=5, polang=90., btype='PO', amplitude=0.5, po_file=blm_name, deconv_q=False, normalize=False) cls.beam_opts = beam_opts # Store blm array cls.lmax = 3 alm_size = hp.Alm.getsize(cls.lmax) blm = np.zeros(alm_size, dtype=np.complex128) # ell = 0, 1, 2, 3, m=0 blm = np.array([1, 1, 1, 1, 2, 2, 2, 3, 3, 4], dtype=np.complex128) cls.blm = blm # Derived using eq.24 in hivon 2016 (gamma=sigma=0). cls.blmm2_expd = np.array([0, 0, 3, 3, 0, -2, -2, 1, 1, 2], dtype=np.complex128) cls.blmp2_expd = np.array([0, 0, 3, 3, 0, 0, 4, 0, 0, 0], dtype=np.complex128) np.save(blm_name, cls.blm) # Older healpy versions have bugs with write_alm writing multiple alm. if int(hp.__version__.replace('.', '')) >= 1101: hp.write_alm(cls.blm_name_fits, cls.blm, overwrite=True) # Also save explicit co- and cross-polar beams # but just use blm three times. np.save(blm_cross_name, np.asarray([cls.blm, cls.blm, cls.blm])) if int(hp.__version__.replace('.', '')) >= 1101: hp.write_alm(cls.blm_cross_name_fits, [cls.blm, cls.blm, cls.blm], overwrite=True) # Write .fits files that have mmax = 2. if int(hp.__version__.replace('.', '')) >= 1101: hp.write_alm(cls.blm_name_mmax_fits, cls.blm, overwrite=True, mmax=2) hp.write_alm(cls.blm_cross_name_mmax_fits, [cls.blm, cls.blm, cls.blm], overwrite=True, mmax=2)
"""Rotate Planck lensing map.""" import healpy as hp fname = "data/COM_Lensing_Szdeproj_4096_R3.00_TT_dat_klm.fits" R = hp.rotator.Rotator(coord=["G", "C"]) alm = hp.read_alm(fname) alm_rot = R.rotate_alm(alm) hp.write_alm("lensing_szdeproj_alm.fits", alm_rot, overwrite=True)
def main(): ''' Loading and calculating power spectrum components ''' # Get run parameters s_fn_params = 'data/params.pkl' (i_lmax, i_nside, s_fn_map, s_map_name, s_fn_mask, s_fn_mll) = get_params(s_fn_params) print "" print "Run parameters:" print "lmax: %i, nside: %i, map name: %s" % (i_lmax, i_nside, s_map_name) # Load Planck map and mask print "" print "Loading map and mask..." na_map = hp.read_map(s_fn_map) # for Planck SMICA, units of K na_map = na_map / 1e6 / 2.7 # convert units to mK -> unitless na_map = hp.remove_dipole(na_map) # removes the dipole and monopole -- turn off other lines doing this... na_mask = hp.read_map(s_fn_mask) na_map_masked = na_map * na_mask na_alm = hp.map2alm(na_map_masked, lmax=i_lmax-1) s_fn_alm = 'output/na_alm_data.fits' hp.write_alm(s_fn_alm, na_alm) # Spherical harmonic transform (map -> power spectrum) print "" print "Calculating power spectra..." na_cltt = hp.anafast(na_map_masked, lmax=i_lmax-1) na_wll = hp.anafast(na_mask, lmax=i_lmax-1) na_wll = na_wll + 2.0 # remove monopole and dipole na_ell = np.arange(len(na_cltt)) na_ell = na_ell + 2.0 # remove monopole and dipole # Load mode coupling matrix and invert it print "" print "Loading and inverting mode coupling matrix..." na_mll = np.load(s_fn_mll) na_mll_inv = np.linalg.inv(na_mll) # Calculate Mll corrected power spectrum na_clttp = np.dot(na_mll_inv, na_cltt) # Save Mll corrected power spectrum s_fn_clttp = 'output/na_cltt.npy' np.save(s_fn_clttp, na_clttp) s_fn_cltt = 'output/na_cltt_not_corrected.npy' np.save(s_fn_cltt, na_cltt) print "" print "Saving power spectrum to %s" % s_fn_clttp ''' Associated plots: map; mask; masked map; power spectrum of mask, power spectrum of map, masked map, and mode coupling corrected map ''' # NOTE: Mollview doesn't seem to work on cirrus -- probably an error with # this version of Healpy # plot_map(na_map, s_title='Raw Planck') # plot_map(na_mask, s_title='Mask') # plot_map(na_map_masked, s_title='Masked Map') plot_ps([na_ell], [na_wll], [''], s_ylabel='$W_\ell$', s_title='', s_fn_plot='plots/fig_mask_ps.png') plot_ps([na_ell, na_ell], [na_cltt, na_clttp], ['Masked', 'Masked, Corrected'], s_ylabel='$C_\ell$', s_title='', s_fn_plot='plots/fig_masked_masked_corrected_ps2.png') return
def main(): """ Loading and calculating power spectrum components """ # Get run parameters s_fn_params = "data/params.pkl" (i_lmax, i_nside, s_fn_map, s_map_name, s_fn_mask, s_fn_mll) = get_params(s_fn_params) print "" print "Run parameters:" print "lmax: %i, nside: %i, map name: %s" % (i_lmax, i_nside, s_map_name) # Load Planck map and mask print "" print "Loading map and mask..." na_map = hp.read_map(s_fn_map) # for Planck SMICA, units of uK na_map = na_map / 1e6 / 2.7 # convert units to K -> unitless na_mask = hp.read_map(s_fn_mask) na_map_masked = na_map * na_mask na_alm = hp.map2alm(na_map_masked, lmax=i_lmax - 1) s_fn_alm = "output/na_alm_data.fits" hp.write_alm(s_fn_alm, na_alm) # Spherical harmonic transform (map -> power spectrum) print "" print "Calculating power spectra..." na_cltt = hp.anafast(na_map_masked, lmax=i_lmax - 1) na_wll = hp.anafast(na_mask, lmax=i_lmax - 1) na_wll = na_wll[2:] # remove monopole and dipole na_ell = np.arange(len(na_cltt)) na_ell = na_ell[2:] # remove monopole and dipole # Load mode coupling matrix and invert it print "" print "Loading and inverting mode coupling matrix..." na_mll = np.load(s_fn_mll) na_mll_inv = np.linalg.inv(na_mll) # Calculate Mll corrected power spectrum na_clttp = np.dot(na_mll_inv, na_cltt) na_clttp = na_clttp[2:] # remove monopole and dipole na_cltt = na_cltt[2:] # remove monopole and dipole # Save Mll corrected power spectrum s_fn_clttp = "output/na_cltt.npy" np.save(s_fn_clttp, na_clttp) print "" print "Saving power spectrum to %s" % s_fn_clttp """ Associated plots: map; mask; masked map; power spectrum of mask, power spectrum of map, masked map, and mode coupling corrected map """ # NOTE: Mollview doesn't seem to work on cirrus -- probably an error with # this version of Healpy # plot_map(na_map, s_title='Raw Planck') # plot_map(na_mask, s_title='Mask') # plot_map(na_map_masked, s_title='Masked Map') plot_ps([na_ell], [na_wll], [""], s_ylabel="$W_\ell$", s_title="", s_fn_plot="plots/fig_mask_ps.png") plot_ps( [na_ell, na_ell], [na_cltt, na_clttp], ["Masked", "Masked, Corrected"], s_ylabel="$C_\ell$", s_title="", s_fn_plot="plots/fig_masked_masked_corrected_ps.png", ) return
def save_gaus_map(self, mapN): print "writing "+str(mapN) hp.write_alm(self.mapsdir+"gmap_"+str(mapN)+".fits", self.gausalm0)