Exemplo n.º 1
0
    def __init__(self,
                 shape_source,
                 wcs_source,
                 patch_width,
                 patch_height,
                 width_multiplier=1.,
                 height_multiplier=1.5,
                 pix_target_override_arcmin=None,
                 proj="car",
                 verbose=False,
                 downsample=True,
                 downsample_pix_arcmin=None):

        self.source_pix = np.min(
            enmap.extent(shape_source, wcs_source) /
            shape_source[-2:]) * 60. * 180. / np.pi
        if pix_target_override_arcmin is None:
            input_dec = enmap.posmap(shape_source, wcs_source)[0]
            max_dec = np.max(np.abs(input_dec))
            del input_dec
            recommended_pix = self.source_pix * np.cos(max_dec)

            if verbose:
                print("INFO: Maximum declination in southern patch : ",
                      max_dec * 180. / np.pi, " deg.")
                print("INFO: Recommended pixel size for northern patch : ",
                      recommended_pix, " arcmin")

        else:
            recommended_pix = pix_target_override_arcmin

        shape_target, wcs_target = enmap.rect_geometry(
            width_arcmin=width_multiplier * patch_width * 60.,
            height_arcmin=height_multiplier * patch_height * 60.,
            px_res_arcmin=recommended_pix,
            yoffset_degree=0.,
            proj=proj)

        self.target_pix = recommended_pix
        self.wcs_target = wcs_target
        if verbose:
            print("INFO: Source pixel : ", self.source_pix, " arcmin")

        if downsample:
            dpix = downsample_pix_arcmin if downsample_pix_arcmin is not None else self.source_pix

            self.shape_final, self.wcs_final = enmap.rect_geometry(
                width_arcmin=width_multiplier * patch_width * 60.,
                height_arcmin=height_multiplier * patch_height * 60.,
                px_res_arcmin=dpix,
                yoffset_degree=0.,
                proj=proj)
        else:
            self.shape_final = shape_target
            self.wcs_final = wcs_target
        self.downsample = downsample

        MapRotator.__init__(self, shape_source, wcs_source, shape_target,
                            wcs_target)
Exemplo n.º 2
0
    Config.set("general", "Nsets", str(Nsets))
    Config.set("general", "pol", str(not (args.skip_pol)))
    Config.write(open(fout_dir + "config.ini", 'w'))

lens_order = Config.get(sim_section, 'lens_order')

pol = args.do_pol
if pol: raise NotImplementedError

shape_sim, wcs_sim, shape_dat, wcs_dat = aio.enmaps_from_config(
    Config, sim_section, analysis_section, pol=pol)
if rank == 0:
    print(("Dat shape : ", shape_dat))
    print(("Sim shape : ", shape_sim))
analysis_resolution = np.min(
    enmap.extent(shape_dat, wcs_dat) / shape_dat[-2:]) * 60. * 180. / np.pi
pixratio = analysis_resolution / Config.getfloat(sim_section, "pixel_arcmin")
min_ell = fmaps.minimum_ell(shape_dat, wcs_dat)

parray_dat = aio.patch_array_from_config(Config,
                                         expf_name,
                                         shape_dat,
                                         wcs_dat,
                                         dimensionless=False)
parray_sim = aio.patch_array_from_config(Config,
                                         expf_name,
                                         shape_sim,
                                         wcs_sim,
                                         dimensionless=False)

# Efficiently distribute sims over MPI cores
print("Lmax: ", modlmap.max())
print("Unmasked area sq.deg.: ", mask.area() * (180. / np.pi)**2.)

# Set up a power-spectrum-doer
fc = maps.FourierCalc(shape, wcs, iau=iau)

# And a binner
bin_edges = np.arange(40, 3000, 100)
binner = stats.bin2D(modlmap, bin_edges)
binit = lambda x: binner.bin(x)[1]
cents = binner.centers
#ells_coupled = cents

# === ENMAP TO NAMASTER ===
# get the extent and shape of our geometry
Ly, Lx = enmap.extent(shape, wcs)
Ny, Nx = shape[-2:]

if not (do_mask):
    mask = mask * 0. + 1.
else:
    if do_apod:
        mask = nmt.mask_apodization_flat(mask,
                                         Lx,
                                         Ly,
                                         aposize=apod_width,
                                         apotype=apotype)

# Mask fraction
frac = np.sum(mask) * 1. / mask.size
area = enmap.area(shape, wcs) * frac * (180. / np.pi)**2.
Exemplo n.º 4
0
def resolution(shape, wcs):
    res = np.min(np.abs(enmap.extent(shape, wcs)) / shape[-2:])
    return res
