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
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
iau=False) gdicts[sid] = pixcov.make_geometry( hole_radius=np.deg2rad(hole_radius / 60.), n=noise_pix, deproject=True, iau=False, pcov=pcov, res=res) gtags.append(sid) pcoords.append(np.array((decs[sid], ras[sid]))) if len(gtags) > 0: pcoords = np.stack(pcoords).swapaxes(0, 1) result = pixcov.inpaint(splits[i], pcoords, deproject=True, iau=False, geometry_tags=gtags, geometry_dicts=gdicts, verbose=True) else: result = splits[i].copy() inpainted.append(result) inpainted = enmap.enmap(np.stack(inpainted), wcs) # Verify inpainting for sid, (ra, dec) in enumerate(zip(ras, decs)): cutout = reproject.cutout(inpainted, ra=np.deg2rad(ra), dec=np.deg2rad(dec), pad=1, corner=False,