def _get_response(comp,param_override=None): if bandpasses: if no_act_color_correction: r = tfg.get_mix_bandpassed(bps, comp, bandpass_shifts=shifts, param_dict_override=param_override) else: r = tfg.get_mix_bandpassed(bps, comp, bandpass_shifts=shifts, ccor_cen_nus=cfreqs, ccor_beams=lbeams, ccor_exps = [ccor_exp] * narrays, param_dict_override=param_override) else: r = tfg.get_mix(bps, comp,param_dict_override=param_override) return r
# Reconvolve to the same beam and filter out modes < ellmin ls, obells = np.loadtxt(bfile, unpack=True) obeam = maps.interp(ls, obells)(modlmap) beam_ratio = kbeam / obeam beam_ratio[~np.isfinite(beam_ratio)] = 0 kmask = maps.mask_kspace(mask.shape, mask.wcs, lmin=lmin, lmax=30000) # Get coadd map of reference array kmap = enmap.fft( dm.get_coadd("s15", region, array, srcfree=True, ncomp=1)[0] * mask) # Get pixel window correction pwin = tutils.get_pixwin(mask.shape[-2:]) bpfile = "data/" + dm.get_bandpass_file_name(array) # Apply a filter that converts array map to Compton Y units (Eq 8) f = tfg.get_mix_bandpassed([bpfile], 'tSZ', ccor_cen_nus=[freqs[array]], ccor_beams=[lbeam])[0] f2d = maps.interp(ells, f)(modlmap) filt = kmask / pwin / f2d filt[~np.isfinite(filt)] = 0 imap = enmap.ifft(kmap * filt).real # Reconvolve Y map ymap = maps.filter_map(enmap.read_map(yfile), beam_ratio * kmask) # Get CMB maps smap = enmap.read_map(yfile2) cmap = enmap.read_map(yfile3) # Width in pixels of stamp cutout pix = 60
def __init__(self,qids,fg_res_version=None,ellmax=8101,bandpassed=True,no_act_color_correction=False,ccor_exp=-1): self.qids = qids self.alms = {} ells = np.arange(ellmax) self.ellmax = ellmax self.cyy = fgs.power_y(ells)[None,None] self.cyy[0,0][ells<2] = 0 narrays = len(qids) if fg_res_version is not None: fpath = sints.dconfig['actsims']['fg_res_path'] + "/" + fg_res_version + "/" self.cfgres = np.zeros((narrays,narrays,ellmax)) for i in range(narrays): for j in range(i,narrays): qid1 = qids[i] qid2 = qids[j] clfilename = "%shfgcov_%s_%s.txt" % (fpath,qid1,qid2) #!!!! Changed fgcov to hfgcov # !!!! clfilename_alt = "%shfgcov_%s_%s.txt" % (fpath,qid2,qid1) #!!!! Changed fgcov to hfgcov try: ls,cls = np.loadtxt(clfilename,unpack=True) except: ls,cls = np.loadtxt(clfilename_alt,unpack=True) assert np.all(np.isclose(ls,ells)) self.cfgres[i,j] = cls.copy() if i!=j: self.cfgres[j,i] = cls.copy() # self.cfgres[j,i] = self.cfgres[i,j] = cls.copy()*0 # !!!! REMOVED CORR #if (qid1 in ['p01','p02']) or (qid2 in ['p01','p02']): self.cfgres[j,i] = self.cfgres[i,j] = 0 # !!!! # self.cfgres[j,i][ls<300] = self.cfgres[i,j][ls<300] = 0 # !!!! else: self.cfgres = None # get tSZ response, and also zero out parts of map that are not observed aspecs = tutils.ASpecs().get_specs bps = [] cfreqs = [] lbeams = [] ells = np.arange(0,35000) for qid in qids: dm = sints.models[sints.arrays(qid,'data_model')]() if dm.name=='act_mr3': season,array1,array2 = sints.arrays(qid,'season'),sints.arrays(qid,'array'),sints.arrays(qid,'freq') array = '_'.join([array1,array2]) lbeam = tutils.get_kbeam(qid,ells,sanitize=True,planck_pixwin=False) # note no pixwin but doesnt matter since no ccorr for planck elif dm.name=='planck_hybrid': season,patch,array = None,None,sints.arrays(qid,'freq') lbeam = None else: raise ValueError lbeams.append(lbeam) lmin,lmax,hybrid,radial,friend,cfreq,fgroup,wrfit = aspecs(qid) if bandpassed: bps.append("data/"+dm.get_bandpass_file_name(array)) else: bps.append(cfreq) cfreqs.append(cfreq) if bandpassed: if no_act_color_correction: self.tsz_fnu = tfg.get_mix_bandpassed(bps, 'tSZ') else: self.tsz_fnu = tfg.get_mix_bandpassed(bps, 'tSZ', ccor_cen_nus=cfreqs, ccor_beams=lbeams, ccor_exps = [ccor_exp] * narrays) else: self.tsz_fnu = tfg.get_mix(bps, 'tSZ')
def build_and_save_ilc(arrays,region,version,cov_version,beam_version, solutions,beams,chunk_size, effective_freq,overwrite,maxval,unsanitized_beam=False,do_weights=False, pa1_shift = None, pa2_shift = None, pa3_150_shift = None, pa3_090_shift = None, no_act_color_correction=False, ccor_exp = -1, isotropize=False, isotropize_width=20): print("Chunk size is ", chunk_size*64./8./1024./1024./1024., " GB.") def warn(): print("WARNING: no bandpass file found. Assuming array ",dm.c['id']," has no response to CMB, tSZ and CIB.") aspecs = tutils.ASpecs().get_specs bandpasses = not(effective_freq) savedir = tutils.get_save_path(version,region) covdir = tutils.get_save_path(cov_version,region) assert os.path.exists(covdir) if not(overwrite): assert not(os.path.exists(savedir)), \ "This version already exists on disk. Please use a different version identifier." try: os.makedirs(savedir) except: if overwrite: pass else: raise mask = enmap.read_map(covdir+"tilec_mask.fits") shape,wcs = mask.shape,mask.wcs Ny,Nx = shape modlmap = enmap.modlmap(shape,wcs) arrays = arrays.split(',') narrays = len(arrays) kcoadds = [] kbeams = [] bps = [] names = [] lmins = [] lmaxs = [] shifts = [] cfreqs = [] lbeams = [] ells = np.arange(0,modlmap.max()) for i,qid in enumerate(arrays): dm = sints.models[sints.arrays(qid,'data_model')](region=mask,calibrated=True) lmin,lmax,hybrid,radial,friend,cfreq,fgroup,wrfit = aspecs(qid) cfreqs.append(cfreq) lmins.append(lmin) lmaxs.append(lmax) names.append(qid) if dm.name=='act_mr3': season,array1,array2 = sints.arrays(qid,'season'),sints.arrays(qid,'array'),sints.arrays(qid,'freq') array = '_'.join([array1,array2]) elif dm.name=='planck_hybrid': season,patch,array = None,None,sints.arrays(qid,'freq') else: raise ValueError kcoadd_name = covdir + "kcoadd_%s.npy" % qid kmask = maps.mask_kspace(shape,wcs,lmin=lmin,lmax=lmax) kcoadd = enmap.enmap(np.load(kcoadd_name),wcs) dtype = kcoadd.dtype kcoadds.append(kcoadd.copy()*kmask) kbeam = tutils.get_kbeam(qid,modlmap,sanitize=not(unsanitized_beam),version=beam_version,planck_pixwin=True) if dm.name=='act_mr3': lbeam = tutils.get_kbeam(qid,ells,sanitize=not(unsanitized_beam),version=beam_version,planck_pixwin=False) # note no pixwin but doesnt matter since no ccorr for planck elif dm.name=='planck_hybrid': lbeam = None else: raise ValueError lbeams.append(lbeam) kbeams.append(kbeam.copy()) if bandpasses: try: fname = dm.get_bandpass_file_name(array) bps.append("data/"+fname) if (pa1_shift is not None) and 'PA1' in fname: shifts.append(pa1_shift) elif (pa2_shift is not None) and 'PA2' in fname: shifts.append(pa2_shift) elif (pa3_150_shift is not None) and ('PA3' in fname) and ('150' in fname): shifts.append(pa3_150_shift) elif (pa3_090_shift is not None) and ('PA3' in fname) and ('090' in fname): shifts.append(pa3_90_shift) else: shifts.append(0) except: warn() bps.append(None) else: try: bps.append(cfreq) except: warn() bps.append(None) kcoadds = enmap.enmap(np.stack(kcoadds),wcs) # Read Covmat cov = maps.SymMat(narrays,shape[-2:]) for aindex1 in range(narrays): for aindex2 in range(aindex1,narrays): icov = enmap.enmap(np.load(covdir+"tilec_hybrid_covariance_%s_%s.npy" % (names[aindex1],names[aindex2])),wcs) if isotropize: bin_edges = np.append([0.],np.arange(min(lmins),modlmap.max(),isotropize_width)) binner = stats.bin2D(modlmap,bin_edges) ls,c1d = binner.bin(icov) icov = maps.interp(ls,c1d)(modlmap) if aindex1==aindex2: icov[modlmap<lmins[aindex1]] = maxval icov[modlmap>lmaxs[aindex1]] = maxval cov[aindex1,aindex2] = icov cov.data = enmap.enmap(cov.data,wcs,copy=False) covfunc = lambda sel: cov.to_array(sel,flatten=True) assert cov.data.shape[0]==((narrays*(narrays+1))/2) # FIXME: generalize assert np.all(np.isfinite(cov.data)) # Make responses responses = {} for comp in ['tSZ','CMB','CIB']: if bandpasses: if no_act_color_correction: responses[comp] = tfg.get_mix_bandpassed(bps, comp, bandpass_shifts=shifts) else: responses[comp] = tfg.get_mix_bandpassed(bps, comp, bandpass_shifts=shifts, ccor_cen_nus=cfreqs, ccor_beams=lbeams, ccor_exps = [ccor_exp] * narrays) else: responses[comp] = tfg.get_mix(bps, comp) ilcgen = ilc.chunked_ilc(modlmap,np.stack(kbeams),covfunc,chunk_size,responses=responses,invert=True) # Initialize containers solutions = solutions.split(',') data = {} kcoadds = kcoadds.reshape((narrays,Ny*Nx)) for solution in solutions: data[solution] = {} comps = solution.split('-') data[solution]['comps'] = comps if len(comps)<=2: data[solution]['noise'] = enmap.zeros((Ny*Nx),wcs) if len(comps)==2: data[solution]['cnoise'] = enmap.zeros((Ny*Nx),wcs) data[solution]['kmap'] = enmap.zeros((Ny*Nx),wcs,dtype=dtype) # FIXME: reduce dtype? if do_weights and len(comps)<=2: for qid in arrays: data[solution]['weight_%s' % qid] = enmap.zeros((Ny*Nx),wcs) for chunknum,(hilc,selchunk) in enumerate(ilcgen): print("ILC on chunk ", chunknum+1, " / ",int(modlmap.size/chunk_size)+1," ...") for solution in solutions: comps = data[solution]['comps'] if len(comps)==1: # GENERALIZE data[solution]['noise'][selchunk] = hilc.standard_noise(comps[0]) if do_weights: weight = hilc.standard_weight(comps[0]) data[solution]['kmap'][selchunk] = hilc.standard_map(kcoadds[...,selchunk],comps[0]) elif len(comps)==2: data[solution]['noise'][selchunk] = hilc.constrained_noise(comps[0],comps[1]) data[solution]['cnoise'][selchunk] = hilc.cross_noise(comps[0],comps[1]) ret = hilc.constrained_map(kcoadds[...,selchunk],comps[0],comps[1],return_weight=do_weights) if do_weights: data[solution]['kmap'][selchunk],weight = ret else: data[solution]['kmap'][selchunk] = ret elif len(comps)>2: data[solution]['kmap'][selchunk] = np.nan_to_num(hilc.multi_constrained_map(kcoadds[...,selchunk],comps[0],*comps[1:])) if len(comps)<=2 and do_weights: for qind,qid in enumerate(arrays): data[solution]['weight_%s' % qid][selchunk] = weight[qind] del ilcgen,cov # Reshape into maps name_map = {'CMB':'cmb','tSZ':'comptony','CIB':'cib'} beams = beams.split(',') for solution,beam in zip(solutions,beams): comps = "tilec_single_tile_"+region+"_" comps = comps + name_map[data[solution]['comps'][0]]+"_" if len(data[solution]['comps'])>1: comps = comps + "deprojects_"+ '_'.join([name_map[x] for x in data[solution]['comps'][1:]]) + "_" comps = comps + version if do_weights and len(data[solution]['comps'])<=2: for qind,qid in enumerate(arrays): enmap.write_map("%s/%s_%s_weight.fits" % (savedir,comps,qid), enmap.enmap(data[solution]['weight_%s' % qid].reshape((Ny,Nx)),wcs)) try: noise = enmap.enmap(data[solution]['noise'].reshape((Ny,Nx)),wcs) enmap.write_map("%s/%s_noise.fits" % (savedir,comps),noise) except: pass try: cnoise = enmap.enmap(data[solution]['cnoise'].reshape((Ny,Nx)),wcs) enmap.write_map("%s/%s_cross_noise.fits" % (savedir,comps),cnoise) except: pass ells = np.arange(0,modlmap.max(),1) try: fbeam = float(beam) kbeam = maps.gauss_beam(modlmap,fbeam) lbeam = maps.gauss_beam(ells,fbeam) except: qid = beam bfunc = lambda x: tutils.get_kbeam(qid,x,version=beam_version,sanitize=not(unsanitized_beam),planck_pixwin=False) kbeam = bfunc(modlmap) lbeam = bfunc(ells) kmap = enmap.enmap(data[solution]['kmap'].reshape((Ny,Nx)),wcs) smap = enmap.ifft(kbeam*kmap,normalize='phys').real enmap.write_map("%s/%s.fits" % (savedir,comps),smap) io.save_cols("%s/%s_beam.txt" % (savedir,comps),(ells,lbeam),header="ell beam") enmap.write_map(savedir+"/tilec_mask.fits",mask)
maxval=maxval, theory_signal=args.theory) cov.data = enmap.enmap(cov.data, ewcs, copy=False) covfunc = lambda sel: cov.to_array(sel, flatten=True) assert cov.data.shape[0] == ( (narrays * (narrays + 1)) / 2) # FIXME: generalize assert np.all(np.isfinite(cov.data)) # bps, kbeams, # Make responses responses = {} for comp in ['tSZ', 'CMB', 'CIB']: if bandpasses: responses[comp] = tfg.get_mix_bandpassed(bps, comp) else: responses[comp] = tfg.get_mix(bps, comp) ilcgen = ilc.chunked_ilc(modlmap, np.stack(kbeams), covfunc, chunk_size, responses=responses, invert=True) Ny, Nx = eshape[-2:] # Initialize containers data = {} for qind, qid in enumerate(aids): lmin = lmins[qind]
lmin, lmax, hybrid, radial, friend, cfreq, fgroup, wrfit = aspecs( qid) dm = sints.models[sints.arrays(qid, 'data_model')]() if dm.name == 'act_mr3': season, array1, array2 = sints.arrays( qid, 'season'), sints.arrays(qid, 'array'), sints.arrays( qid, 'freq') array = '_'.join([array1, array2]) marker = 's' elif dm.name == 'planck_hybrid': season, patch, array = None, None, sints.arrays(qid, 'freq') marker = 'o' bps = ["data/" + dm.get_bandpass_file_name(array)] fr = tfg.get_mix_bandpassed(bps, comp, normalize_cib=False)[0] fc = tfg.get_mix(cfreq, comp=comp)[0] if comp == 'CIB': print(comp, fr, fc, f0) f0 = tfg.get_mix_bandpassed( ['data/PA2_avg_passband_wErr_trunc.txt'], comp, normalize_cib=False)[0] fs = np.abs(fr / f0) if cfreq == 93: cfreq = 97 if cfreq == 148: cfreq = 149 print(cfreq) pl._ax.scatter(cfreq, fs, color=col, edgecolors='k', zorder=1,
'weight': 'bold', 'size': 16, } freqs = [30, 44, 70, 100, 143, 217, 353, 545] for f in freqs: pl._ax.text(f * 0.9, 0.02, "%d" % f, fontdict=font) # pl._ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()) #pl._ax.get_xaxis().get_major_formatter().labelOnlyBase = False pl.legend(loc='upper right', labsize=ftsize) pl.done("fig_bandpass.pdf") sys.exit() comp = 'tSZ' mix = fg.get_mix_bandpassed(fnames, comp) mixp = fg.get_mix_bandpassed(fnames, comp, shifts=[2.4, 2.4, 1.5, 2.4] + [0] * 8) mixn = fg.get_mix_bandpassed(fnames, comp, shifts=[-2.4, -2.4, -1.5, -2.4] + [0] * 8) diff = (mixp - mix) * 100. / mix diff2 = (mixn - mix) * 100. / mix diff3 = (mixp - mixn) * 100. / mix print(diff, diff2, diff3) sys.exit() dm = sints.ACTmr3() for season in dm.cals.keys():
for qid in qids: dm = sints.models[sints.arrays(qid, 'data_model')]() lmin, lmax, hybrid, radial, friend, cfreq, fgroup, wrfit = aspecs(qid) cfreqs.append(cfreq) season, array1, array2 = sints.arrays(qid, 'season'), sints.arrays( qid, 'array'), sints.arrays(qid, 'freq') array = '_'.join([array1, array2]) bps.append("data/" + dm.get_bandpass_file_name(array)) lbeams.append(tutils.get_kbeam(qid, ells, sanitize=False)) lexps1.append(exp1[cfreq]) lexps2.append(exp2[cfreq]) r1 = tfg.get_mix_bandpassed(bps, 'tSZ', ccor_cen_nus=cfreqs, ccor_beams=lbeams, ccor_exps=lexps1) r2 = tfg.get_mix_bandpassed(bps, 'tSZ', ccor_cen_nus=cfreqs, ccor_beams=lbeams, ccor_exps=lexps2) pl = io.Plotter(xlabel='$\\ell$', ylabel='$\\Delta R/R$') for i, qid in enumerate(qids): season, array1, array2, region = sints.arrays(qid, 'season'), sints.arrays( qid, 'array'), sints.arrays(qid, 'freq'), sints.arrays(qid, 'region') array = '_'.join([array1, array2]) pl.add(ells, (r2[i] - r1[i]) / r1[i],
from __future__ import print_function from orphics import maps, io, cosmology from pixell import enmap import numpy as np import os, sys from soapack import interfaces as sints import healpy as hp from tilec import fg as tfg import glob for freq in [30, 44, 70, 100, 143, 217, 353, 545, 857]: v1 = tfg.get_mix_bandpassed([ glob.glob('data/*_BANDPASS_F%s_reformat.txt' % str(freq).zfill(3))[0] ], 'CIB', normalize_cib=False) v2 = tfg.get_mix(freq, 'CIB_Jysr') v3 = tfg.get_mix(freq, 'CIB') print(v1 / 1e26, v2 / 1e26, v3 / 1e26) sys.exit() # print(tfg.ItoDeltaT(545)) # sys.exit() lmax = 6000 #8192*3 cmb_set = 0 nu_ghz = 92 def get_cls(alms): calm = maps.change_alm_lmax(alms, lmax) cls = hp.alm2cl(calm)