def test_pixwin_base(self): # Base case nsides = [2**p for p in np.arange(1, 14)] [hp.pixwin(nside) for nside in nsides] # Test invalid nside with self.assertRaises(ValueError): hp.pixwin(15)
def test_pixwin_lmax(self): nside = 128 pixwin = hp.pixwin(nside, lmax=None) self.assertEqual(len(pixwin), 3 * nside) lmax = 200 pixwin = hp.pixwin(nside, lmax=lmax) self.assertEqual(len(pixwin)-1, lmax)
def test_pixwin_lmax(self): nside = 128 pixwin = hp.pixwin(nside, lmax=None) self.assertEqual(len(pixwin), 3 * nside) lmax = 200 pixwin = hp.pixwin(nside, lmax=lmax) self.assertEqual(len(pixwin) - 1, lmax)
def test_pixwin_base(self): # Base case nsides = [2 ** p for p in np.arange(1, 14)] [hp.pixwin(nside) for nside in nsides] # Test invalid nside with self.assertRaises(ValueError): hp.pixwin(15)
def run_propag(): """ Follows the SMICA propagation code to combine maps with set of weights. Only runs for the chosen bins and up to the max value of bins """ CMB = dict() CMB["TQU"] = dict() almT, almE, almB = dict(), dict(), dict() lmaxbin = int(bins[-1][1]+1) W = io.load_data(io.fh.weight_smica_path_name) # full maps maps = io.load_plamap(csu.cf, field=(0,1,2), nside_out=csu.nside_out) maps = trsf_m.process_all(maps) for freq in csu.PLANCKMAPFREQ_f: ns = csu.nside_out[0] if int(freq) < 100 else csu.nside_out[1] alms = pw.map2alm_spin(maps[freq], hp.ud_grade(pmask[freq], nside_out=ns), 2, lmaxbin-1) # full sky QU->EB # almT[det] = alms[0] almE[freq] = alms[0] almB[freq] = alms[1] nalm = int((lmaxbin)*(lmaxbin-1+2)/2) # combalmT = np.zeros((nalm), dtype=np.complex128) combalmE = np.zeros((nalm), dtype=np.complex128) combalmB = np.zeros((nalm), dtype=np.complex128) beamf = io.load_beamf(freqcomb=csu.freqcomb) xnew = np.arange(0,lmaxbin,1) for it, det in enumerate(csu.PLANCKMAPFREQ): #weights do not depend on freqfilter, but almE/B do if det in csu.PLANCKMAPFREQ_f: ns = csu.nside_out[0] if int(det) < 100 else csu.nside_out[1] W_Einterp = interpolate.interp1d(np.mean(bins, axis=1), W[1,it,:], bounds_error = False, fill_value='extrapolate') #TODO switch to W[2,:] once BB-weights are correctly calculated W_Binterp = interpolate.interp1d(np.mean(bins, axis=1), W[1,it,:], bounds_error = False, fill_value='extrapolate') # combalmT += hp.almxfl(almT[name], np.squeeze(W[0,m,:])) LHFI = "LFI" if int(det)<100 else "HFI" combalmE += hp.almxfl(hp.almxfl(almE[det],1/beamf[str(det)+'-'+str(det)][LHFI][1].data.field(1)[:lmaxbin]), np.squeeze(W_Einterp(xnew))) combalmE = hp.almxfl(combalmE, 1/hp.pixwin(ns, pol=True)[0][:lmaxbin]) combalmB += hp.almxfl(hp.almxfl(almB[det],1/beamf[str(det)+'-'+str(det)][LHFI][1].data.field(2)[:lmaxbin]), np.squeeze(W_Binterp(xnew))) combalmB = hp.almxfl(combalmB, 1/hp.pixwin(ns, pol=True)[1][:lmaxbin]) CMB["TQU"]['out'] = hp.alm2map([np.zeros_like(combalmE), combalmE, combalmB], csu.nside_out[1]) io.save_data(CMB["TQU"]['out'], io.fh.cmbmap_smica_path_name) smica_C_lmin_unsc = np.array(ps.map2cl_spin(qumap=CMB["TQU"]['out'][1:3], spin=2, mask=pmask['100'], lmax=lmaxbin-1, lmax_mask=lmaxbin*2))*1e12 #maps are different scale than processed powerspectra from this' package pipeline, thus *1e12 io.save_data(smica_C_lmin_unsc, io.fh.clmin_smica_path_name)
def apply_ivf(self, dets, tmaps): assert(self.lmax == 1000) n_inv_filts = [] for det in dets: mask_t = qcinv.util.load_map(self.mask_t) bl = dmc.get_bl(self.sim_lib.year, det)[0:self.lmax+1] pxw = hp.pixwin( self.sim_lib.nside )[0:self.lmax+1] # qcinv filtering for temperature dcf = self.lib_dir + "/dense_cache_det_" + det + ".pk" # id preconditioners lmax nside im em tr cache chain_descr = [ [ 2, ["split(dense("+dcf+"), 64, diag_cl)"], 256, 128, 3, 0.0, qcinv.cd_solve.tr_cg, qcinv.cd_solve.cache_mem()], [ 1, ["split(stage(2), 256, diag_cl)"], 512, 256, 3, 0.0, qcinv.cd_solve.tr_cg, qcinv.cd_solve.cache_mem()], [ 0, ["split(stage(1), 512, diag_cl)"], 1000, 512, np.inf, 1.0e-6, qcinv.cd_solve.tr_cg, qcinv.cd_solve.cache_mem()] ] ninv = ( hp.read_map( dmc.get_fname_imap(self.sim_lib.year, det, self.sim_lib.forered), hdu=1, field=1 ) / dmc.sigma0[(self.sim_lib.year, self.sim_lib.forered, 'T')][det]**2 / 1e6 * mask_t ) n_inv_filt = qcinv.opfilt_tt.alm_filter_ninv( ninv, bl*pxw, marge_monopole=True, marge_dipole=True, marge_maps=[] ) n_inv_filts.append( n_inv_filt ) n_inv_filts = qcinv.opfilt_tt_multi_simple.alm_filter_ninv_filts( n_inv_filts, degrade_single=True ) chain = qcinv.multigrid.multigrid_chain( qcinv.opfilt_tt_multi_simple, chain_descr, self.cl, n_inv_filts ) tlm = np.zeros( qcinv.util_alm.lmax2nlm(self.lmax), dtype=np.complex ) chain.solve( tlm, tmaps ) return tlm
def get_maps_cl(frec, fconv=None, lmin=20, delta_ell=40, apodization_degrees=5.): mrec, resid, seenmap = get_maps_residuals(frec, fconv=fconv) sh = np.shape(mrec) print(sh, np.shape(resid)) nbsub = sh[1] ns = hp.npix2nside(sh[2]) from qubic import apodize_mask mymask = apodize_mask(seenmap, apodization_degrees) # Create XPol object from qubic import Xpol lmax = 2 * ns xpol = Xpol(mymask, lmin, lmax, delta_ell) ell_binned = xpol.ell_binned nbins = len(ell_binned) # Pixel window function pw = hp.pixwin(ns) pwb = xpol.bin_spectra(pw[:lmax + 1]) # Calculate all crosses and auto m_autos = np.zeros((nbsub, 6, nbins)) s_autos = np.zeros((nbsub, 6, nbins)) m_cross = np.zeros((nbsub, 6, nbins)) s_cross = np.zeros((nbsub, 6, nbins)) fact = ell_binned * (ell_binned + 1) / 2. / np.pi for isub in range(nbsub): m_autos[isub, :, :], s_autos[isub, :, :], m_cross[isub, :, :], s_cross[isub, :, :] = \ allcross_par(xpol, mrec[:, isub, :, :], silent=False, verbose=0) return mrec, resid, seenmap, ell_binned, m_autos * fact / pwb ** 2, \ s_autos * fact / pwb ** 2, m_cross * fact / pwb ** 2, s_cross * fact / pwb ** 2
def apply_ivf(self, det, tmap, pmap): assert(self.lmax == 1000) mask_t = qcinv.util.load_map(self.mask_t) mask_p = qcinv.util.load_map(self.mask_p) bl = dmc.get_bl(self.sim_lib.year, det)[0:self.lmax+1] pxw = hp.pixwin( self.sim_lib.nside )[0:self.lmax+1] # qcinv filtering for temperature dcf = self.lib_dir + "/dense_cache_det_" + det + ".pk" # id preconditioners lmax nside im em tr cache chain_descr = [ [ 2, ["split(dense("+dcf+"), 64, diag_cl)"], 256, 128, 3, 0.0, qcinv.cd_solve.tr_cg, qcinv.cd_solve.cache_mem()], [ 1, ["split(stage(2), 256, diag_cl)"], 512, 256, 3, 0.0, qcinv.cd_solve.tr_cg, qcinv.cd_solve.cache_mem()], [ 0, ["split(stage(1), 512, diag_cl)"], 1000, 512, np.inf, 1.0e-6, qcinv.cd_solve.tr_cg, qcinv.cd_solve.cache_mem()] ] ninv = ( hp.read_map( dmc.get_fname_iqumap(self.sim_lib.year, det, self.sim_lib.forered), hdu=1, field=3 ) / dmc.sigma0[(self.sim_lib.year, self.sim_lib.forered, 'T')][det]**2 / 1e6 * mask_t ) n_inv_filt = qcinv.opfilt_tt.alm_filter_ninv( ninv, bl*pxw, marge_monopole=True, marge_dipole=True, marge_maps=[] ) chain = qcinv.multigrid.multigrid_chain( qcinv.opfilt_tt, chain_descr, self.cl, n_inv_filt ) tlm = np.zeros( qcinv.util_alm.lmax2nlm(self.lmax), dtype=np.complex ) chain.solve( tlm, tmap ) # simple filtering for polarization. elm, blm = hp.map2alm_spin( (pmap.real * mask_p, pmap.imag * mask_p), 2, lmax=self.lmax ) ftl, fel, fbl = self.get_ftebl(det) hp.almxfl( elm, fel / bl / pxw, inplace=True ) hp.almxfl( blm, fbl / bl / pxw, inplace=True ) return tlm, elm, blm
def masked_cl(ellmax, mask_name, mask_fn, outfits_root, final_maps): #Masked spectra print "Calculating masked C_l" mask = hp.read_map(mask_fn) #0 where holes #RING ordering f_sky = np.sum(mask) / len(mask) print "f_sky =", f_sky masked_maps = [final_maps[0] * mask, final_maps[1] * mask] #(Q*mask,U*mask) T_map = [ np.zeros_like(final_maps[0]), ] masked_cls = hp.anafast(T_map + masked_maps, lmax=ellmax - 1) #(TT,EE,BB,TE,EB,TB) pixrecip = np.concatenate( (np.ones(2), np.reciprocal( hp.pixwin( hp.get_nside(masked_maps[0]), pol=True)[1][2:ellmax]))) #P pixwin #Not defined for l < 2 clscode = ['EE', 'BB', 'EB'] clsidx = [1, 2, 4] for i in xrange(len(clscode)): cl_outfits = outfits_root + '_' + clscode[ i] + 'cls_' + mask_name + '.fits' hp.write_cl(cl_outfits, masked_cls[clsidx[i]] * pixrecip * pixrecip / f_sky) return 0
def get_kbeam(qid, modlmap, sanitize=False, version=None, planck_pixwin=False, **kwargs): dmodel = sints.arrays(qid, 'data_model') season = sints.arrays(qid, 'season') region = sints.arrays(qid, 'region') array = sints.arrays(qid, 'array') freq = sints.arrays(qid, 'freq') dm = sints.models[dmodel]() gfreq = array + "_" + freq if not (is_planck(qid)) else freq if planck_pixwin and (qid in [ 'p01', 'p02', 'p03', 'p04', 'p05', 'p06', 'p07', 'p08' ]): nside = get_nside(qid) pixwin = hp.pixwin(nside=nside, pol=False) ls = np.arange(len(pixwin)) assert pixwin.ndim == 1 assert ls.size in [6144, 3072] pwin = maps.interp(ls, pixwin)(modlmap) else: pwin = 1. return dm.get_beam(modlmap, season=season, patch=region, array=gfreq, kind='normalized', sanitize=sanitize, version=version, **kwargs) * pwin
def worker_power(map, lmax): logger = logging.getLogger(__name__) factor = 1 - np.sum(map == hp.UNSEEN) / len(map) logger.info("processing map ({:.0f} deg^2)".format(4*factor*np.pi*(180/np.pi)**2)) return hp.anafast(map, lmax=lmax) / hp.pixwin(hp.npix2nside(len(map)))[:lmax + 1] ** 2 / factor
def compute_ds_dcb_line_par(map,mask,ellmin,ellmax,fwhmrad,nthreads): npix=np.size(np.where(mask)) ellbins=ellmin.size ellval=(ellmin+ellmax)/2 print(' Calculating dS/dCb') pixwin=hp.pixwin(hp.npix2nside(len(map))) maxell=np.max(ellmax) ll=np.arange(int(maxell)+1) bl=np.exp(-0.5*ll**2*(fwhmrad/2.35)**2) nside=np.sqrt(map.size/12) iprings=np.arange(map.size) vecs=hp.pix2vec(int(nside),iprings[mask]) cosangles=np.dot(np.transpose(vecs),vecs) nshots=npix//nthreads+1 ntot=nshots*nthreads indices=np.arange(ntot) indices[indices>npix-1]=npix-1 lines=np.reshape(indices,(nshots,nthreads)) ds_dcb=np.zeros((ellbins,npix,npix)) for num in np.arange(nshots): # initiate multithreading tasks=multiprocessing.JoinableQueue() results = multiprocessing.Queue() # start consumers num_consumers = nthreads #print 'Creating %d consumers' % num_consumers consumers = [ Consumer(tasks, results) for i in xrange(num_consumers) ] for w in consumers: w.start() # Enqueue jobs num_jobs = nthreads for i in np.arange(num_jobs): #print('Calling task ',lines[num,i]) tasks.put(Task(cosangles[lines[num,i],:],lines[num,i],ll,bl,pixwin,ellmin,ellmax,ellval)) # poison them when finished for i in np.arange(num_jobs): #print 'Killing Task %d ' % i tasks.put(None) #print('****** Now ready to join tasks ******') tasks.join() #print('###### Tasks were joined ######') while num_jobs: #print('Getting result for task %d' % num_jobs) result = results.get() bla=result[0] num=result[1] ds_dcb[:,num,num:]=bla[:,num:] ds_dcb[:,num:,num]=bla[:,num:] num_jobs -= 1 return(ds_dcb)
def get_pixwin_correction(self, nside): pw = hp.pixwin(nside, pol=True, lmax=self.lmax) pw = [pw[0][:self.lmax + 1], pw[1][:self.lmax + 1]] ell_binned, b = self.get_binning(nside) pwb = 2 * np.pi / (ell_binned * (ell_binned + 1)) * b.bin_cell(np.array(pw)) return pwb
def get_ftl_eff( lmax, year, nside, det, forered, cl, mask_t ): noiseT_uK_arcmin = dmc.get_nlev_t_uK_arcmin(year, det, forered, mask_t) bl = dmc.get_bl( year, det)[0:lmax+1] pxw = hp.pixwin( nside )[0:lmax+1] ftl = 1.0 / (cl.cltt[0:lmax+1] + (noiseT_uK_arcmin * np.pi/180./60.)**2 / (bl * pxw)**2); ftl[0:2] = 0.0 return ftl
def get_pixwin_correction(self, nside): """Return the binned pixel window function multiplied by 2pi/(l(l+1)) for temperature and polarization """ pw = list(hp.pixwin(nside, pol=True, lmax=self.lmax)) ell_binned, b = self.get_binning(nside) pwb = 2 * np.pi / (ell_binned * (ell_binned + 1)) * b.bin_cell(np.array(pw)) return pwb
def build_cov(cl, nside, mask=None, tree_depth=0, lmax=None, apply_pixwin=False, ninterp=10000, log=False, shift=None): tree_depth = 2**(tree_depth) if mask is None: mask = np.ones(hp.nside2npix(nside), dtype=bool) if lmax is not None and lmax > len(cl) - 1: lmax = len(cl) - 1 cl = cl[:lmax + 1] if apply_pixwin: pw = hp.pixwin(nside * tree_depth, lmax=lmax) cl = cl[:len(pw)] * (pw**2) npix = int(mask.sum()) mask_inds = hp.ring2nest(nside, np.arange(hp.nside2npix(nside))[mask]) thetas = np.linspace(0, np.pi, ninterp) xis = cl2xi_theta(cl, thetas) exe_path = os.path.join(os.path.dirname(__file__), 'healcov') proc = subprocess.Popen(exe_path, stdin=subprocess.PIPE, stdout=subprocess.PIPE) proc.stdin.write(nside.to_bytes(8, 'little')) proc.stdin.write(npix.to_bytes(8, 'little')) proc.stdin.write(mask_inds.tobytes()) proc.stdin.write(tree_depth.to_bytes(8, 'little')) proc.stdin.write(ninterp.to_bytes(8, 'little')) proc.stdin.write(thetas.tobytes()) proc.stdin.write(xis.tobytes()) proc.stdin.close() cov = np.frombuffer(proc.stdout.read()).reshape([npix, npix]) info = proc.wait() if info != 0: raise Exception() if log is True: cov = np.log(cov / (shift**2) + 1) return cov
def apply_ivf(self, det, tmap, pmap): mask_t = qcinv.util.load_map(self.mask_t) bl = self.bl pxw = hp.pixwin(self.nside)[0:self.lmax+1] tlm = hp.map2alm( tmap * mask_t, lmax=self.lmax, iter=0, regression=False ) hp.almxfl( tlm, self.ftl / bl / pxw, inplace=True ) return tlm
def get_ftebl_eff( lmax, year, nside, det, forered, cl, mask_t, mask_p ): noiseT_uK_arcmin, noiseP_uK_arcmin = dmc.get_nlev_tp_uK_arcmin(year, det, forered, mask_t, mask_p) bl = dmc.get_bl( year, det)[0:lmax+1] pxw = hp.pixwin( nside )[0:lmax+1] ftl = 1.0 / (cl.cltt[0:lmax+1] + (noiseT_uK_arcmin * np.pi/180./60.)**2 / (bl * pxw)**2); ftl[0:2] = 0.0 fel = 1.0 / (cl.clee[0:lmax+1] + (noiseP_uK_arcmin * np.pi/180./60.)**2 / (bl * pxw)**2); fel[0:2] = 0.0 fbl = 1.0 / (cl.clbb[0:lmax+1] + (noiseP_uK_arcmin * np.pi/180./60.)**2 / (bl * pxw)**2); fbl[0:2] = 0.0 return ftl, fel, fbl
def apply_pixwin(data: np.array, freqcomb, nside) -> Dict: """Applies Pixel Windowfunction with nside specified Args: data (Dict): powerspectra with spectrum and frequency-combinations in the columns Returns: Dict: Pixel Windowfunction applied Powerspectra with spectrum and frequency-combinations in the columns """ for fidx, freql in enumerate(data): for sidx, specl in enumerate(freql): lmaxp1 = data.shape[-1] freqc = freqcomb[fidx] if int(freqc.split("-")[0]) < 100: data[fidx, sidx] /= hp.pixwin(nside[0], lmax=lmaxp1 - 1) else: data[fidx, sidx] /= hp.pixwin(nside[1], lmax=lmaxp1 - 1) if int(freqc.split("-")[1]) < 100: data[fidx, sidx] /= hp.pixwin(nside[0], lmax=lmaxp1 - 1) else: data[fidx, sidx] /= hp.pixwin(nside[1], lmax=lmaxp1 - 1) return data
def get_pixel_window_function(self, nside, lmax, pol=True): """ Get the pixel window function assigned to self.spectra for the given nside """ pixel_window_np = hp.pixwin(nside=nside, pol=pol) if pol: self.spectra['TT'] = pixel_window_np[0] self.spectra['EE'] = pixel_window_np[1] self.spectra['BB'] = pixel_window_np[1] self.spectra['TE'] = pixel_window_np[1] else: self.spectra['TT'] = pixel_window_np self.truncate(after=lmax)
def apply_ivf(self, det, tmap): mask_t = qcinv.util.load_map(self.mask_t) tlm = hp.map2alm( tmap * mask_t, lmax=self.lmax, iter=0, regression=False ) bl = dmc.get_bl(self.sim_lib.year, det)[0:self.lmax+1] pxw = hp.pixwin( self.sim_lib.nside )[0:self.lmax+1] ftl = self.get_ftl(det) hp.almxfl( tlm, ftl / bl / pxw, inplace=True ) return tlm
def load_chan_realization_alms(chan_fmt, chani, reali, lmax): ''' Loads a channel and computes the spherical harmonic expansion of the map. For polarized channels (nu < 545 GHz), returns a 3-tuple of (T, E, B) alms, while for unpolarized channels (545 and 857 GHz) it returns T. All alms are in healpix order. Already adjusts for the pixel window function. ''' fname = chan_fmt.format(chan=planck_freqs[chani], real=reali) if chani < 7: maps = 1e6*hp.read_map(fname, field=(0, 1, 2)) nside = hp.npix2nside(maps[0].size) alms = hp.map2alm(maps, pol=True, lmax=lmax) # First adjust T by pwT, then E&B by pwE pwT, pwE = hp.pixwin(nside=nside, pol=True, lmax=lmax) alms[0] = hp.almxfl(alms[0], 1 / pwT) # Set the pwE=0 \ells to 0 pol_adj = np.zeros_like(pwE) pol_adj[pwE != 0] = 1 / pwE[pwE != 0] alms[1] = hp.almxfl(alms[1], pol_adj) alms[2] = hp.almxfl(alms[2], pol_adj) return alms factor = 1e6 / cfact_545_857[chani-7] map = factor * hp.read_map(fname) nside = hp.npix2nside(map.size) alms = np.zeros((3, hp.Alm.getsize(lmax=lmax)), dtype=np.complex) alms[0] = hp.map2alm(map, pol=False, lmax=lmax) alms[0] = hp.almxfl(alms[0], 1 / hp.pixwin(nside=nside, lmax=lmax)) return alms
def gen_lgmca_like(lgmca_file, cov_file, lmin=70, lmax=2000, delta_ell=30): # Load covariance cov = np.loadtxt(cov_file) # Add l (l + 1) factor (i.e. convert cl -> dl) ells = np.arange(lmax + 1) ll1 = ells * (ells + 1) / (2 * np.pi) # Load data vector dls = hp.read_cl(lgmca_file)[:lmax + 1] / (getbeam(5, lmax) * hp.pixwin(2048, lmax=lmax))**2 # 1e12 is K^2 -> \mu K^2 return bare_gen_like(1e12 * ll1 * dls, cov, lmin=lmin, lmax=lmax, delta_ell=delta_ell)
def apply_ivf(self, det, tmap, pmap): mask_t = qcinv.util.load_map(self.mask_t) mask_p = qcinv.util.load_map(self.mask_p) bl = self.bl pxw = hp.pixwin(self.nside)[0:self.lmax+1] tlm = hp.map2alm( tmap * mask_t, lmax=self.lmax, iter=0, regression=False ) elm, blm = hp.map2alm_spin( (pmap.real * mask_p, pmap.imag * mask_p), 2, lmax=self.lmax ) hp.almxfl( tlm, self.ftl / bl / pxw, inplace=True ) hp.almxfl( elm, self.fel / bl / pxw, inplace=True ) hp.almxfl( blm, self.fbl / bl / pxw, inplace=True ) return tlm, elm, blm
def get_alm_maps(pixel_maps, fwhms, resol_correction=False, ref_fwhm=0, pixwin_correction=False, verbose=False): """ Compute alm maps from pixel maps and format them for FgBuster. """ sh = np.shape(pixel_maps) nside = hp.npix2nside(sh[2]) n = sh[0] lmax = 2 * nside + 1 ell = np.arange(start=0, stop=lmax + 1) ref_sigma_rad = np.deg2rad(ref_fwhm) / 2.355 #ref_fl = np.exp(- 0.5 * np.square(ref_sigma_rad * ell)) ref_fl = hp.gauss_beam(np.deg2rad(ref_fwhm), lmax=lmax) if verbose: print('In get_alm_maps: FWHM = ', fwhms) beam_sigmas_rad = np.deg2rad(fwhms) / (2 * np.sqrt(2 * np.log(2))) pixwin = hp.pixwin(nside, lmax=lmax) if pixwin_correction else np.ones(lmax + 1) # compute maps #figure() alm_maps = None for f in range(n): alms = hp.map2alm(pixel_maps[f], lmax=lmax, pol=True) correction = None if f == 0: sh = np.shape(alms) alm_maps = np.empty((n, sh[0], 2 * sh[1])) if resol_correction: print('Applying Resol Correction') #gauss_fl = np.exp(- 0.5 * np.square(beam_sigmas_rad[f] * ell)) gauss_fl = hp.gauss_beam(np.deg2rad(fwhms[f]), lmax=lmax) correction = ref_fl / gauss_fl / pixwin #plot(correction, label='freq {}'.format(f)) else: print('No Resol Correction applied') for i, t in enumerate(alms): alm_maps[f, i] = format_alms( hp.almxfl(t, correction) if resol_correction else t) #legend() #title('Bl ratio in get_alm_maps') return alm_maps
def get_xpol(seenmap, ns, lmin=20, delta_ell=20, apodization_degrees=5.): """ Returns a Xpoll object to get spectra, the bin used and the pixel window function. """ # Create a mask mymask = apodize_mask(seenmap, apodization_degrees) # Create XPol object lmax = 2 * ns xpol = Xpol(mymask, lmin, lmax, delta_ell) ell_binned = xpol.ell_binned # Pixel window function pw = hp.pixwin(ns) pwb = xpol.bin_spectra(pw[:lmax + 1]) return xpol, ell_binned, pwb
def get_ftl(self, detstr): dets = self.detstr2dets(detstr) clnn = np.zeros(self.lmax+1) for det in dets: noiseT_uK_arcmin = dmc.get_nlev_t_uK_arcmin(self.sim_lib.year, det, self.sim_lib.forered, qcinv.util.load_map(self.mask_t)) bl = dmc.get_bl( self.sim_lib.year, det)[0:self.lmax+1] pxw = hp.pixwin( self.sim_lib.nside )[0:self.lmax+1] clnn += (bl * pxw)**2 / (noiseT_uK_arcmin * np.pi/180./60.)**2 clnn = 1./clnn ftl = 1.0 / (self.cl.cltt[0:self.lmax+1] + clnn); ftl[0:2] = 0.0 return ftl
def apply_ivf(self, det, tmap, pmap): mask_t = qcinv.util.load_map(self.mask_t) mask_p = qcinv.util.load_map(self.mask_p) tlm = hp.map2alm( tmap * mask_t, lmax=self.lmax, iter=0, regression=False ) elm, blm = hp.map2alm_spin( (pmap.real * mask_p, pmap.imag * mask_p), 2, lmax=self.lmax ) bl = dmc.get_bl(self.sim_lib.year, det)[0:self.lmax+1] pxw = hp.pixwin( self.sim_lib.nside )[0:self.lmax+1] ftl, fel, fbl = self.get_ftebl(det) hp.almxfl( tlm, ftl / bl / pxw, inplace=True ) hp.almxfl( elm, fel / bl / pxw, inplace=True ) hp.almxfl( blm, fbl / bl / pxw, inplace=True ) return tlm, elm, blm
def FilterMap(pixmap, freq, mask=None, lmax=None, pixelwindow=False): if mask is None: mask = np.ones_like(pixmap) else: assert hp.isnpixok(mask.size) Fl = GetFl(pixmap,freq, mask=mask, lmax=lmax) alm_fname='alm_%s.fits'%freq if not os.path.exists(alm_fname): print "computing alms" alm = hp.map2alm(pixmap, lmax=lmax) hp.write_alm(alm_fname,alm) else: print "reading alms",alm_fname alm = hp.read_alm(alm_fname) if pixelwindow: print "Correcting alms for pixelwindow" pl=hp.pixwin(hp.npix2nside(pixmap.size))[:lmax+1] else: pl=np.ones(len(Fl)) return hp.alm2map(hp.almxfl(alm, Fl/pl), hp.npix2nside(pixmap.size), lmax=lmax), Fl
def GetSpectra(map1, mask, lmax, map2=None, pixwin=True, beam=None, flat=True): if pixwin: pl = hp.pixwin(hp.npix2nside(len(map1)))[:lmax + 1] else: pl = np.ones(lmax + 1) if beam is not None: bl = hp.gauss_beam(np.radians(beam / 60.), lmax=lmax) else: bl = np.ones(lmax + 1) fsky = np.mean(mask**2) cl = hp.anafast(map1 * mask, map2=map2, lmax=lmax) if flat: l = np.arange(lmax + 1) fact = l * (l + 1) / 2. / np.pi else: fact = 1. return cl / fsky / bl**2 / pl**2 * fact
def simulate(self, det, idx): assert(det == '') tlm = self.sim_teblm_cmb.get_sim_tlm(idx) elm = self.sim_teblm_cmb.get_sim_elm(idx) blm = self.sim_teblm_cmb.get_sim_blm(idx) beam = self.bl[0:self.lmax+1] * hp.pixwin(self.nside)[0:self.lmax+1] hp.almxfl(tlm, beam, inplace=True) hp.almxfl(elm, beam, inplace=True) hp.almxfl(blm, beam, inplace=True) tmap = hp.alm2map(tlm, self.nside) qmap, umap = hp.alm2map_spin( (elm, blm), self.nside, 2, lmax=self.lmax ) npix = 12*self.nside**2 tmap += np.random.standard_normal(npix) * (self.noiseT_uK_arcmin * np.sqrt(npix / 4. / np.pi) * np.pi / 180. / 60.) qmap += np.random.standard_normal(npix) * (self.noiseP_uK_arcmin * np.sqrt(npix / 4. / np.pi) * np.pi / 180. / 60.) umap += np.random.standard_normal(npix) * (self.noiseP_uK_arcmin * np.sqrt(npix / 4. / np.pi) * np.pi / 180. / 60.) return tmap, qmap + 1.j*umap
def analworker(i): print "This is analysis worker starting for map", i + 1, "/", nmaps QU_maps = hp.read_map(fits[i], field=(1, 2)) #(Q,U) pixrecip = np.concatenate((np.ones(2), np.reciprocal( hp.pixwin(hp.get_nside(QU_maps[0]), pol=True)[1][2:smoothing_lmax]) )) #P pixwin #Not defined for l < 2 pm_alms = hp.map2alm_spin(QU_maps, spin, lmax=smoothing_lmax - 1) del QU_maps hp.almxfl(pm_alms[0], pixrecip, inplace=True) #Correcting for pixwin hp.almxfl(pm_alms[1], pixrecip, inplace=True) #Correcting for pixwin #Reorder to S2LET alms pm_alms[0] = ps.lm_hp2lm(pm_alms[0], smoothing_lmax) pm_alms[1] = ps.lm_hp2lm(pm_alms[1], smoothing_lmax) P_alms = -1. * pm_alms[0] - 1.j * pm_alms[1] #CHECK THIS IS CORRECT! del pm_alms wav_maps, scal_maps = ps.analysis_lm2wav_manualtiling( P_alms, smoothing_lmax, ndir, spin, scal_tiles, wav_tiles.T.ravel(), scal_bandlims, wav_bandlims) del P_alms np.save(scal_outfits[i], scal_maps) del scal_maps #Splitting up output wavelet maps offset = 0 for j in xrange(jmax + 1): for n in xrange(ndir): bandlim = wav_bandlims[j] nelem = bandlim * (2. * bandlim - 1.) wav_outfits = wav_outfits_root[i] + '_j' + str(j) + '_n' + str( n + 1) + '.npy' np.save(wav_outfits, wav_maps[offset:offset + nelem]) offset += nelem del wav_maps return 0
def estimate_cl(sky_map, lmax, binary_mask=None, beam_fwhm=0.0, pixwin=False): """ Estimates the TT, EE, BB and TE auto/cross power spectrum. The sky map(s) will be masked by the binary mask provided. beam_fwhm input is in arcmins. It is converted internally to radians. If pixwin is True, the spectra is debeamed with the pixel window function. """ if sky_map.ndim == 1: pol = False else: pol = True if binary_mask is not None: sky_map_masked = mu.mask_map(sky_map, binary_mask=binary_mask, pol=pol) f_sky = mu.get_sky_fraction(binary_mask) else: sky_map_masked = sky_map if hp.maptype == 0: f_sky = 1.0 else: f_sky = np.aray([1.0, 1.0, 1.0]) Bl = hp.gauss_beam(fwhm=np.radians(fwhm / 60.0), lmax=lmax, pol=pol) if pixwin: pixel_window = hp.pixwin(hp.get_nside(sky_map), pol) if pol: spectra = hp.anafast(sky_map_masked.filled(), lmax=lmax)[:4] spectra /= f_sky[1] * Bl.T**2 if pixwin: spectra[0] /= pixwin[0]**2 spectra[1:] /= pixwin[1]**2 else: spectra = hp.anafast(sky_map_masked.filled(), lmax=lmax) spectra /= f_sky * Bl**2 if pixwin: spectra /= pixel_window**2 return spectra
def compute_ds_dcb(map,mask,ellmin,ellmax,fwhmrad): npix=np.size(np.where(mask)) maxell=np.max(ellmax) ellbins=ellmin.size ellval=(ellmin+ellmax)/2 print(' Calculating dS/dCb') pixwin=hp.pixwin(hp.npix2nside(len(map)))[0:maxell+1] ll=np.arange(int(maxell)+1) bl=np.exp(-0.5*ll**2*(fwhmrad/2.35)**2) nside=np.sqrt(map.size/12) iprings=np.arange(map.size) vecs=hp.pix2vec(int(nside),iprings[mask]) cosangles=np.dot(np.transpose(vecs),vecs) ds_dcb=np.zeros((ellbins,npix,npix)) for i in np.arange(npix): bla,aa=do_a_line(cosangles[i,:],i,ll,bl,pixwin,ellmin,ellmax,ellval) ds_dcb[:,i,i:]=bla[:,i:] ds_dcb[:,i:,i]=bla[:,i:] return(ds_dcb)
def apply_ivf(self, det, tmap, pmap): assert(self.lmax == 1000) mask_t = qcinv.util.load_map(self.mask_t) mask_p = qcinv.util.load_map(self.mask_p) bl = dmc.get_bl(self.sim_lib.year, det)[0:self.lmax+1] pxw = hp.pixwin( self.sim_lib.nside )[0:self.lmax+1] # qcinv filtering for temperature dcf = self.lib_dir + "/dense_cache_det_" + det + ".pk" # id preconditioners lmax nside im em tr cache chain_descr = [ [ 2, ["split(dense("+dcf+"), 32, diag_cl)"], 256, 128, 3, 0.0, qcinv.cd_solve.tr_cg, qcinv.cd_solve.cache_mem()], [ 1, ["split(stage(2), 256, diag_cl)"], 512, 256, 3, 0.0, qcinv.cd_solve.tr_cg, qcinv.cd_solve.cache_mem()], [ 0, ["split(stage(1), 512, diag_cl)"], 1000, 512, np.inf, 1.0e-6, qcinv.cd_solve.tr_cg, qcinv.cd_solve.cache_mem()] ] # temperature noise covariance ninv_t = ( hp.read_map( dmc.get_fname_iqumap(self.sim_lib.year, det, self.sim_lib.forered), hdu=1, field=3 ) / dmc.sigma0[(self.sim_lib.year, self.sim_lib.forered, 'T')][det]**2 / 1e6 * mask_t ) # polarization noise covariance tfname = dmc.get_fname_iqumap(self.sim_lib.year, det, False) #NOTE: Nobs counts taken from non-FG reduced maps. n_inv_qq = hp.read_map( tfname, hdu=2, field=1 ) / (dmc.sigma0[(self.sim_lib.year, self.sim_lib.forered, 'P')][det] * 1.e3)**2 * mask_p n_inv_qu = hp.read_map( tfname, hdu=2, field=2 ) / (dmc.sigma0[(self.sim_lib.year, self.sim_lib.forered, 'P')][det] * 1.e3)**2 * mask_p n_inv_uu = hp.read_map( tfname, hdu=2, field=3 ) / (dmc.sigma0[(self.sim_lib.year, self.sim_lib.forered, 'P')][det] * 1.e3)**2 * mask_p # construct filter if self.diag_n == True: n_inv_filt = qcinv.opfilt_tp.alm_filter_ninv( [ninv_t, 0.5*(n_inv_qq + n_inv_uu)], bl*pxw, marge_monopole=True, marge_dipole=True ) else: n_inv_filt = qcinv.opfilt_tp.alm_filter_ninv( [ninv_t, n_inv_qq, n_inv_qu, n_inv_uu], bl*pxw, marge_monopole=True, marge_dipole=True ) chain = qcinv.multigrid.multigrid_chain( qcinv.opfilt_tp, chain_descr, self.cl, n_inv_filt ) alm = qcinv.opfilt_tp.teblm( [np.zeros( qcinv.util_alm.lmax2nlm(self.lmax), dtype=np.complex ) for i in xrange(0,3)] ) chain.solve( alm, [tmap, pmap.real, pmap.imag] ) return alm.tlm, alm.elm, alm.blm
def Clgg(gal_map, gal_mask, N_side, lmax, normalized=False): # CMASS map, number counts. Should convert to delta_n/n gal = hp.read_map(gal_map) if gal_mask != False: gal_mask = hp.read_map(gal_mask) gal_mask = hp.ud_grade(gal_mask, N_side) # Bring to same resolution else: gal_mask = np.ones_like(gal) # The mask is product of PLANCK lensing and CMASS masks mask = gal_mask fsky = np.sum(mask) * 1. / len(mask) # Apply the mask. Same for all, for now masked_pl = mask masked_gal = gal * mask # Compute shot noise mean_gal = np.sum(masked_gal) / np.sum(mask) A_per_healpix = (4 * np.pi) / (12 * N_side**2) shot_noise = (mean_gal / A_per_healpix)**(-1.) # Normalized tells you if the map has already been normalized or is a raw map of counts if normalized == False: mean_gal = np.sum(masked_gal) / np.sum(mask) masked_gal_dn = masked_gal / mean_gal - 1. masked_gal_dn = mask * masked_gal_dn else: masked_gal_dn = masked_gal Clgg = hp.anafast(masked_gal_dn, lmax=lmax) ls = np.arange(len(Clgg)) pixwinf = hp.pixwin(N_side)[0:len(Clgg)] Clgg = Clgg / (pixwinf**2) Clgg = Clgg / fsky return ls, Clgg, fsky, shot_noise
def load_chan_realization(chan_fmt, chani, reali, lmax, doppler=True, beam=r2_channel_beams): ''' Loads a single array of `D_\ells` from an FFP8 fits file, adjusting for the observation beam and (possibly) for doppler modulation. Returns the TT power spectrum for `\ell = 0` to `\ell = lmax`, inclusive. chan_fmt: Path to the .fits file. chani: Planck observation channel, 0 <= chani < 9. I.e. 0 means 30 GHz. reali: The realization number. doppler: Whether to remove doppler modulation from the realization. returns: An array of `D_\ell = \ell (\ell + 1) / (2 \pi) C_\ell`. A `numpy.array` of `dtype == np.double` and size `lmax + 1`. ''' m = hp.read_map(chan_fmt.format(freq=planck_freqs[chani], real=reali)) if chani >= 7: m /= cfact_545_857[chani - 7] if doppler: m = doppler_adjust(m, chani) cls = hp.anafast(1e6*m, lmax=lmax) ells = np.arange(lmax + 1) ll1 = ells * (ells + 1) / (2 * np.pi) nside = hp.npix2nside(m.size) beam = beam[chani, :lmax + 1] * hp.pixwin(nside)[:lmax + 1] beam_msk = beam != 0 # Avoid divide-by-zero warnings adj_dls = np.zeros_like(cls) adj_dls[beam_msk] = (ll1 * cls)[beam_msk] / beam[beam_msk]**2 return adj_dls
), 'Set env. variable PLENS to a writeable folder' TEMP = os.path.join(os.environ['PLENS'], 'temp', 'anisofilt_example') cls_path = os.path.join(os.path.dirname(os.path.abspath(plancklens.__file__)), 'data', 'cls') #--- definition of simulation and inverse-variance filtered simulation libraries: lmax_ivf = 2048 lmin_ivf = 100 # We will use in the QE only CMB modes between lmin_ivf and lmax_ivf lmax_qlm = 4096 # We will calculate lensing estimates until multipole lmax_qlm. nside = 2048 # Healpix resolution of the data and sims. nlev_t = 35. # Filtering noise level in temperature (here also used for the noise simulations generation). nlev_p = 55. # Filtering noise level in polarization (here also used for the noise simulations generation). nsims = 300 # Total number of simulations to consider. transf = hp.gauss_beam(5. / 60. / 180. * np.pi, lmax=lmax_ivf) * hp.pixwin(nside)[:lmax_ivf + 1] #: CMB transfer function. Here a 5' Gaussian beam and healpix pixel window function. cl_unl = utils.camb_clfile( os.path.join(cls_path, 'FFP10_wdipole_lenspotentialCls.dat')) cl_len = utils.camb_clfile( os.path.join(cls_path, 'FFP10_wdipole_lensedCls.dat')) #: Fiducial unlensed and lensed power spectra used for the analysis. cl_weight = utils.camb_clfile( os.path.join(cls_path, 'FFP10_wdipole_lensedCls.dat')) cl_weight['bb'] *= 0. #: CMB spectra entering the QE weights (the spectra multplying the inverse-variance filtered maps in the QE legs) libdir_pixphas = os.path.join(TEMP, 'pix_phas_nside%s' % nside) pix_phas = phas.pix_lib_phas(libdir_pixphas, 3, (hp.nside2npix(nside), ))
#Smoothing W-beam for 're-convolving' '''smoothbeam = cp.deepcopy(combbeam[4]) smoothbegin = 875 smoothend = 1024 smoothbeam[smoothbegin:smoothend+1] = smoothfunc(np.arange(smoothbegin,smoothend+1),smoothbeam[smoothbegin])''' #For Planck, effective beam is 5' FWHM gaussian smoothbeam = hp.gauss_beam(mh.radians(5. / 60.), lmax=ellmax - 1) '''smoothbegin = 3400 smoothend = ellmax-1 smoothbeam[smoothbegin:smoothend+1] = smoothfunc(np.arange(smoothbegin,smoothend+1),smoothbeam[smoothbegin]) smoothbeam_orig = hp.gauss_beam(mh.radians(5./60.),lmax=ellmax-1)''' #Take reciprocal of beam transfer functions #pixrecip = np.reciprocal(hp.pixwin(hp.get_nside(maps))[:ellmax+1]) #TESTING pixwin pixrecip1024 = np.reciprocal( hp.pixwin(1024)[:ellmax]) #30 & 44 GHz at Nside=1024 pixrecip2048 = np.reciprocal( hp.pixwin(2048)[:ellmax]) #70 & HFI @ Nside=2048 #Not 70 for MC recipcombbeam = np.reciprocal(rawbeam) #combbeam) #Thresholding of deconvolved beams to 1/b_l <= 1000 recipcombbeam[recipcombbeam > 1000] = 1000 deconbeam = [None] * len(recipcombbeam) for i in xrange(2): #len(recipcombbeam)): deconbeam[i] = recipcombbeam[ i] * smoothbeam * pixrecip1024 #Deconvolving to tapered 5' gaussian beam for i in xrange(2, len(recipcombbeam)): deconbeam[i] = recipcombbeam[ i] * smoothbeam * pixrecip2048 #Deconvolving to tapered 5' gaussian beam deconbeam = np.array(deconbeam)
clf() plot(theell, sigcosvardee / samplevar_th_ee, 'r', label = 'EE: MC / Knox') plot(theell, sigcosvardee / deltacl_ee_like, 'r--', label = 'EE: MC / Likelihood') plot(theell, sigcosvardbb / samplevar_th_bb, 'b', label = 'BB: MC / Knox') plot(theell, sigcosvardbb / deltacl_bb_like, 'b--', label = 'BB: MC / Likelihood') ylim(0,2) legend(numpoints=1) ##### Noise variance signoisedee = sigdee-sigdee[0,:] signoisedbb = sigdbb-sigdbb[0,:] pixwin=hp.pixwin(nside)[0:max(ll)+1] omega = 4* pi * fsky fact = 2 * np.interp(theell, ell, np.sqrt(2./((2*ell+1)*fsky))) / np.sqrt(ellmax-ellmin) * theell * (theell +1) / (2*pi) / np.interp(theell, ll,bl**2*pixwin**2) * omega clf() subplot(1,2,1) for i in xrange(4, len(allvalnoise)): plot(theell, signoisedee[i,:], label='$\sigma$ = {}'.format(allvalnoise[i]), color=cm.jet(allvalnoise[i]/max(allvalnoise))) plot(theell, (fact * allvalnoise[i]**2)/100, '--', color=cm.jet(allvalnoise[i]/max(allvalnoise))) legend(fontsize=8, loc='upper left') yscale('log') subplot(1,2,2) for i in xrange(4, len(allvalnoise)): plot(theell, signoisedbb[i,:], label='$\sigma$ = {}'.format(allvalnoise[i]), color=cm.jet(allvalnoise[i]/max(allvalnoise))) plot(theell, (fact * allvalnoise[i]**2)/1000, '--', color=cm.jet(allvalnoise[i]/max(allvalnoise)))
def test(useCLASS=1, useLensing=1, classCamb=1, nSims=1000, lmax=100, lmin=2, newSMICA=False, newDeg=False, suppressC2=False, suppFactor=0.23, filterC2=False, filtFacLow=0.1, filtFacHigh=0.2, R1=False): """ code for testing the other functions in this module Inputs: useCLASS: set to 1 to use CLASS, 0 to use CAMB CLASS Cl has early/late split at z=50 CAMB Cl has ISWin/out split: ISWin: 0.4<z<0.75, ISWout: the rest Note: CAMB results include primary in ISWin and ISWout (not as intended) default: 1 useLensing: set to 1 to use lensed Cl, 0 for non-lensed default: 1 classCamb: if 1: use the CAMB format of CLASS output, if 0: use CLASS format Note: parameter not used if useCLASS = 0 default: 1 nSims: the number of simulations to do for ensemble default: 1000 lmax: the highest l to include in Legendre transforms default: 100 lmin: the lowest l to include in S_{1/2} = CIC calculations default: 2 newSMICA: set to True to recalculate SMICA results default: False newDeg: set to True to recalculate map and mask degredations (only if newSMICA is also True) default: False suppressC2: set to True to suppress theoretical C_2 (quadrupole) by suppFactor before creating a_lm.s Default: False suppFactor: multiplies C_2 if suppressC2 is True Default: 0.23 # from Tegmark et. al. 2003, figure 13 (WMAP) filterC2 : set to true to filter simulated CMBs after spice calculates cut sky C_l. Sims will pass filter if C_2 * filtFacLow < C_2^sim < C_2 * filtFacHigh. Default: False filtFacLow,filtFacHigh: defines C_2 range for passing simulated CMBs Default: 0.1,0.2 R1: set to True to use SMICA and Mask R1. Otherwise, R2 used. Only affects calculation of newly degraded map. Default: False """ # load data ell, fullCl, primCl, lateCl, crossCl = gcp.loadCls(useCLASS=useCLASS, useLensing=useLensing, classCamb=classCamb) # fill beginning with zeros startEll = int(ell[0]) ell = np.append(np.arange(startEll), ell) fullCl = np.append(np.zeros(startEll), fullCl) primCl = np.append(np.zeros(startEll), primCl) lateCl = np.append(np.zeros(startEll), lateCl) crossCl = np.append(np.zeros(startEll), crossCl) # suppress C_2 to see what happens in enesmble if suppressC2: fullCl[2] *= suppFactor primCl[2] *= suppFactor lateCl[2] *= suppFactor crossCl[2] *= suppFactor conv = ell * (ell + 1) / (2 * np.pi) #print ell,conv #ell[0]=2.0 """ # verify statistical properties of alm realizations nSims = 1000 lmax = 100 Clprim_sum = np.zeros(lmax+1) Cllate_sum = np.zeros(lmax+1) Clcros_sum = np.zeros(lmax+1) # prim x late for nSim in range(nSims): print 'starting sim ',nSim+1, ' of ',nSims #alm_prim,alm_late = getAlms(A_lij,lmax=lmax) #AKW method defunct # see if synalm can do it alm_prim,alm_late = hp.synalm((primCl,lateCl,crossCl),lmax=lmax,new=True) Clprim_sum = Clprim_sum + hp.alm2cl(alm_prim) Cllate_sum = Cllate_sum + hp.alm2cl(alm_late) Clcros_sum = Clcros_sum + hp.alm2cl(alm_prim,alm_late) Cl_prim_avg = Clprim_sum/nSims Cl_late_avg = Cllate_sum/nSims Cl_cros_avg = Clcros_sum/nSims doPlot = True if doPlot: plt.plot(ell[:lmax+1],Cl_prim_avg*conv[:lmax+1]) plt.plot(ell[:lmax+1],primCl[:lmax+1]*conv[:lmax+1]) plt.title('primary') plt.ylabel('D_l') plt.show() plt.plot(ell[:lmax+1],Cl_late_avg*conv[:lmax+1]) plt.plot(ell[:lmax+1],lateCl[:lmax+1]*conv[:lmax+1]) plt.title('late') plt.ylabel('D_l') plt.show() plt.plot(ell[:lmax+1],Cl_cros_avg*conv[:lmax+1]) plt.plot(ell[:lmax+1],crossCl[:lmax+1]*conv[:lmax+1]) plt.title('cross') plt.ylabel('D_l') plt.show() """ # get covariances from SMICA map and mask theta_i = 0.0 #degrees theta_f = 180.0 #degrees nSteps = 1800 #lmax = 100 """ # don't want anafast after all # get unmasked and masked SMICA covariances # note: getSMICA uses linspace in theta for thetaArray #newSMICA = False#True thetaArray2, C_SMICA, C_SMICAmasked, S_SMICAnomask, S_SMICAmasked = \ getSMICA(theta_i=theta_i,theta_f=theta_f,nSteps=nSteps,lmax=lmax,lmin=lmin, newSMICA=newSMICA,newDeg=newDeg,useSPICE=False,R1=R1) print '' print 'S_{1/2}(anafast): SMICA, no mask: ',S_SMICAnomask,', masked: ',S_SMICAmasked print '' """ # get C_l from SPICE to compare to above method # note: getSMICA uses linspace in theta for thetaArray #newSMICA = False#True thetaArray2sp, C_SMICAsp, C_SMICAmaskedsp, S_SMICAnomasksp, S_SMICAmaskedsp = \ getSMICA(theta_i=theta_i,theta_f=theta_f,nSteps=nSteps,lmax=lmax,lmin=lmin, newSMICA=newSMICA,newDeg=newDeg,useSPICE=True,R1=R1) print '' print 'S_{1/2}(spice): SMICA, no mask: ', S_SMICAnomasksp, ', masked: ', S_SMICAmaskedsp print '' # Find S_{1/2} in real space to compare methods nTerms = 10000 #SSnm2 = SOneHalf(thetaArray2, C_SMICA, nTerms=nTerms) #SSmd2 = SOneHalf(thetaArray2, C_SMICAmasked, nTerms=nTerms) SSnm2sp = SOneHalf(thetaArray2sp, C_SMICAsp, nTerms=nTerms) SSmd2sp = SOneHalf(thetaArray2sp, C_SMICAmaskedsp, nTerms=nTerms) # create ensemble of realizations and gather statistics covEnsembleFull = np.zeros([nSims, nSteps + 1]) # for maskless covEnsembleCut = np.zeros([nSims, nSteps + 1]) # for masked sEnsembleFull = np.zeros(nSims) sEnsembleCut = np.zeros(nSims) covTheta = np.array([]) #nSims = 1000 # apply beam and pixel window functions to power spectra # note: to ignore the non-constant pixel shape, W(l) must be > B(l) # however, this is not true for NSIDE=128 and gauss_beam(5') # Here I ignore this anyway and proceed myNSIDE = 128 # must be same NSIDE as in getSMICA function Wpix = hp.pixwin(myNSIDE) Bsmica = hp.gauss_beam(5. / 60 * np.pi / 180) # 5 arcmin WlMax = Wpix.size if WlMax < lmax: print 'die screaming!!!' return 0 primCl = primCl[:WlMax] * (Wpix * Bsmica)**2 lateCl = lateCl[:WlMax] * (Wpix * Bsmica)**2 crossCl = crossCl[:WlMax] * (Wpix * Bsmica)**2 # note: i tried sims without this scaling, and results seemed the same at a glance # collect simulated Cl for comparison to model Clsim_full_sum = np.zeros(lmax + 1) # get Jmn matrix for harmonic space S_{1/2} calc. myJmn = getJmn(lmax=lmax) # set up ramdisk for SpICE # super lame that spice needs to read/write from disk, but here goes... RAMdisk = '/Volumes/ramdisk/' ClTempFile = RAMdisk + 'tempCl.fits' mapTempFile = RAMdisk + 'tempMap.fits' mapDegFile = RAMdisk + 'smicaMapDeg.fits' # this should have been created by sims.getSMICA maskDegFile = RAMdisk + 'maskMapDeg.fits' # this should have been created by sims.getSMICA # create RAM Disk for SpICE and copy these files there using bash RAMsize = 4 #Mb ramDiskOutput = subprocess.check_output('./ramdisk.sh create ' + str(RAMsize), shell=True) print ramDiskOutput diskID = ramDiskOutput[ 31:41] # this might not grab the right part; works for '/dev/disk1' subprocess.call('cp smicaMapDeg.fits ' + RAMdisk, shell=True) subprocess.call('cp maskMapDeg.fits ' + RAMdisk, shell=True) doTime = True # to time the run and print output startTime = time.time() #for nSim in range(nSims): nSim = 0 while nSim < nSims: print 'starting sim ', nSim + 1, ' of ', nSims alm_prim, alm_late = hp.synalm((primCl, lateCl, crossCl), lmax=lmax, new=True) # calculate C(theta) of simulation Clsim_prim = hp.alm2cl(alm_prim) Clsim_late = hp.alm2cl(alm_late) Clsim_cros = hp.alm2cl(alm_prim, alm_late) Clsim_full = Clsim_prim + 2 * Clsim_cros + Clsim_late # use Cl_sim_full to omit prim/late distinction for now # start with a mask # -> for optional C2 filtering based on cut sky map # alm2map should create map with default RING ordering # pixel window and beam already accounted for in true Cls #mapSim = hp.alm2map(alm_prim+alm_late,myNSIDE,lmax=lmax,pixwin=True,sigma=5./60*np.pi/180) mapSim = hp.alm2map(alm_prim + alm_late, myNSIDE, lmax=lmax) hp.write_map(mapTempFile, mapSim) ispice(mapTempFile, ClTempFile, maskfile1=maskDegFile, subav="YES", subdipole="YES") Cl_masked = hp.read_cl(ClTempFile) ell2 = np.arange(Cl_masked.shape[0]) # Check for low power of cut sky C_2 if (filterC2 == True and fullCl[2] * filtFacHigh > Cl_masked[2] and Cl_masked[2] > fullCl[2] * filtFacLow) or filterC2 == False: # note: getCovar uses linspace in x for thetaArray thetaArray, cArray2 = getCovar(ell2[:lmax + 1], Cl_masked[:lmax + 1], theta_i=theta_i, theta_f=theta_f, nSteps=nSteps, lmin=lmin) covEnsembleCut[nSim] = cArray2 # S_{1/2} sEnsembleCut[nSim] = np.dot( Cl_masked[lmin:lmax + 1], np.dot(myJmn[lmin:, lmin:], Cl_masked[lmin:lmax + 1])) doPlot = False #True if doPlot: plt.plot(thetaArray, cArray) plt.xlabel('theta (degrees)') plt.ylabel('C(theta)') plt.title('covariance of CMB simulation ' + str(nSim + 1)) plt.show() # now without the mask # uses the same sims that passed the C2 filter Clsim_full_sum += Clsim_full # note: getCovar uses linspace in x for thetaArray thetaArray, cArray = getCovar(ell[:lmax + 1], Clsim_full[:lmax + 1], theta_i=theta_i, theta_f=theta_f, nSteps=nSteps, lmin=lmin) covEnsembleFull[nSim] = cArray covTheta = thetaArray # S_{1/2} sEnsembleFull[nSim] = np.dot( Clsim_full[lmin:], np.dot(myJmn[lmin:, lmin:], Clsim_full[lmin:])) nSim += 1 if doTime: print 'time elapsed: ', int( (time.time() - startTime) / 60.), ' minutes' # free the RAM used by SpICE's RAM disk ramDiskOutput = subprocess.check_output('./ramdisk.sh delete ' + diskID, shell=True) print ramDiskOutput avgEnsembleFull = np.average(covEnsembleFull, axis=0) stdEnsembleFull = np.std(covEnsembleFull, axis=0) # do I need a better way to describe confidence interval? avgEnsembleCut = np.average(covEnsembleCut, axis=0) stdEnsembleCut = np.std(covEnsembleCut, axis=0) Clsim_full_avg = Clsim_full_sum / nSims # save results saveFile1 = "simStatResultC.npy" np.save( saveFile1, np.vstack((thetaArray, avgEnsembleFull, stdEnsembleFull, avgEnsembleCut, stdEnsembleCut))) saveFile2 = "simStatC_SMICA.npy" np.save(saveFile2, np.vstack((thetaArray2sp, C_SMICAsp, C_SMICAmaskedsp))) saveFile3 = "simStatResultS.npy" np.save( saveFile3, np.vstack((np.hstack((np.array(S_SMICAnomasksp), sEnsembleFull)), np.hstack((np.array(S_SMICAmaskedsp), sEnsembleCut))))) doPlot = True if doPlot: print 'plotting C_l... ' #print ell.size,conv.size,primCl.size,crossCl.size,lateCl.size plt.plot(ell[:lmax + 1], conv[:lmax + 1] * (primCl + 2 * crossCl + lateCl)[:lmax + 1], label='model D_l') plt.plot(ell[:lmax + 1], conv[:lmax + 1] * Clsim_full_avg, label='ensemble average D_l') plt.legend() plt.show() makePlots(saveFile1=saveFile1, saveFile2=saveFile2, saveFile3=saveFile3) # S_{1/2} output print '' print 'using CIC method: ' #print 'S_{1/2}(anafast): SMICA, no mask: ',S_SMICAnomask,', masked: ',S_SMICAmasked print 'S_{1/2}(spice): SMICA, no mask: ', S_SMICAnomasksp, ', masked: ', S_SMICAmaskedsp print '' print 'using CCdx method: ' #print 'S_{1/2}(anafast): SMICA, no mask: ',SSnm2,', masked: ',SSmd2 print 'S_{1/2}(spice): SMICA, no mask: ', SSnm2sp, ', masked: ', SSmd2sp print ''
#### Get MC results mclsout = np.zeros(nbins) sclsout = np.zeros(nbins) for j in np.arange(nbins): mclsout[j] = np.mean(allclsout[:,j]) sclsout[j] = np.std(allclsout[:,j])/sqrt(nbmc) mcls = np.zeros(lmax+1) scls = np.zeros(lmax+1) for j in np.arange(lmax+1): mcls[j] = np.mean(allcls[:,j]) scls[j] = np.std(allcls[:,j])/sqrt(nbmc) ell = np.arange(lmax+1) pw = hp.pixwin(nside)[0:lmax+1] pwb = np.interp(newl,ell,pw) fact = ell * (ell + 1) / (2 * np.pi) * len(maskmap) / maskok.sum() factth = lll * (lll + 1) / (2 * np.pi) clf() subplot(2,1,1) title('TT') xlim(0,lmax) errorbar(newl,mclsout/pwb**2,yerr=sclsout,fmt='bo',label='Corrected') plot(ell,fact*mcls/pw**2,'g',label='Anafast rescaled') plot(spectra[0],spectra[1]*factth,'r',label='Input') legend(loc='lower right',frameon=False,fontsize=10) subplot(2,1,2) title('Residuals')
def test(useCLASS=1, useLensing=1, classCamb=1, nSims=1000, lmax=100, lmin=2, newSMICA=False, newDeg=False, suppressC2=False, suppFactor=0.23): """ code for testing the other functions in this module Inputs: useCLASS: set to 1 to use CLASS, 0 to use CAMB CLASS Cl has early/late split at z=50 CAMB Cl has ISWin/out split: ISWin: 0.4<z<0.75, ISWout: the rest Note: CAMB results include primary in ISWin and ISWout (not as intended) default: 1 useLensing: set to 1 to use lensed Cl, 0 for non-lensed default: 1 classCamb: if 1: use the CAMB format of CLASS output, if 0: use CLASS format Note: parameter not used if useCLASS = 0 default: 1 nSims: the number of simulations to do for ensemble default: 1000 lmax: the highest l to include in Legendre transforms default: 100 lmin: the lowest l to include in S_{1/2} = CIC calculations default: 2 newSMICA: set to True to recalculate SMICA results default: False newDeg: set to True to recalculate map and mask degredations (only if newSMICA is also True) default: False suppressC2: set to True to suppress theoretical C_2 by suppFactor before creating a_lm.s Default: False suppFactor: multiplies C_2 if suppressC2 is True Default: 0.23 # from Tegmark et. al. 2003, figure 13 (WMAP) """ ############################################################################## # load theoretical power spectra # load data ell, fullCl, primCl, lateCl, crossCl = gcp.loadCls(useCLASS=useCLASS, useLensing=useLensing, classCamb=classCamb) # fill beginning with zeros startEll = int(ell[0]) ell = np.append(np.arange(startEll), ell) fullCl = np.append(np.zeros(startEll), fullCl) primCl = np.append(np.zeros(startEll), primCl) lateCl = np.append(np.zeros(startEll), lateCl) crossCl = np.append(np.zeros(startEll), crossCl) # suppress C_2 to see what happens in enesmble #suppressC2 = False #suppFactor = 0.23 # from Tegmark et. al. 2003, figure 13 (WMAP) if suppressC2: fullCl[2] *= suppFactor primCl[2] *= suppFactor lateCl[2] *= suppFactor crossCl[2] *= suppFactor conv = ell * (ell + 1) / (2 * np.pi) #print ell,conv #ell[0]=2.0 # apply beam and pixel window functions to power spectra # note: to ignore the non-constant pixel shape, W(l) must be > B(l) # however, this is not true for NSIDE=128 and gauss_beam(5') # Here I ignore this anyway and proceed myNSIDE = 128 # must be same NSIDE as in sims.getSMICA function Wpix = hp.pixwin(myNSIDE) Bsmica = hp.gauss_beam(5. / 60 * np.pi / 180) # 5 arcmin WlMax = Wpix.size if WlMax < lmax: print 'die screaming!!!' return 0 fullCl = fullCl[:WlMax] * (Wpix * Bsmica)**2 primCl = primCl[:WlMax] * (Wpix * Bsmica)**2 lateCl = lateCl[:WlMax] * (Wpix * Bsmica)**2 crossCl = crossCl[:WlMax] * (Wpix * Bsmica)**2 # note: i tried sims without this scaling, and results seemed the same at a glance ############################################################################## # load SMICA data, converted to C(theta), via SpICE if newSMICA: theta_i = 0.0 #degrees theta_f = 180.0 #degrees nSteps = 1800 thetaArray2sp, C_SMICAsp, C_SMICAmaskedsp, S_SMICAnomasksp, S_SMICAmaskedsp = \ sims.getSMICA(theta_i=theta_i,theta_f=theta_f,nSteps=nSteps,lmax=lmax,lmin=lmin, newSMICA=newSMICA,newDeg=newDeg,useSPICE=True) # filenames for SpICE to use # super lame that spice needs to read/write from disk, but here goes... RAMdisk = '/Volumes/ramdisk/' ClTempFile = RAMdisk + 'tempCl.fits' mapTempFile = RAMdisk + 'tempMap.fits' mapDegFile = RAMdisk + 'smicaMapDeg.fits' # this should have been created by sims.getSMICA maskDegFile = RAMdisk + 'maskMapDeg.fits' # this should have been created by sims.getSMICA # create RAM Disk for SpICE and copy these files there using bash RAMsize = 4 #Mb ramDiskOutput = subprocess.check_output('./ramdisk.sh create ' + str(RAMsize), shell=True) print ramDiskOutput diskID = ramDiskOutput[ 31:41] # this might not grab the right part; works for '/dev/disk1' subprocess.call('cp smicaMapDeg.fits ' + RAMdisk, shell=True) subprocess.call('cp maskMapDeg.fits ' + RAMdisk, shell=True) ispice(mapDegFile, ClTempFile, maskfile1=maskDegFile, subav="YES", subdipole="YES") ClsmicaCut = hp.read_cl(ClTempFile) # find S_{1/2} for SMICA. Should actually optimize but see what happens here first. #myJmn = legprodint.getJmn(endX=0.5,lmax=lmax,doSave=False) #Ssmica = np.dot(ClsmicaCut[lmin:lmax+1],np.dot(myJmn[lmin:,lmin:], # ClsmicaCut[lmin:lmax+1]))*1e24 #K^4 to microK^4 ############################################################################## # create ensemble of realizations and gather statistics spiceMax = myNSIDE * 3 # should be lmax+1 for SpICE ClEnsembleCut = np.zeros([nSims, spiceMax]) simEll = np.arange(spiceMax) doTime = True # to time the run and print output startTime = time.time() for nSim in range(nSims): print 'starting masked Cl sim ', nSim + 1, ' of ', nSims alm_prim, alm_late = hp.synalm((primCl, lateCl, crossCl), lmax=lmax, new=True) mapSim = hp.alm2map(alm_prim + alm_late, myNSIDE, lmax=lmax) hp.write_map(mapTempFile, mapSim) ispice(mapTempFile, ClTempFile, maskfile1=maskDegFile, subav="YES", subdipole="YES") ClEnsembleCut[nSim] = hp.read_cl(ClTempFile) doPlot = False #True if doPlot: gcp.showCl(simEll[:lmax + 1], ClEnsembleCut[nSim, :lmax + 1], title='power spectrum of simulation ' + str(nSim + 1)) timeInterval1 = time.time() - startTime if doTime: print 'time elapsed: ', int(timeInterval1 / 60.), ' minutes' # free the RAM used by SpICE's RAM disk ramDiskOutput = subprocess.check_output('./ramdisk.sh delete ' + diskID, shell=True) print ramDiskOutput # put SMICA in as 0th member of the ensemble; 1e12 to convert K^2 to microK^2 ClEnsembleCut = np.vstack((ClsmicaCut * 1e12, ClEnsembleCut)) nSims += 1 ############################################################################## # create S(x) for each C_l, using interpolation nXvals = 181 thetaVals = np.linspace(0, 180, nXvals) # one degree intervals xVals = np.cos(thetaVals * np.pi / 180) Jmnx = np.empty([nXvals, lmax + 1, lmax + 1]) for index, xVal in enumerate(xVals): Jmnx[index] = legprodint.getJmn(endX=xVal, lmax=lmax, doSave=False) SxToInterpolate = np.empty(nXvals) # create list of functions dummy = lambda x: x**2 SofXList = [dummy for i in range(nSims)] for nSim in range(nSims): print 'starting S(x) sim ', nSim + 1, ' of ', nSims for index, xVal in enumerate(xVals): SxToInterpolate[index] = np.dot( ClEnsembleCut[nSim, lmin:lmax + 1], np.dot(Jmnx[index, lmin:, lmin:], ClEnsembleCut[nSim, lmin:lmax + 1])) SofX = interp1d(xVals, SxToInterpolate) #SofXList = SofXList.append(SofX) # Apparently appending a function to an empty list is not allowed. Instead: SofXList[nSim] = SofX #print SofXList#[nSim] doPlot = False #True if doPlot: nplotx = (nXvals - 1) * 10 + 1 plotTheta = np.linspace(0, 180, nplotx) plotx = np.cos(plotTheta * np.pi / 180) plotS = SofXList[nSim](plotx) plt.plot(plotx, plotS) plt.title('S(x) for simulation ' + str(nSim + 1)) plt.show() doPlot = True if doPlot: for nSim in range(nSims): nplotx = (nXvals - 1) * 10 + 1 plotTheta = np.linspace(0, 180, nplotx) plotx = np.cos(plotTheta * np.pi / 180) plotS = SofXList[nSim](plotx) plt.plot(plotx, plotS, label='sim ' + str(nSim + 1)) #plt.legend() plt.title('S(x) for ' + str(nSims) + ' simulations') plt.xlabel('x') plt.ylabel('S_x') plt.show() ############################################################################## # create Pval(x) for each S(x), using ensemble # Pval: probability of result equal to or more extreme # create list of functions PvalOfXList = [dummy for i in range(nSims)] for nSim in range(nSims): print 'starting Pval(x) sim ', nSim + 1, ' of ', nSims def PvalOfX(x): nUnder = 0 # will also include nEqual nOver = 0 threshold = SofXList[nSim](x) for nSim2 in range(nSims): Sx = SofXList[nSim2](x) if Sx > threshold: nOver += 1 #print "Over! mySx: ",Sx,", threshold: ",threshold else: nUnder += 1 #print "Under! mySx: ",Sx,", threshold: ",threshold #print "nUnder: ",nUnder,", nOver: ",nOver return nUnder / float(nUnder + nOver) PvalOfXList[nSim] = PvalOfX ############################################################################## # find global minimum for each Pval(x) # simply use same xVals as above, at one degree intervals # if there are equal p-values along the range, the one with the highest xVal # will be reported PvalMinima = np.empty(nSims) xValMinima = np.empty(nSims) doTime = True # to time the run and print output startTime = time.time() for nSim in range(nSims): print 'starting minimum Pval(x) search for sim ', nSim + 1, ' of ', nSims PvalOfX = PvalOfXList[nSim] #print 'function: ',PvalOfX PvalMinima[nSim] = PvalOfX(1.0) xValMinima[nSim] = 1.0 Pvals = np.empty(nXvals) for index, xVal in enumerate( xVals): # will start from 1 and go down to -1 myPval = PvalOfX(xVal) Pvals[index] = myPval #print "nSim: ",nSim,", n: ",index,", myPval: ",myPval,", PvalMinima[nSim]: ",PvalMinima[nSim] if myPval < PvalMinima[ nSim] and xVal > -0.999: #avoid the instabililility PvalMinima[nSim] = myPval xValMinima[nSim] = xVal #print 'nSim: ',nSim+1,', new x for minimum Pval: ',xVal #raw_input("Finished sim "+str(nSim+1)+" of "+str(nSims)+". Press enter to continue") doPlot = True #False#True if doPlot: # and np.random.uniform() < 0.1: #randomly choose about 1/10 of them plt.plot(xVals, Pvals) plt.vlines(xValMinima[nSim], 0, 1) plt.xlabel('x = cos(theta), min at ' + str(xValMinima[nSim])) plt.ylabel('P-value') plt.title('P-values for simulation ' + str(nSim + 1) + ' of ' + str(nSims) + ', p_min = ' + str(PvalMinima[nSim])) plt.xlim(-1.05, 1.05) plt.ylim(-0.05, 1.05) plt.show() timeInterval2 = time.time() - startTime if doTime: print 'time elapsed: ', int(timeInterval2 / 60.), ' minutes' """ # A MYSTERY! Something about the following code causes Pvals to always take # the values of PvalOfXList[nSims](xVals) WTF? Omit for now. # Testing seems to indicate that PvalOfXList functions still have different # locations in memory, but they all seem to be evaluating the same. # However, when the previous block of code is copied to come again after # this one, it behaves properly again. # see how well it did doPlot = False#True if doPlot: nPlots = 10 for nPlot in range(nPlots): print 'plot ',nPlot+1,' of ',nPlots toPlot = nPlot#np.random.randint(0,high=nSims) #for nSim in range(nSims): Pvals = np.empty(nXvals) PvalOfX = PvalOfXList[nPlot] print 'function: ',PvalOfX for index, xVal in enumerate(xVals): Pvals[index] = PvalOfX(xVal) #print index,Pvals[index] #print Pvals plt.plot(xVals,Pvals) plt.vlines(xValMinima[toPlot],0,1) plt.xlabel('x = cos(theta), min at '+str(xValMinima[toPlot])) plt.ylabel('P-value') plt.title('P-values for simulation '+str(toPlot+1)+' of '+str(nSims)) plt.show() """ ############################################################################## # create distribution of S(xValMinima) SxEnsembleMin = np.empty(nSims) for nSim in range(nSims): SxEnsembleMin[nSim] = SofXList[nSim](xValMinima[nSim]) # extract SMICA result Ssmica = SxEnsembleMin[0] ############################################################################## # plot/print results print 'plotting S_x distribution... ' myBins = np.logspace(1, 7, 100) plt.axvline(x=Ssmica, color='g', linewidth=3, label='SMICA masked') plt.hist(SxEnsembleMin[1:], bins=myBins, histtype='step', label='cut sky') # [1:] to omit SMICA value plt.gca().set_xscale("log") plt.legend() plt.xlabel('S_x (microK^4)') plt.ylabel('Counts') plt.title('S_x of ' + str(nSims - 1) + ' simulated CMBs') #-1 due to SMICA in zero position plt.show() print ' ' print 'nSims = ', nSims - 1 print 'time interval 1: ', timeInterval1, 's, time interval 2: ', timeInterval2, 's' print ' => ', timeInterval1 / (nSims - 1), ' s/sim, ', timeInterval2 / ( nSims - 1), ' s/sim' print 'SMICA optimized S_x: S = ',Ssmica,', for x = ',xValMinima[0], \ ', with p-value ',PvalMinima[0] print ' ' print 'step 3: profit' print ''
def test_pixwin_pol(self): pixwin = hp.pixwin(128, pol=True) self.assertEqual(len(pixwin), 2)
#theoretical power spectra. cl = qcinv.util.camb_clfile('inputs/wmap/bestfit_lensedCls.dat', lmax=lmax) # noise level # http://lambda.gsfc.nasa.gov/product/map/dr4/skymap_info.cfm ninv = hp.read_map("inputs/wmap/wmap_band_forered_iqumap_r9_7yr_V_v4.fits", field=3) / (3.137*1.0e3)**2 assert( len(ninv) == npix ) ncov = np.zeros(npix) ncov[ np.where(ninv != 0) ] = 1.0 / ninv[ np.where(ninv != 0) ] assert( npix == len(ninv) ) beam = 0.5 * (np.loadtxt('inputs/wmap/wmap_ampl_bl_V1_7yr_v4.txt')[:,1][0:(lmax+1)] + np.loadtxt('inputs/wmap/wmap_ampl_bl_V2_7yr_v4.txt')[:,1][0:(lmax+1)]) beam *= hp.pixwin(nside)[0:(lmax+1)] clnn = np.sum(ncov) * (4.*np.pi) / npix**2 / beam**2 ncov *= mask ninv *= mask # noise map print "generating noise map" nmap = np.random.standard_normal(npix) * np.sqrt(ncov) # cmb map print "generating cmb map" tmap, talm = hp.synfast(cl.cltt * beam * beam, nside, lmax=lmax, alm=True) hp.almxfl(talm, 1.0/beam, inplace=True)
def get_beam(self, det): pxw = hp.pixwin(self.nside)[0:self.lmax+1] beam = dmc.get_bl(self.year, det)[0:self.lmax+1] return beam * pxw
lmax = len(cross_cls) beam_lmax = lmax l = np.arange(beam_lmax) ll = l*(l+1)/(2*np.pi) beam_14 = hp.gauss_beam(14.4*np.pi/(180.*60.),beam_lmax-1) beam_5 = hp.gauss_beam(5.*np.pi/(180.*60.),beam_lmax-1) b14_180 = hp.gauss_beam(np.sqrt((3*60.)**2 - (14.4)**2)*np.pi/(180.*60.),beam_lmax-1) b5_180 = hp.gauss_beam(np.sqrt((3*60.)**2 - (5.)**2)*np.pi/(180.*60.),beam_lmax-1) beam_14 *= (1. - b14_180) beam_5 *= (1. - b5_180) pix = hp.pixwin(256)[:beam_lmax] theory_cls= hp.read_cl(theory_cl_file) theory_cls=theory_cls[0][:beam_lmax] #theory_cls[:2]=1e-10 cross_cls = cross_cls[:beam_lmax] radio_cls = radio_cls[:beam_lmax] cmb_cls = cmb_cls[:beam_lmax] wls = hp.anafast((~radio_fr.mask).astype(float))[:beam_lmax] fskyw2 = np.sum([(2*m+1)*wls[mi] if m > 0 else 0 for mi,m in enumerate(xrange(len(wls)))])/(4*np.pi) wls = wls fsky = 1. - np.sum(mask_bool).astype(float)/len(mask_bool) L = np.sqrt(4*np.pi*fsky)
plot(lll,np.sqrt(abs(spectra[4])*(lll*(lll+1))/(2*np.pi)),label='$C_\ell^{TE}$') plot(lll,np.sqrt(spectra[2]*(lll*(lll+1))/(2*np.pi)),label='$C_\ell^{EE}$') plot(lll,np.sqrt(spectra[3]*(lll*(lll+1))/(2*np.pi)),label='$C_\ell^{BB}$') yscale('log') xlim(0,lmaxcamb+1) ylim(0.0001,100) xlabel('$\ell$') ylabel('$\sqrt{\ell(\ell+1)C_\ell/(2\pi)}$'+' '+'$[\mu K]$ ') legend(loc='lower right',frameon=False) ############################################################################# nside = 128 lmax = 2*nside-1 maps=hp.synfast(spectra[1:],nside,fwhm=0,pixwin=True,new=True) pw = hp.pixwin(nside, pol=True) lmax = 1*nside-1 cls1 = hp.anafast(maps,pol=True, lmax=lmax) ell1= np.arange(lmax+1) pw1 = [pw[0][0:lmax+1], pw[1][0:lmax+1]] lmax = 2*nside-1 cls2 = hp.anafast(maps,pol=True, lmax=lmax) ell2= np.arange(lmax+1) pw2 = [pw[0][0:lmax+1], pw[1][0:lmax+1]] lmax = 3*nside-1 cls3 = hp.anafast(maps,pol=True, lmax=lmax) ell3= np.arange(lmax+1) pw3 = [pw[0][0:lmax+1], pw[1][0:lmax+1]]
def covth_bins(ellbins,nside,ipok,bl,spectra,polar=True,temp=True,allinone=True): #### define bins in ell nbins=len(ellbins)-1 minell=np.array(ellbins[0:nbins]) maxell=np.array(ellbins[1:nbins+1])-1 ellval=(minell+maxell)*0.5 lmax=np.max(ellbins) #maxell[nbins-1]=lmax print('minell:',minell) print('maxell:',maxell) #### define Stokes all=['I','Q','U'] if not temp: all=['Q','U'] if not polar: all=['I'] #### define pixels print('rpix calculation') rpix=np.array(hp.pix2vec(nside,ipok)) print('rpix done: ',rpix.shape) allcosang=np.dot(np.transpose(rpix),rpix) print('cosang done') #### define Pixel window function pixwin=hp.pixwin(nside)[0:lmax+1] #### get ell values from spectra and restrict spectra to good values nspec,nell=np.shape(spectra) ell=spectra[0] maskl=ell<(lmax+1) ell=ell[maskl] ctt=spectra[1][maskl] cee=spectra[2][maskl] cbb=spectra[3][maskl] cte=spectra[4][maskl] if nspec==5: print('Assuming EB and TB are zero') ceb=0 ctb=0 else: ceb=spectra[5][maskl] ctb=spectra[6][maskl] # Effective spectra print('Calculating effective spectra') norm=(2*ell+1)/(4*np.pi)*(pixwin**2)*(bl[maskl]**2) norm[1:]=norm[1:]/(ell[1:]*(ell[1:]+1)) effctt=norm*ctt effcte=norm*cte effcee=norm*cee effcbb=norm*cbb effceb=norm*ceb effctb=norm*ctb #### define masks for ell bins masks=[] for i in np.arange(nbins): masks.append((ell>=minell[i]) & (ell<=maxell[i])) ### Create array for covariances matrices per bin nbpixok=ipok.size nstokes=np.size(all) print('creating array') cov=np.zeros((nbins,nstokes,nstokes,nbpixok,nbpixok)) for i in np.arange(nbpixok): print(i) pyquad.progress_bar(i,nbpixok) for j in np.arange(i,nbpixok): if nstokes==1: pl=pl0(allcosang[i,j],lmax) TT=effctt*pl for b in np.arange(nbins): cov[b,0,0,i,j]=np.sum(TT[masks[b]])*ellval[b]*(ellval[b]+1) cov[b,0,0,j,i]=cov[b,0,0,i,j] if nstokes==2: cij,sij=polrotangle(rpix[:,i],rpix[:,j]) cji,sji=polrotangle(rpix[:,j],rpix[:,i]) f12=F1l2(allcosang[i,j],lmax) f22=F2l2(allcosang[i,j],lmax) QQ=f12*effcee-f22*effcbb UU=f12*effcbb-f22*effcee QU=(f12+f22)*effceb cQQpsQU = ( cij*QQ + sij*QU ) cQUpsUU = ( cij*QU + sij*UU ) cQUmsQQ = ( cij*QU - sij*QQ ) cUUmsQU = ( cij*UU - sij*QU ) for b in np.arange(nbins): cov[b,0,0,i,j] = np.sum( cji*cQQpsQU[masks[b]] + sji*cQUpsUU[masks[b]] )*ellval[b]*(ellval[b]+1) cov[b,0,0,j,i] = cov[b,0,0,i,j] cov[b,0,1,i,j] = np.sum(-sji*cQQpsQU[masks[b]] + cji*cQUpsUU[masks[b]] )*ellval[b]*(ellval[b]+1) cov[b,1,0,j,i] = cov[b,0,1,i,j] cov[b,1,1,i,j] = np.sum(-sji*cQUmsQQ[masks[b]] + cji*cUUmsQU[masks[b]] )*ellval[b]*(ellval[b]+1) cov[b,1,1,j,i] = cov[b,1,1,i,j] cov[b,0,1,j,i] = np.sum( cji*cQUmsQQ[masks[b]] + sji*cUUmsQU[masks[b]] )*ellval[b]*(ellval[b]+1) cov[b,1,0,i,j] = cov[b,0,1,j,i] if nstokes==3: cij,sij=polrotangle(rpix[:,i],rpix[:,j]) cji,sji=polrotangle(rpix[:,j],rpix[:,i]) pl=pl0(allcosang[i,j],lmax) f10=F1l0(allcosang[i,j],lmax) f12=F1l2(allcosang[i,j],lmax) f22=F2l2(allcosang[i,j],lmax) TT=effctt*pl QQ=f12*effcee-f22*effcbb UU=f12*effcbb-f22*effcee TQ=-f10*effcte TU=-f10*effctb QU=(f12+f22)*effceb cQQpsQU = ( cij*QQ + sij*QU ) cQUpsUU = ( cij*QU + sij*UU ) cQUmsQQ = ( cij*QU - sij*QQ ) cUUmsQU = ( cij*UU - sij*QU ) for b in np.arange(nbins): cov[b,0,0,i,j] = np.sum( TT[masks[b]] )*ellval[b]*(ellval[b]+1) cov[b,0,0,j,i] = cov[b,0,0,i,j] cov[b,0,1,i,j] = np.sum( cji*TQ[masks[b]] + sji*TU[masks[b]] )*ellval[b]*(ellval[b]+1) cov[b,1,0,j,i] = cov[b,0,1,i,j] cov[b,1,0,i,j] = np.sum( cij*TQ[masks[b]] + sij*TU[masks[b]] )*ellval[b]*(ellval[b]+1) cov[b,0,1,j,i] = cov[b,1,0,i,j] cov[b,0,2,i,j] = np.sum(-sji*TQ[masks[b]] + cij*TU[masks[b]] )*ellval[b]*(ellval[b]+1) cov[b,2,0,j,i] = cov[b,0,2,i,j] cov[b,2,0,i,j] = np.sum(-sij*TQ[masks[b]] + cij*TU[masks[b]] )*ellval[b]*(ellval[b]+1) cov[b,0,2,j,i] = cov[b,2,0,i,j] cov[b,1,1,i,j] = np.sum( cji*cQQpsQU[masks[b]] + sji*cQUpsUU[masks[b]] )*ellval[b]*(ellval[b]+1) cov[b,1,1,j,i] = cov[b,1,1,i,j] cov[b,1,2,i,j] = np.sum(-sji*cQQpsQU[masks[b]] + cji*cQUpsUU[masks[b]] )*ellval[b]*(ellval[b]+1) cov[b,2,1,j,i] = cov[b,1,2,i,j] cov[b,2,1,i,j] = np.sum( cji*cQUmsQQ[masks[b]] + sji*cUUmsQU[masks[b]] )*ellval[b]*(ellval[b]+1) cov[b,1,2,j,i] = cov[b,2,1,i,j] cov[b,2,2,i,j] = np.sum(-sji*cQUmsQQ[masks[b]] + cji*cUUmsQU[masks[b]] )*ellval[b]*(ellval[b]+1) cov[b,2,2,j,i] = cov[b,2,2,i,j] if allinone: newcov=np.zeros((nbins,nstokes*nbpixok,nstokes*nbpixok)) for i in np.arange(nbins): for si in np.arange(nstokes): for sj in np.arange(nstokes): newcov[i,si*nbpixok:(si+1)*nbpixok,sj*nbpixok:(sj+1)*nbpixok]=cov[i,si,sj,:,:] return(newcov) else: return(cov)
def pixfunc(self): if self._pixfunc is None: self._pixfunc = hp.pixwin(self.nside, pol=False)[:self.lmax] return self._pixfunc
# add noise map=mapin.copy() map[mask]+=np.random.randn(npix)*signoise allmaps[:,i]=map[mask] allmaps_nosig[:,i]=map[mask]-mapin[mask] mcl=np.zeros(251) for i in arange(251): mcl[i]=np.mean(allcl[i,:]) clf() plot(ell,spectra[1]*(ell*(ell+1))/(2*np.pi),'r') xlim(0,251) thel=np.arange(251) thebell=np.exp(-0.5*(thel**2)*((fwhm/2.35)**2)) pixwin=hp.pixwin(hp.npix2nside(len(mapin)))[0:251] plot(thel,mcl*thel*(thel+1)/(2*np.pi)/thebell**2/pixwin**2) covmc=np.zeros((npix,npix)) covmc_nonoise=np.zeros((npix,npix)) covmc_nosig=np.zeros((npix,npix)) for i in np.arange(npix): print(i) for j in np.arange(i,npix): covmc[i,j]=mean( (allmaps[i,:]-mean(allmaps[i,:]))*(allmaps[j,:]-mean(allmaps[j,:]))) covmc_nonoise[i,j]=mean( (allmaps_nonoise[i,:]-mean(allmaps_nonoise[i,:]))*(allmaps_nonoise[j,:]-mean(allmaps_nonoise[j,:]))) covmc_nosig[i,j]=mean( (allmaps_nosig[i,:]-mean(allmaps_nosig[i,:]))*(allmaps_nosig[j,:]-mean(allmaps_nosig[j,:]))) covmc[j,i]=covmc[i,j] covmc_nosig[j,i]=covmc_nosig[i,j]
def fix_for_mask(config, logger=log.null_logger): with log.Timer(logger, 'Step 4: Accounting for mask').with_level(logging.INFO): coupling_matrix_fname = config.get('mask_coupling_matrix_fname') # If the coupling matrix is not specified, we need to make it if not coupling_matrix_fname: output_file = os.path.join(config['output_dir'], 'master_deconv_spectra.fits') maxl = config.get('matmask_maxl', 2500) # mrsp_matmask -t -w -v -z -l 1024 -o TQU <smap> <mask> <output_file> cmd = [ matmask_command, '-t', '-w', '-v', '-z', '-l', str(maxl), '-o', 'TQU', config['aggregated_map_fname'], config['mask_fname'], output_file ] rc = log.run_command(cmd, timeout=5e-2, logger=logger) if rc != 0: raise RuntimeError(matmask_command + ' failed with exit code: ' + str(rc)) # Outputs are: # master_deconv_spectra.fits # master_deconv_spectra_maskedmap_pspec.fits # master_deconv_spectra_mask_pspec.fits # master_deconv_spectra_mask_spec_radii.fits # master_deconv_spectra_coupling_matrices.fits if config.get('delete_tmps', True): tmps = [ os.path.join(config['output_dir'], fname) for fname in ( 'master_deconv_spectra.fits', 'master_deconv_spectra_maskedmap_pspec.fits', 'master_deconv_spectra_mask_pspec.fits', 'master_deconv_spectra_mask_spec_radii.fits', 'master_deconv_spectra_coupling_matrices.fits') ] for tmp in tmps: logger.debug('deleting ' + tmp) os.remove(tmp) coupling_matrix_fname = os.path.join( config['output_dir'], 'master_deconv_spectra_coupling_matrices.fits') coupling_matrix = fitsio.FITS(coupling_matrix_fname)[0].read() # FIXME: mrsp_matmask does not produce the correct spectrum, but it does # produce the correct coupling matrix. we can use the coupling matrix # and invert it ourselves. # TODO: get to the bottom of this. Why does mrsp_matmask give the wrong # answers? Can Florent or JLS figure this out? mask = hp.read_map(config['mask_fname'], verbose=False) aggregated_cmb = hp.read_map(config['aggregated_map_fname'], verbose=False) # 1e6 to convert K -> mu K masked_powerspec = hp.anafast(mask * 1e6 * aggregated_cmb, lmax=coupling_matrix.shape[0] - 1) recovered_pspec = np.linalg.solve(coupling_matrix, masked_powerspec) ells = np.arange(recovered_pspec.size) ll1 = ells * (ells + 1) / (2 * np.pi) / hp.pixwin( 2048, lmax=ells.size - 1)**2 hp.write_cl( os.path.join(config['output_dir'], 'mask_corrected_spectra.fits'), ll1 * recovered_pspec)
clnoiseless[j,:,:] = np.mean(allclnoiseless, axis = 2) dclnoiseless[j,:,:] = np.std(allclnoiseless, axis = 2) clnoisy[j,:,:] = np.mean(allclnoisy, axis = 2) dclnoisy[j,:,:] = np.std(allclnoisy, axis = 2) clres[j,:,:] = np.mean(allclres, axis = 2) dclres[j,:,:] = np.std(allclres, axis = 2) clspoiled_noiseless[j,:,:] = np.mean(allclspoiled_noiseless, axis = 2) dclspoiled_noiseless[j,:,:] = np.std(allclspoiled_noiseless, axis = 2) clspoiled_noisy[j,:,:] = np.mean(allclspoiled_noisy, axis = 2) dclspoiled_noisy[j,:,:] = np.std(allclspoiled_noisy, axis = 2) clspoiled_res[j,:,:] = np.mean(allclspoiled_res, axis = 2) dclspoiled_res[j,:,:] = np.std(allclspoiled_res, axis = 2) #### Pixel Window function wpix = hp.pixwin(ns,pol=True) #### Synthesized Beam window function fwhm = 0.47 ll = np.arange(len(wpix[0])) bl = exp(-ll**2*(np.radians(fwhm/2.35))**2/2) #### Correction corrT = np.interp(newl, ll, wpix[0]**2*bl**2) corrP = np.interp(newl, ll, wpix[1]**2*bl**2) corr=corrT cl = clinit col = ['black','red', 'blue', 'green', 'magenta'] clf() for i in np.arange(len(sigptgvals)): for j in np.arange(6): subplot(2,3,j+1)
def plot_mc(): f=open('cl_theory_FR_QxaU.json','r') theory1_array_in=json.load(f) f.close() f=open('cl_theory_FR_UxaQ.json','r') theory2_array_in=json.load(f) f.close() f=open('cl_array_FR_QxaU.json','r') cross1_array_in=json.load(f) f.close() f=open('cl_array_FR_UxaQ.json','r') cross2_array_in=json.load(f) f.close() # f=open('cl_noise_FR_QxaU.json','r') # noise1_array_in=json.load(f) # f.close() # f=open('cl_noise_FR_UxaQ.json','r') # noise2_array_in=json.load(f) # f.close() f=open('cl_Nau_FR_QxaU.json','r') Nau_array_in=json.load(f) f.close() f=open('cl_Ndq_FR_QxaU.json','r') Ndq_array_in=json.load(f) f.close() f=open('cl_Naq_FR_UxaQ.json','r') Naq_array_in=json.load(f) f.close() f=open('cl_Ndu_FR_UxaQ.json','r') Ndu_array_in=json.load(f) f.close() bins=[1,5,10,20,25,50] N_runs=500 # bls=hp.gauss_beam(smoothing_scale*np.pi/(180.*60.),383)**2 #bls=hp.gauss_beam(smoothing_scale*np.pi/(180.*60.),3*nside_out-1)**2 bls=(hp.gauss_beam(smoothing_scale*np.pi/(180.*60.),3*nside_out-1)*hp.pixwin(nside_out)[:3*nside_out])**2 #bls=np.repeat(1,3*nside_out) fsky=225.*(np.pi/180.)**2/(4*np.pi) l=np.arange(len(cross1_array_in[0])) ll=l*(l+1)/(2*np.pi) L=np.sqrt(fsky*4*np.pi) dl_eff=2*np.pi/L theory1_array_in=np.array(theory1_array_in)/(fsky*bls) theory2_array_in=np.array(theory2_array_in)/(fsky*bls) cross1_array_in=np.array(cross1_array_in)/(fsky*bls) cross2_array_in=np.array(cross2_array_in)/(fsky*bls) Ndq_array_in=np.array(Ndq_array_in)/(fsky) Ndu_array_in=np.array(Ndu_array_in)/(fsky) Nau_array_in=np.array(Nau_array_in)/(fsky) Naq_array_in=np.array(Naq_array_in)/(fsky) #noise1_array_in=np.array(noise1_array_in)/(fsky*bls) #noise2_array_in=np.array(noise2_array_in)/(fsky*bls) Ndq_array_in.shape += (1,) Ndu_array_in.shape += (1,) Nau_array_in.shape += (1,) Naq_array_in.shape += (1,) for b in bins: theory_cls=hp.read_cl('/home/matt/Planck/data/faraday/correlation/fr_theory_cl.fits') # N_dq=np.mean(Ndq_array_in) # N_au=np.mean(Nau_array_in) # #delta1=np.sqrt(2.*abs((np.mean(cross1_array_in,axis=0)-np.mean(noise1_array_in,axis=0))**2+(np.mean(cross1_array_in,axis=0)-np.mean(noise1_array_in,axis=0))/2.*(N_dq+N_au)+N_dq*N_au/2.)/((2.*l+1.)*np.sqrt(b**2+dl_eff**2)*fsky)) # delta1=np.sqrt(2.*((np.mean(theory1_array_in,axis=0))**2+(np.mean(theory1_array_in,axis=0))/2.*(N_dq+N_au)+N_dq*N_au/2.)/((2.*l+1.)*np.sqrt(b**2+dl_eff**2)*fsky)) # cosmic1=np.sqrt(2./((2.*l+1)*np.sqrt(b**2+dl_eff**2)*fsky)*np.mean(theory1_array_in,axis=0)**2) # N_du=np.mean(Ndu_array_in) # N_aq=np.mean(Naq_array_in) # #delta2=np.sqrt(2.*abs((np.mean(cross2_array_in,axis=0)-np.mean(noise2_array_in,axis=0))**2+(np.mean(cross2_array_in,axis=0)-np.mean(noise2_array_in,axis=0))/2.*(N_dq+N_au)+N_dq*N_au/2.)/((2.*l+1.)*np.sqrt(b**2+dl_eff**2)*fsky)) # delta2=np.sqrt(2.*((np.mean(theory2_array_in,axis=0))**2+(np.mean(theory2_array_in,axis=0))/2.*(N_du+N_aq)+N_du*N_aq/2.)/((2.*l+1.)*np.sqrt(b**2+dl_eff**2)*fsky)) cosmic2=np.sqrt(2./((2*l+1)*np.sqrt(b**2+dl_eff**2)*fsky)*np.mean(theory2_array_in,axis=0)**2) theory1_array=[] theory2_array=[] cross1_array=[] cross2_array=[] # noise1_array=[] # noise2_array=[] Ndq_array=[] Ndu_array=[] Nau_array=[] Naq_array=[] plot_l=[] if( b != 1): tmp_t1=bin_llcl.bin_llcl(ll*theory1_array_in,b) tmp_t2=bin_llcl.bin_llcl(ll*theory2_array_in,b) tmp_c1=bin_llcl.bin_llcl(ll*cross1_array_in,b) tmp_c2=bin_llcl.bin_llcl(ll*cross2_array_in,b) # tmp_n1=bin_llcl.bin_llcl(ll*noise1_array_in,b) # tmp_n2=bin_llcl.bin_llcl(ll*noise2_array_in,b) theory1_array=tmp_t1['llcl'] theory2_array=tmp_t2['llcl'] theory1_array.shape += (1,) theory2_array.shape += (1,) theory1_array=theory1_array.T theory2_array=theory2_array.T plot_l= tmp_t1['l_out'] cross1_array=tmp_c1['llcl'] cross2_array=tmp_c2['llcl'] # noise1_array=tmp_n1['llcl'] # noise2_array=tmp_n2['llcl'] Ndq_array=bin_llcl.bin_llcl(ll*Ndq_array_in,b)['llcl'] Ndu_array=bin_llcl.bin_llcl(ll*Ndu_array_in,b)['llcl'] Naq_array=bin_llcl.bin_llcl(ll*Naq_array_in,b)['llcl'] Nau_array=bin_llcl.bin_llcl(ll*Nau_array_in,b)['llcl'] tmp_c1=bin_llcl.bin_llcl((ll*cosmic1)**2,b) #tmp_d1=bin_llcl.bin_llcl((ll*delta1)**2,b) cosmic1=np.sqrt(tmp_c1['llcl']) #delta1=np.sqrt(tmp_d1['llcl']) tmp_c2=bin_llcl.bin_llcl((ll*cosmic2)**2,b) #tmp_d2=bin_llcl.bin_llcl((ll*delta2)**2,b) cosmic2=np.sqrt(tmp_c2['llcl']) #delta2=np.sqrt(tmp_d2['llcl']) t_tmp=bin_llcl.bin_llcl(ll*theory_cls,b) theory_cls=t_tmp['llcl'] else: plot_l=l theory1_array=np.multiply(ll,theory1_array_in) cross1_array=np.multiply(ll,cross1_array_in) # noise1_array=np.multiply(ll,noise1_array_in) theory2_array=np.multiply(ll,theory2_array_in) cross2_array=np.multiply(ll,cross2_array_in) # noise2_array=np.multiply(ll,noise2_array_in) cosmic1*=ll cosmic2*=ll #delta1*=ll #delta2*=ll Ndq_array=np.multiply(ll,Ndq_array_in) Ndu_array=np.multiply(ll,Ndu_array_in) Naq_array=np.multiply(ll,Naq_array_in) Nau_array=np.multiply(ll,Nau_array_in) theory_cls*=ll #ipdb.set_trace() bad=np.where(plot_l < 24) N_dq=np.mean(Ndq_array,axis=0) N_du=np.mean(Ndu_array,axis=0) N_aq=np.mean(Naq_array,axis=0) N_au=np.mean(Nau_array,axis=0) #noise1=np.mean(noise1_array,axis=0) #noise2=np.mean(noise2_array,axis=0) theory1=np.mean(theory1_array,axis=0) theory2=np.mean(theory1_array,axis=0) theory_array = np.add(theory1_array,theory2_array) theory=np.mean(theory_array,axis=0) #dtheory=np.sqrt(np.var(theory1_array,ddof=1) + np.var(theory2_array,ddof=1)) #cross_array = np.add(np.subtract(cross1_array,noise1),np.subtract(cross2_array,noise2)) cross_array = np.add(cross1_array,cross2_array) cross=np.mean(cross_array,axis=0) #dcross=np.std(cross_array,axis=0,ddof=1) dcross=np.sqrt( ( np.var(cross1_array,axis=0,ddof=1) + np.var(cross2_array,axis=0,ddof=1))) cosmic=np.sqrt(cosmic1**2+cosmic2**2) delta1=np.sqrt(2./((2*plot_l+1)*fsky*np.sqrt(b**2+dl_eff**2))*(theory1**2 + theory1*(N_dq+N_au)/2. + N_dq*N_au/2.)) delta2=np.sqrt(2./((2*plot_l+1)*fsky*np.sqrt(b**2+dl_eff**2))*(theory2**2 + theory2*(N_du+N_aq)/2. + N_du*N_aq/2.)) delta=np.sqrt(delta1**2+delta2**2) #cosmic=np.abs(theory_cls)*np.sqrt(2./((2*plot_l+1)*fsky*np.sqrt(dl_eff**2+b**2))) #theory1=np.mean(theory1_array,axis=0) #dtheory1=np.std(theory1_array,axis=0,ddof=1) #cross1=np.mean(cross1_array,axis=0) #dcross1=np.std(np.subtract(cross1_array,noise1),axis=0,ddof=1) #ipdb.set_trace() good_l=np.logical_and(plot_l <= 250,plot_l >25) plot_binned.plotBinned((cross)*1e12,dcross*1e12,plot_l,b,'Cross_43x95_FR', title='QUIET FR Correlator',theory=theory*1e12,delta=delta*1e12,cosmic=cosmic*1e12) #theory2=np.mean(theory2_array,axis=0) #dtheory2=np.std(theory2_array,axis=0,ddof=1) #cross2=np.mean(cross2_array,axis=0) ##delta2=np.mean(delta2_array,axis=0) #dcross2=np.std(np.subtract(cross2_array,noise2),axis=0,ddof=1) ##ipdb.set_trace() #plot_binned.plotBinned((cross2-noise2)*1e12,dcross2*1e12,plot_l,b,'Cross_43x95_FR_UxaQ', title='Cross 43x95 FR UxaQ',theory=theory2*1e12,dtheory=dtheory2*1e12,delta=delta2*1e12,cosmic=cosmic2*1e12) #ipdb.set_trace() if b == 25 : good_l=np.logical_and(plot_l <= 250,plot_l >25) likelihood(cross[good_l],delta[good_l],theory[good_l],'field1','c2bfr') #if b == 1 : # xbar= np.matrix(ll[1:]*(cross-np.mean(cross))[1:]).T # vector=np.matrix(ll[1:]*cross[1:]).T # mu=np.matrix(ll[1:]*theory[1:]).T # fact=len(xbar)-1 # cov=(np.dot(xbar,xbar.T)/fact).squeeze() ## ipdb.set_trace() # U,S,V =np.linalg.svd(cov) # _cov= np.einsum('ij,j,jk', V.T,1./S,U.T) # likelhd=np.exp(-np.dot(np.dot((vector-mu).T,_cov),(vector-mu))/2. )/(np.sqrt(2*np.pi*np.prod(S))) ## print('Likelihood of fit is #{0:.5f}'.format(likelihood[0,0])) # f=open('FR_likelihood.txt','w') # f.write('Likelihood of fit is #{0:.5f}'.format(likelhd[0,0])) # f.close() subprocess.call('mv *01*.png bin_01/', shell=True) subprocess.call('mv *05*.png bin_05/', shell=True) subprocess.call('mv *10*.png bin_10/', shell=True) subprocess.call('mv *20*.png bin_20/', shell=True) subprocess.call('mv *25*.png bin_25/', shell=True) subprocess.call('mv *50*.png bin_50/', shell=True) subprocess.call('mv *.eps eps/', shell=True)
def main(): ##Parameters for Binning, Number of Runs ## Beam correction use_beam=0 # bls=hp.gauss_beam(smoothing_scale*np.pi/(180.*60.),383)**2 #bls=hp.gauss_beam(smoothing_scale*np.pi/(180.*60.),3*nside_out-1)**2 bls=(hp.gauss_beam(smoothing_scale*np.pi/(180.*60.),3*nside_out-1)*hp.pixwin(nside_out)[:3*nside_out])**2 N_runs=500 bins=[1,5,10,20,50] map_prefix='/home/matt/quiet/quiet_maps/' i_file=map_prefix+'quiet_simulated_43.1' j_file=map_prefix+'quiet_simulated_94.5' alpha_file='/data/wmap/faraday_MW_realdata.fits' bands=[43.1,94.5] names=['43','95'] wl=np.array([299792458./(band*1e9) for band in bands]) cross1_array_in=[] cross2_array_in=[] Ndq_array_in=[] Ndu_array_in=[] Nau_array_in=[] Naq_array_in=[] noise1_array_in=[] noise2_array_in=[] theory1_array_in=[] theory2_array_in=[] #simulate_fields.main() ttmp1,ttmp2=faraday_theory_quiet(i_file+'.fits',j_file+'.fits',wl[0],wl[1],alpha_file,names[0]+'x'+names[1],beam=use_beam) theory1_array_in.append(ttmp1) theory2_array_in.append(ttmp2) #for n in xrange(N_runs): for i in xrange(N_runs): print(Fore.WHITE+Back.GREEN+Style.BRIGHT+'Correlation #{:03d}'.format(i+1)+Back.RESET+Fore.RESET+Style.RESET_ALL) tmp1,tmp2,n1,n2,n3,n4=faraday_correlate_quiet(i_file+'.fits',j_file+'.fits',wl[0],wl[1],alpha_file,names[0]+'x'+names[1],beam=use_beam) # ntmp1,ntmp2=faraday_noise_quiet(i_file+'.fits',j_file+'.fits',wl[0],wl[1],alpha_file,names[0]+'x'+names[1],beam=use_beam) cross1_array_in.append(tmp1) cross2_array_in.append(tmp2) Ndq_array_in.append(n1) Ndu_array_in.append(n2) Nau_array_in.append(n3) Naq_array_in.append(n4) # noise1_array_in.append(ntmp1) # noise2_array_in.append(ntmp2) f=open('cl_theory_FR_QxaU.json','w') json.dump(np.array(theory1_array_in).tolist(),f) f.close() f=open('cl_theory_FR_UxaQ.json','w') json.dump(np.array(theory2_array_in).tolist(),f) f.close() theory1=np.mean(theory1_array_in,axis=0) theory2=np.mean(theory2_array_in,axis=0) hp.write_cl('cl_theory_FR_QxaU.fits',theory1) hp.write_cl('cl_theory_FR_UxaQ.fits',theory2) #f=open('cl_theory_FR_QxaU.json','r') #theory1_array=json.load(f) #f.close() #f=open('cl_theory_FR_UxaQ.json','r') #theory2_array=json.load(f) #f.close() f=open('cl_array_FR_QxaU.json','w') json.dump(np.array(cross1_array_in).tolist(),f) f.close() f=open('cl_array_FR_UxaQ.json','w') json.dump(np.array(cross2_array_in).tolist(),f) f.close() f=open('cl_Ndq_FR_QxaU.json','w') json.dump(np.array(Ndq_array_in).tolist(),f) f.close() f=open('cl_Ndu_FR_UxaQ.json','w') json.dump(np.array(Ndu_array_in).tolist(),f) f.close() f=open('cl_Nau_FR_QxaU.json','w') json.dump(np.array(Nau_array_in).tolist(),f) f.close() f=open('cl_Naq_FR_UxaQ.json','w') json.dump(np.array(Naq_array_in).tolist(),f) f.close() #f=open('cl_noise_FR_QxaU.json','w') #json.dump(np.array(noise1_array_in).tolist(),f) #f.close() #f=open('cl_noise_FR_UxaQ.json','w') #json.dump(np.array(noise2_array_in).tolist(),f) #f.close() bins=[1,5,10,20,25,50] fsky=225.*(np.pi/180.)**2/(4*np.pi) l=np.arange(len(cross1_array_in[0])) ll=l*(l+1)/(2*np.pi) L=np.sqrt(fsky*4*np.pi) dl_eff=2*np.pi/L theory1_array_in=np.array(theory1_array_in)/(fsky*bls) theory2_array_in=np.array(theory2_array_in)/(fsky*bls) cross1_array_in=np.array(cross1_array_in)/(fsky*bls) cross2_array_in=np.array(cross2_array_in)/(fsky*bls) Ndq_array_in=np.array(Ndq_array_in)/(fsky) Ndu_array_in=np.array(Ndu_array_in)/(fsky) Nau_array_in=np.array(Nau_array_in)/(fsky) Naq_array_in=np.array(Naq_array_in)/(fsky) #noise1_array_in=np.array(noise1_array_in)/(fsky*bls) #noise2_array_in=np.array(noise2_array_in)/(fsky*bls) Ndq_array_in.shape += (1,) Ndu_array_in.shape += (1,) Nau_array_in.shape += (1,) Naq_array_in.shape += (1,) for b in bins: theory_cls=hp.read_cl('/home/matt/Planck/data/faraday/correlation/fr_theory_cl.fits') # N_dq=np.mean(Ndq_array_in) # N_au=np.mean(Nau_array_in) # #delta1=np.sqrt(2.*abs((np.mean(cross1_array_in,axis=0)-np.mean(noise1_array_in,axis=0))**2+(np.mean(cross1_array_in,axis=0)-np.mean(noise1_array_in,axis=0))/2.*(N_dq+N_au)+N_dq*N_au/2.)/((2.*l+1.)*np.sqrt(b**2+dl_eff**2)*fsky)) # delta1=np.sqrt(2.*((np.mean(theory1_array_in,axis=0))**2+(np.mean(theory1_array_in,axis=0))/2.*(N_dq+N_au)+N_dq*N_au/2.)/((2.*l+1.)*np.sqrt(b**2+dl_eff**2)*fsky)) # cosmic1=np.sqrt(2./((2.*l+1)*np.sqrt(b**2+dl_eff**2)*fsky)*np.mean(theory1_array_in,axis=0)**2) # N_du=np.mean(Ndu_array_in) # N_aq=np.mean(Naq_array_in) # #delta2=np.sqrt(2.*abs((np.mean(cross2_array_in,axis=0)-np.mean(noise2_array_in,axis=0))**2+(np.mean(cross2_array_in,axis=0)-np.mean(noise2_array_in,axis=0))/2.*(N_dq+N_au)+N_dq*N_au/2.)/((2.*l+1.)*np.sqrt(b**2+dl_eff**2)*fsky)) # delta2=np.sqrt(2.*((np.mean(theory2_array_in,axis=0))**2+(np.mean(theory2_array_in,axis=0))/2.*(N_du+N_aq)+N_du*N_aq/2.)/((2.*l+1.)*np.sqrt(b**2+dl_eff**2)*fsky)) cosmic2=np.sqrt(2./((2*l+1)*np.sqrt(b**2+dl_eff**2)*fsky)*np.mean(theory2_array_in,axis=0)**2) theory1_array=[] theory2_array=[] cross1_array=[] cross2_array=[] # noise1_array=[] # noise2_array=[] Ndq_array=[] Ndu_array=[] Nau_array=[] Naq_array=[] plot_l=[] if( b != 1): tmp_t1=bin_llcl.bin_llcl(ll*theory1_array_in,b) tmp_t2=bin_llcl.bin_llcl(ll*theory2_array_in,b) tmp_c1=bin_llcl.bin_llcl(ll*cross1_array_in,b) tmp_c2=bin_llcl.bin_llcl(ll*cross2_array_in,b) # tmp_n1=bin_llcl.bin_llcl(ll*noise1_array_in,b) # tmp_n2=bin_llcl.bin_llcl(ll*noise2_array_in,b) theory1_array=tmp_t1['llcl'] theory2_array=tmp_t2['llcl'] theory1_array.shape += (1,) theory2_array.shape += (1,) theory1_array=theory1_array.T theory2_array=theory2_array.T plot_l= tmp_t1['l_out'] cross1_array=tmp_c1['llcl'] cross2_array=tmp_c2['llcl'] # noise1_array=tmp_n1['llcl'] # noise2_array=tmp_n2['llcl'] Ndq_array=bin_llcl.bin_llcl(ll*Ndq_array_in,b)['llcl'] Ndu_array=bin_llcl.bin_llcl(ll*Ndu_array_in,b)['llcl'] Naq_array=bin_llcl.bin_llcl(ll*Naq_array_in,b)['llcl'] Nau_array=bin_llcl.bin_llcl(ll*Nau_array_in,b)['llcl'] tmp_c1=bin_llcl.bin_llcl((ll*cosmic1)**2,b) #tmp_d1=bin_llcl.bin_llcl((ll*delta1)**2,b) cosmic1=np.sqrt(tmp_c1['llcl']) #delta1=np.sqrt(tmp_d1['llcl']) tmp_c2=bin_llcl.bin_llcl((ll*cosmic2)**2,b) #tmp_d2=bin_llcl.bin_llcl((ll*delta2)**2,b) cosmic2=np.sqrt(tmp_c2['llcl']) #delta2=np.sqrt(tmp_d2['llcl']) t_tmp=bin_llcl.bin_llcl(ll*theory_cls,b) theory_cls=t_tmp['llcl'] else: plot_l=l theory1_array=np.multiply(ll,theory1_array_in) cross1_array=np.multiply(ll,cross1_array_in) # noise1_array=np.multiply(ll,noise1_array_in) theory2_array=np.multiply(ll,theory2_array_in) cross2_array=np.multiply(ll,cross2_array_in) # noise2_array=np.multiply(ll,noise2_array_in) cosmic1*=ll cosmic2*=ll #delta1*=ll #delta2*=ll Ndq_array=np.multiply(ll,Ndq_array_in) Ndu_array=np.multiply(ll,Ndu_array_in) Naq_array=np.multiply(ll,Naq_array_in) Nau_array=np.multiply(ll,Nau_array_in) theory_cls*=ll #ipdb.set_trace() bad=np.where(plot_l < 24) N_dq=np.mean(Ndq_array,axis=0) N_du=np.mean(Ndu_array,axis=0) N_aq=np.mean(Naq_array,axis=0) N_au=np.mean(Nau_array,axis=0) #noise1=np.mean(noise1_array,axis=0) #noise2=np.mean(noise2_array,axis=0) theory1=np.mean(theory1_array,axis=0) theory2=np.mean(theory1_array,axis=0) theory_array = np.add(theory1_array,theory2_array) theory=np.mean(theory_array,axis=0) #dtheory=np.sqrt(np.var(theory1_array,ddof=1) + np.var(theory2_array,ddof=1)) #cross_array = np.add(np.subtract(cross1_array,noise1),np.subtract(cross2_array,noise2)) cross_array = np.add(cross1_array,cross2_array) cross=np.mean(cross_array,axis=0) #dcross=np.std(cross_array,axis=0,ddof=1) dcross=np.sqrt( ( np.var(cross1_array,axis=0,ddof=1) + np.var(cross2_array,axis=0,ddof=1))) cosmic=np.sqrt(cosmic1**2+cosmic2**2) delta1=np.sqrt(2./((2*plot_l+1)*fsky*np.sqrt(b**2+dl_eff**2))*(theory1**2 + theory1*(N_dq+N_au)/2. + N_dq*N_au/2.)) delta2=np.sqrt(2./((2*plot_l+1)*fsky*np.sqrt(b**2+dl_eff**2))*(theory2**2 + theory2*(N_du+N_aq)/2. + N_du*N_aq/2.)) delta=np.sqrt(delta1**2+delta2**2) #cosmic=np.abs(theory_cls)*np.sqrt(2./((2*plot_l+1)*fsky*np.sqrt(dl_eff**2+b**2))) #theory1=np.mean(theory1_array,axis=0) #dtheory1=np.std(theory1_array,axis=0,ddof=1) #cross1=np.mean(cross1_array,axis=0) #dcross1=np.std(np.subtract(cross1_array,noise1),axis=0,ddof=1) #ipdb.set_trace() plot_binned.plotBinned((cross)*1e12,dcross*1e12,plot_l,b,'Cross_43x95_FR', title='QUIET FR Correlator',theory=theory*1e12,delta=delta*1e12,cosmic=cosmic*1e12) #theory2=np.mean(theory2_array,axis=0) #dtheory2=np.std(theory2_array,axis=0,ddof=1) #cross2=np.mean(cross2_array,axis=0) ##delta2=np.mean(delta2_array,axis=0) #dcross2=np.std(np.subtract(cross2_array,noise2),axis=0,ddof=1) ##ipdb.set_trace() #plot_binned.plotBinned((cross2-noise2)*1e12,dcross2*1e12,plot_l,b,'Cross_43x95_FR_UxaQ', title='Cross 43x95 FR UxaQ',theory=theory2*1e12,dtheory=dtheory2*1e12,delta=delta2*1e12,cosmic=cosmic2*1e12) #ipdb.set_trace() if b == 25 : good_l=np.logical_and(plot_l <= 200,plot_l >25) likelihood(cross[good_l],delta[good_l],theory[good_l],'field1','c2bfr') #if b == 1 : # xbar= np.matrix(ll[1:]*(cross-np.mean(cross))[1:]).T # vector=np.matrix(ll[1:]*cross[1:]).T # mu=np.matrix(ll[1:]*theory[1:]).T # fact=len(xbar)-1 # cov=(np.dot(xbar,xbar.T)/fact).squeeze() ## ipdb.set_trace() # U,S,V =np.linalg.svd(cov) # _cov= np.einsum('ij,j,jk', V.T,1./S,U.T) # likelhd=np.exp(-np.dot(np.dot((vector-mu).T,_cov),(vector-mu))/2. )/(np.sqrt(2*np.pi*np.prod(S))) ## print('Likelihood of fit is #{0:.5f}'.format(likelihood[0,0])) # f=open('FR_likelihood.txt','w') # f.write('Likelihood of fit is #{0:.5f}'.format(likelhd[0,0])) # f.close() subprocess.call('mv *01*.png bin_01/', shell=True) subprocess.call('mv *05*.png bin_05/', shell=True) subprocess.call('mv *10*.png bin_10/', shell=True) subprocess.call('mv *20*.png bin_20/', shell=True) subprocess.call('mv *25*.png bin_25/', shell=True) subprocess.call('mv *50*.png bin_50/', shell=True) subprocess.call('mv *.eps eps/', shell=True)
def getSMICA(theta_i=0.0, theta_f=180.0, nSteps=1800, lmax=100, lmin=2, newSMICA=False, useSPICE=True, newDeg=False, R1=False): """ Purpose: load CMB and mask maps from files, return correlation function for unmasked and masked CMB Mostly follows Copi et. al. 2013 for cut sky C(theta) Uses: get_crosspower.py (for plotting) C(theta) save file getSMICAfile.npy Inputs: theta_i,theta_f: starting and ending points for C(theta) in degrees nSteps: number of intervals between i,f points lmax: the maximum l value to include in legendre series for C(theta) lmin: the lowest l to use in C(theta,Cl) and S_{1/2} = CIC calculation newSMICA: set to True to reload data from files and recompute if False, will load C(theta) curves from file useSPICE: if True, will use SPICE to find power spectra if False, will use anafast, following Copi et. al. 2013 Default: True newDeg: set to True to recalculate map and mask degredations Note: the saved files are dependent on the value of lmax that was used Default: False R1: set to True to use R1 versions of SMICA and mask. Otherwise, R2 is used Only affects which Planck files are used; irrelevant if newDeg=False. Default: False Outupts: theta: nSteps+1 angles that C(theta) arrays are for (degrees) unmasked: C(theta) unmasked (microK^2) masked: C(theta) masked (microK^2) """ saveFile = 'getSMICAfile.npy' #for anafast saveFile2 = 'getSMICAfile2.npy' #for spice if newSMICA: # start with map degredations mapDegFile = 'smicaMapDeg.fits' maskDegFile = 'maskMapDeg.fits' if newDeg: # load maps; default files have 2048,NESTED,GALACTIC dataDir = '/Data/' if R1: smicaFile = 'COM_CompMap_CMB-smica-field-I_2048_R1.20.fits' maskFile = 'COM_Mask_CMB-union_2048_R1.10.fits' else: smicaFile = 'COM_CMB_IQU-smica-field-int_2048_R2.01_full.fits' maskFile = 'COM_CMB_IQU-common-field-MaskInt_2048_R2.01.fits' print 'opening file ', smicaFile, '... ' smicaMap, smicaHead = hp.read_map(dataDir + smicaFile, nest=True, h=True) print 'opening file ', maskFile, '... ' maskMap, maskHead = hp.read_map(dataDir + maskFile, nest=True, h=True) if R1: smicaMap *= 1e-6 #microK to K # degrade map and mask resolutions from 2048 to 128; convert NESTED to RING useAlm = True # set to True to do harmonic space scaling, False for ud_grade NSIDE_big = 2048 NSIDE_deg = 128 while 4 * NSIDE_deg < lmax: NSIDE_deg *= 2 print 'resampling maps at NSIDE = ', NSIDE_deg, '... ' order_out = 'RING' if useAlm: # transform to harmonic space smicaMapRing = hp.reorder(smicaMap, n2r=True) maskMapRing = hp.reorder(maskMap, n2r=True) smicaCl, smicaAlm = hp.anafast(smicaMapRing, alm=True, lmax=lmax) maskCl, maskAlm = hp.anafast(maskMapRing, alm=True, lmax=lmax) # this gives 101 Cl values and 5151 Alm values. Why not all 10201 Alm.s? # scale by pixel window functions bigWin = hp.pixwin(NSIDE_big) degWin = hp.pixwin(NSIDE_deg) winRatio = degWin / bigWin[:degWin.size] degSmicaAlm = hp.almxfl(smicaAlm, winRatio) degMaskAlm = hp.almxfl(maskAlm, winRatio) # re-transform back to real space smicaMapDeg = hp.alm2map(degSmicaAlm, NSIDE_deg) maskMapDeg = hp.alm2map(degMaskAlm, NSIDE_deg) else: smicaMapDeg = hp.ud_grade(smicaMap, nside_out=NSIDE_deg, order_in='NESTED', order_out=order_out) maskMapDeg = hp.ud_grade(maskMap, nside_out=NSIDE_deg, order_in='NESTED', order_out=order_out) # note: degraded resolution mask will no longer be only 0s and 1s. # Should it be? Yes. # turn smoothed mask back to 0s,1s mask threshold = 0.9 maskMapDeg[np.where(maskMapDeg > threshold)] = 1 maskMapDeg[np.where(maskMapDeg <= threshold)] = 0 #testing #hp.mollview(smicaMapDeg) #plt.show() #hp.mollview(maskMapDeg) #plt.show() #return 0 hp.write_map(mapDegFile, smicaMapDeg, nest=False) # use False if order_out='RING' above hp.write_map(maskDegFile, maskMapDeg, nest=False) else: # just load previous degradations (dependent on previous lmax) print 'loading previously degraded map and mask...' smicaMapDeg = hp.read_map(mapDegFile, nest=False) maskMapDeg = hp.read_map(maskDegFile, nest=False) # find power spectra print 'find power spectra... ' if useSPICE: ClFile1 = 'spiceCl_unmasked.fits' ClFile2 = 'spiceCl_masked.fits' # note: lmax for spice is 3*NSIDE-1 or less ispice(mapDegFile, ClFile1, subav="YES", subdipole="YES") Cl_unmasked = hp.read_cl(ClFile1) ispice(mapDegFile, ClFile2, maskfile1=maskDegFile, subav="YES", subdipole="YES") Cl_masked = hp.read_cl(ClFile2) Cl_mask = np.zeros(Cl_unmasked.shape[0]) # just a placeholder ell = np.arange(Cl_unmasked.shape[0]) else: # use anafast Cl_unmasked = hp.anafast(smicaMapDeg, lmax=lmax) Cl_masked = hp.anafast(smicaMapDeg * maskMapDeg, lmax=lmax) Cl_mask = hp.anafast(maskMapDeg, lmax=lmax) ell = np.arange(lmax + 1) #anafast output seems to start at l=0 # plot them doPlot = False #True if doPlot: gcp.showCl(ell, np.array([Cl_masked, Cl_unmasked]), title='power spectra of unmasked, masked SMICA map') # Legendre transform to real space print 'Legendre transform to real space... ' # note: getCovar uses linspace in x for thetaArray thetaDomain, CofTheta = getCovar(ell[:lmax + 1], Cl_unmasked[:lmax + 1], theta_i=theta_i, theta_f=theta_f, nSteps=nSteps, lmin=lmin) thetaDomain, CCutofThetaTA = getCovar(ell[:lmax + 1], Cl_masked[:lmax + 1], theta_i=theta_i, theta_f=theta_f, nSteps=nSteps, lmin=lmin) CofTheta *= 1e12 # K^2 to microK^2 CCutofThetaTA *= 1e12 # K^2 to microK^2 if useSPICE: CCutofTheta = CCutofThetaTA #/(4*np.pi) else: thetaDomain, AofThetaInverse = getCovar( ell[:lmax + 1], Cl_mask[:lmax + 1], theta_i=theta_i, theta_f=theta_f, nSteps=nSteps, lmin=0) # don't zilch the mask # note: zilching the mask's low power drastically changed C(theta) for masked anafast # Not sure why. CCutofTheta = CCutofThetaTA / AofThetaInverse xArray = np.cos(thetaDomain * np.pi / 180.) # back to frequency space for S_{1/2} = CIC calculation if useSPICE: CCutofL = Cl_masked[:lmax + 1] * 1e12 #K^2 to microK^2 else: legCoefs = legfit(xArray, CCutofTheta, lmax) CCutofL = legCoefs * (4 * np.pi) / (2 * ell[:lmax + 1] + 1) # S_{1/2} myJmn = getJmn(lmax=lmax) SMasked = np.dot(CCutofL[lmin:], np.dot(myJmn[lmin:, lmin:], CCutofL[lmin:])) SNoMask = np.dot( Cl_unmasked[lmin:lmax + 1], np.dot(myJmn[lmin:, lmin:], Cl_unmasked[lmin:lmax + 1])) * 1e24 #two factors of K^2 to muK^2 # save results if useSPICE: np.save( saveFile2, np.array( [thetaDomain, CofTheta, CCutofTheta, SNoMask, SMasked])) else: np.save( saveFile, np.array( [thetaDomain, CofTheta, CCutofTheta, SNoMask, SMasked])) else: # load from file if useSPICE: fileData = np.load(saveFile2) else: fileData = np.load(saveFile) thetaDomain = fileData[0] CofTheta = fileData[1] CCutofTheta = fileData[2] SNoMask = fileData[3] SMasked = fileData[4] return thetaDomain, CofTheta, CCutofTheta, SNoMask, SMasked
# ok so this is a bit overcomplicated because we need to take into account CAR and HEALPIX # for CAR the pixel window function deconvolution is done in Fourier space and take into account # the anisotropy if the pixwin # In HEALPIX it's a simple 1d function in multipole space # we also need to take account the case where we have projected Planck into a CAR pixellisation since # the native pixel window function of Planck need to be deconvolved if win_T.pixel == "CAR": wy, wx = enmap.calc_window(win_T.data.shape) inv_pixwin_lxly = (wy[:, None] * wx[None, :])**(-1) pixwin_l[sv] = np.ones(2 * lmax) if sv == "Planck": print("Deconvolve Planck pixel window function") # we include this special case for Planck projected in CAR taking into account the Planck native pixellisation # we should check if the projection doesn't include an extra pixel window inv_pixwin_lxly = None pixwin_l[sv] = hp.pixwin(2048) elif win_T.pixel == "HEALPIX": pixwin_l[sv] = hp.pixwin(win_T.nside) else: inv_pixwin_lxly = None t = time.time() for k, map in enumerate(maps): if win_T.pixel == "CAR": split = so_map.read_map(map, geometry=win_T.data.geometry) if d["src_free_maps_%s" % sv] == True:
#det_eff=.85 #ndays=15.*24*3600 #FPU=np.array([5,5,3]) # #noise_const_array=ukdet*np.sqrt(4125.*60**2)/np.sqrt(FPU*ndet*det_eff*ndays)/pix_area_array*1e-6 #gamma_dust=6.626e-34/(1.38e-23*21) #dust_factor=krj_to_kcmb*np.array([1e-6*(np.exp(gamma_dust*353e9)-1)/(np.exp(gamma_dust*x*1e9)-1)* (x/353.)**2.54 for x in bands]) #krj_to_kcmb=np.array([1.,1.,1.]) krj_to_kcmb=np.ones_like(bands) # sync_factor=krj_to_kcmb*np.array([1e-6*(30./x)**2 for x in bands]) beam=hp.gauss_beam(smoothing_scale*np.pi/(180.*60.),3*nside_out-1) bls=(hp.gauss_beam(smoothing_scale*np.pi/(180.*60.),3*nside_out-1)*hp.pixwin(nside_out)[:3*nside_out])**2 l=np.arange(3*nside_out) ll=l*(l+1)/(2*np.pi) def likelihood(cross,dcross,theory,name,title): Sig=np.sum(cross/(dcross**2))/np.sum(1./dcross**2) #Noise=np.std(np.sum(cross_array/dcross**2,axis=1)/np.sum(1./dcross**2)) \ Noise=np.sqrt(1./np.sum(1./dcross**2)) Sig1=np.sum(cross*(theory/dcross)**2)/np.sum((theory/dcross)**2) Noise1=np.sqrt(np.sum(dcross**2*(theory/dcross)**2)/np.sum((theory/dcross)**2)) SNR=Sig/Noise SNR1=Sig1/Noise1
def covth(nside,ipok,lmax,bl,spectra,polar=True,temp=True,allinone=True): all=['I','Q','U'] if not temp: all=['Q','U'] if not polar: all=['I'] rpix=np.array(hp.pix2vec(nside,ipok)) allcosang=np.dot(np.transpose(rpix),rpix) pixwin=hp.pixwin(nside)[0:lmax+1] nspec,nell=np.shape(spectra) ell=spectra[0] maskl=ell<(lmax+1) ell=ell[maskl] ctt=spectra[1][maskl] cee=spectra[2][maskl] cbb=spectra[3][maskl] cte=spectra[4][maskl] if nspec==5: print('Assuming EB and TB are zero') ceb=0 ctb=0 else: ceb=spectra[5][maskl] ctb=spectra[6][maskl] norm=(2*ell+1)/(4*np.pi)*(pixwin**2)*(bl[maskl]**2) effctt=norm*ctt effcte=norm*cte effcee=norm*cee effcbb=norm*cbb effceb=norm*ceb effctb=norm*ctb nbpixok=ipok.size nstokes=np.size(all) print(nstokes) cov=np.zeros((nstokes,nstokes,nbpixok,nbpixok)) for i in np.arange(nbpixok): pyquad.progress_bar(i,nbpixok) for j in np.arange(i,nbpixok): if nstokes==1: pl=pl0(allcosang[i,j],lmax) TT=effctt*pl cov[0,0,i,j]=np.sum(TT) cov[0,0,j,i]=cov[0,0,i,j] if nstokes==2: cij,sij=polrotangle(rpix[:,i],rpix[:,j]) cji,sji=polrotangle(rpix[:,j],rpix[:,i]) f12=F1l2(allcosang[i,j],lmax) f22=F2l2(allcosang[i,j],lmax) QQ=f12*effcee-f22*effcbb UU=f12*effcbb-f22*effcee QU=(f12+f22)*effceb cQQpsQU = ( cij*QQ + sij*QU ) cQUpsUU = ( cij*QU + sij*UU ) cQUmsQQ = ( cij*QU - sij*QQ ) cUUmsQU = ( cij*UU - sij*QU ) cov[0,0,i,j] = np.sum( cji*cQQpsQU + sji*cQUpsUU ) cov[0,0,j,i] = cov[0,0,i,j] cov[0,1,i,j] = np.sum(-sji*cQQpsQU + cji*cQUpsUU ) cov[1,0,j,i] = cov[0,1,i,j] cov[1,1,i,j] = np.sum(-sji*cQUmsQQ + cji*cUUmsQU ) cov[1,1,j,i] = cov[1,1,i,j] cov[0,1,j,i] = np.sum( cji*cQUmsQQ + sji*cUUmsQU ) cov[1,0,i,j] = cov[0,1,j,i] if nstokes==3: cij,sij=polrotangle(rpix[:,i],rpix[:,j]) cji,sji=polrotangle(rpix[:,j],rpix[:,i]) pl=pl0(allcosang[i,j],lmax) f10=F1l0(allcosang[i,j],lmax) f12=F1l2(allcosang[i,j],lmax) f22=F2l2(allcosang[i,j],lmax) TT=effctt*pl QQ=f12*effcee-f22*effcbb UU=f12*effcbb-f22*effcee TQ=-f10*effcte TU=-f10*effctb QU=(f12+f22)*effceb cQQpsQU = ( cij*QQ + sij*QU ) cQUpsUU = ( cij*QU + sij*UU ) cQUmsQQ = ( cij*QU - sij*QQ ) cUUmsQU = ( cij*UU - sij*QU ) cov[0,0,i,j] = np.sum( TT ) cov[0,0,j,i] = cov[0,0,i,j] cov[0,1,i,j] = np.sum( cji*TQ + sji*TU ) cov[1,0,j,i] = cov[0,1,i,j] cov[1,0,i,j] = np.sum( cij*TQ + sij*TU ) cov[0,1,j,i] = cov[1,0,i,j] cov[0,2,i,j] = np.sum(-sji*TQ + cij*TU ) cov[2,0,j,i] = cov[0,2,i,j] cov[2,0,i,j] = np.sum(-sij*TQ + cij*TU ) cov[0,2,j,i] = cov[2,0,i,j] cov[1,1,i,j] = np.sum( cji*cQQpsQU + sji*cQUpsUU ) cov[1,1,j,i] = cov[1,1,i,j] cov[1,2,i,j] = np.sum(-sji*cQQpsQU + cji*cQUpsUU ) cov[2,1,j,i] = cov[1,2,i,j] cov[2,1,i,j] = np.sum( cji*cQUmsQQ + sji*cUUmsQU ) cov[1,2,j,i] = cov[2,1,i,j] cov[2,2,i,j] = np.sum(-sji*cQUmsQQ + cji*cUUmsQU ) cov[2,2,j,i] = cov[2,2,i,j] if allinone==True: return(allmat2bigmat(cov)) else: return cov