def get_cuts(mask, ymap, cmap, dmap, wtmap, ra, dec, arcmin, pix): mcut = reproject.cutout(mask, ra=np.deg2rad(ra), dec=np.deg2rad(dec), npix=int(arcmin / pix)) if mcut is None: return None, None, None, None, None if np.any(mcut) <= 0: return None, None, None, None, None ycut = reproject.cutout(ymap, ra=np.deg2rad(ra), dec=np.deg2rad(dec), npix=int(arcmin / pix)) ccut = reproject.cutout(cmap, ra=np.deg2rad(ra), dec=np.deg2rad(dec), npix=int(arcmin / pix)) dcut = reproject.cutout(dmap, ra=np.deg2rad(ra), dec=np.deg2rad(dec), npix=int(arcmin / pix)) wcut = reproject.cutout(wtmap, ra=np.deg2rad(ra), dec=np.deg2rad(dec), npix=int(arcmin / pix)) weight = wcut.mean() return mcut, ycut, ccut, dcut, weight
def inpaint_map_const_cov(imap,mask,union_sources_version=None,noise_pix = 20,hole_radius = 3.,plots=False): """ Inpaints a map under the assumption of constant 2D Fourier covariance. This uses the average PS of the full map for the noise model at each source location and thus does not handle inhomogenity. Pros: no products needed other than map-maker outputs of map Cons: imap -- (npol,Ny,Nx) ivar -- (Ny,Nx) """ ras,decs = sints.get_act_mr3f_union_sources(version=union_sources_version) kmap = enmap.fft(mask*imap,normalize='phys') gtags = [] gdicts = {} pcoords = [] for i,(ra,dec) in enumerate(zip(ras,decs)): sel = reproject.cutout(ivar, ra=np.deg2rad(ra), dec=np.deg2rad(dec), pad=1, corner=False,npix=noise_pix,return_slice=True) if sel is None: continue civar = ivar[sel] if np.any(civar<=0): continue modrmap = civar.modrmap() modlmap = civar.modlmap() res = maps.resolution(civar.shape,civar.wcs) cimap = imap[sel] print(ra,dec) if plots: for p in range(3): io.plot_img(cimap[p],os.environ['WORK']+"/cimap_%d_%s" % (p,str(i).zfill(2))) mimap = cimap.copy() mimap[...,modrmap<np.deg2rad(hole_radius/60.)] = np.nan for p in range(3): io.plot_img(mimap[p],os.environ['WORK']+"/masked_cimap_%d_%s" % (p,str(i).zfill(2))) scov = pixcov.scov_from_theory(modlmap,cmb_theory_fn,fn_beam,iau=False) ncov = pixcov.ncov_from_ivar(civar) pcov = scov + ncov gdicts[i] = pixcov.make_geometry(hole_radius=np.deg2rad(hole_radius/60.),n=noise_pix,deproject=True,iau=False,pcov=pcov,res=res) pcoords.append(np.array((dec,ra))) gtags.append(i) if len(gtags)>0: pcoords = np.stack(pcoords).swapaxes(0,1) result = pixcov.inpaint(imap,pcoords,deproject=True,iau=False,geometry_tags=gtags,geometry_dicts=gdicts,verbose=True) if plots: for i,(ra,dec) in enumerate(zip(ras,decs)): sel = reproject.cutout(ivar, ra=np.deg2rad(ra), dec=np.deg2rad(dec), pad=1, corner=False,npix=noise_pix,return_slice=True) if sel is None: continue civar = ivar[sel] if np.any(civar<=0): continue modrmap = civar.modrmap() modlmap = civar.modlmap() res = maps.resolution(civar.shape,civar.wcs) cimap = result[sel] print("Inpainted ", ra,dec) if plots: for p in range(3): io.plot_img(cimap[p],os.environ['WORK']+"/inpainted_cimap_%d_%s" % (p,str(i).zfill(2))) return result
if random: box = mask.box() ramin = min(box[0, 1], box[1, 1]) ramax = max(box[0, 1], box[1, 1]) decmin = min(box[0, 0], box[1, 0]) decmax = max(box[0, 0], box[1, 0]) ra = np.rad2deg(np.random.uniform(ramin, ramax)) dec = np.rad2deg(np.random.uniform(decmin, decmax)) # SNR cut if sn < 5: continue # Reject based on mask cutout mcut = reproject.cutout(bmask, ra=np.deg2rad(ra), dec=np.deg2rad(dec), npix=pix) if mcut is None: continue if np.any(mcut) <= 0: continue # Stamp cutouts icut = reproject.cutout(imap, ra=np.deg2rad(ra), dec=np.deg2rad(dec), npix=pix) ycut = reproject.cutout(ymap, ra=np.deg2rad(ra), dec=np.deg2rad(dec), npix=pix)
div = dm.get_split_ivar(freq, i, ncomp=None)[0] sras = [] sdecs = [] amps = [] #ras,decs,iamps = np.loadtxt("/scratch/r/rbond/msyriac/data/planck/data/hybrid/planck_hybrid_545_2way_1_map_srcfree_pccs_sub_catalog.txt",unpack=True) # !!! for k, (ra, dec) in enumerate(zip(ras, decs)): if np.abs(np.rad2deg(dec)) > decmax: continue #if iamps[k]>-500: continue # !!! # if sns[k]<20: continue # if k<300 or k>400: continue if evals[k] < 3: continue stamp = reproject.cutout(imap, ra=ra, dec=dec, pad=1, npix=npix) if stamp is None: continue divstamp = reproject.cutout(div, ra=ra, dec=dec, pad=1, npix=npix) famp, cov, pfit = ptfit.ptsrc_fit(stamp, dec, ra, maps.sigma_from_fwhm( np.deg2rad(pfwhm / 60.)), div=divstamp, ps=ps, beam=pfwhm, n2d=None) # model = pointsrcs.sim_srcs(stamp.shape, stamp.wcs, # np.array((dec,ra,famp.reshape(-1)[0]))[None], # maps.sigma_from_fwhm(np.deg2rad(pfwhm/60.)))
array, ncomp=None, srcfree=False)[0] print("Found %s %s %s" % (season, patch, array)) except: continue ivars = dm.get_splits_ivar(season, patch, array, ncomp=None)[0] wcs = ivars.wcs nsplits = ivars.shape[0] ids = [] sel_ivars = [] for sid, (ra, dec) in enumerate(zip(ras, decs)): cutout = reproject.cutout(splits, ra=np.deg2rad(ra), dec=np.deg2rad(dec), pad=1, corner=False, npix=noise_pix) pcutout = reproject.cutout(psplits, ra=np.deg2rad(ra), dec=np.deg2rad(dec), pad=1, corner=False, npix=noise_pix) if (cutout is not None): sel_ivar = reproject.cutout(ivars, ra=np.deg2rad(ra), dec=np.deg2rad(dec), pad=1, corner=False, npix=noise_pix,
def inpaint_map_white(imap,ivar,fn_beam,union_sources_version=None,noise_pix = 40,hole_radius = 6.,plots=False,cache_name=None,verbose=True): """ Inpaints a map under the assumption of inhomogenous but white uncorrelated instrument noise. Pros: no products needed other than map-maker outputs of map and ivar, good inpainting despite crappy noise model. Cons: noise model has to be built for each source, so this is actually quite slow. imap -- (npol,Ny,Nx) ivar -- (Ny,Nx) fn_beam -- lambda ells: beam(ells) cache_name -- a unique string identifying the catalog+map/array/frequency/split combination to/from which the geometries are cached """ if cache_name is not None: cache_name = cache_name + "_catversion_%s" % union_sources_version try: ras,decs,gtags,pcoords,gdicts = load_cached_inpaint_geometries(cache_name) do_geoms = False if verbose: print("actsims.inpaint: loaded cached geometries for ", cache_name) except: if verbose: print("actsims.inpaint: no cached geometries found for ", cache_name, ". Generating and saving...") do_geoms = True else: do_geoms = True if do_geoms: ras,decs = sints.get_act_mr3f_union_sources(version=union_sources_version) cmb_theory_fn = lambda s,l: cosmology.default_theory().lCl(s,l) gtags = [] gdicts = {} pcoords = [] for i,(ra,dec) in enumerate(zip(ras,decs)): sel = reproject.cutout(ivar, ra=np.deg2rad(ra), dec=np.deg2rad(dec), pad=1, corner=False,npix=noise_pix,return_slice=True) if sel is None: continue civar = ivar[sel] if np.any(civar<=0): continue modrmap = civar.modrmap() modlmap = civar.modlmap() res = maps.resolution(civar.shape,civar.wcs) cimap = imap[sel] if verbose: print("actsims.inpaint: built noise model for source ",i," / ",len(ras)) scov = pixcov.scov_from_theory(modlmap,cmb_theory_fn,fn_beam,iau=False) ncov = pixcov.ncov_from_ivar(civar) pcov = scov + ncov gdicts[i] = pixcov.make_geometry(hole_radius=np.deg2rad(hole_radius/60.),n=noise_pix,deproject=True,iau=False,pcov=pcov,res=res) pcoords.append(np.array((dec,ra))) gtags.append(i) if len(gtags)>0: pcoords = np.stack(pcoords).swapaxes(0,1) if cache_name is not None: save_cached_inpaint_geometries(cache_name,ras,decs,gtags,pcoords,gdicts) if verbose: print("actsims.inpaint: cached geometries for ",cache_name) if len(gtags)>0: result = pixcov.inpaint(imap,pcoords,deproject=True,iau=False,geometry_tags=gtags,geometry_dicts=gdicts,verbose=verbose) if plots: for i,(ra,dec) in enumerate(zip(ras,decs)): sel = reproject.cutout(ivar, ra=np.deg2rad(ra), dec=np.deg2rad(dec), pad=1, corner=False,npix=noise_pix,return_slice=True) if sel is None: continue civar = ivar[sel] if np.any(civar<=0): continue modrmap = civar.modrmap() modlmap = civar.modlmap() res = maps.resolution(civar.shape,civar.wcs) cimap = imap[sel] for p in range(3): io.plot_img(cimap[p],os.environ['WORK']+"/cimap_%d_%s" % (p,str(i).zfill(2))) mimap = cimap.copy() mimap[...,modrmap<np.deg2rad(hole_radius/60.)] = np.nan for p in range(3): io.plot_img(mimap[p],os.environ['WORK']+"/masked_cimap_%d_%s" % (p,str(i).zfill(2))) cimap = result[sel] for p in range(3): io.plot_img(cimap[p],os.environ['WORK']+"/inpainted_cimap_%d_%s" % (p,str(i).zfill(2))) return result
array, ncomp=None, srcfree=True)[0] print("Found %s %s %s" % (season, patch, array)) except: continue ivars = dm.get_splits_ivar(season, patch, array, ncomp=None)[0] wcs = ivars.wcs nsplits = ivars.shape[0] ids = [] sel_ivars = [] for sid, (ra, dec) in enumerate(zip(ras, decs)): cutout = reproject.cutout(splits, ra=np.deg2rad(ra), dec=np.deg2rad(dec), pad=1, corner=False, npix=noise_pix) if (cutout is not None): sel_ivar = reproject.cutout(ivars, ra=np.deg2rad(ra), dec=np.deg2rad(dec), pad=1, corner=False, npix=noise_pix, return_slice=True) cut_ivar = ivars[sel_ivar] retv = plot_cutout(nsplits, cutout, cut_ivar, skip_plots=True)
import numpy as np import os, sys fg = "tsz" #ppath = "/scratch/r/rbond/msyriac/data/depot/tilec/map_rad_src_test_v1.0.0_rc_deep56/" ppath = "/scratch/r/rbond/msyriac/data/depot/tilec/map_%s_src_test_v1.0.0_rc_deep56/" % fg dec = 0 ra = 10 #imap = enmap.read_map(ppath + "tilec_single_tile_deep56_comptony_map_rad_src_test_v1.0.0_rc.fits") imap = enmap.read_map( ppath + "tilec_single_tile_deep56_comptony_map_%s_src_test_v1.0.0_rc.fits" % fg) cut = reproject.cutout(imap, ra=np.deg2rad(ra), dec=np.deg2rad(dec), npix=80) #* 1e6 _, nwcs = enmap.geometry(pos=(0, 0), shape=cut.shape, res=np.deg2rad(0.5 / 60.)) print(cut.shape) io.hplot(enmap.enmap(cut, nwcs), "%scut" % fg, color='gray', colorbar=True, ticks=5, tick_unit='arcmin', font_size=12, upgrade=4, quantile=1e-3)
if random: box = mask.box() ramin = min(box[0, 1], box[1, 1]) ramax = max(box[0, 1], box[1, 1]) decmin = min(box[0, 0], box[1, 0]) decmax = max(box[0, 0], box[1, 0]) ra = np.rad2deg(np.random.uniform(ramin, ramax)) dec = np.rad2deg(np.random.uniform(decmin, decmax)) if sn < 5: continue # if ra>15: continue mcut = reproject.cutout(bmask, ra=np.deg2rad(ra), dec=np.deg2rad(dec), npix=pix) if mcut is None: continue if np.any(mcut) <= 0: continue ycut = reproject.cutout(ymap, ra=np.deg2rad(ra), dec=np.deg2rad(dec), npix=pix) ccut = reproject.cutout(cmap, ra=np.deg2rad(ra), dec=np.deg2rad(dec), npix=pix) cut150 = reproject.cutout(m150,