Esempio n. 1
0
 def get_covariance_from_samples(self,
                                 v,
                                 covar_type='dense',
                                 off_diagonal_cut=0):
     """
     Computes a covariance matrix from a set of samples in the form [nsamples, ndata]
     """
     if covar_type == 'diagonal':
         cov = np.std(v, axis=0)**2
         return sacc.Precision(matrix=cov,
                               is_covariance=True,
                               mode="diagonal")
     else:
         nsim, nd = v.shape
         vmean = np.mean(v, axis=0)
         cov = np.einsum('ij,ik', v, v)
         cov = cov / nsim - vmean[None, :] * vmean[:, None]
         if covar_type == 'block_diagonal':
             nblocks = nd // self.n_bpws
             cuts = np.ones([self.n_bpws, self.n_bpws])
             if nblocks * self.n_bpws != nd:
                 raise ValueError("Vector can't be divided into blocks")
             for i in range(off_diagonal_cut + 1, self.n_bpws):
                 cuts -= np.diag(np.ones(self.n_bpws - i), k=i)
                 cuts -= np.diag(np.ones(self.n_bpws - i), k=-i)
             cov = cov.reshape([nblocks, self.n_bpws, nblocks, self.n_bpws])
             cov = (cov * cuts[None, :, None, :]).reshape([nd, nd])
         return sacc.Precision(matrix=cov, is_covariance=True, mode="dense")
Esempio n. 2
0
    def write_vector_to_sacc(self,
                             fname_out,
                             sacc_t,
                             sacc_b,
                             cls,
                             covar=None,
                             verbose=False):
        """
        Write a vector of power spectrum measurements into a SACC file.
        :param fname_out: path to output file
        :param sacc_t: list of SACC tracers
        :param sacc_b: SACC Binning object.
        :param cls: list of power spectrum measurements.
        :param covar: covariance matrix:
        :param verbose: do you want to print out information about the SACC file?
        """
        sacc_mean = sacc.MeanVec(cls.flatten())
        if covar is None:
            sacc_precision = None
        else:
            sacc_precision = sacc.Precision(covar,
                                            "dense",
                                            is_covariance=True,
                                            binning=sacc_b)

        sacc_meta = {'Area_rad': self.area_patch}
        s = sacc.SACC(sacc_t,
                      sacc_b,
                      sacc_mean,
                      precision=sacc_precision,
                      meta=sacc_meta)
        if verbose:
            s.printInfo()
        s.saveToHDF(fname_out)
Esempio n. 3
0
def getPrecisionMatrix(s, cls_theory):

    #Compute theory covariance matrix
    fsky = 0.4
    Np = s.size()
    Nt = len(s.tracers)
    covar_theory = np.zeros((Np, Np))
    for a in np.arange(Nt):
        for b in np.arange(a, Nt):
            for c in np.arange(Nt):
                for d in np.arange(c, Nt):
                    c_ac = cls_theory[(a, c)]
                    c_ad = cls_theory[(a, d)]
                    c_bc = cls_theory[(b, c)]
                    c_bd = cls_theory[(b, d)]
                    cdiag = (c_ac * c_bd + c_ad * c_bc) / (
                        fsky * (2. * s.lrange(a, b) + 1.) * dl
                    )  ## we happen to have identical lranges everywhere
                    for l1, l2 in zip(s.ilrange(a, b), s.ilrange(c, d)):
                        covar_theory[l1, l2] = cdiag
                        covar_theory[l2, l1] = cdiag

    precision = la.inv(covar_theory)
    return sacc.Precision(precision,
                          mode='ell_block_diagonal',
                          binning=s.binning), covar_theory
Esempio n. 4
0
               axis=0) / area)
    for i_w, ww in enumerate(sc['GAMA09H'].binning.windows)
]

# Bins
s_bn = sc['GAMA09H'].binning
s_bn.windows = wins

# Tracers
s_tr = []
for i_t in range(4):
    T = sacc.Tracer('bin_%d' % i_t,
                    'point',
                    zs[i_t],
                    Nz[i_t],
                    exp_sample="HSC_DESC")
    T.addColumns({pn: ec[pn][i_t] for pn in pz_codes})
    s_tr.append(T)

