def calculate(self): """ calculate various properties of the gaussian, fnl, gnl maps i.e. compute * Ais * As * Cls * dipoles using remove_dipole """ inpCls=self.inputCls[:self.lmax+1] if (self.gausmap0!=None): self.gausCls0=hp.alm2cl(self.gausalm0)[0:self.lmax+1] self.gausCls1=hp.alm2cl(self.gausalm1)[0:self.lmax+1] self.gausA0=get_A0(self.gausCls0[1:], inpCls[1:]) self.gausAi=Ais(self.gausmap1, self.lmax); self.gausA=AistoA(self.gausAi) self.gausAi2=Ais(self.gausmap0, self.lmax); self.gausA2=AistoA(self.gausAi2) self.gausmp=hp.remove_monopole(self.gausmap0, fitval=True)[1] self.gausdipole=get_dipole(self.gausmap1) if (self.fnlmaps0!=None): self.fnlCls0=[]; self.fnlCls1=[]; self.fnlA0=[]; self.fnlAi=[]; self.fnlAi2=[] self.fnlmp=[]; self.fnldipole=[]; self.fnlA=[]; self.fnlA2=[] for i in range(self.Nfnls): self.fnlCls0.append(hp.anafast(self.fnlmaps0[i], nspec=self.lmax)) self.fnlCls1.append(hp.anafast(self.fnlmaps1[i], nspec=self.lmax)) self.fnlA0.append(get_A0(self.fnlCls0[i][1:], inpCls[1:])) self.fnlAi.append(Ais(self.fnlmaps1[i], self.lmax)); self.fnlA.append(AistoA(self.fnlAi[i])) self.fnlAi2.append(Ais(self.fnlmaps0[i], self.lmax)); self.fnlA2.append(AistoA(self.fnlAi2[i])) self.fnlmp.append(hp.remove_monopole(self.fnlmaps0[i], fitval=True)[1]) self.fnldipole.append(get_dipole(self.fnlmaps1[i]))
def get_hem_Cls(skymap, direction, LMAX=256, deg=90.): """ from the given healpix skymap, return Cls for two hemispheres defined by the direction given, useful to study the possible scale dependence of power modulation direction should be a unit vector """ # generate hemispherical mask NPIX=len(skymap) NSIDE=hp.npix2nside(NPIX) maskp=np.array([0.]*NPIX) disc=hp.query_disc(nside=NSIDE, vec=direction, radius=0.0174532925*deg) maskp[disc]=1. #skymap=hp.remove_monopole(skymap) map1=hp.ma(skymap) map1.mask=maskp Clsp=hp.anafast(map1, lmax=LMAX) if (deg<90.): maskm=np.array([0.]*NPIX) disc=hp.query_disc(nside=NSIDE, vec=-direction, radius=0.0174532925*deg) maskm[disc]=1. map1.mask=maskm else: map1.mask=np.logical_not(maskp) Clsm=hp.anafast(map1, lmax=LMAX) return [Clsp, Clsm]
def get_spectra(maps, maskmap, lmax, min_ell, delta_ell, wl=None, Mllmat=None, MllBinned=None, ellpq=None, MllBinnedInv=None): nside = hp.npix2nside(len(maps[0])) if wl == None: wl = hp.anafast(maskmap,regression=False) wl = wl[0:lmax+1] if Mllmat == None: Mllmat=Mll(lmax,wl) cutmaps = np.array([maps[0] * maskmap, maps[1] * maskmap, maps[2] * maskmap]) cls = hp.anafast(cutmaps,pol=True) allcls= np.array([cls[0][0:lmax+1], cls[1][0:lmax+1], cls[2][0:lmax+1], cls[3][0:lmax+1], cls[4][0:lmax+1], cls[5][0:lmax+1]]) if ellpq == None: all_ell = min_ell-1 + np.arange(1000)*delta_ell max_ell = np.max(all_ell[all_ell < lmax]) nbins = np.int((max_ell + 1 - min_ell) / delta_ell) ell_low = min_ell + np.arange(nbins)*delta_ell ell_hi = ell_low + delta_ell - 1 the_ell = np.arange(lmax+1) newl, p, q = make_pq(the_ell, ell_low, ell_hi) else: newl = ellpq[0] p = ellpq[1] q = ellpq[2] if MllBinned == None: MllBinned = MllBin(Mllmat, p, q) if MllBinnedInv == None: MllBinnedInv = np.linalg.inv(MllBinned) ClsBinned = BinCls(p,allcls) newcls = np.dot(MllBinnedInv, np.ravel(np.array(ClsBinned))) newcls = np.reshape(newcls, (6, p.shape[0])) return newcls, newl, Mllmat, MllBinned, MllBinnedInv, p, q, allcls
def G_plus_C_vs_G_four(freq): """plots fourier of GSM and CMB added together compared to GSM alone User defines GSM frequency in MHz WARNING: Takes some time to run Note: GSM default shape is 512 CMB default shape is 2048 GSM and CMB shape must match to add arrays GSM has been cast to a higher nside of 2048 """ gsm=GlobalSkyModel() #generates a model of the GSM gsmdata=gsm.generate(freq) #upgrade the GSM data to 2048 gsmdata=hp.ud_grade(gsmdata,2048) #the raw CMB data cmbdata=hp.read_map('/home/amber/data/cmb/COM_CMB_IQU-commander-field-Int_2048_R2.01_full.fits') LMAX=1024 bothmap=hp.anafast(gsmdata+cmbdata,lmax=LMAX) ell=np.arange(len(bothmap)) plt.figure() freq_str=str(freq) plt.plot(ell,ell*(ell+1)*bothmap,color='b',label='GSM at '+freq_str+' MHz +CMB') plt.xlabel('ell');plt.ylabel('ell(ell+1)');plt.grid() #delete this line? #hp.write_bothmap('bothmap.fits',bothmap) gsmdataonly=gsm.generate(freq) gsmmaponly=hp.anafast(gsmdataonly,lmax=LMAX) ell=np.arange(len(gsmmaponly)) plt.plot(ell,ell*(ell+1)*gsmmaponly,color='r',label='GSM at '+freq_str+' MHz') #delete this line? #hp.write_gsmmaponly('gsmmaponly.fits',gsmmaponly) plt.legend(loc='upper right') plt.show()
def run(self, dataSlice, slicePoint=None): # Calculate the power spectrum. if self.removeDipole: cl = hp.anafast(hp.remove_dipole(dataSlice[self.colname])) else: cl = hp.anafast(dataSlice[self.colname]) l = np.arange(np.size(cl)) condition = np.where((l <= self.lmax) & (l >= self.lmin))[0] totalpower = np.sum(cl[condition]*l[condition]*(l[condition]+1)/2.0/np.pi) return totalpower
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 wpixel(instr, ell, nside=1024, idet=231, theta_max=30): nu = instr.filter.name NPOINTS = instr.detector.NPOINTS if NPOINTS == 1: return np.ones(len(ell)) path_ = '/home/fincardona/Qubic/Compare_poly/maps/poly/interfero' pathN = '/home/fincardona/Qubic/spatial_extension/maps/poly/interfero' beam_ = read_map('%s/sb_nside{}_mono_{}Ghz_idet{}_tmax{}.fits'. format(nside, int(nu/1e9), idet, theta_max) % path_) beamN = read_map('%s/sb_nside{}_mono_{}Ghz_idet{}_tmax{}_Npoints{}.fits'. format(nside, int(nu/1e9), idet, theta_max, NPOINTS) % pathN) return (hp.anafast(beamN, lmax=ell[-1], pol=False) / hp.anafast(beam_, lmax=ell[-1], pol=False))[ell[0]:]
def get_spectra(self, map1, map2=None): """ Return biased and Xpol-debiased estimations of the power spectra of a Healpix map or of the cross-power spectra if *map2* is provided. xpol = Xpol(mask, lmin, lmax, delta_ell) biased, unbiased = xpol.get_spectra(map1, [map2]) The unbiased Cls are binned. The number of bins is given by (lmax - lmin) // delta_ell, using the values specified in the Xpol's object initialisation. As a consequence, the upper bound of the highest l bin may be less than lmax. The central value of the bins can be obtained through the attribute `xpol.ell_binned`. Parameters ---------- map1 : Nx3 or 3xN array The I, Q, U Healpix maps. map2 : Nx3 or 3xN array, optional The I, Q, U Healpix maps. Returns ------- biased : float array of shape (6, lmax+1) The anafast's pseudo (cross-) power spectra for TT, EE, BB, TE, EB, TB. The corresponding l values are given by `np.arange(lmax + 1)`. unbiased : float array of shape (6, nbins) The Xpol's (cross-) power spectra for TT, EE, BB, TE, EB, TB. The corresponding l values are given by `xpol.ell_binned`. """ map1 = np.asarray(map1) if map1.shape[-1] == 3: map1 = map1.T if map2 is None: biased = hp.anafast(map1 * self.mask, pol=True) else: map2 = np.asarray(map2) if map2.shape[-1] == 3: map2 = map2.T biased = hp.anafast(map1 * self.mask, map2 * self.mask, pol=True) biased = np.array([cl[:self.lmax+1] for cl in biased]) binned = self.bin_spectra(biased) fact_binned = self.ell_binned * (self.ell_binned + 1) / (2 * np.pi) binned *= fact_binned unbiased = np.dot(self.mll_binned_inv, binned.ravel()).reshape(6, -1) unbiased /= fact_binned return biased, unbiased
def compute_pcl_estimate(data_file,inv_noise_file,beam_file): d = hp.read_map(data_file) inv_n = hp.read_map(inv_noise_file) print "zero pixels= ",len(inv_n[inv_n<=0]) if len(inv_n[inv_n<=0]) > 0: inv_n[inv_n<=0] = min(inv_n[inv_n>0]) n = 1./np.sqrt(inv_n) nside = hp.npix2nside(np.shape(d)[0]) # compute the total power spectrum C_l = hp.anafast(d,lmax=2*nside) num_samps = int(1000) N_l = np.zeros(np.shape(C_l)) B_l_in = np.loadtxt(beam_file,delimiter=",") B_l = B_l_in[:,1] #apply beam to the cls C_l /= B_l**2 # Monte Carlo mu = np.zeros(np.shape(d)) for samp in range(num_samps): if samp % 100 == 0 : print "samples =",samp # draw a realisation from noise n_i = np.random.normal(mu,n) # find the power spectrum of this realisation N_l_i = hp.anafast(n_i,lmax=2*nside) # accumulate N_l += N_l_i N_l /= float(num_samps) #apply beam to the nls N_l /= B_l**2 # subtract S_l = C_l - N_l return (C_l,N_l,S_l)
def generate_map(t, p, nside, plot=False): """Generate map and bands for the given galaxy locations""" npix = 12*nside**2 ix = hp.ang2pix(nside, t, p, nest=True) M = np.zeros(npix) for i in xrange(len(ix)): M[ix[i]] += 1. ave = 1.*len(t)/npix M = M/ave-1.0 #Calculate overdensity cl = hp.anafast(M) ell = np.arange(len(cl))+1 ell2 = (ell[::3]+ell[1::3]+ell[2::3])/3. cl2 = (cl[::3]+cl[1::3]+cl[2::3])/3. if plot: plot_galaxies_and_density(t, p, M) plot_anafast_band_powers(ell2, cl2) plt.show() ell3 = ell2[1:] cl3 = cl2[1:] #Alex to Matias: Why are these removed? cl_err = np.ones(len(ell3))*0.00002 index = np.arange(len(ell3)) ell_min = ell3-1 ell_max = ell3+1 cl3 = cl3*1000 #Bigger c_l values for convergence bands = zip(index, ell3, ell_min, ell_max, cl3, cl_err, cl3) return M, bands
def __init__(self, mask, lmin, lmax, delta_ell): """ Parameters ---------- mask : boolean Healpix map Mask defining the region of interest (of value True) lmin : int Lower bound of the first l bin. lmax : int Highest l value to be considered. The inclusive upper bound of the last l bin is lesser or equal to this value. delta_ell : The l bin width. """ mask = np.asarray(mask) lmin = int(lmin) lmax = int(lmax) if lmin < 1: raise ValueError('Input lmin is less than 1.') if lmax < lmin: raise ValueError('Input lmax is less than lmin.') delta_ell = int(delta_ell) wl = hp.anafast(mask)[:lmax+1] self.mask = mask self.lmin = lmin self.lmax = lmax self.delta_ell = delta_ell self.wl = wl self.ell_binned, self._p, self._q = self._bin_ell() mll_binned = self._get_Mll() self.mll_binned_inv = np.linalg.inv(mll_binned)
def study_multiband_planck(quick=True): savename = datadir+'cl_multiband.pkl' bands = [100, 143, 217, 'mb'] if quick: cl = pickle.load(open(savename,'r')) else: cl = {} mask = load_planck_mask() mask_factor = np.mean(mask**2.) for band in bands: this_map = load_planck_data(band) this_cl = hp.anafast(this_map*mask, lmax=lmax)/mask_factor cl[band] = this_cl pickle.dump(cl, open(savename,'w')) cl_theory = {} pl.clf() for band in bands: l_theory, cl_theory[band] = get_cl_theory(band) this_cl = cl[band] pl.plot(this_cl/cl_theory[band]) pl.legend(bands) pl.plot([0,4000],[1,1],'k--') pl.ylim(.7,1.3) pl.ylabel('data/theory')
def test_anafast_iqu(self): cl = hp.anafast([m.filled() for m in self.map1], lmax = 1024) cliqu = np.array(pyfits.open(os.path.join(self.path, 'data', 'cl_iqu_wmap_fortran.fits'))[1].data) self.assertEqual(len(cl[0]), 1025) self.assertEqual(len(cl), 6) for comp in range(6): np.testing.assert_array_almost_equal(cl[comp], np.double(cliqu[cliqu.dtype.names[comp]]), decimal=4)
def test_anafast_iqu(self): self.map1[0] = hp.remove_monopole(self.map1[0]) cl = hp.anafast(self.map1, lmax=self.lmax) self.assertEqual(len(cl[0]), 65) self.assertEqual(len(cl), 6) for i in range(6): np.testing.assert_array_almost_equal(cl[i], self.cliqu[i], decimal=8)
def G_plus_C_cross_C_four(freq,scale): """plot of fourier of GSM(scale)+CMB crossed with CMB compared to CMB only User defines GSM freq in MHz User defines scale of GSM such as 0.1 or 0.01 WARNING: takes some time to run """ gsm=GlobalSkyModel() #create the GSM model gsmdata=gsm.generate(freq) #rather than upgrading GSM, downgrade the CMB to 512 cmbdata=hp.read_map('/home/amber/data/cmb/COM_CMB_IQU-commander-field-Int_2048_R2.01_full.fits') cmbmap=hp.ud_grade(cmbdata,512) LMAX=1024 both=(gsmdata*scale)+cmbmap #cross cmb+gsm with the cmb c_plus_g_cross_c=hp.sphtfunc.anafast(both,map2=cmbmap,lmax=LMAX) ell=np.arange(len(c_plus_g_cross_c)) plt.figure() #format variables as text for labels freq_str=str(freq) scale_str=str(scale) plt.plot(ell,ell*(ell+1)*c_plus_g_cross_c,color='b',label='GSM('+scale_str+') at '+freq_str+' MHZ+CMB cross CMB') cmbmaponly=hp.anafast(cmbdata,lmax=LMAX) ell=np.arange(len(cmbmaponly)) plt.plot(ell,ell*(ell+1)*cmbmaponly,color='r',label='CMB') #delete this line? #hp.write_cmbmaponly('cmbmaponly.fits',cmbmaponly) plt.xlabel('ell');plt.ylabel('ell(ell+1)');plt.grid() #delete this line? #hp.write_cp_plus_g_cross_c('c_plus_g_cross_c.fits',c_plus_g_cross_c) plt.legend(loc='upper right') plt.show()
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 read_and_diff_files_fast(f1,f2,nside=256,tmask=None,return_map=False): #assume tmask input is already degraded mm1=hp.read_map(f1,[0,1,2],verbose=False) mm2=hp.read_map(f2,[0,1,2],verbose=False) mmm1=[] mmm2=[] for m1,m2 in zip(mm1,mm2): m1=hp.ud_grade(hp.ma(m1),nside_out=nside) m2=hp.ud_grade(hp.ma(m2),nside_out=nside) tmask=m1.mask | m2.mask | tmask mmm1.append(m1) mmm2.append(m2) diff=[] for m1,m2 in zip(mmm1,mmm2): d=m1-m2 d.mask=tmask diff.append(d) skyfrac=1-float(tmask.sum())/len(tmask) cldata=hp.anafast(diff) cldata_out=[] for cl in cldata: cldata_out.append(cl/skyfrac) if return_map is False: return cldata_out if return_map is True: return cldata_out,diff
def GetPs(freq): name = "21cm_Syn_35beam_noise" # plt.close('all') re = tt.ICA.rebuild(N, A_, S_, freq, Plot) rebuild_21cm = map[freq, :] - re rebuild_21cm = rebuild_21cm - rebuild_21cm.mean() cl1 = hp.anafast(map1[freq, pol]) cl2 = hp.anafast(rebuild_21cm) l = np.arange(len(cl1)) plt.figure("APS") plt.semilogy(l, l * (l + 1) * cl1, "r-", label="simulation") plt.semilogy(l, l * (l + 1) * cl2, "b-", label="result") plt.xlabel("$l$") plt.ylabel("$l(l+1)C_l$") plt.title(name) plt.legend() plt.savefig("./result/APS/" + name + ".eps")
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 estimate_cl(sky_map, lmax, binary_mask=None, fwhm=0.0, pol=True): sky_map_masked, binary_mask = mask_map(sky_map, binary_mask=binary_mask, pol=pol, ret_mask=True, fill_zeros=True) f_sky = float(np.sum(binary_mask))/binary_mask.size Bl = hp.gauss_beam(fwhm=fwhm, lmax=lmax, pol=pol) if pol: spectra = hp.anafast((sky_map_masked[0].filled(), sky_map_masked[1].filled(), sky_map_masked[2].filled()), lmax=lmax)[:4] spectra = np.array(spectra) spectra /= f_sky*Bl.T**2 else: spectra = hp.anafast(sky_map_masked.filled(), lmax=lmax) spectra /= f_sky*Bl**2 spectra = np.array(spectra) return spectra
def generate_powerspectrum(mask,lmax): print "Reading mask file" m = hp.read_map(mask) clfn = mask + '.cl' nside = hp.npix2nside(m.size) print "Generating power spectrum" cl = hp.anafast(m,lmax=lmax) np.savetxt(clfn,cl) return clfn
def test_anafast_iqu(self): cl = hp.anafast([m.filled() for m in self.map1], lmax = 1024) cliqu = pyfits.open(os.path.join(self.path, 'data', 'cl_iqu_wmap_fortran.fits'))[1].data self.assertEqual(len(cl[0]), 1025) self.assertEqual(len(cl), 6) for comp in range(6): comphealpix = comp if comp in [4,5]: # order of HEALPIX is TB, EB while in healpy is EB, TB comphealpix = {4:5, 5:4}[comp] np.testing.assert_array_almost_equal(cl[comp], cliqu.field(comphealpix), decimal=8)
def plotPowerSpectrum(self, metricValue, title=None, fignum=None, maxl=500., label=None, addLegend=False, legendloc='upper right', removeDipole=True, logPlot=True, verbose=False, **kwargs): """Generate and plot the power spectrum of metricValue. maxl = maximum ell value to plot (default 500 .. to plot all l, set to value > 3500) title = plot Title (default None) fignum = figure number (default None and create new plot) label = label to add in figure legend (default None) addLegend = flag to add legend (default False). removeDipole = remove dipole when calculating power spectrum (default True) (monopole removed automatically.) """ if fignum: fig = plt.figure(fignum) else: fig = plt.figure() # If the mask is True everywhere (no data), just plot zeros if False not in metricValue.mask: return None else: if removeDipole: cl = hp.anafast(hp.remove_dipole(metricValue.filled(self.badval), verbose=verbose)) else: cl = hp.anafast(metricValue.filled(self.badval)) l = np.arange(np.size(cl)) # Plot the results. if removeDipole: condition = ((l < maxl) & (l > 1)) else: condition = (l < maxl) plt.plot(l[condition], (cl[condition]*l[condition]*(l[condition]+1))/2.0/np.pi, label=label) if cl[condition].max() > 0 and logPlot: plt.yscale('log') plt.xlabel(r'$l$') plt.ylabel(r'$l(l+1)C_l/(2\pi)$') if addLegend: plt.legend(loc=legendloc, fancybox=True, prop={'size':'smaller'}) if title!=None: plt.title(title) # Return figure number (so we can reuse/add onto/save this figure if desired). return fig.number
def __init__(self,threshold,sig,freq,nside,LMAX,CMB_file_name='/home/amber/data/cmb/COM_CMB_IQU-commander-field-Int_2048_R2.01_full.fits'): self.threshold=threshold self.sig=sig self.freq=freq #check that nside value is valid if not np.all(hp.isnsideok(nside)): raise ValueError("%s is not a valid nside parameter (must be a power of 2, less than 2**30)"%str(nside)) self.nside=nside self.LMAX=LMAX self.CMB_file_name=CMB_file_name #can read and downgrade cmb in same step #use nside instead of 512. change both cmb and gsm to nside self.CMB_map=hp.ud_grade(hp.read_map(CMB_file_name),nside) #power spectrum of downgraded CMB map self.CMB_PS=hp.anafast(self.CMB_map,lmax=LMAX) self.ell=np.arange(len(self.CMB_PS)) self.signal=np.sqrt((np.sum((self.ell*(self.ell+1)*self.CMB_PS)**2))/(len(self.CMB_PS))) self.GSM_map=hp.ud_grade(gsm.generate(self.freq),nside) self.GSM_PS=hp.anafast(self.GSM_map,lmax=LMAX)
def make_cl_planck_data(): savename = datadir+'cl_planck_217.pkl' # get mask mask = load_planck_mask() mask_factor = np.mean(mask**2.) # get planck data planck = load_planck_data() cl_planck = hp.anafast(mask*planck, lmax=4000)/mask_factor l_planck = np.arange(len(cl_planck)) pickle.dump((l_planck, cl_planck), open(savename,'w'))
def w_function( lmax, nside, n, nu_cent, idet, theta_max, syb_f=None, NPOINTS=1, pol=False): cls = np.zeros((len(n), len(NPOINTS), lmax)) ell = np.arange(lmax) for i, enn in enumerate(n): for j, N in enumerate(NPOINTS): beam = read_( nside, enn, nu_cent, idet, theta_max, syb_f=syb_f, NPOINTS=N) cls[i, j] = hp.anafast(beam, lmax=lmax-1, pol=pol) * (2*ell+1) return cls, cls.max()
def __call__(self, metricValue, slicer, userPlotDict, fignum=None): """ Generate and plot the power spectrum of metricValue (calculated on a healpix grid). """ if 'Healpix' not in slicer.slicerName: raise ValueError('HealpixPowerSpectrum for use with healpix metricBundles.') plotDict = {} plotDict.update(self.defaultPlotDict) plotDict.update(userPlotDict) fig = plt.figure(fignum, figsize=plotDict['figsize']) ax = fig.add_subplot(plotDict['subplot']) # If the mask is True everywhere (no data), just plot zeros if False not in metricValue.mask: return None if plotDict['removeDipole']: cl = hp.anafast(hp.remove_dipole(metricValue.filled(slicer.badval)), lmax=plotDict['maxl']) else: cl = hp.anafast(metricValue.filled(slicer.badval), lmax=plotDict['maxl']) ell = np.arange(np.size(cl)) if plotDict['removeDipole']: condition = (ell > 1) else: condition = (ell > 0) ell = ell[condition] cl = cl[condition] # Plot the results. plt.plot(ell, (cl * ell * (ell + 1)) / 2.0 / np.pi, color=plotDict['color'], linestyle=plotDict['linestyle'], label=plotDict['label']) if cl.max() > 0 and plotDict['logScale']: plt.yscale('log') plt.xlabel(r'$l$', fontsize=plotDict['fontsize']) plt.ylabel(r'$l(l+1)C_l/(2\pi)$', fontsize=plotDict['fontsize']) if plotDict['labelsize'] is not None: plt.tick_params(axis='x', labelsize=plotDict['labelsize']) plt.tick_params(axis='y', labelsize=plotDict['labelsize']) if plotDict['title'] is not None: plt.title(plotDict['title']) # Return figure number (so we can reuse/add onto/save this figure if desired). return fig.number
def make_multiband_map(quick=True): savename = datadir+'map_mb.pkl' if quick: return pickle.load(open(savename,'r')) mask = load_planck_mask() mask_factor = np.mean(mask**2.) bands=[100, 143, 217] # first calculate the band-dependent, ell-dependent weights weight = {} cl_theory = {} total_weight = 0. for band in bands: # load biased, beam-convolved spectrum l_theory, this_cl_theory = get_cl_theory(band) # debias (correct for beam) bl, l_bl = load_planck_bl(band) this_cl_theory /= (bl**2.) cl_theory[band] = this_cl_theory weight[band] = 1./this_cl_theory total_weight += 1./this_cl_theory for k in weight.keys(): weight[k]/=total_weight alm_multiband = 0. for band in bands: # load map this_map = load_planck_data(band) # get alm's this_cl, this_alm = hp.anafast(this_map*mask, lmax=lmax, alm=True) # you might be tempted to correct alm by sqrt(mask_factor), but we're going to # transfer back to map space anyway. we'll account for the mask later. # debias these alm's (i.e. correct for one power of beam) bl, l_bl = load_planck_bl(band) assert len(bl)==(lmax+1) this_alm = hp.sphtfunc.almxfl(this_alm, 1./bl, inplace=False) # multiply by ell-dependent weights this_alm = hp.sphtfunc.almxfl(this_alm, weight[band], inplace=False) alm_multiband += this_alm # apply a nominal multi-band beam, which we'll take to be a FWHM=5' gaussian bl_mb, l_bl = load_planck_bl('mb') alm_multiband = hp.sphtfunc.almxfl(alm_multiband, bl_mb, inplace=False) # transfer back to real space map_mb = hp.sphtfunc.alm2map(alm_multiband, nside, lmax=lmax) # save pickle.dump(map_mb, open(savename,'w')) return map_mb
def correlated_cl(par,mp) : nb=len(par.bins)-1 lmax=3*par.nside-1 pixsize=4*np.pi/hp.nside2npix(par.nside) cb=np.zeros(nb) larr=np.arange(lmax+1) z=invert_covar(par,mp) clz_scaled=(2*larr+1.)*hp.anafast(z) for ib in np.arange(nb) : cb[ib]=np.sum(clz_scaled[par.bins[ib]:par.bins[ib+1]])/pixsize**2 return cb
def Beam_G_plus_C_cross_C_four(freq,scale,itheta,iphi,nside,sig=np.pi/10): """plot of fourier for GSM(scale)+CMB crossed with CMB beam applied then accounted for all compared to CMB alone User defines frequency of GSM and scale to decrease it User selects point of interest: theta and phi User may designate sigma for beam User selects nside Default sigma is pi/10 """ gsm=GlobalSkyModel() #generates GSM model gsmdata=gsm.generate(freq) #rather than upgrading GSM, downgrade the CMB to 512 cmbdata=hp.read_map('/home/amber/data/cmb/COM_CMB_IQU-commander-field-Int_2048_R2.01_full.fits') cmbmap=hp.ud_grade(cmbdata,512) LMAX=1024 both=(gsmdata*scale)+cmbmap #run the beam function with the given input showbeam=beam(itheta,iphi,nside,sig) npix=12*nside**2 #correct the data to account for the beam correction=npix/(np.sum(showbeam)) both=both*showbeam*correction #cross GSM+CMB with CMB and transform c_plus_g_cross_c=hp.sphtfunc.anafast(both,map2=cmbmap,lmax=LMAX) ell=np.arange(len(c_plus_g_cross_c)) plt.figure() #convert variables into text for labels freq_str=str(freq) scale_str=str(scale) plt.plot(ell,ell*(ell+1)*c_plus_g_cross_c,color='b',label='GSM('+scale_str+') at '+freq_str+' MHZ+CMB cross CMB') #transform CMB cmbmaponly=hp.anafast(cmbmap,lmax=LMAX) ell=np.arange(len(cmbmaponly)) plt.plot(ell,ell*(ell+1)*cmbmaponly,color='r',label='CMB') #delete this line? #hp.write_cmbmaponly('cmbmaponly.fits',cmbmaponly) plt.xlabel('ell');plt.ylabel('ell(ell+1)');plt.grid() #delete this line? #hp.write_c_plus_g_cross_c('c_plus_g_cross_c.fits',c_plus_g_cross_c) #convert variable into text for labels theta_str=str(itheta) phi_str=str(iphi) plt.legend(loc='upper right') plt.title('Point of Interest:('+theta_str+','+phi_str+')') #set the limits for the y axis plt.ylim(-0.00000001,0.00000006) plt.show()
) noise = sim.other_components["noise"] hitmaps, _ = noise.get_hitmaps(tube) for i, ch in enumerate(mapsims.parse_channels("tube:" + tube)[0]): filename = glob(folder + f"*{ch.tag}*1_of_1*")[0] print("reading " + filename) m[i] = hp.read_map(filename, (0, 1, 2)) npix = len(m[i][0]) nside = hp.npix2nside(npix) sky_fraction = hp.mask_good(m[i][0]).sum() / npix print("sky_fraction", sky_fraction) m[i] *= hitmaps[i] cl.append( np.array( hp.anafast(m[i], lmax=3*nside-1, use_pixel_weights=True) ) / np.mean(hitmaps[i] ** 2) / sky_fraction ) cl.append( np.array( hp.anafast(m[0], m[1], lmax=3*nside-1, use_pixel_weights=True) ) / np.mean(hitmaps[i] ** 2) / sky_fraction ) cl = np.array(cl) with open(f"output/noise/N_ell_tube_{tube}.pkl", "wb") as f: pickle.dump(cl, f, protocol=-1)
# y = data[0] # popt, pcov = optimize.curve_fit(f1, x, y) # a, b, c = popt # y_fit = f(x_fit, *popt) # lable = r'$a \, \exp{(- \frac{|x - \mu|} {c})}$' + '\n\n' + r'$a = %f$' % a + '\n' + r'$\mu = %f$' % b + '\n' + r'$\sigma = %f$' % np.abs(c) # plt.plot(x_fit, y_fit, lw=2, color="g", label=lable) # plt.legend() plt.savefig(out_dir + 'hist_%d.png' % i) plt.close() # compute cl cl_sim = healpy.anafast(cm_map[cind]) cl_est = healpy.anafast(rec_cm[cind]) cl_simxest = healpy.anafast(cm_map[cind], rec_cm[cind]) # plot cl plt.figure() plt.plot(cl_sim, label='Input HI') plt.plot(cl_est, label='Recovered HI') plt.plot(cl_simxest, label='cross', color='magenta') plt.legend(loc='best') plt.xlabel(r'$l$') plt.ylabel(r'$C_l^{TT}$') plt.savefig(out_dir + 'cl_%d.png' % i) plt.close() # plot transfer function cl_out / cl_in
#print(lines[i+1].split()[0]) thetas.append(float(lines[i].split()[1])) phis.append(float(lines[i].split()[2])) i += 1 indices = hp.ang2pix(nside, thetas, phis) hpxmap = np.zeros(npix, dtype=np.float) for k in range(len(thetas)): hpxmap[indices[k]] += 1.0 #hpxmap[indices[k]] += npix*(1.0/len(thetas)) hpxmapFinal = np.zeros(npix, dtype=np.float) for v in range(len(thetas2)): hpxmapFinal[indices2[v]] += ( hpxmap[indices2[v]] / hpxmap2[indices2[v]]) * (npix * (1.0 / len(thetas))) c = hp.anafast(hpxmapFinal) print(c) print("") print("") DPI = 100 SIZE = 400 hp_smoothed = hp.sphtfunc.smoothing(hpxmap, fwhm=np.radians(3.), iter=1) hp.mollview(hp_smoothed, cmap=cm.jet, norm="hist", xsize=SIZE, title='Real Data smoothed') plt.savefig("AMPT_smoothed1.png", dpi=DPI)
#copy input data as we need it later outputMap = copy.copy(inputMap) #transform map lens.transformModelVector(outputMap, 5 * crpropa.EeV) #plot transformed map healpy.mollview(map=outputMap, title='Lensed') # In[17]: #Calculate power spectra lmax = 20 mmUnlensed = healpy.anafast(inputMap, lmax=lmax) mmLensed = healpy.anafast(outputMap, lmax=lmax) #plot power spectra figure() l = arange(0, 20.01) step(l, mmUnlensed / mmUnlensed[0], c='k', label='Unlensed', where='mid') step(l, mmLensed / mmLensed[0], c='r', label='Lensed', where='mid') semilogy() ylim(1E-7,1) xlabel('$l$') ylabel(' $C_l / C_0$') legend() # In[17]:
def clerr_jack(delta, mask, weight, njack=20, lmax=512): ''' ''' npix = delta.size hpix = np.argwhere(mask).flatten() dummy = np.ones(mask.sum()) hpixl, wl, deltal,_ = split_jackknife(hpix, weight[mask], delta[mask], dummy, njack=njack) print('# of jackknifes %d, input : %d'%(len(hpixl), njack)) cljks = {} # get the cl of the jackknife mask wlt = wl.copy() hpixt = hpixl.copy() wlt.pop(0) hpixt.pop(0) wlc = np.concatenate(wlt) hpixc = np.concatenate(hpixt) maski = np.zeros(npix, '?') maski[hpixc] = True map_i = hp.ma(maski.astype('f8')) map_i.mask = np.logical_not(maski) clmaskj = hp.anafast(map_i.filled(), lmax=lmax) sfj = ((2*np.arange(clmaskj.size)+1)*clmaskj).sum()/(4.*np.pi) for i in range(njack): hpixt = hpixl.copy() wlt = wl.copy() deltalt = deltal.copy() # hpixt.pop(i) wlt.pop(i) deltalt.pop(i) # hpixc = np.concatenate(hpixt) wlc = np.concatenate(wlt) deltac = np.concatenate(deltalt) # maski = np.zeros(npix, '?') deltai = np.zeros(npix) wlci = np.zeros(npix) # maski[hpixc] = True deltai[hpixc] = deltac wlci[hpixc] = wlc # map_i = hp.ma(deltai * wlci) map_i.mask = np.logical_not(maski) cljks[i] = hp.anafast(map_i.filled(), lmax=lmax)/sfj # hpixt = hpixl.copy() wlt = wl.copy() deltalt = deltal.copy() # hpixc = np.concatenate(hpixt) wlc = np.concatenate(wlt) deltac = np.concatenate(deltalt) # maski = np.zeros(npix, '?') deltai = np.zeros(npix) wlci = np.zeros(npix) # maski[hpixc] = True deltai[hpixc] = deltac wlci[hpixc] = wlc # map_i = hp.ma(maski.astype('f8')) map_i.mask = np.logical_not(maski) clmask = hp.anafast(map_i.filled(), lmax=lmax) sf = ((2*np.arange(clmask.size)+1)*clmask).sum()/(4.*np.pi) map_i = hp.ma(deltai * wlci) map_i.mask = np.logical_not(maski) cljks[-1] = hp.anafast(map_i.filled(), lmax=lmax)/sf # entire footprint # clvar = np.zeros(len(cljks[-1])) for i in range(njack): clvar += (cljks[-1] - cljks[i])*(cljks[-1] - cljks[i]) clvar *= (njack-1)/njack return dict(clerr=np.sqrt(clvar), cljks=cljks, clmaskj=clmaskj, clmask=clmask, sf=sf, sfj=sfj)
deltai[~mask] = hp.UNSEEN deltai[mask] = deltai[mask] / deltai[mask].mean() - 1.0 hp.fitsfunc.write_map( 'Maps/delta_' + params.runflag + '_matter_fullsky_{}.fits'.format(str(round(zl, 4))), deltai, overwrite=True, dtype=np.float32) kappai = (1.0+zl) * ( ( 1.0 - cosmology.lcdm.comoving_distance(zl)/cosmology.lcdm.comoving_distance(params.zsource) ) *\ cosmology.lcdm.comoving_distance(zl) *\ ( cosmology.lcdm.comoving_distance(z2) - cosmology.lcdm.comoving_distance(z1) ) ).to_value() * deltai kappai *= (3.0 * cosmology.lcdm.Om0 * cosmology.lcdm.H0**2 / 2.0 / cosmology.cspeed**2).to_value() kappa[mask] += kappai[mask] kappai[~mask] = hp.UNSEEN hp.fitsfunc.write_map( 'Maps/kappa_' + params.runflag + '_field_fullsky_{}.fits'.format(str(round(zl, 4))), kappai, overwrite=True, dtype=np.float32) print("++++++++++++++++++++++") print("Computing convergence Cl") cl = hp.anafast(kappa, lmax=512) ell = np.arange(len(cl)) np.savetxt("Maps/Cls_kappa_z{}.txt".format(params.zsource), np.transpose([ell, cl, ell * (ell + 1) * cl])) print("All done!")
def proj_all(dens_type=0, ngrid=512, nside=256, lmax=750, rsd=True, write=True): npix = hp.nside2npix(nside) dirin = os.path.join("batch", "ngrid{}".format(ngrid), "dens_type{}".format(dens_type)) files = glob.glob(os.path.join(dirin, "cat*.fits")) ncat = len(files) print("{} files in {}".format(len(files), dirin)) zval = (0, 0.1, 0.2, 0.3, 0.4, 0.5) cpt = 1 cls = zeros((4, lmax + 1)) for file in files: cat = Catalog(file, rsd) print("OK {}".format(cpt)) for i in range(0, 4): ishell = i + 2 zmax = zval[ishell] zmin = zval[ishell - 1] zrec, ra, dec = cat.get([zmin, zmax]) Nsamp = len(ra) nbar = Nsamp / (4 * np.pi) mp = np.bincount(hp.ang2pix(nside, np.radians(90 - dec), np.radians(ra)), minlength=npix) Nmean = mp.mean() map = mp.astype(float) / Nmean - 1. #anafast cl = hp.anafast(map, lmax=lmax, iter=0, pol=False, use_weights=True, datapath=os.environ['HEALPIXDATA']) l = arange(len(cl)) s2 = sum((2 * l + 1) * cl) / (4 * pi) print( "\t -> shell={}: z in [{},{}] Nsamp={:5.2}M sigma={:5.2} shot noise={:5.2}" .format(ishell, zmin, zmax, Nsamp / 1e6, sqrt(s2), 1 / nbar)) #remove SN cl -= 1. / nbar cls[i, :] += cl cpt += 1 #normalize for i in range(0, 4): cls[i, :] /= ncat if write: for i in range(0, 4): ishell = i + 2 dirout = os.path.join(dirin, "shell{:d}".format(ishell)) os.makedirs(dirout, exist_ok=True) clname = "clmean.fits" if not rsd: clname = "clmean_norsd.fits" f1 = os.path.join(dirout, clname) print("writing {}".format(f1)) hp.write_cl(f1, cls[i], overwrite=True) return cls
def proj(dens_type=0, ishell=5, ngrid=512, nside=256, lmax=750, rsd=True, write=True): dirin = get_path(dens_type, ishell, ngrid) os.makedirs(dirin, exist_ok=True) zval = (0, 0.1, 0.2, 0.3, 0.4, 0.5) zmax = zval[ishell] zmin = zval[ishell - 1] files = glob.glob(os.path.join(dirin, "..", "cat*.fits")) print("Analyzing: {}".format(dirin)) print("shell #{}: z=[{},{}] ".format(ishell, zmin, zmax)) print("there are {} files".format(len(files))) zcut = [zmin, zmax] print(" --> slice z=[{},{}] onto nside={} map".format( zcut[0], zcut[1], nside)) npix = hp.nside2npix(nside) cls = [] cpt = 1 for file in files: #zrec,ra,dec=read_catalog(file,zcut,rsd) catalog = Catalog(file, rsd) zrec, ra, dec = catalog.get(zcut) Nsamp = len(ra) nbar = Nsamp / (4 * np.pi) print(" {} -> Nsamp={}, SN={}".format(cpt, Nsamp, 1 / nbar)) mp = np.bincount(hp.ang2pix(nside, np.radians(90 - dec), np.radians(ra)), minlength=npix) Nmean = mp.mean() map = mp.astype(float) / Nmean - 1. #anafast cl = hp.anafast(map, lmax=lmax, iter=0, pol=False, use_weights=True, datapath=os.environ['HEALPIXDATA']) #remove SN cl -= 1. / nbar cls.append(cl) cpt += 1 clm = np.mean(cls, 0) covmat = np.cov(np.transpose(cls)) if write: dirout = os.path.join(dirin, "..", "shell{:d}".format(ishell)) os.makedirs(dirout, exist_ok=True) clname = "clmean.fits" if not rsd: clname = "clmean_norsd.fits" f1 = os.path.join(dirout, clname) hp.write_cl(f1, clm, overwrite=True) covname = "covmat.fits" if not rsd: clname = "covmat_norsd.fits" f2 = os.path.join(dirout, "covmat.fits") hdu = fits.ImageHDU(covmat) hdu.writeto(f2, overwrite=True) print("writing {} and {}".format(f1, f2)) return clm, covmat
tickfs(ax) plt.loglog() plt.savefig("../plots_paper/cls_lss.pdf",bbox_inches='tight') plt.show() if (o.whichfig==4) or (o.whichfig==-1) : #Plotting LSS contaminant power spectra l,cltt,clee,clbb,clte,nltt,nlee,nlbb,nlte=np.loadtxt("cls_lss.txt",unpack=True) ll=l[:3*nside_lss]; cl_x=cltt[:3*nside_lss]; cw_x=clee[:3*nside_lss] msk_s=hp.read_map("mask_lss_ns%d.fits"%nside_lss,verbose=False); fsky=np.mean(msk_s) l_s=hp.read_map("cont_lss_star_ns%d.fits"%nside_lss,verbose=False) l_d=hp.read_map("cont_lss_dust_ns%d.fits"%nside_lss,verbose=False) w_pq,w_pu=hp.read_map("cont_wl_psf_ns%d.fits"%nside_lss,verbose=False,field=[0,1]) w_sq,w_su=hp.read_map("cont_wl_ss_ns%d.fits"%nside_lss ,verbose=False,field=[0,1]) cl_d,cw_pe,cw_pb,_,_,_=hp.anafast([l_d*msk_s,w_pq*msk_s,w_pu*msk_s],pol=True,iter=0)/fsky cl_s,cw_se,cw_sb,_,_,_=hp.anafast([l_s*msk_s,w_sq*msk_s,w_su*msk_s],pol=True,iter=0)/fsky def rebin_and_plot(x,y,b,lt,c,ax,lb=None) : xp=np.mean(x.reshape([len(x)//b,b]),axis=1) yp=np.mean(y.reshape([len(x)//b,b]),axis=1) if lb is None : ax.plot(xp,yp,lt,c=c) else : ax.plot(xp,yp,lt,c=c,label=lb) plt.figure(figsize=(7,8)) plt.subplots_adjust(hspace=0) ax1=plt.subplot(311) c_x='#000099'; c_d='#00CCCC'; c_s='#CC0000'; c_t='#FF9933'; rb=8 rebin_and_plot(ll,cl_x,rb,'-',c_x,ax1,lb='Signal') rebin_and_plot(ll,cl_d,rb,'--',c_d,ax1,lb='Dust')
def CreateAnafastPartialSky(cl, nside, lmin, lmax, delta_ell, f_sky=1., plot_results=False, noise_rms=0): import qubic.NamasterLib as nam npix = 12 * nside**2 # Determine SEEN pixels from f_sky using query_disc vec = hp.pixelfunc.ang2vec(np.pi / 2, np.pi * 3 / 4) radius = f_sky * np.pi ipix_disc = hp.query_disc(nside=nside, vec=vec, radius=radius, nest=False) while len(ipix_disc) < f_sky * npix: radius += 0.01 * np.pi ipix_disc = hp.query_disc(nside=nside, vec=vec, radius=radius, nest=False) #print("npix_partial_sky: ", len(ipix_disc)) m = np.arange(npix) m = np.delete(m, ipix_disc, axis=None) # Define the seen pixels seenpix = ipix_disc ### Making mask - it will be automaticall apodized when instanciating the object with default (tunable) parameters mask = np.zeros(npix) mask[seenpix] = 1 Namaster = nam.Namaster(mask, lmin=lmin, lmax=lmax, delta_ell=delta_ell) if plot_results: if not os.path.isdir(path_): os.mkdir(path_) plt.figure() hp.mollview(mask) figname = 'fig_map_partial_mask.png' dest = os.path.join(path_, figname) plt.savefig(dest) # write image to file plt.clf() plt.figure() mask_apo = Namaster.mask_apo hp.mollview(mask_apo) figname = 'fig_map_partial_mask_apo.png' dest = os.path.join(path_, figname) plt.savefig(dest) # write image to file plt.clf() ell_binned, b = Namaster.get_binning(nside) # Get binned input spectra cl_theo_binned = np.zeros(shape=(4, ell_binned.shape[0])) for i in range(4): cl_theo_binned[i, :] = Namaster.bin_spectra(cl.T[i, :], nside) print(cl_theo_binned.shape) # Create map map_ = hp.synfast(cl.T, nside, pixwin=False, verbose=False, new=True) noise = np.random.randn(npix) * noise_rms map_ = map_ + noise # constructing partial map map_partial = map_.copy() # Set UNSEEN pixels to zero for NaMaster map_partial[:, m] = 0 # Get spectra leff, dells, w = Namaster.get_spectra(map_partial, purify_e=True, purify_b=False, beam_correction=None, pixwin_correction=None, verbose=False) dells = dells.T if plot_results: clnames = ['TT', 'EE', 'BB', 'TE'] ll = np.arange(cl.shape[0]) #rc('figure', figsize=(12, 8)) plt.figure(figsize=(12, 8)) for i in range(dells.shape[0]): plt.subplot(2, 2, i + 1) plt.plot(ll, ll * (ll + 1) * cl[:, i] / (2 * np.pi), label="Input spectra") plt.plot(leff, leff * (leff + 1) * cl_theo_binned[i, :] / (2 * np.pi), "o", label="Binned input spectra") plt.plot(leff, dells[i], label="NaMaster") plt.xlabel('$\\ell$') plt.ylabel('$D_\\ell$') plt.title(clnames[i]) plt.legend() plt.tight_layout() figname = 'fig_theor_namaster_all_Dl.png' dest = os.path.join(path_, figname) plt.savefig(dest) # write image to file plt.clf() # Anafast spectrum of this map # Set UNSEEN pixels to hp.UNSEEN for Anafast map_partial[:, m] = hp.UNSEEN cl_ana, alm_ana = hp.anafast(map_partial, alm=True, lmax=lmax) # Get binned anafast spectra cl_ana_binned = np.zeros(shape=(4, ell_binned.shape[0])) for i in range(4): cl_ana_binned[i, :] = Namaster.bin_spectra(cl_ana[i, :], nside) if plot_results: mapnames = ['T', 'Q', 'U'] for i in range(3): plt.figure() hp.mollview(map_partial[i]) figname = 'fig_map_partial_{}.png'.format(mapnames[i]) dest = os.path.join(path_, figname) plt.savefig(dest) # write image to file plt.clf() for i in range(4): # Plot theoretical and anafast spectra plt.figure() plt.plot(ll, ll * (ll + 1) * cl_ana[i][:lmax + 1], label="Anafast") plt.plot(leff, leff * (leff + 1) * cl_ana_binned[i], "o", label="Anafast binned") plt.plot(ll, ll * (ll + 1) * cl.T[i], 'r', label="Input spectra") plt.xlim(0, max(ll)) plt.title(clnames[i]) plt.ylabel(r"$\ell (\ell + 1) C_{\ell}^{" + clnames[i] + r"}$") plt.legend() figname = 'fig_theor_anaf_{}.png'.format(clnames[i]) dest = os.path.join(path_, figname) plt.savefig(dest) # write image to file plt.clf() return dells, alm_ana, cl_theo_binned, cl_ana_binned
#np.save('cl_PySM_dust_TQU_ns4096.fits', cl_dust) #SYNCH synchT = hp.read_map( '/global/homes/k/krach/usr/python/PySM_public/pysm/SO_template/synch_T.fits' ) synchQ = hp.read_map( '/global/homes/k/krach/usr/python/PySM_public/pysm/SO_template/synch_Q.fits' ) synchU = hp.read_map( '/global/homes/k/krach/usr/python/PySM_public/pysm/SO_template/synch_U.fits' ) synch_pysm = np.array([synchT, synchQ, synchU]) synch4096 = add_gaussian_small_scales(synch_pysm, 4096, pol=True) hp.write_map('maps/PySM_synch_TQU_ns4096_2.fits', synch4096, overwrite=True) cl_synch = hp.anafast(synch4096) np.save('cl_PySM_synch_TQU_ns4096_2.fits', cl_synch) ##FREE-FREE #ffT = hp.read_map('/global/homes/k/krach/usr/python/PySM_public/pysm/SO_template/freefree_T.fits') #ff4096 = add_gaussian_small_scales(ffT, 4096, pol=False) #hp.write_map('maps/PySM_freefree_T_ns4096.fits', ff4096, overwrite=True) #cl_ff = hp.anafast(ff4096) #np.save('cl_PySM_freefree_T_ns4096.fits', cl_ff) ##AME 1 #ame1 = hp.read_map('/global/homes/k/krach/usr/python/PySM_public/pysm/SO_template/ame_T.fits') #ame1_4096 = add_gaussian_small_scales(ame1, 4096, pol=False) #hp.write_map('maps/PySM_ame1_T_ns4096.fits', ame1_4096, overwrite=True) #cl_ame1 = hp.anafast(ame1_4096) #np.save('cl_PySM_ame1_T_ns4096.fits', cl_ame1)
def polsample_test(niter=1000): import camb from camb import model, initialpower 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.5, ombh2=0.022, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.WantTensors = True pars.InitPower.set_params(ns=0.965, r=0.05) pars.set_for_lmax(2500, lens_potential_accuracy=0) results = camb.get_results(pars) powers = results.get_cmb_power_spectra(pars, CMB_unit='muK') var = 1e-4 nside = 8 lmax = 3 * nside - 1 ell = np.arange(lmax + 1.) Cl_arr = powers['total'][:lmax + 1].T Z = ell * (ell + 1) / (2 * np.pi) Z[:2] = 1. Cls = np.array([Cl_arr[0]/Z, Cl_arr[1]/Z, Cl_arr[2]/Z,\ Cl_arr[3]/Z, 0*Cl_arr[0], 0*Cl_arr[0]]) plt.figure() for i in range(4): plt.subplot(2, 2, i + 1) plt.loglog(ell[2:], Cls[i][2:], color='C{0}'.format(i)) s_true = hp.synfast(Cls, nside, new=True) m = np.copy(s_true) inds = (mask == 0) for i in range(3): m[i] = s_true[i] + np.random.randn(npix) * var**0.5 m[i][inds] = 0 Clhat_true = hp.anafast(s_true) Clhat_noise = hp.anafast(m) for i in range(4): plt.subplot(2, 2, i + 1) plt.loglog(ell[2:], Clhat_true[i][2:], '.', color='C{0}'.format(i)) for i in range(4): plt.subplot(2, 2, i + 1) plt.loglog(ell[2:], Clhat_noise[i][2:], 'o', color='C{0}'.format(i)) plt.savefig('1.png') #plt.show() plt.figure() plt.suptitle('Input') hp.mollview(s_true[0], min=-200, max=200, sub=131, title='') hp.mollview(s_true[1], min=-0.5, max=0.5, sub=132, title='') hp.mollview(s_true[2], min=-0.5, max=0.5, sub=133, title='') plt.savefig('2.png') #plt.show() plt.figure() Clhat = hp.anafast(m) s = np.copy(m) Cli = np.copy(Cls) for n in range(niter): print(n, niter) si = maparr_sample(Cli, m, var=var) Cli = clarr_sample(si) s = np.vstack((s, si)) Clhat = np.vstack((Clhat, Cli)) for j in range(4): plt.subplot(2, 2, j + 1) plt.plot(ell[2:], Cli[j][2:], '.', alpha=0.05,\ color='C{0}'.format(j)) plt.figure() hp.mollview(si[0], min=-200, max=200, sub=131, title='', cbar=False, cmap=cmap) hp.mollview(si[1], min=-.5, max=.5, sub=132, title='', cbar=False, cmap=cmap) hp.mollview(si[2], min=-.5, max=.5, sub=133, title='', cbar=False, cmap=cmap) fnames = glob('realization_*') plt.savefig('realization_{0}.png'.format(str(len(fnames)).zfill(4))) plt.close() for i in range(4): plt.subplot(2, 2, i + 1) plt.plot(ell[2:], Cls[i][2:], 'k') for i in range(4): plt.subplot(2, 2, i + 1) plt.plot(ell[2:], Clhat_true[i][2:], 'k.') plt.yscale('log') plt.xscale('log') plt.savefig('3.png') #plt.show() np.save('map_samples.npy', s) np.save('cl_samples.npy', clhat) plt.figure() plt.suptitle('Output') hp.mollview(si[0], min=-200, max=200, sub=131, title='') hp.mollview(si[1], min=-0.5, max=0.5, sub=132, title='') hp.mollview(si[2], min=-0.5, max=0.5, sub=133, title='') plt.savefig('4.png') #plt.show() return
cldict_normal_test = calculations.ilc_foreground_cleaning.polarized_ilc_reconstruction( frequencies=np.array([200., 270., 350., 600.]) * 1.e9, fname=fname, regnoise=regnoise, lensed=lensed, modcov=modcov, verbose=True) save_dict_to_hd5(cntpath, cldict_normal_test) clnpath = os.path.abspath(datapath + 'cldict_noise.hd5') if os.path.exists(clnpath): print("cldict_noise already exists at {0}. Loading it.".format(clnpath)) cldict_noise = read_dict_from_hd5(clnpath) else: print("Computing noise C_l's.") noisemap = cldict_normal_test['noisemaps'][0][0] cls_noise = hp.anafast([noisemap, noisemap, noisemap]) labels = ['TT', 'EE', 'BB', 'TE', 'EB', 'TB'] cldict_noise = {labels[i]: cls_noise[i] for i in range(len(labels))} save_dict_to_hd5(clnpath, cldict_noise) cldict_noise['ell'] = np.arange(len(cldict_noise['TT']), dtype='float') prefix_noise = cldict_noise['ell'] * (cldict_noise['ell'] + 1) / (2 * np.pi) print("Finished importing calibration_gain.") print("=" * 80) def myplot(cld, name, label, nf=1., bbmax=0.1,