Exemplo n.º 1
0
 def __init__(self, map, iP, S, noise):
     self.map, self.iP, self.S, self.noise = map, iP, S, noise
     # Construct preconditioner. This assumes constant noise
     pixarea = map.area() / np.prod(map.shape[-2:])
     ntyp = (np.median(inoise[inoise > np.max(inoise) * 0.1]) /
             pixarea)**-0.5
     lmax = np.max(np.sum(map.lmap()**2, 0)**0.5) + 1
     ps_ntyp = np.zeros(lmax)[None, None] + ntyp**2
     Ntyp = enmap.spec2flat(map.shape, map.wcs, ps_ntyp, 1.0)
     self.prec = pow(mul(S + Ntyp, iP) + 1, -1)
Exemplo n.º 2
0
    def __init__(self, maps, inoise, ps, T=None):
        self.d = maps
        self.iN = inoise
        self.hN = en.multi_pow(inoise, 0.5, axes=[1, 2])
        self.iS = en.spec2flat(maps.shape[-3:], maps.wcs, ps, -1.0)
        self.hS = en.spec2flat(maps.shape[-3:], maps.wcs, ps, -0.5)
        self.ps = ps
        self.b, self.x = None, None
        # Prepare the preconditioner. It approximates the noise as the
        # same in every pixel, and ignores the cmb-template coupling.
        # See M(self,u) for details.
        iN_white = np.array(np.sum(np.mean(np.mean(self.iN, -1), -1), 0))
        # iN_white is now in pixel space, but the preconditioner needs it
        # in harmonic space, which introduces a
        #norm = np.prod((maps.box[1]-maps.box[0])/maps.shape[-2:])
        #norm = 1./np.prod(maps.shape[-2:])
        #iN_white /= norm
        self.S_prec = en.multi_pow(self.iS + iN_white[:, :, None, None], -1)

        # The template
        self.set_template(T)
Exemplo n.º 3
0
L.info("Reading spectrum " + args.powspec)
ps_cmb_tmp = powspec.read_spectrum(args.powspec)[:1, :1, :]
# Extend to max l
lmax = np.max(np.sum(map.lmap()**2, 0)**0.5).astype(int) + 1
lmax_tmp = min(ps_cmb_tmp.shape[-1], lmax)
ps_cmb = np.zeros([1, 1, lmax])
ps_cmb[:, :, :lmax_tmp] = ps_cmb_tmp[:, :, :lmax_tmp]
ps_cmb[:, :, lmax_tmp:] = ps_cmb_tmp[:, :, -1]

sigma = args.beam * np.pi / 180 / 60 / (8 * np.log(2))
l = np.arange(ps_cmb.shape[-1])
ps_src = np.exp(-l * (l + 1) * sigma**2)[None, None, :] * (args.srcrms *
                                                           np.pi / 180 / 60)**2

L.info("Setting up signal and noise matrices")
S = enmap.spec2flat(map.shape, map.wcs, ps_cmb, 1.0)
iP = enmap.spec2flat(map.shape, map.wcs, ps_src, -1.0)
N = (inoise + np.max(inoise) * 1e-3)**-1

# apodize map based on a smooth noise map
L.info("Apodizing")
print inoise.shape, inoise.dtype
inoise_smooth = enmap.smooth_gauss(inoise[0, 0], 10 * np.pi / 180 / 60)[None,
                                                                        None]
apod = (np.minimum(1, inoise_smooth / (np.max(inoise_smooth) * 0.05))**4)[0, 0]
map *= apod[None]

enmap.write_map(args.odir + "/inoise.fits", inoise)
enmap.write_map(args.odir + "/inoise_smooth.fits", inoise_smooth)
enmap.write_map(args.odir + "/apod.fits", apod)
enmap.write_map(args.odir + "/map_apod.fits", map)
Exemplo n.º 4
0
# === TEMPLATE MAPS ===
px = 0.5
arc = 10. * 60.
hwidth = arc / 2.
arcmin = utils.arcmin
shape, wcs = enmap.geometry(pos=[[-hwidth * arcmin, -hwidth * arcmin],
                                 [hwidth * arcmin, hwidth * arcmin]],
                            res=px * arcmin,
                            proj="car")

shape = (3, ) + shape

np.random.seed(100)

ps2d = enmap.spec2flat(shape, wcs, ps)

ps2dtest = ps2d[0, 0]
psfunc = interp1d(ells, cctt, fill_value=0., bounds_error=False)
modlmap = enmap.modlmap(shape, wcs)
ps2dalt = psfunc(modlmap)

io.quickPlot2d(np.log(ps2dtest), out_dir + "ps1.png")
io.quickPlot2d(np.log(ps2dalt), out_dir + "ps2.png")
#io.quickPlot2d(np.nan_to_num(ps2dtest/ps2dalt),out_dir+"psrat.png")

ellbin_edges = np.arange(2, 3000, 60)
binner = stats.bin2D(modlmap, ellbin_edges)
cents, ps1dtest = binner.bin(ps2dtest)
cents, ps1dalt = binner.bin(ps2dalt)
Exemplo n.º 5
0
def sim_scan_fast(imap, sigma, rad, n):
	mask = np.zeros(imap.shape[-2:])
	mask[rad:-rad,rad:-rad] = 1
	w = np.maximum(1 - ndimage.distance_transform_edt(1-mask)/rad,0)**3
	w = w[None,:,:]*np.array([1,0.5,0.5])[:,None,None]*sigma**-2*n
	w = enmap.samewcs(w, imap)
	m = imap + np.random.standard_normal(imap.shape)*w**-0.5
	m[~np.isfinite(m)] = 0
	return m, w

template = enmap.read_map(args.template)[:,::2,::2]
ps       = powspec.read_spectrum(args.powspec, expand="diag")
sim      = enmap.rand_map(template.shape, template.wcs, ps)

# Simulate noisy data
m, w = sim_scan_fast(sim, args.sigma, 128, 1)
ncomp = m.shape[0]
iN = enmap.zeros((ncomp,ncomp)+w.shape[-2:])
for i in range(ncomp): iN[i,i] = w[i]
iS = enmap.spec2flat(m.shape, m.wcs, ps, -1)

# Draw samples
sampler = gibbs.FieldSampler(iS, iN, m)

res = enmap.zeros((1+args.nsamp,)+m.shape,m.wcs)
res[0] = m
for i in range(args.nsamp):
	res[i] = sampler.sample(True)
enmap.write_map(args.ofile, res)