# Signal spectra
s_mean = sacc.MeanVec(mean)
s_prec = sacc.Precision(cov, "dense", is_covariance=True, binning=s_bn)
s_meta = {'Area_rad': area}
s = sacc.SACC(s_tr, s_bn, s_mean, precision=s_prec, meta=s_meta)
s.saveToHDF("COADD/power_spectra_wdpj.sacc")
# Noise spectra
s_mean = sacc.MeanVec(mean_n)
s_bn.windows = None
s = sacc.SACC(s_tr, s_bn, s_mean, meta=s_meta)
s.saveToHDF("COADD/noi_bias.sacc")
Esempio n. 5
0
prec = np.linalg.inv(cov)

# Tmat computations
Tmatfn = "Tmat_%s_%i.npy"%(read,upsample)
if os.path.isfile(Tmatfn):
    print ("Loading cached Tmat")
    Tmat = np.load(Tmatfn)
else:
    Tmat = obtain_Tmat(s_m,s_noi,hod_params,Z_params,HMCorrection)
    np.save(Tmatfn,Tmat)

# Obtain the new covariance matrix
cov_CVnoismo = obtain_improved_cov(cov,Tmat,prior_smo)

# Weirdly it seems like this object actually expects the covariance rather than the precision 
cov_n = sacc.Precision(cov_CVnoismo)

# Create new sacc file (TODO: ask about "dense")
s_n = sacc.SACC(s_d.tracers,s_d.binning,s_d.mean.vector,cov_n,meta=s_d.meta)


# Save the new covariance and the power spectrum of the data which shall be run via MCMC
if not os.path.exists(dir_write):
    os.makedirs(dir_write)
s_n.saveToHDF(dir_write+"/power_spectra_wdpj.sacc")

# Copying noise bias file
shutil.copy(dir_read +"/noi_bias.sacc",dir_write)

# !!!!! Everything below is just for checking that the code works !!!!!!
Esempio n. 6
0
    d = np.loadtxt("BK15_cosmomc/data/BK15/bandpass_" + name + ".txt",
                   unpack=True)
    return sacc.Tracer(name, "spin2", d[0], d[1], exp_sample)


#Tracers
tracers = [get_tracer_from_name(t, e) for t, e in zip(tracer_names, exp_names)]

#Mean vector
dv = np.loadtxt("BK15_cosmomc/data/BK15/BK15_cl_hat.dat", unpack=True)[1:]
ncls, nells = dv.shape
meanvec = sacc.MeanVec(dv.flatten())

#Precision matrix
precis = sacc.Precision(
    np.transpose(np.loadtxt("BK15_cosmomc/data/BK15/BK15_covmat_dust.dat",
                            unpack=True).reshape([nells, ncls, nells, ncls]),
                 axes=[1, 0, 3, 2]))

#Binning
ls = np.loadtxt("BK15_cosmomc/data/BK15/windows/BK15_bpwf_bin1.txt",
                unpack=True)[0]
windows = np.array([
    np.loadtxt("BK15_cosmomc/data/BK15/windows/BK15_bpwf_bin%d.txt" % (i + 1),
               unpack=True)[1:] for i in range(nells)
])
typ_arr = []
ls_arr = []
t1_arr = []
t2_arr = []
q1_arr = []
q2_arr = []
Esempio n. 7
0
            val.append(np.random.uniform(0, 10))
            err.append(np.random.uniform(1, 2))

binning = sacc.Binning(type, ell, t1, q1, t2, q2, windows=wins)
mean = sacc.MeanVec(val)

## We need to add covariance matrix. We will use ell_block_diagonal
## where everything is coupled across tracers/redshifts at the same ell but not
## across ell with fixed 10% off-diagonal elements
Np = binning.size()
cov = np.zeros((Np, Np))
for i in range(Np):
    for j in range(i, Np):
        if ell[i] == ell[j]:
            cov[i, j] = err[i] * err[j]
            if (i != j):
                cov[i, j] /= 10
            cov[j, i] = cov[i, j]
precision = sacc.Precision(cov,
                           "ell_block_diagonal",
                           is_covariance=True,
                           binning=binning)

## Add some meta data
meta = {"Creator": "McGyver", "Project": "Victory"}

## finally, create SACC object
s = sacc.SACC(tracers, binning, mean, precision, meta)
s.printInfo()
s.saveToHDF("test.sacc")
Esempio n. 8
0

#Tracers
def get_tracer_from_Bpass(b):
    return sacc.Tracer(b.name, "spin2", b.nu, b.bnu, 'SO_SAT')


tracers = [get_tracer_from_Bpass(b) for b in bpss]

#Vectors
v_signoi = sacc.MeanVec(bpw_tot)
v_signal = sacc.MeanVec(bpw_sig)
v_noise = sacc.MeanVec(bpw_noi)

