def setUp(self): self.nside = 64 self.lmax = self.nside seed = 12345 np.random.seed(seed) self.mapr = hp.synfast( np.ones(self.lmax+1), self.nside, pixwin=False, fwhm=0.0, sigma=None ) self.mapi = hp.synfast( np.ones(self.lmax+1), self.nside, pixwin=False, fwhm=0.0, sigma=None ) self.almg = hp.synalm( np.ones(self.lmax+1), self.lmax ) self.almc = hp.synalm( np.ones(self.lmax+1), self.lmax )
def create_gaussian_alm(): ng_data.load_cl() map_gaussian,alm_g = hp.synfast(ng_data.cl,nside=ng_config.nside,lmax=ng_config.lmax,alm=True) #print alm_g hp.fitsfunc.write_alm(ng_config.get_filename_alm_g(),alm_g) #save it print 'Map written: ' + ng_config.get_filename_alm_g() return alm_g
def setUp(self): self.path = os.path.dirname( os.path.realpath( __file__ ) ) try: self.map1 = [hp.ma(m) for m in hp.read_map(os.path.join(self.path, 'data', 'wmap_band_iqumap_r9_7yr_W_v4.fits'), (0,1,2))] self.map2 = [hp.ma(m) for m in hp.read_map(os.path.join(self.path, 'data', 'wmap_band_iqumap_r9_7yr_V_v4.fits'), (0,1,2))] self.mask = hp.read_map(os.path.join(self.path, 'data', 'wmap_temperature_analysis_mask_r9_7yr_v4.fits')).astype(np.bool) except exceptions.IOError: warnings.warn("""Missing Wmap test maps from the data folder, please download them from Lambda and copy them in the test/data folder: http://lambda.gsfc.nasa.gov/data/map/dr4/skymaps/7yr/raw/wmap_band_iqumap_r9_7yr_W_v4.fits http://lambda.gsfc.nasa.gov/data/map/dr4/skymaps/7yr/raw/wmap_band_iqumap_r9_7yr_V_v4.fits http://lambda.gsfc.nasa.gov/data/map/dr4/ancillary/masks/wmap_temperature_analysis_mask_r9_7yr_v4.fits on Mac or Linux you can run the bash script get_wmap_maps.sh from the same folder """) for m in chain(self.map1, self.map2): m.mask = np.logical_not(self.mask) self.cla = hp.read_cl(os.path.join(self.path, 'data', 'cl_wmap_fortran.fits')) cls = pyfits.open(os.path.join(self.path, 'data', 'cl_iqu_wmap_fortran.fits'))[1].data # order of HEALPIX is TB, EB while in healpy is EB, TB self.cliqu = [cls.field(i) for i in (0,1,2,3,5,4)] nside = 32 lmax = 64 fwhm_deg = 7. seed = 12345 np.random.seed(seed) self.mapiqu = hp.synfast(self.cliqu, nside, lmax=lmax, pixwin=False, fwhm=np.radians(fwhm_deg), new=False)
def synfast(self): """Function for the calculation of lensed CMB maps directly from lensed Cls using healpix's synfast routine. """ # get the spectra. These are in CAMB format, we discard the last # three corresponding to dd, dt, de, respectively. ell, tt, ee, bb, te, _, _, _ = self.CMB_Specs lmax_cl = len(ell) + 1 ell = np.arange(lmax_cl + 1) # in CAMB format so we must divide by the scaling factor factor = ell * (ell + 1.) / 2. / np.pi cl_teb = np.zeros((6, lmax_cl + 1)) cl_teb[0, 2:] = tt / factor[2:] cl_teb[1, 2:] = ee / factor[2:] cl_teb[2, 2:] = bb / factor[2:] cl_teb[3, 2:] = te / factor[2:] cl_teb[4, 2:] = 0. cl_teb[5, 2:] = 0. np.random.seed(self.CMB_Seed) T, Q, U = hp.synfast(cl_teb, self.Nside, pol=True, new=True, verbose=False) @FloatOrArray def model(nu, **kwargs): cmb_map = np.array([T, Q, U]) * convert_units("uK_CMB", "uK_RJ", nu) if self.pixel_indices is None: return cmb_map else: return cmb_map[:, self.pixel_indices] return model
def setUp(self): self.lmax = 64 self.path = os.path.dirname( os.path.realpath( __file__ ) ) self.map1 = [hp.ma(m) for m in hp.read_map(os.path.join(self.path, 'data', 'wmap_band_iqumap_r9_7yr_W_v4_udgraded32.fits'), (0,1,2))] self.map2 = [hp.ma(m) for m in hp.read_map(os.path.join(self.path, 'data', 'wmap_band_iqumap_r9_7yr_V_v4_udgraded32.fits'), (0,1,2))] self.mask = hp.read_map(os.path.join(self.path, 'data', 'wmap_temperature_analysis_mask_r9_7yr_v4_udgraded32.fits')).astype(np.bool) for m in chain(self.map1, self.map2): m.mask = np.logical_not(self.mask) self.cla = hp.read_cl(os.path.join(self.path, 'data', 'cl_wmap_band_iqumap_r9_7yr_W_v4_udgraded32_II_lmax64_rmmono_3iter.fits')) self.cl_fortran_nomask = hp.read_cl(os.path.join(self.path, 'data', 'cl_wmap_band_iqumap_r9_7yr_W_v4_udgraded32_II_lmax64_rmmono_3iter_nomask.fits')) cls_file = pyfits.open(os.path.join(self.path, 'data', 'cl_wmap_band_iqumap_r9_7yr_W_v4_udgraded32_IQU_lmax64_rmmono_3iter.fits')) # fix for pyfits to read the file with duplicate column names for i in range(2, 6): cls_file[1].header['TTYPE%d' % i] += '-%d' % i cls = cls_file[1].data # order of HEALPIX is TB, EB while in healpy is EB, TB self.cliqu = [np.array(cls.field(i)) for i in (0,1,2,3,5,4)] nside = 32 lmax = 64 fwhm_deg = 7. seed = 12345 np.random.seed(seed) self.mapiqu = hp.synfast(self.cliqu, nside, lmax=lmax, pixwin=False, fwhm=np.radians(fwhm_deg), new=False)
def main(nsim=0): nl = h._nl # Load map, mask, mll print "" print "Loading map, mask, mll, and calculating mll_inv..." map_data = hp.read_map(h._fn_map) mask = hp.read_map(h._fn_mask) mll = np.load(h._fn_mll) mll_inv = np.linalg.inv(mll) # Read in Planck map: normalize, remove mono-/dipole, mask print "Normalizing, removing mono-/dipole, and masking map..." map_masked = map_data * mask # Create cltt (cltt_data_masked) and correct it (cltt_data_corrected) print "Calculating cltt_data_masked, cltt_data_corrected..." cltt_data_masked = hp.anafast(map_masked) cltt_data_masked = cltt_data_masked[:nl] cltt_data_corrected = np.dot(mll_inv, cltt_data_masked) # Create simulation of map (map_sim) from cltt_data_corrected print "Creating and saving map_sim_%i..." % nsim map_sim = hp.synfast(cltt_data_corrected, h._nside) hp.write_map('output/map_sim_%i.fits' % nsim, map_sim)
def simulate_GaussianSky(self, NSIDE,seed = None): """ Returns a healpy sky map from the Cl with synfast """ if self.lmin > 0: if self.verbose: utils.PrtMsg("Filling ell (0 : " + str(self.lmin) + ") with zeros.", self.verbose) Cl = Cl_lmax(self.ell(), self.Cl).Cl # In order to fill in the missing ells else: Cl = self.Cl if seed is not None : np.random.set_state(seed) return hp.synfast(Cl, NSIDE, new=True)
def test_synfast(self): nside = 32 lmax = 64 fwhm_deg = 7. seed = 12345 np.random.seed(seed) map_pregen = hp.read_map(os.path.join(self.path, 'data', 'map_pregen_seed%d.fits' % seed)) sim_map = hp.synfast(self.cla, nside, lmax = lmax, pixwin=False, fwhm=np.radians(fwhm_deg), new=False, pol=False) np.testing.assert_array_almost_equal(sim_map, map_pregen, decimal=8)
def toy_test_real_vs_harmonic(amp=100., nside=2**5, nexp=1000): # generate the signal map npix = hp.nside2npix(nside) signal = np.zeros(npix) ind_hpix = hp.ang2pix(nside, 0., 0., nest=False) signal[ind_hpix] = 1. # get autospectrum of signal lmax_tmp = 4*nside-1 cl_signal = hp.anafast(signal, lmax=lmax_tmp) l = np.arange(len(cl_signal)) # create white CL's with unit variance nl = np.ones_like(l, dtype=np.float) nl /= (np.sum(nl*(2.*l+1.))/4./np.pi) # create harmonic space weights. the factor of # 2L+1 is because we'll be dealing with 1d spectra rather than 2d. weight = (2.*l+1.)/nl est_real = [] est_harm = [] for i in range(nexp): print '%i/%i'%(i,nexp) # make noise map noise = hp.synfast(nl, nside) data = amp*signal + noise # get real space estimate of signal amplitude est_real.append(data[ind_hpix]) # get harmonic space estimate of signal amplitude amp_tmp = np.sum(hp.anafast(signal, map2=data, lmax=lmax_tmp)*weight) / np.sum(cl_signal*weight) est_harm.append(amp_tmp) est_real = np.array(est_real) est_harm = np.array(est_harm) print ' ' print ' mean(est_real): %0.2f'%est_real.mean() print ' mean(est_harm): %0.2f'%est_harm.mean() print ' var(est_real): %0.2f'%est_real.var() print ' var(est_harm): %0.2f'%est_harm.var() print ' VarRatio: %0.2f'%(est_harm.var()/est_real.var()) print ' std(est_real): %0.2f'%est_real.std() print ' std(est_harm): %0.2f'%est_harm.std() print ' StdRatio: %0.2f'%(est_harm.std()/est_real.std()) print ' ' nbins=15 pl.clf() pl.hist(est_real,bins=nbins, alpha=0.5) pl.hist(est_harm,bins=nbins, alpha=0.5) pl.legend(['real space','harmonic space']) pl.title('AMP=%0.3f, NEXP=%i, VarRatio=%0.3f'%(amp, nexp, est_harm.var()/est_real.var())) ipdb.set_trace()
def mk_map_onescale(nside,lmin=0,alpha=-2.17,smooth=1.): """ One power law model -> random field realization """ lmax = 3*nside - 1 ll = np.array(range(lmin,lmax,1)) Cl = np.power(ll,alpha) Cl[0] = 0. #zero mean Q = hp.synfast(Cl,nside,lmax=lmax) Qsm = hp.smoothing(Q,fwhm=np.radians(smooth)) return Qsm
def getpolsky(self): nside = self.nside tmap = self.getsky() cl = np.arange(3.0*nside-1)**(-FullSkySynchrotron.beta) cl[0] = 0.0 qamp = healpy.synfast(cl, nside) uamp = healpy.synfast(cl, nside) polfrac = (qamp**2 + uamp**2).mean()**0.5 qamp = qamp / polfrac * 0.3 uamp = uamp / polfrac * 0.3 tqumap = np.zeros((tmap.shape[0], 3, tmap.shape[1]), dtype=tmap.dtype) tqumap[:, 0] = tmap tqumap[:, 1] = qamp * tmap tqumap[:, 2] = uamp * tmap return tqumap
def get_kernel(self, nside,lmax): cltt=abl.get_cltt(2*lmax) map1=hp.synfast([cltt,cltt, cltt ,cltt*0], nside,lmax=lmax, new=False, pixwin=False ) hp.write_map(self.path_temp+'map1.fits', map1) spice.spice(self.path_temp+'map1.fits', nlmax=lmax, polarization=True, kernel=self.path_ker+'ker.fits', clfile=self.path_temp+'cldum.dat', apodizesigma=self.apodizesigma, thetamax=self.thetamax ) self.ker=pyf.open(self.path_ker+'ker.fits')[0].data[0] os.system('rm '+self.path_temp+'map1.fits') os.system('rm '+self.path_temp+'cldum.dat')
def test_map2alm(self): nside = 32 lmax = 64 fwhm_deg = 7. seed = 12345 np.random.seed(seed) orig = hp.synfast(self.cla, nside, lmax=lmax, pixwin=False, fwhm=np.radians(fwhm_deg), new=False) tmp = np.empty(orig.size * 2) tmp[::2] = orig maps = [orig, orig.astype(np.float32), tmp[::2]] for input in maps: alm = hp.map2alm(input, iter=10) output = hp.alm2map(alm, nside) np.testing.assert_allclose(input, output, atol=1e-4)
def test_rotate_map_polarization_with_spectrum(): """Rotation of reference frame should not change the angular power spectrum. In this test we create a map from a spectrum with a pure EE signal and check that the spectrum of this map and the spectrum of the same map rotated from Galactic to Ecliptic agrees. This test checks if the QU rotation is correct""" nside = 32 cl = np.zeros((6, 96), dtype=np.double) # Set ell=1 for EE to 1 cl[1][2] = 1 gal2ecl = Rotator(coord=["G", "E"]) m_original = hp.synfast(cl, nside=nside, new=True) cl_from_m_original = hp.anafast(m_original) m_rotated = gal2ecl.rotate_map_pixel(m_original) cl_from_m_rotated = hp.anafast(m_rotated) assert np.abs(cl_from_m_rotated - cl_from_m_original).sum() < 1e-2
def genmap(dir_maps='data/',nside=512, lmax=700): """ Reads in an input power spectrum and generates a set of I,Q,U maps using synfast """ # Read model spectra ellm, cltmp = readmodel(lfac=False,bbzero=False) cltmp = cltmp[:4] clm = cltmp.copy() #clm[1] = cltmp[3] # TE #clm[2] = cltmp[1] # EE #clm[3] = cltmp[2] # BB (syt,syq,syu) = hp.synfast(clm, nside, pixwin=True, lmax=lmax, new=True) hp.write_map(dir_maps+"map_input.fits", [syt,syq,syu]) return syt, syq, syu
def test_synfast(self): nside = 32 lmax = 64 fwhm_deg = 7.0 seed = 12345 np.random.seed(seed) map_pregen = hp.read_map( os.path.join(self.path, "data", "map_synfast_seed%d.fits" % seed), (0, 1, 2) ) sim_map = hp.synfast( self.cliqu, nside, lmax=lmax, pixwin=False, fwhm=np.radians(fwhm_deg), new=False, pol=True, ) np.testing.assert_array_almost_equal(sim_map, map_pregen, decimal=8)
def get_cmb_sim_hp(signal, nside_out): """Generate a healpix realization of the spectra. Parameters ---------- signal: dictionary dictionary containing spectra starting from ell = 0 with keys TT, EE, BB, TE. nside_out: int output map resolution """ cmb_sim = hp.synfast( cls=(signal["TT"], signal["EE"], signal["BB"], signal["TE"]), nside=nside_out, pixwin=True, verbose=False, new=True, ) return cmb_sim
def test_synfast(self): nside = 32 lmax = 64 fwhm_deg = 7.0 seed = 12345 np.random.seed(seed) map_pregen = hp.read_map( os.path.join(self.path, "data", "map_synfast_seed%d.fits" % seed), (0, 1, 2)) sim_map = hp.synfast( self.cliqu, nside, lmax=lmax, pixwin=False, fwhm=np.radians(fwhm_deg), new=False, pol=True, ) np.testing.assert_array_almost_equal(sim_map, map_pregen, decimal=8)
def get_cov(self, nside,lmax,N, cl): self.cov=nm.zeros((lmax+1, lmax+1)) cov=self.cov.copy() for i in range(N): ####### simulate maps map1=hp.synfast(cl, nside, lmax=nside, new=False, pixwin=False ) hp.write_map(self.path_maps+'sim_%d.fits'%i,map1) ##### spice the maps spice.spice(self.path_maps+'sim_%d.fits'%i, nlmax=lmax+400, clfile=self.path_cls+'cl_spice_sim_%d.dat'%i, maskfile=self.maskf, apodizesigma=self.apodizesigma, thetamax=self.thetamax) ######### Erase the map os.system('rm %s'%self.path_maps+'sim_%d.fits'%i) ## load the cls tab=nm.zeros((N, lmax+1)) for i in range(N): tab[i,:]=nm.loadtxt(self.path_cls+'cl_spice_sim_%d.dat'%i)[0:lmax+1,1] self.tab=tab ## compute the cov self.meancl=nm.mean(tab, axis=0) for i in range(N): cov+=nm.dot(nm.matrix(tab[i,:]).T, nm.matrix(tab[i,:])) cov/=N cov-=nm.dot(nm.matrix(self.meancl).T, nm.matrix(self.meancl)) ##cov cl, clp = E ( cl* clp) - E[cl] E[clp] self.cov=cov nm.savetxt(self.path_ker+'cov.dat', cov) l=nm.arange(lmax+1) cov_th=2./(2*l+1)/nm.mean(self.mask) *( cl[0:lmax+1]**2 ) nm.savetxt(self.path_ker+'cov_th.dat', cov_th)
def compute_cmb(self): """ Function to calculate a set of CMB Q and U maps from a given cosmology and instrument parameters. We generate a CMB realization from a run of CLASS for the given cosmology. This is then scaled to a the requested set of frequencies specified in the instrument configuration. """ try: assert not self.nmc % 2 except AssertionError: print('Odd number of MC simulations, can not compute pairs of ' 'cross-spectra. Exiting.') raise nus = np.array(self.instrument['nus']) nfreqs = len(nus) fwhm = self.instrument['beam_fwhm'] # claculate the power spectrum for this cosmology cmb_cls = self.compute_theory_cls() # set the random seed and iterate over the number of mc realizations # of the CMB requested. for now this will always be one, but may be # changed in the future. np.random.seed(None) # define array containing CMB. This is of shape (Nmc, Nfreq, Npol, Npix) # The same realization is placed at imc, and Nmc - imc - 1, as # we will use these maps to calculate cross spectra later. cmb = np.empty((self.nmc, nfreqs, 3, self.npix)) print("Generating CMB realizations") for imc in tqdm(range(int(self.nmc / 2))): print("Computing CMB map ", imc, " of ", self.nmc / 2) cmb_real = np.array( hp.synfast(cmb_cls, self.nside, new=True, pol=True, verbose=False, fwhm=np.pi / 180. * fwhm / 60.)) cmb_real_ukrj = kcmb2rj(nus)[..., None, None] * cmb_real cmb[imc, :, :, :] = cmb_real_ukrj cmb[self.nmc - imc - 1, :, :, :] = cmb_real_ukrj # scale the CMB map from the CMB units to uK_RJ. return cmb
def test_single(cl, mask, rseed, lmin=0, lmax=100, nside=256): print_message('Single test with my code') print_message('Mask coverage = %f' % (np.average(mask))) #mask = np.logical_not(hp.ud_grade(mask, nside_out = nside)) mask = hp.ud_grade(mask, nside_out=nside) print_message('Mask coverage for nside %d = %f' % (nside, np.average(mask))) np.random.seed(rseed) st = time.time() m = hp.synfast(cl, nside=nside, new=True) print_debug('Time for synfast:', time.time() - st) st = time.time() cl_ana = hp.anafast(m) print_debug('Time for get anafast:', time.time() - st) st = time.time() lmins = np.arange(lmin, lmax) lmaxs = lmins + 1 bins = xpol.Bins(lmins, lmaxs) xp = xpol.Xpol(mask, bins) print_debug('Time for initalizing x-pol:', time.time() - st) st = time.time() biased, unbiased_tmp = xp.get_spectra(m) print_debug('Time for get spectra from x-pol:', time.time() - st) unbiased = np.zeros(biased.shape) unbiased[:, 2:] = unbiased_tmp plt.figure() plt.loglog(np.abs(cl2dl(cl[:3].T))) plt.loglog(np.abs(cl2dl(cl_ana[:3].T)), '*-', lw=0.5) plt.loglog(np.abs(cl2dl(biased[:3].T)), 'x-', lw=0.5) plt.loglog(np.abs(cl2dl(unbiased[:3])).T, '+-', lw=0.5) plt.ylim(1e-6, 1e4) plt.xlabel = 'multipole moment ($l$)' plt.ylabel = '$D_l (K^2)' plt.title("x-pol") return biased, unbiased
def __init__(self, mask_file=None, f=None, maps=None, bl=None, fidcl=None): mask = ones(12 * 16**2) if mask_file is None else H.read_map(mask_file) self.fsky = mask.sum() / mask.size self.f = ones(30) if f is None else f qk = quickkern(200) imll = inv(qk.get_mll(H.anafast(mask, lmax=60))[:49, :49]) if maps is None: mp1 = mp2 = H.synfast(fidcl, 16, new=True, verbose=False) bl = ones(50) else: mp1, mp2 = (H.ud_grade(H.read_map(m), 16) * 1e6 for m in maps) self.clobs = dot(imll, H.anafast(mask * mp1, mask * mp2, lmax=48)) / bl[:49]**2 self.chi2s = [ stats.chi2((2 * l + 1) * self.fsky * self.f[l]) for l in range(30) ]
def getcov_est(dls, nside=4, pls=None, lmax=None, nsample=100, isDl=True): if (lmax==None): lmax = nside*3-1 if (isDl): cls = dl2cl(dls) else: cls = dls.copy() ell = np.arange(lmax+1) maparr = [] for i in range(nsample): np.random.seed(i) mapT = hp.synfast(cls, nside=nside, verbose=False) maparr.append(mapT) maparr = (np.array(maparr)).T cov = np.cov(maparr) return cov
def sim_whitenoise(nside, lmax, w, theta, ellkneeT=0, alphaT=0, ellkneeP=0, alphaP=0, lmin=2): nl, pTl, pPl, bl = whitenoise(lmax, w, theta, ellkneeT, alphaT, ellkneeP, alphaP, lmin=lmin) return hp.synfast( (nl / 2. * pTl * bl, nl * pPl * bl, nl * pPl * bl, np.zeros(lmax + 1)), nside, lmax=lmax, new=True)[:3]
def mk_map_SCK(nside,nu,A,beta,alpha,lmin=0,l_f=1000,nu_f=130,smooth=1.): """ Santos Cooray and Knox (2005) diffuse foreground model -> random field realization #Fiducial parameters at l_f=1000, nu_f=130 MHz # # Src (i) A (mK^2) beta \bar{alpha} xi # #Extragalactic point sources 57 1.1 2.07 1.0 #Extragalactic bremstrahlung 0.014 1.0 2.10 35 #Galactic synchrotron 700 2.4 2.80 4.0 #Galactic bremstrahlung 0.088 3.0 2.15 35 """ lmax = 3*nside - 1 ll = np.array(range(lmin,lmax,1)) Cl = A*np.power(float(l_f)/ll,beta)*np.power(nu_f/nu,2*alpha) Cl[0] = 0. #zero mean Q = hp.synfast(Cl,nside,lmax=lmax) Qsm = hp.smoothing(Q,fwhm=np.radians(smooth)) return Qsm
def random_map(seed,mask,fname_cl,fname_out='none',use_wiener=False) : """Generates gaussian random field with correct power spectrum mask : healpix map containing a binary mask fname_cl : path to ascii file containing the field's power spectrum fname_out : path to output FITS file """ np.random.seed(seed) nside=hp.npix2nside(len(mask)) ll,nll,cll=np.loadtxt(fname_cl,unpack=True) cl=np.zeros(int(ll[-1]+1)); cl[int(ll[0]):]=cll nl=np.zeros(int(ll[-1]+1)); nl[int(ll[0]):]=nll if use_wiener : alm=hp.synalm(cl,lmax=2048,new=True,verbose=False) alm=hp.almxfl(alm,(cl-nl)/np.maximum(cl,np.ones_like(cl)*1E-10)) mp=hp.alm2map(alm,nside,lmax=2048) else : mp=hp.synfast(cl,nside,lmax=2048,new=True,verbose=False) mp*=mask if fname_out!='none' : hp.write_map(fname_out,mp) return mp
def test_map2alm_lsq(self): nside = 32 lmax = 64 fwhm_deg = 7.0 seed = 12345 np.random.seed(seed) orig = hp.synfast( self.cla, nside, lmax=lmax, pixwin=False, fwhm=np.radians(fwhm_deg), new=False, ) tmp = np.empty(orig.size * 2) tmp[::2] = orig maps = [orig, orig.astype(np.float32), tmp[::2]] for input in maps: alm, l2, it = hp.map2alm_lsq(input, tol=1e-4, lmax=lmax, mmax=lmax) np.testing.assert_equal(l2 < 1e-3, True) np.testing.assert_equal(it < 15, True) output = hp.alm2map(alm, nside) np.testing.assert_allclose(input, output, atol=1e-4)
def get_cmb(Nside, px_unseen): pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.74, ombh2=0.0223, omch2=0.1188, omk=0, tau=0.066) # Tensors on pars.WantTensors = True pars.InitPower.set_params(ns=0.9667, r=0.5) # Power spectrum will be 3 * Nside - 1 pars.set_for_lmax(3 * Nside - 1, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra cl_total = results.get_total_cls(3 * Nside - 1) # Needs to be re-arranged tmp = [cl_total[:, 0], cl_total[:, 1], cl_total[:, 2], cl_total[:, 3]] # This comes out as a list cmb_synfast = hp.synfast(tmp, Nside, new=True) # Rather cast it into a numpy array and change K->micro K! cmb_out = np.zeros((3, len(px_unseen)), dtype='float32') cmb_out[0] = cmb_synfast[0][px_unseen] * 1e6 cmb_out[1] = cmb_synfast[1][px_unseen] * 1e6 cmb_out[2] = cmb_synfast[2][px_unseen] * 1e6 return cmb_out
def get_beta_map(nside, beta0, amp, gamma, l0=80, l_cutoff=2, seed=None): """ Returns realization of the spectral index map. Args: nside: HEALPix resolution parameter. beta0: mean spectral index. amp: amplitude gamma: tilt l0: pivot scale (default: 80) l_cutoff: ell below which the power spectrum will be zero. (default: 2). seed: seed (if None, a random seed will be used). Returns: Spectral index map """ if seed is not None: np.random.seed(seed) ls = np.arange(3*nside) cls = get_delta_beta_cl(amp, gamma, ls, l0, l_cutoff) mp = hp.synfast(cls, nside, verbose=False) mp += beta0 return mp
def test_rotate_ellipticities(): nside = 128 npix = hp.nside2npix(nside) ls = np.arange(3 * nside) cl = 1 / (ls + 10)**2 cl0 = np.zeros(3 * nside) _, q, u = hp.synfast([cl0, cl, cl0, cl0], nside, new=True) r = hp.Rotator(coord=['G', 'C']) qr, ur = r.rotate_map_pixel([q, u]) ra, dec = hp.pix2ang(nside, np.arange(npix), lonlat=True) # The catalog now has positions and ellipticities # in Equatorial coordinates. cat = {'RA': ra, 'DEC': dec, 'e1': qr, 'e2': ur} r = hp.Rotator(coord=['C', 'G']) # Rotate back to Galactic coordinates and compare with # input maps. Differences are expected due to different # interpolation types used by healpy's `rotate_map_pixel` # and averaging by galaxy positions, so we compare against # half of a standard deviation of the input maps (even though # maps actually agree at the ~0.25*sigma level). ns = 32 n1 = xc.mappers.get_map_from_points(cat, ns, rot=r) q1, u1 = xc.mappers.get_map_from_points(cat, ns, rot=r, qu=[cat['e1'], cat['e2']]) q1 /= n1 u1 /= n1 q = hp.ud_grade(q, nside_out=ns) u = hp.ud_grade(u, nside_out=ns) std_comp = np.sqrt(np.mean(q**2 + u**2)) assert np.all(np.fabs(q - q1) < 0.5 * std_comp) assert np.all(np.fabs(u - u1) < 0.5 * std_comp)
np.log10(hpm_cl[fitrange[0]:fitrange[1]]), 1)) except TypeError: print 'fitrange should be a tuple of two integer for min and max of \ fitting range' # Put together our extrapolated power spectrum original_cl = np.concatenate((hpm_cl, np.zeros(3 * nside_out - 1 - hpm_cl.size))) extrap_cl = np.copy(original_cl) extrap_cl[fitrange[1]:] =\ 10 ** (hpm_cl_fit(np.log10(np.arange(fitrange[1], extrap_cl.size)))) diff_cl = -1 * (original_cl - extrap_cl) diff_cl[diff_cl < 0] = 0. # else we have a problem # Convert the different in power spectrum to a Gaussian random field hpm_diff = hp.synfast(diff_cl, nside_out) # Interpolate the input hpm map to higher NSide # healpy has a ud_grade function to interpolate map to higher side, but it # does not allow clipping of power spectrum before interpolation of the map. hpm_original = hp.alm2map(hpm_alm, nside_out) # Our diffuse model is the original hpm map interpolated to higher nside, # plus the small structure map. hpm_extrap = hpm_original + hpm_diff hp.write_map(hpm_out_name, hpm_extrap, dtype=np.float64, fits_IDL=False, coord='G') hpm_extrap_cl = hp.anafast(hpm_extrap) plt.loglog(np.arange(hpm_extrap_cl.size), hpm_extrap_cl, 'y', label='Model') plt.loglog(np.arange(hpm_cl.size), hpm_cl, 'b--', lable='Haslam') plt.loglog(np.arange(diff_cl.size), diff_cl, 'r-.', lable='Small Structure')
if True: randmap=hp.read_map("../recon_semifake/rand_map.fits.gz",0) hp.write_map("rand_map.fits.gz",randmap) maskedrandmap=mask*(randmap+np.random.normal(scale=0.02,size=len(randmap))) hp.write_map("maskedrand_map.fits.gz",maskedrandmap) if True: #load galaxy map randmap=hp.read_map("2MPZ.gz_0.01_0.1_smoothed.fits.gz",0) randmap=hp.pixelfunc.ud_grade(randmap,nside_out = nside, order_in = 'RING', order_out = 'RING') meanrandmap=np.mean(randmap) # mask[randmap<1e-2*meanrandmap]=0 maskedrandmap=mask*randmap # GENERATE RANDOM MAP WITH THE SAME CLS AS LOADED if False: cl = hp.anafast(randmap) randmap=hp.synfast(cl,nside) maskedrandmap=mask*randmap hp.write_map("rand_map.fits.gz",randmap) hp.write_map("maskedrand_map.fits.gz",maskedrandmap) hp.mollview(mask,coord='C',rot = [0,0.3], title='Mask', unit='prob', xsize=1024) hp.graticule() plt.savefig("Mask.png") # plt.show() plt.close() hp.mollview(randmap,coord='C',rot = [0,0.3], title='Random Map', unit='prob', xsize=1024, norm='hist') hp.graticule() plt.savefig("RandMap.png")
import healpy as hp import numpy as np # Add SPT noise, chi-by-eye to Henning SPT-500deg^2 paper N_l and # functional form in http://users.physics.harvard.edu/~buza/20161220_chkS4/ #sigmap = 9.0 # uK-arcmin, SPT sigmap = 1.2 # uK-arcmin, CMB-S4 lknee = 250. lexp = -1.8 l = np.arange(8000) * 1.0 Nl = 4 * np.pi / (41253. * 60**2) * (1 + (l / lknee)**(lexp)) * sigmap**2 Nl[0] = 0 # Get noise realization Nside = 1024 for rlz in range(26, 100): print(rlz) hmapTn = hp.synfast(Nl, Nside, new=True, verbose=False) hmapQn = hp.synfast(Nl, Nside, new=True, verbose=False) hmapUn = hp.synfast(Nl, Nside, new=True, verbose=False) hp.write_map('input_maps/S4_noise_map_r{:04d}.fits'.format(rlz), [hmapTn, hmapQn, hmapUn])
covlim=0.1 ####### Create some sampling center = equ2gal(racenter, deccenter) sampling = create_sweeping_pointings( [racenter, deccenter], duration, ts, angspeed, delta_az, nsweeps_el, angspeed_psi, maxpsi) ####### Full instrument instrument = QubicInstrument(filter_nu=150e9, detector_nep=4.7e-17*np.sqrt(len(sampling) * sampling.period / (365 * 86400))) ### Input map nside_in=64 mapi,mapq,mapu=hp.synfast(spectra[1:],nside_in,new=True) input_maps=np.array([mapi,mapq,mapu]).T ### Prepare input / output data nside = 64 scene = QubicScene(nside, kind='IQU') from Quad import mapmake_jc_lib as mm reload(mm) tod_signal, tod_noise, tod = mm.get_tod(instrument, sampling, scene, input_maps, withplanck = False, photon_noise=False)
def setUp(self): fixture_name = os.path.splitext(os.path.basename(__file__))[0] self.outdir = create_outdir(self.comm, fixture_name) self.rank = 0 if self.comm is not None: self.rank = self.comm.rank if self.rank == 0: for fname in glob.glob("{}/*".format(self.outdir)): try: os.remove(fname) except OSError: pass if self.comm is not None: self.comm.barrier() self.nobs = 1 self.data = create_distdata(self.comm, obs_per_group=self.nobs) self.ndet = self.data.comm.group_size self.sigma = 1 self.rate = 50.0 self.net = self.sigma / np.sqrt(self.rate) self.alpha = 2 self.fknee = 1e0 # Create detectors ( dnames, dquat, depsilon, drate, dnet, dfmin, dfknee, dalpha, ) = boresight_focalplane( self.ndet, samplerate=self.rate, fknee=self.fknee, alpha=self.alpha, net=self.net, ) # Pixelization self.sim_nside = 32 self.map_nside = 32 self.pointingmode = "IQU" self.nnz = 3 # Samples per observation self.npix = 12 * self.sim_nside**2 self.ninterval = 4 self.totsamp = self.ninterval * self.npix # Define intervals intervals = [] interval_length = self.npix istart = 0 while istart < self.totsamp: istop = istart + interval_length intervals.append( Interval( start=istart / self.rate, stop=istop / self.rate, first=istart, last=istop - 1, )) istart = istop # Construct an uncorrelated analytic noise model for the detectors noise = AnalyticNoise( rate=drate, fmin=dfmin, detectors=dnames, fknee=dfknee, alpha=dalpha, NET=dnet, ) # Populate the observations for iobs in range(self.nobs): if iobs % 3 == 1: rot = qa.from_angles(np.pi / 2, 0, 0) if iobs % 3 == 2: rot = qa.from_angles(np.pi / 2, np.pi / 2, 0) else: rot = None tod = TODHpixSpiral( self.data.comm.comm_group, dquat, self.totsamp, detranks=self.data.comm.group_size, rate=self.rate, nside=self.sim_nside, rot=rot, ) self.data.obs[iobs]["tod"] = tod self.data.obs[iobs]["noise"] = noise self.data.obs[iobs]["intervals"] = intervals # Write processing masks self.npix = 12 * self.map_nside**2 pix = np.arange(self.npix) pix = hp.reorder(pix, r2n=True) self.binary_mask = np.logical_or(pix < self.npix * 0.45, pix > self.npix * 0.55) self.maskfile_binary = os.path.join(self.outdir, "binary_mask.fits") hp.write_map( self.maskfile_binary, self.binary_mask, dtype=np.float32, overwrite=True, nest=True, ) self.smooth_mask = (np.abs(pix - self.npix / 2) / (self.npix / 2))**0.5 self.maskfile_smooth = os.path.join(self.outdir, "apodized_mask.fits") hp.write_map( self.maskfile_smooth, self.smooth_mask, dtype=np.float32, overwrite=True, nest=True, ) # Synthesize an input map self.lmax = 2 * self.sim_nside self.cl = np.ones([4, self.lmax + 1]) self.cl[:, 0:2] = 0 fwhm = np.radians(10) self.inmap = hp.synfast( self.cl, self.sim_nside, lmax=self.lmax, mmax=self.lmax, pol=True, pixwin=True, fwhm=np.radians(30), verbose=False, ) self.inmap = hp.reorder(self.inmap, r2n=True) self.inmapfile = os.path.join(self.outdir, "input_map.fits") hp.write_map(self.inmapfile, self.inmap, overwrite=True, nest=True) return
colombi1_cmap.set_bad("gray") # color of missing pixels colombi1_cmap.set_under( "white") # color of background, necessary if you want to use # this colormap directly with hp.mollview(m, cmap=colombi1_cmap) cmap_planck = colombi1_cmap import matplotlib matplotlib.use("Agg") Nside = 512 cl = hp.fitsfunc.read_cl("cl.fits") gauss_map = hp.synfast(cl[0], 512, lmax=3 * Nside - 1, fwhm=0, pixwin=True, verbose=False, pol=False) dpi = 800 fig_size_inch = 6, 4.5 fig = plt.figure(1, figsize=(6, 4.5)) hp.mollview(gauss_map, fig=fig.number, xsize=2000, nest=False, title='', cmap=cmap_planck, cbar=False)
import healpy as hp nside=256 ell=np.arange(3*nside) sig=0.1 l_low=5 l_mid=20 l_high=60 cl_low=exp(-(ell-l_low)**2/(2*sig**2)) cl_mid=exp(-(ell-l_mid)**2/(2*sig**2)) cl_high=exp(-(ell-l_high)**2/(2*sig**2)) map_low=hp.synfast(cl_low,nside) map_mid=hp.synfast(cl_mid,nside) map_high=hp.synfast(cl_high,nside) hp.mollview(map_low) hp.mollview(map_mid) hp.mollview(map_high) figure() plot(ell,cl_low,lw=3) xlim(0,100) ylim(0,1.2) xlabel('$\ell$',fontsize=20) ylabel('$C_\ell$',fontsize=20) figure() plot(ell,cl_mid,lw=3) xlim(0,100) ylim(0,1.2)
jds = np.load(opts.jd)['jd'] jds = jds[40:50] # range of frequencies frequency = np.linspace(opts.start, opts.stop, opts.chan) np.savez(open('frequency.npz', 'wb'), frequency=frequency) if opts.healpix: ra, dec = convHealCoord(opts.nside) if opts.pol: if opts.pmap == None: # generates realizations of polarized map from angular power spectrum random.seed(1001) ll = np.arange(1, 2700) A_700 = 0.64 #0.05 # RM normalization factor P_cl = A_700 * ((ll / 700.)**(-1.65)) # angular power spectrum _dat = hp.synfast(P_cl, nside) # generating healpix realization polmap = _dat.copy() pfrac = None RM = None else: polmap = hp.read_map(opts.pmap) polmap = hp.ud_grade(polmap, opts.nside) else: data = hp.read_map(args[0]) data = hp.ud_grade(data, opts.nside) pfrac = 0.1 RM = 12 alpha = -2.6 polmap = None else: data, ra, dec, alpha, RM, pfrac = loadCat(args[0])
xlim(0,600) ylim(0.01,100) xlabel('$\ell$') ylabel('$\sqrt{\ell(\ell+1)C_\ell/(2\pi)}$'+' '+'$[\mu K]$ ') legend(loc='lower right',frameon=False) nside=64 lmax=3*nside ell=spectra[0] maskl=ell<(lmax+1) bl=np.ones(maskl.size) fwhmrad=0.5*np.pi/180 mapi,mapq,mapu=hp.synfast(spectra[1:],nside,fwhm=fwhmrad,pixwin=True,new=True) hp.mollview(mapi) hp.mollview(mapq) hp.mollview(mapu) maps=[mapi,mapq,mapu] ############## T only #ds_dcb=0 #nbpixok=1000 #mask=(np.arange(12*nside**2) >= nbpixok) #maskok=~mask #ip=np.arange(12*nside**2) #ipok=ip[~mask] #mapi,mapq,mapu=hp.synfast(spectra[1:],nside,fwhm=fwhmrad,pixwin=True,new=True)
from astropy.io import fits import matplotlib.pyplot as plt import cltools as ct import matplotlib.cm as cm import claw from numpy import * n=500000 Nside=64 Npix=12*Nside**2 Cls_in_bin=12 lmax=(3*Nside)-1 #mapa=ct.randmap(n,Nside) clth=np.loadtxt('theory_fit.txt') mapa=hp.synfast(clth[:,1][0:lmax+1],Nside) plt.figure() plt.plot(clth[:,0],clth[:,1], label=' Power spectrum icecube\n podatkov brez shot noise') plt.xlim(0,lmax) plt.xlabel('$\ell$') plt.ylabel('$C_\ell$') plt.show() print ("max,min,mean=",mapa.max(), mapa.min(),mapa.mean()) b=cm.BuPu # b.set_under("w") # hp.mollview(mapa, cmap=b, title='Celotno nebo', cbar=True, xsize=1400, unit='Gostota objektov [objektov/piksel]') #izris mape ct.mollaxes() hp.graticule(coord=('E')) #
cosmo_params = { l[0]: l[1] for l in zip(COSMO_PARAMS_NAMES, COSMO_PARAMS_MEANS) } params = { 'output': OUTPUT_CLASS, 'l_max_scalars': L_MAX_SCALARS, 'lensing': LENSING } params.update(cosmo_params) cosmo.set(params) cosmo.compute() cls = cosmo.lensed_cl(L_MAX_SCALARS) eb_tb = np.zeros(shape=cls["tt"].shape) I, Q, U = hp.synfast( (cls['tt'], cls['ee'], cls['bb'], cls['te'], eb_tb, eb_tb), nside=NSIDE, new=True) print(cls["tt"].shape) end_generation = time.clock() - start cosmo.struct_cleanup() cosmo.empty() start = time.clock() res = sphtfunc.map2alm((I, Q, U), lmax=L_MAX_SCALARS) #res = sphtfunc.map2alm(U) print(res.shape) print(cls["tt"].shape) end_back = time.clock() - start s = 0 for l in range(0, 20):
def setUp(self): self.lmax = 64 self.path = os.path.dirname(os.path.realpath(__file__)) self.map1 = [ hp.ma(m) for m in hp.read_map( os.path.join(self.path, "data", "wmap_band_iqumap_r9_7yr_W_v4_udgraded32.fits"), (0, 1, 2), ) ] self.map2 = [ hp.ma(m) for m in hp.read_map( os.path.join(self.path, "data", "wmap_band_iqumap_r9_7yr_V_v4_udgraded32.fits"), (0, 1, 2), ) ] self.mask = hp.read_map( os.path.join( self.path, "data", "wmap_temperature_analysis_mask_r9_7yr_v4_udgraded32.fits", )).astype(np.bool) for m in chain(self.map1, self.map2): m.mask = np.logical_not(self.mask) self.cla = hp.read_cl( os.path.join( self.path, "data", "cl_wmap_band_iqumap_r9_7yr_W_v4_udgraded32_II_lmax64_rmmono_3iter.fits", )) self.cl_fortran_nomask = hp.read_cl( os.path.join( self.path, "data", "cl_wmap_band_iqumap_r9_7yr_W_v4_udgraded32_II_lmax64_rmmono_3iter_nomask.fits", )) cls_file = pf.open( os.path.join( self.path, "data", "cl_wmap_band_iqumap_r9_7yr_W_v4_udgraded32_IQU_lmax64_rmmono_3iter.fits", )) # fix for pyfits to read the file with duplicate column names for i in range(2, 6): cls_file[1].header["TTYPE%d" % i] += "-%d" % i cls = cls_file[1].data # order of HEALPIX is TB, EB while in healpy is EB, TB self.cliqu = [np.array(cls.field(i)) for i in (0, 1, 2, 3, 5, 4)] nside = 32 lmax = 64 fwhm_deg = 7. seed = 12345 np.random.seed(seed) self.mapiqu = hp.synfast( self.cliqu, nside, lmax=lmax, pixwin=False, fwhm=np.radians(fwhm_deg), new=False, )
veccenter = hp.ang2vec(pi / 2 - np.radians(center[1]), np.radians(center[0])) vecpix = hp.pix2vec(nside, np.arange(12 * nside ** 2)) cosang = np.dot(veccenter, vecpix) maskok = np.degrees(np.arccos(cosang)) < maxang msk_apo = nmt.mask_apodization(maskok, 1, apotype='C1') # Select a binning scheme b = nmt.NmtBin(nside, nlb=20, is_Dell=True) leff = b.get_effective_ells() # gaussian beam beam = hp.gauss_beam(np.radians(0.39), lmax) # initial maps and workspaces mp_t, mp_q, mp_u = hp.synfast(spectra, nside=nside, fwhm=np.radians(0.39), pixwin=True, new=True, verbose=False) f0 = nmt.NmtField(msk_apo, [mp_t], beam=beam) f2 = nmt.NmtField(msk_apo, [mp_q, mp_u], beam=beam) # We initialize two workspaces for the fields: w = nmt.NmtWorkspace() w.compute_coupling_matrix(f0, f0, b) w2 = nmt.NmtWorkspace() w2.compute_coupling_matrix(f0, f2, b) # We now iterate over several simulations, # computing the power spectrum for each of them data_00 = []
import matplotlib.pyplot as plt import pymaster as nmt import healpy as hp np.random.seed(1234) l, cltt, clee, clbb, clte, nltt, nlee, nlbb, nlte = np.loadtxt("cls_lss.txt", unpack=True) cltt[:2] = 0 fl = np.sqrt(l * (l + 1)) fl[0] = 1 fl = 1. / fl nside_out = 64 # Generate spin-0 field m = hp.synfast(cltt[:3 * nside_out], nside_out, new=True, verbose=False) # Compute alm / sqrt(l*(l+1)) and then its derivatives a = hp.map2alm(m) a = hp.almxfl(a, fl) _, mdth, mdph = hp.alm2map_der1(a, nside_out) hp.write_map("mps_sp1.fits", [m, mdth, mdph], overwrite=True) msk = np.ones_like(m) f0 = nmt.NmtField(msk, [m], spin=0, n_iter=0) f1 = nmt.NmtField(msk, [mdth, mdph], spin=1, n_iter=0) cl00 = nmt.compute_coupled_cell(f0, f0) cl01 = nmt.compute_coupled_cell(f0, f1) cl11 = nmt.compute_coupled_cell(f1, f1) msk = hp.read_map("msk.fits", verbose=False)
# Use random pointings for the test p = create_random_pointings(center, 1000, 10) # Polychromatic instrument model q = QubicMultibandInstrument(filter_nus=nus * 1e9, filter_relative_bandwidths=nus / deltas, ripples=True) # The peaks of the synthesized beam are modeled with "ripples" s = QubicScene(nside=nside, kind='IQU') # Polychromatic acquisition model a = QubicPolyAcquisition(q, p, s, effective_duration=2) sp = read_spectra(0) x0 = np.array(hp.synfast(sp, nside, new=True, pixwin=True)).T TOD, maps_convolved = a.get_observation(x0) maps_recon = a.tod2map(TOD, tol=tol) cov = a.get_coverage().sum(axis=0) mp.figure(1) _max = [300, 5, 5] for i, (inp, rec, iqu) in enumerate(zip(maps_convolved.T, maps_recon.T, 'IQU')): inp[cov < cov.max() * 0.01] = hp.UNSEEN rec[cov < cov.max() * 0.01] = hp.UNSEEN diff = inp - rec diff[cov < cov.max() * 0.01] = hp.UNSEEN hp.gnomview(inp, rot=center_gal, reso=5, xsize=700, fig=1, sub=(3, 3, i + 1), min=-_max[i], max=_max[i], title='Input, {}'.format(iqu))
def setUp(self): fixture_name = os.path.splitext(os.path.basename(__file__))[0] self.outdir = create_outdir(self.comm, fixture_name) self.rank = 0 self.ntask = 1 if self.comm is not None: self.rank = self.comm.rank self.ntask = self.comm.size if self.rank == 0: for fname in glob.glob("{}/*".format(self.outdir)): try: os.remove(fname) except OSError: pass if self.comm is not None: self.comm.barrier() self.nobs = 1 self.data = create_distdata(self.comm, obs_per_group=self.nobs) self.ndet = 4 * self.ntask self.sigma = 1 self.rate = 50.0 self.net = self.sigma / np.sqrt(self.rate) self.alpha = 2 self.fknee = 1e0 # Create detectors ( dnames, dquat, depsilon, drate, dnet, dfmin, dfknee, dalpha, ) = boresight_focalplane( self.ndet, samplerate=self.rate, fknee=self.fknee, alpha=self.alpha, net=self.net, pairs=True, ) # Pixelization self.sim_nside = 8 self.map_nside = 8 self.nnz = 3 self.pointingmode = "IQU"[:self.nnz] # Samples per observation self.npix = 12 * self.sim_nside**2 self.ninterval = 4 self.totsamp = self.ninterval * self.npix # Define intervals with gaps in between intervals = [] interval_length = self.npix istart = 0 while istart < self.totsamp: istop = istart + interval_length interval_start = istart + 100 interval_stop = istop - 100 intervals.append( Interval( start=interval_start / self.rate, stop=interval_stop / self.rate, first=interval_start, last=interval_stop - 1, )) istart = istop # Construct an uncorrelated analytic noise model for the detectors noise = AnalyticNoise( rate=drate, fmin=dfmin, detectors=dnames, fknee=dfknee, alpha=dalpha, NET=dnet, ) # Populate the observations for iobs in range(self.nobs): if iobs % 3 == 1: rot = qa.from_angles(np.pi / 2, 0, 0) elif iobs % 3 == 2: rot = qa.from_angles(np.pi / 2, np.pi / 2, 0) else: rot = None tod = TODHpixSpiral( self.data.comm.comm_group, dquat, self.totsamp, detranks=self.data.comm.group_size, rate=self.rate, nside=self.sim_nside, rot=rot, ) self.data.obs[iobs]["tod"] = tod self.data.obs[iobs]["noise"] = noise self.data.obs[iobs]["intervals"] = intervals cflags = tod.local_common_flags() cflags[:] = 255 for ival in tod.local_intervals(intervals): cflags[ival.first:ival.last + 1] = 0 tod._az = (tod.local_times() % 100) / 100 tod.scan_range = [0, 1, 0, 0] def read_boresight_az(self, local_start=0, n=None): if n is None: n = self.local_samples[1] - local_start ind = slice(local_start, local_start + n) return tod._az[ind] tod.read_boresight_az = read_boresight_az.__get__(tod) self.npix = 12 * self.map_nside**2 pix = np.arange(self.npix) pix = hp.reorder(pix, r2n=True) # Synthesize an input map self.lmax = 2 * self.sim_nside self.cl = np.ones([4, self.lmax + 1]) self.cl[:, 0:2] = 0 #self.cl[1:] = 0 # DEBUG fwhm = np.radians(10) self.inmap = hp.synfast( self.cl, self.sim_nside, lmax=self.lmax, mmax=self.lmax, pol=True, pixwin=True, fwhm=np.radians(30), verbose=False, ) self.inmap = hp.reorder(self.inmap, r2n=True) self.inmapfile = os.path.join(self.outdir, "input_map.fits") hp.write_map(self.inmapfile, self.inmap, overwrite=True, nest=True) return
cl_file='/home/matt/wmap/simul_scalCls.fits' radio_file='/data/wmap/faraday_MW_realdata.fits' fwhm=[27.3,11.7] bands=[43.1,94.5] wl=np.array([299792458./(b*1e9) for b in bands]) fwhm_gen=[0,5,11.7,27.3,45,60] nside=512 npix=hp.nside2npix(512) cl_gen=hp.read_cl(cl_file) alpha=hp.read_map(radio_file,hdu='maps/phi') alpha=hp.ud_grade(alpha,nside) const=2*(wl[0]**2-wl[1]**2) plt.figure() for f in fwhm_gen: simul_cmb=hp.synfast(cl_gen,nside,pol=1,new=1,fwhm=f*np.pi/(180.*60)) rot_1=rotate_tqu(simul_cmb,wl[0],alpha) rot_2=rotate_tqu(simul_cmb,wl[1],alpha) Delta_Q=(rot_1[1]-rot_2[1])/const alpha_U=alpha*rot_1[2] dQ=hp.ma(Delta_Q) aU=hp.ma(alpha_U) dQ=hp.smoothing(dQ,fwhm=np.pi/180.) aU=hp.smoothing(dQ,fwhm=np.pi/180.) cls=hp.anafast(dQ,map2=aU) l=np.arange(len(cls)) ll=np.array([i*(i+1)/(2*np.pi) for i in l]) plt.plot(l[:384],ll[:384]*cls[:384]*1e12,'.',label='fwhm= {:2d}'.format(int(f))) plt.legend(loc='upper right',numpoints=1) plt.show()
import numpy as np import healpy as hp total = np.load('../simulation/Nside = 1024/noise_map.npy') for n in range(20): #white_noise = np.load('/home/yao/Desktop/EM-smica/ABS/simulation/Nside = 1024/noise_map_NO%s.npy'%(n+2)) white_noise = np.ones_like(total) cl_noise = np.ones((Nf, L + 1)) Nl = (0.066**2, 0.065**2, 0.063**2, 0.028**2, 0.015**2, 0.023**2, 0.068**2) Nl_real = [] Nl_matrix = [] for i in range(Nf): cl_noise[i] = Nl[i] wn_j = hp.synfast(cl_noise[i], nside) for j in range(3): white_noise[i, j, :] = wn_j noise_power_spectrum = hp.anafast(white_noise[i][0], lmax=L, gal_cut=0) Nl_real.append(noise_power_spectrum) Nl_real = np.array(Nl_real) Nl_T = Nl_real.T for i in range(L): Nl_matrix.append(np.diag((Nl_T[i]))) #np.save('/home/yao/Desktop/EM-smica/ABS/simulation/noise/noise_map_NO%s.npy'%(n+2), white_noise) np.save( '/home/yao/Desktop/EM-smica/ABS/simulation/Nside = 1024/noise_power_spectrum_NO%s.npy' % (n + 1), Nl_matrix) ##white_noise_masked = Mask(white_noise) #total_masked_power_spectrum = power_spectrum(total + white_noise,0) ##total_power_spectrum = power_spectrum(total + white_noise)
hp.mollview(galP) hp.mollview(mask) plt.show() # load results of cleaning config_dict = pebbles.configurations.run[ARGS.name] res = pebbles.Residuals(**config_dict) if ARGS.cmb_gen: cl_config = pebbles.configurations.cos['planck2015']['params'] class_cls = pebbles.pebbles.class_spectrum(cl_config) for i in range(nmc): synth_cmb = np.array( hp.synfast(class_cls, res.nside, pol=True, new=True, verbose=False, fwhm=np.pi / 180. * fwhm / 60.)) fname = "maps/nongaussian/cmb{:04d}_fwhm{:02d}amin.fits".format( i, int(fwhm)) hp.write_map(fname, synth_cmb, overwrite=True) if ARGS.cmb_mc_stats: synth_map = np.empty((nmc, 2 * hp.nside2npix(res.nside))) for i in range(nmc): fname = "maps/nongaussian/cmb{:04d}_fwhm{:02d}amin.fits".format( i, int(fwhm)) synth_map[i] = hp.read_map(fname, verbose=False, field=(1, 2)).ravel() skews = skew(synth_map, axis=1)
def simulate( self, tube, output_units="uK_CMB", seed=None, nsplits=1, mask_value=None, atmosphere=True, hitmap=None, white_noise_rms=None, ): """Create a random realization of the noise power spectrum Parameters ---------- tube : str Specify a tube (for SO: ST0-ST3, LT0-LT6) see the `tubes` attribute output_units : str Output unit supported by PySM.units, e.g. uK_CMB or K_RJ seed : integer or tuple of integers, optional Specify a seed. The seed is converted to a tuple if not already one and appended to (0,0,6,tube_id) to avoid collisions between tubes, with the signal sims and with ACT noise sims, where tube_id is the integer ID of the tube. nsplits : integer, optional Number of splits to generate. The splits will have independent noise realizations, with noise power scaled by a factor of nsplits, i.e. atmospheric noise is assumed to average down with observing time the same way the white noise does. By default, only one split (the coadd) is generated. mask_value : float, optional The value to set in masked (unobserved) regions. By default, it uses the value in default_mask_value, which for healpix is healpy.UNSEEN and for CAR is numpy.nan. atmosphere : bool, optional Whether to include the correlated 1/f from the noise model. This is True by default. If it is set to False, then a pure white noise map is generated from the white noise power in the noise model, and the covariance between arrays is ignored. hitmap : string or map, optional Provide the path to a hitmap to override the default used for the tube. You could also provide the hitmap as an array directly. white_noise_rms : float or tuple of floats, optional Optionally scale the simulation so that the small-scale limit white noise level is white_noise_rms in uK-arcmin (either a single number or a pair for the dichroic array). Returns ------- output_map : ndarray or ndmap Numpy array with the HEALPix or CAR map realization of noise. The shape of the returned array is (2,3,nsplits,)+oshape, where oshape is (npix,) for HEALPix and (Ny,Nx) for CAR. The first dimension of size 2 corresponds to the two different bands within a dichroic tube. See the `band_id` attribute of the Channel class to identify which is the index of a Channel in the array. The second dimension corresponds to independent split realizations of the noise, e.g. it is 1 for full mission. The third dimension corresponds to the three polarization Stokes components I,Q,U The last dimension is the number of pixels """ assert nsplits >= 1 if mask_value is None: mask_value = (default_mask_value["healpix"] if self.healpix else default_mask_value["car"]) # This seed tuple prevents collisions with the signal sims # but we should eventually switch to centralized seed # tracking. if seed is not None: try: iter(seed) except: seed = (seed, ) tube_id = self.tubes[tube][0].tube_id seed = (0, 0, 6, tube_id) + seed np.random.seed(seed) # In the third row we return the correlation coefficient P12/sqrt(P11*P22) # since that can be used straightforwardly when the auto-correlations are re-scaled. ell, ps_T, ps_P, fsky, wnoise_power, weightsMap = self.get_noise_properties( tube, nsplits=nsplits, hitmap=hitmap, white_noise_rms=white_noise_rms, atmosphere=atmosphere, ) if not (atmosphere): if self.apply_beam_correction: raise NotImplementedError( "Beam correction is not currently implemented for pure-white-noise sims." ) # If no atmosphere is requested, we use a simpler/faster method # that generates white noise in real-space. if self.healpix: ashape = (hp.nside2npix(self.nside), ) sel = np.s_[:, None, None, None] pmap = self.pixarea_map else: ashape = self.shape[-2:] sel = np.s_[:, None, None, None, None] pmap = pixell.enmap.enmap(self.pixarea_map, self.wcs) spowr = np.sqrt(wnoise_power[sel] / pmap) output_map = spowr * np.random.standard_normal( (self.channel_per_tube, nsplits, 3) + ashape) output_map[:, :, 1:, :] = output_map[:, :, 1:, :] * np.sqrt(2.0) else: if self.healpix: npix = hp.nside2npix(self.nside) output_map = np.zeros( (self.channel_per_tube, nsplits, 3, npix)) for i in range(nsplits): for i_pol in range(3): output_map[:, i, i_pol] = np.array( hp.synfast( ps_T if i_pol == 0 else ps_P, nside=self.nside, pol=False, new=True, verbose=False, )) else: output_map = pixell.enmap.zeros((2, nsplits, 3) + self.shape, self.wcs) ps_T = pixell.powspec.sym_expand(np.asarray(ps_T), scheme="diag") ps_P = pixell.powspec.sym_expand(np.asarray(ps_P), scheme="diag") # TODO: These loops can probably be vectorized for i in range(nsplits): for i_pol in range(3): output_map[:, i, i_pol] = pixell.curvedsky.rand_map( (self.channel_per_tube, ) + self.shape, self.wcs, ps_T if i_pol == 0 else ps_P, spin=0, ) for i in range(self.channel_per_tube): freq = self.tubes[tube][i].center_frequency if not (self.homogeneous): good = weightsMap[i] != 0 # Normalize on the Effective sky fraction, see discussion in: # https://github.com/simonsobs/mapsims/pull/5#discussion_r244939311 output_map[i, :, :, good] /= np.sqrt(weightsMap[i][good][..., None, None]) output_map[i, :, :, np.logical_not(good)] = mask_value unit_conv = (1 * u.uK_CMB).to_value( u.Unit(output_units), equivalencies=u.cmb_equivalencies(freq)) output_map[i] *= unit_conv return output_map
vecpix = hp.pix2vec(nside,np.arange(12*nside**2)) cosang = np.dot(veccenter,vecpix) maskok = np.degrees(np.arccos(cosang)) < maxang maskmap=np.zeros(12*nside**2) maskmap[maskok]=1 wl = hp.anafast(maskmap,regression=False) wl = wl[0:lmax+1] #### Do a Monte-Carlo nbmc = 1000 allcls = np.zeros((nbmc, lmax+1)) for i in np.arange(nbmc): print(i) map = hp.synfast(spectra[1],nside,fwhm=0,pixwin=True,new=True) allcls[i,:] = hp.anafast(map*maskmap) #### Mll Matrix Mll = Mllmatrix(lmax, wl) #### ell binning min_ell = 20 deltal = 30 all_ell = min_ell-1 + np.arange(1000)*deltal max_ell = np.max(all_ell[all_ell < lmax]) nbins = (max_ell + 1 - min_ell) / deltal ell_low = min_ell + np.arange(nbins)*deltal ell_hi = ell_low + deltal - 1 the_ell = np.arange(lmax+1)
plt.savefig(survey+'_region.png',format='png') plt.savefig(survey+'_region.eps',format='eps') plt.clf() plt.close() fsky= 1. - np.sum(mask_bool)/float(len(mask_bool)) L=np.sqrt(fsky*4*np.pi) dl_eff=2*np.pi/L print('\tGenerating Control Map') alpha_radio=hp.read_map(alpha_file,hdu='maps/phi',verbose=False) tmp_alpha=hp.ud_grade(alpha_radio,nside_in) alpha_radio=hp.read_map(alpha_file,hdu='maps/phi',verbose=False) sigma_alpha=hp.read_map(alpha_file,hdu='uncertainty/phi',verbose=False) iqu_cmb=hp.synfast(cmb_cls,new=1,fwhm=0,pol=1,nside=nside_in,verbose=False) iqu_array=[rotate_tqu.rotate_tqu(iqu_cmb,w,tmp_alpha) for w in wl] iqu_array = [ np.array([iqu[0], \ iqu[1] + np.copy(simul_sync_q*sync_factor[i] + simul_dust_q*dust_factor[i]), iqu[2] + np.copy(simul_sync_u*sync_factor[i] + simul_dust_u*dust_factor[i])]) for i,iqu in enumerate(iqu_array)] iqu_array= [ hp.smoothing(iqu,pol=1,fwhm=beam_fwhm[cnt]*np.pi/(180.*60.),verbose=False) for cnt,iqu in enumerate(iqu_array)] #iqu_array_fr= [ hp.smoothing(iqu,pol=1,fwhm=beam_fwhm[cnt]*np.pi/(180.*60.),verbose=False) for cnt,iqu in enumerate(iqu_array_fr)] iqu_back=np.copy(iqu_array) const_array=[] band_names=[] the_au_index=[] au_index=[] for i in xrange(len(bands)-1): for j in xrange(i+1,len(bands)):
return np.std(map_masked, axis=0) p_name = options.param p_val = options.value print p_name, "=", p_val nrealizations = 1 ItoQU = np.zeros((nrealizations, 4, 3)) Noise = np.zeros((nrealizations, 4, 3)) QUmix = np.zeros((nrealizations, 4, 3)) for realization in xrange(nrealizations): seed = int(options.seed) + realization print seed np.random.seed(seed) spectra = read_spectra(0) input_map = np.array(hp.synfast(spectra, nside)).T ItoQU_, Noise_, QUmix_, o, e = run_ss(p_name, p_val, input_map) ItoQU[realization] = ItoQU_ Noise[realization] = Noise_ QUmix[realization] = QUmix_ dict = {p_name: p_val, 'ItoQU': ItoQU, 'Noise': Noise, 'QUmix': QUmix, 'Omega': o, 'Eta': e} if rank == 0: f_name = 'scan_ss_' + p_name + str(p_val) + 'seed' + str(options.seed) +'.pkl' with open(f_name, 'w') as f: dump(dict, f)
# Polychromatic instrument model q = QubicMultibandInstrument(filter_nus=nus * 1e9, filter_relative_bandwidths=nus / deltas, ripples=True) # The peaks of the synthesized beam are modeled with "ripples" s = QubicScene(nside=nside, kind='IQU') # Multi-band acquisition model # Nsb=2 sub-bands to reconstruct the CMB Nsb = 2 Nbfreq, nus_edge, nus_, deltas_, Delta_, Nbbands_ = compute_freq(band, relative_bandwidth, Nsb) a = QubicMultibandAcquisition(q, p, s, nus_edge, effective_duration=2) # Generate the input CMB map sp = read_spectra(0) cmb = np.array(hp.synfast(sp, nside, new=True, pixwin=True)).T # Generate the dust map coef = 1.39e-2 ell = np.arange(1, 1000) fact = (ell * (ell + 1)) / (2 * np.pi) spectra_dust = [np.zeros(len(ell)), coef * (ell / 80.)**(-0.42) / (fact * 0.52), coef * (ell / 80.)**(-0.42) / fact, np.zeros(len(ell))] dust = np.array(hp.synfast(spectra_dust, nside, new=True, pixwin=True)).T # Combine CMB and dust. As output we have N 3-component maps of sky. x0 = cmb_plus_dust(cmb, dust, Nbbands, nus) # Simulate the TOD. Here we use Nf frequencies over the 150 GHz band
def cross_template_with_planck(template, nrandom=0): #band='mb' band=217 # get mask mask = load_planck_mask() mask_factor = np.mean(mask**2.) # get planck beam bl, l_bl = load_planck_bl(band) l_bl = l_bl[0:lmax+1] bl = bl[0:lmax+1] # get CL_PLANCK_THEORY l_planck, cl_planck = get_cl_theory(band) # store a version of the biased, beam-convolved spectrum. cl_planck_biased = cl_planck.copy() # "unbias" this spectrum, i.e. correct for Planck beam cl_planck /= (bl**2.) # Define how we'll weight the differnet multipoles. # Since we've already gone from 2d to 1d power spectrum, we # need to weight by nmodes=2L+1. weight = (2.*l_planck+1.)/cl_planck weight[0:10] = 0. # get template auto-spectrum cl_template = hp.anafast(template*mask, lmax=lmax)/mask_factor # estimate amplitude(s) amps = [] if (nrandom==0): print ' ' print 'data' planck = load_planck_data(band) cl_template_planck = hp.anafast(template*mask, map2=planck*mask, lmax=lmax)/mask_factor # correct by one power of planck beam function cl_template_planck /= bl amp = np.sum(cl_template_planck*weight)/np.sum(cl_template*weight) print '%0.3f'%amp print amp amps.append(amp) else: print ' ' print 'randoms' # loop over nrandom for irandom in range(nrandom): print '%i/%i'%(irandom,nrandom) # generate a Planck map. # it's roundabout to generate a map from some CL's, then calculate # alm's to cross with the template. why not directly generate alm's? # because i want to include the effect of multiplying by the real-space mask. planck = hp.synfast(cl_planck_biased, nside, lmax=lmax) cl_template_planck = hp.anafast(template*mask, map2=planck*mask, lmax=lmax)/mask_factor # correct by one power of planck beam function cl_template_planck /= bl amp = np.sum(cl_template_planck*weight)/np.sum(cl_template*weight) print '%0.3f'%amp print amp amps.append(amp) print 'current RMS:' print np.std(amps) # return list of amplitudes return amps
def add_gaussian_small_scales(map_in, nside_out, pol=False): cl_in = hp.anafast(map_in) map_in_ns_out = hp.ud_grade(map_in, nside_out) map_in_ns_out_smt = hp.smoothing(map_in_ns_out, fwhm=np.radians(180. / 1000.)) print('map smoothed') ell_to_fit = np.arange(100, 500) hpf = create_high_pass_filter(300, 1000, 4 * nside_out) ell_hell = np.arange(len(hpf)) if pol == False: cl_T_to_fit = cl_in[ell_to_fit] else: cl_T_to_fit = cl_in[0][ell_to_fit] fit_cl_T = np.polyfit(np.log(ell_to_fit), np.log(cl_T_to_fit), 1) cl_hell_T = np.exp(fit_cl_T[0] * np.log(ell_hell) + fit_cl_T[1]) * hpf cl_hell_T[0] = 0 cl_hell_zero = np.zeros(len(cl_hell_T)) if pol: cl_E_to_fit = cl_in[1][ell_to_fit] cl_B_to_fit = cl_in[2][ell_to_fit] fit_cl_E = np.polyfit(np.log(ell_to_fit), np.log(cl_E_to_fit), 1) fit_cl_B = np.polyfit(np.log(ell_to_fit), np.log(cl_B_to_fit), 1) cl_hell_E = np.exp(fit_cl_E[0] * np.log(ell_hell) + fit_cl_E[1]) * hpf cl_hell_B = np.exp(fit_cl_B[0] * np.log(ell_hell) + fit_cl_B[1]) * hpf cl_hell_E[0] = 0 cl_hell_B[0] = 0 cl_hell = np.array([ cl_hell_T, cl_hell_E, cl_hell_B, cl_hell_zero, cl_hell_zero, cl_hell_zero ]) map_ss = hp.synfast(cl_hell, nside_out, lmax=nside_out * 3, pol=True, new=True) else: map_ss = hp.synfast(cl_hell_T, nside_out, lmax=nside_out * 3) print('map small scales computed') map_ss_mod = map_ss * map_in_ns_out_smt if pol == False: coeff_T = np.std(map_ss_mod) / np.std(map_ss) map_out_T = map_ss_mod / coeff_T + map_in_ns_out_smt else: coeff_T = np.std(map_ss_mod[0]) / np.std(map_ss[0]) map_out_T = map_ss_mod[0] / coeff_T + map_in_ns_out_smt[0] if np.any(map_out_T < 0): negative_pix = np.where(map_out_T < 0)[0] print('negative pixels ', len(negative_pix)) if pol == False: map_out_T[negative_pix] = map_in_ns_out[negative_pix] else: map_out_T[negative_pix] = map_in_ns_out[0][negative_pix] if pol: coeff_Q = np.std(map_ss_mod[1]) / np.std(map_ss[1]) coeff_U = np.std(map_ss_mod[2]) / np.std(map_ss[2]) coeff_P = (coeff_Q + coeff_U) / 2. map_out_Q = map_ss_mod[1] / coeff_P + map_in_ns_out_smt[1] map_out_U = map_ss_mod[2] / coeff_P + map_in_ns_out_smt[2] map_out = np.array([map_out_T, map_out_Q, map_out_U]) else: map_out = map_out_T return map_out
for g in range(0, 11): almtemp = h.map2alm(n.zeros(len(truediffCRmap)), lmax=lmax) almtemp[h.sphtfunc.Alm.getidx(lmax=lmax, l=1, m=0)] = g almtemp[h.sphtfunc.Alm.getidx(lmax=lmax, l=1, m=1)] = 10 - g truediffCRmap = h.alm2map(almtemp, nside, lmax=lmax) truediffCRmap *= 1e-2 / max(truediffCRmap) almtemp = h.map2alm(truediffCRmap) Cldipole = h.anafast(truediffCRmap, lmax=lmax) dipolepower = Cldipole[1] Cltrue = n.zeros(len(Cldipole)) Cltrue[0] = 0.0 # no monopole for i in range(1, lmax + 1): Cltrue[i] = 18 * dipolepower / (2. * i + 1.) / (i + 1.) / (i + 2.) psmap = h.synfast(Cltrue, 64, lmax=lmax) psalm = h.map2alm(psmap, lmax=lmax) psalm[h.sphtfunc.Alm.getidx( lmax=lmax, l=1, m=0)] = almtemp[h.sphtfunc.Alm.getidx(lmax=lmax, l=1, m=0)] psalm[h.sphtfunc.Alm.getidx( lmax=lmax, l=1, m=1)] = almtemp[h.sphtfunc.Alm.getidx(lmax=lmax, l=1, m=1)] truediffCRmap = h.alm2map(psalm, 64, lmax=lmax) h.write_map(sourcefolder + "true_relint_dipole%02d.fits.gz" % g, truediffCRmap)
## Covariance matrix covmc=FitsArray('covmc'+str(signoise)+'_'+str(nbmc)+'.dat') cormc=FitsArray('cormc'+str(signoise)+'_'+str(nbmc)+'.dat') ########################################################### ##### build coverage a=np.loadtxt('./cl_r=0.1bis2.txt') ell=a[:,0] ctt=np.concatenate([[0,0],a[:,1]*1e12*2*np.pi/(ell*(ell+1))]) cee=np.concatenate([[0,0],a[:,2]*1e12*2*np.pi/(ell*(ell+1))]) cte=np.concatenate([[0,0],a[:,4]*1e12*2*np.pi/(ell*(ell+1))]) cbb=np.concatenate([[0,0],a[:,7]*1e12*2*np.pi/(ell*(ell+1))]) ell=np.concatenate([[0,1],ell]) spectra=[ell,ctt,cte,cee,cbb] nside=128 map_orig=hp.synfast(spectra[4],nside,fwhm=0,pixwin=True) input_map=map_orig.copy() kmax = 2 qubic = QubicInstrument('monochromatic,nopol',nside=128) obs = QubicConfiguration(qubic, pointings) C = obs.get_convolution_peak_operator() P = obs.get_projection_peak_operator(kmax=kmax) H = P * C tod = H(input_map) coverage = P.T(np.ones_like(tod)) # study covariance matrix