Exemplo n.º 5
0
def make_geometry(shape,
                  wcs,
                  hole_radius,
                  cmb2d_TEB=None,
                  n2d_IQU=None,
                  context_width=None,
                  n=None,
                  beam2d=None,
                  deproject=True,
                  iau=False,
                  res=None,
                  tot_pow2d=None,
                  store_pcov=False):
    """
    Make covariances for brute force maxlike inpainting of CMB maps.
    Eq 3 of arXiv:1109.0286

    shape,wcs -- enmap geometry of big map
    cmb2d_TEB -- (ncomp,ncomp,Ny,Nx) 2D CMB power in physical units
    n2d_IQU -- (ncomp,ncomp,Ny,Nx) 2D noise power in physical units
    hole_radius in radians
    context_width in radians or n as number of pixels
    beam2d -- (Ny,Nx) 2D beam template
    tot_pow2d -- (ncomp,ncomp,Ny,Nx) 2D total IQU power in physical units (includes CMB, beam and noise). If this argument is provided
                 cmb2d_TEB, n2d_IQU and beam2d will be ignored.
    deproject -- whether to deproject common mode
    iau -- whether to use IAU convention for polarization
    res -- specify resolution in radians instead of inferring from enmap geometry


    """

    # Make a flat stamp geometry
    if res is None: res = np.min(np.abs(enmap.extent(shape, wcs)) / shape[-2:])
    if n is None: n = int(context_width / res)

    # Do we have polarization?
    n2d = n2d_IQU
    ncomp = n2d.shape[0]
    assert ncomp == 1 or ncomp == 2 or ncomp == 3

    # Select the hole (m1) and context(m2) across all components
    m1, m2 = get_geometry_regions(ncomp, n, res, hole_radius)

    # Get the pix-pix covariance on the stamp geometry given CMB theory, beam and 2D noise on the big map
    if tot_pow2d is not None:
        pcov = fcov_to_rcorr(shape, wcs, tot_pow2d, n)
    else:
        pcov = stamp_pixcov_from_theory(n,
                                        cmb2d_TEB,
                                        n2d,
                                        beam2d=beam2d,
                                        iau=iau)
    # --- Make sure that the pcov is in the right order vector(I,Q,U) ---
    # It is currently in (ncomp,ncomp,n,n) order
    # We transpose it to (ncomp,n,ncomp,n) order
    # so that when it is reshaped into a 2D array, a row/column will correspond to an (I,Q,U) vector
    pcov = np.transpose(pcov, (0, 2, 1, 3))
    pcov = pcov.reshape((ncomp * n**2, ncomp * n**2))

    # Invert
    Cinv = np.linalg.inv(pcov)

    # Woodbury deproject common mode
    if deproject:
        # Deproject I,Q,U common mode separately
        #u = (np.zeros((n*n,ncomp,ncomp))+np.eye(ncomp)).reshape(n*n*ncomp,ncomp)
        u = np.zeros((n * n * ncomp, ncomp))
        for i in range(ncomp):
            u[i * n * n:(i + 1) * n * n, i] = 1
        #u = np.ones((ncomp*n**2,1)) # Deproject mode common to all of I,Q,U
        Cinvu = np.linalg.solve(pcov, u)
        precalc = np.dot(Cinvu, np.linalg.solve(np.dot(u.T, Cinvu), u.T))
        correction = np.dot(precalc, Cinv)
        Cinv -= correction

    # Get matrices for maxlike solution Eq 3 of arXiv:1109.0286
    cslice = Cinv[m1][:, m1]
    mul2 = Cinv[m1][:, m2]
    mean_mul = -np.linalg.solve(cslice, mul2)
    cov = np.linalg.inv(Cinv[m1][:, m1])
    cov_root = utils.eigpow(cov, 0.5)

    geometry = {}
    geometry['covsqrt'] = cov_root
    geometry['meanmul'] = mean_mul
    geometry['n'] = n
    geometry['res'] = res
    geometry['m1'] = m1
    geometry['m2'] = m2
    geometry['ncomp'] = ncomp
    geometry['hole_radius'] = hole_radius
    if store_pcov: geometry['pcov'] = pcov

    return geometry
Exemplo n.º 6
0
recon_section = args.Recon
expf_name = args.Exp
fout_dir = rConfig.get('general','output_dir')+args.Out+"/"


Config = io.config_from_file(fout_dir+"config.ini")
analysis_section = Config.get("general","analysis")
sim_section = Config.get("general","sims")
cosmology_section = Config.get("general","cosmology")
Nsims = Config.getint("general","Nsims")
Nsets = Config.getint("general","Nsets")
polsims = Config.getboolean("general","pol")
pol = False

shape_sim, wcs_sim, shape_dat, wcs_dat = aio.enmaps_from_config(Config,sim_section,analysis_section,pol=pol)
analysis_resolution =  np.min(enmap.extent(shape_dat,wcs_dat)/shape_dat[-2:])*60.*180./np.pi
pixratio = analysis_resolution/Config.getfloat(sim_section,"pixel_arcmin")
min_ell = fmaps.minimum_ell(shape_dat,wcs_dat)

parray_dat = aio.patch_array_from_config(rConfig,expf_name,shape_dat,wcs_dat,dimensionless=False)

# Efficiently distribute sims over MPI cores
Nuse = Nsims if args.num_sims is None else args.num_sims
assert args.num_sims < Nsims
num_each,each_tasks = mpi_distribute(Nuse,numcores)
# Initialize a container for stats and stacks
mpibox = MPIStats(comm,num_each,tag_start=333)
if rank==0: print(("At most ", max(num_each) , " tasks..."))
# What am I doing?
my_tasks = each_tasks[rank]