#Covariance
precis = sacc.Precision(cov_bpw, is_covariance=True)

#Ordering
typ_arr = []
ls_arr = []
t1_arr = []
t2_arr = []
q1_arr = []
q2_arr = []
w_arr = []
for ic, c in enumerate(corr_ordering):
    s1, s2 = c
    tn1 = s1[:-2]
    q1 = s1[-1]
    t1 = np.where(tracer_names == tn1)[0][0]
    tn2 = s2[:-2]
Esempio n. 9
0
print('Reading noise saccs {}.'.format(saccs_noise_names))
saccs_noise = [sacc.SACC.loadFromHDF(fn) for fn in saccs_noise_names]

# Add precision matrix to noise saccs
for i, s in enumerate(saccs):
    saccs_noise[i].precision = s.precision

# Coadding the files normalizing by area
sacc_coadded = sacc.coadd(saccs, mode='area')
sacc_noise_coadded = sacc.coadd(saccs_noise, mode='area')

# Change the covmat to the new one if requested
if want_newcovmat:
    lmin = [0, 0, 0, 0]
    lmax = [2170.58958919, 2515.39193451, 3185.36076391, 4017.39370804]
    sacc_coadded.cullLminLmax(lmin, lmax)
    sacc_noise_coadded.cullLminLmax(lmin, lmax)

    sacc_coadded.precision = sacc.Precision(newcov,
                                            'dense',
                                            is_covariance=True)
    sacc_noise_coadded.precision = sacc.Precision(newcov,
                                                  'dense',
                                                  is_covariance=True)

# Recording the final sacc files
if not os.path.exists(dir_write):
    os.makedirs(dir_write)
sacc_coadded.saveToHDF(dir_write + "/power_spectra_wdpj.sacc")
sacc_noise_coadded.saveToHDF(dir_write + "/noi_bias.sacc")
Esempio n. 10
0
    sacc_tracers.append(T)
#Binning and mean
type, ell, dell, t1, q1, t2, q2 = [], [], [], [], [], [], []
for t1i in range(nbins):
    for t2i in range(t1i, nbins):
        for i_l, l in enumerate(ell_eff):
            type.append('F')  #Fourier-space
            ell.append(l)
            dell.append(lend[i_l] - lini[i_l])
            t1.append(t1i)
            q1.append('P')
            t2.append(t2i)
            q2.append('P')
sacc_binning = sacc.Binning(type, ell, t1, q1, t2, q2, deltaLS=dell)
sacc_mean = sacc.MeanVec(cls_all.flatten())
if covar is None:
    sacc_precision = None
else:
    sacc_precision = sacc.Precision(covar,
                                    "dense",
                                    is_covariance=True,
                                    binning=sacc_binning)
sacc_meta = {'Field': o.hsc_field, 'Area_rad': area_patch}
s = sacc.SACC(sacc_tracers,
              sacc_binning,
              sacc_mean,
              precision=sacc_precision,
              meta=sacc_meta)
s.printInfo()
s.saveToHDF(o.fname_out)
Esempio n. 11
0
        nu = [90.]
    if name=='A150':
        nu = [150.]
    bandpass = [1.] 
    return sacc.Tracer(name, "spin2", np.asarray(nu), np.asarray(bandpass), exp_sample)

#Tracers
tracers=[get_tracer_from_name(t,e) for t,e in zip(tracer_names,exp_names)]

#Mean vector
dv = np.loadtxt(datadir + 'mr3c_20181012_190130_TT_TE_EE_C_ell_iter0.dat')[:, 1]
meanvec = sacc.MeanVec(dv)

#Precision matrix
cov = np.loadtxt(datadir + 'mr3c_20181012_190130_TT_TE_EE_cov_diag_C_ell.dat')
precis = sacc.Precision(cov)

#Binning
windows = {}
windows['TT'] = np.loadtxt(datadir + 'TT_C_ell_bpwf_v2_lmin2.dat')
windows['TE'] = np.loadtxt(datadir + 'TE_C_ell_bpwf_v2_lmin2.dat')
windows['ET'] = np.loadtxt(datadir + 'TE_C_ell_bpwf_v2_lmin2.dat')
windows['EE'] = np.loadtxt(datadir + 'EE_C_ell_bpwf_v2_lmin2.dat')

nells = windows['TT'].shape[0]
ellmax = windows['TT'].shape[1]
ls = np.arange(ellmax) + 2

typ_arr=[]
ls_arr=[]
t1_arr=[]