Example #1
0
def run(prim_template='equilateral', out_dir=None,
        lmin=2, lmax=5000):

    '''    
    Calculate and save beta.

    Keyword Arguments
    ---------
    prim_template : str
    out_dir : str
    camb_dir : str
    lmin : int
    lmax : int
    '''

#    camb_opts = dict(camb_out_dir = camb_dir,
#                     tag='',
#                     lensed=False,
#                     high_ell=False,
#                     interp_factor=None)

    F = Fisher(out_dir)

    ac = 16
    ke = 10
    interp_factor = 1
    radii_factor = 1


#    F.get_camb_output(**camb_opts)
    F.get_cosmo(lmax=lmax, load=True, tag='{}_{}_{}'.format(lmax, ac, ke),
                AccuracyBoost=ac, k_eta_fac=ke)
    F.get_bins(lmin=lmin, lmax=lmax, load=True,
                parity='odd', verbose=True, tag=str(lmax))
 
    beta_tag = 'r{}_i{}_l{}_{}_{}'.format(radii_factor, interp_factor, lmax, ac, ke)

    radii = F.get_updated_radii()
    radii = radii[::radii_factor]


    F.get_beta(func=prim_template, radii=radii, verbose=True, optimize=False,
               interp_factor=interp_factor, load=False, sparse=False, tag=beta_tag)
    exit()
    F.get_binned_bispec('equilateral', load=False)
    if F.mpi_rank == 0:
        print(F.bispec['bispec'].shape)
        print(F.bispec['bispec'][F.bispec['bispec'] != 0])
        print(F.bispec['bispec'][F.bispec['bispec'] != 0].size)
        print(np.sum(F.bins['num_pass_full'].astype(bool)))
        print(F.bispec['bispec'][F.bins['num_pass_full'].astype(bool)])
        print(np.sum(F.bispec['bispec'][~F.bins['num_pass_full'].astype(bool)]))
        print(F.bispec['bispec'][np.isnan(F.bispec['bispec'])])
        print(F.bispec['bispec'][np.logical_and(F.bispec['bispec'] == 0,F.bins['num_pass_full'][:,:,:,np.newaxis].astype(bool))])
        print(F.bispec['bispec'][0,:4,:4,:])
        print(F.bins['num_pass_full'][0,:4,:4])
Example #2
0
def run(ell, prim_template='local', out_dir=None, lmin=2, lmax=120):
    '''    
    Calculate and save bins, beta and bispec. Then return bispec slice.

    Keyword Arguments
    ---------
    ell : int
        Multipole for bispec slice.
    prim_template : str
    out_dir : str
    lmin : int
    lmax : int
    '''

    interp_factor = 10
    ac = 16
    ke = 10

    #    tag = 'l{}'.format(lmax)
    #    beta_tag = 'l{}_i{}'.format(lmax, interp_factor)
    #    cosmo_tag = 'l{}_ac{}_ke{}'.format(lmax, ac, ke)

    bins_tag = '5200'
    beta_tag = 'r1_i1_l{}_16_7'.format(lmax, interp_factor)
    cosmo_tag = '5200_16_7'.format(lmax, ac, ke)
    bispec_tag = '5200_16_7'

    F = Fisher(out_dir)

    F.get_cosmo(load=True,
                tag=cosmo_tag,
                verbose=True,
                lmax=lmax * 2,
                AccuracyBoost=ac,
                k_eta_fac=ke)
    F.get_bins(load=True,
               parity='odd',
               verbose=True,
               tag=bins_tag,
               lmin=lmin,
               lmax=lmax)

    F.get_beta(load=True,
               tag=beta_tag,
               verbose=True,
               interp_factor=interp_factor)

    F.get_binned_bispec(prim_template, load=True, tag=bispec_tag)

    b_slice, doublets = F.get_bispec_slice(ell, verbose=2)

    return F, b_slice, doublets
Example #3
0
def run(out_dir, num_chunks, camb_opts=None, bin_opts=None, beta_opts=None):

    F = Fisher(out_dir)

    F.get_camb_output(**camb_opts)

    cls = get_cls(camb_opts['camb_out_dir'],
                  bin_opts['lmax'],
                  A_lens=1,
                  tag=camb_opts['tag'])
    lmax = cls.shape[1] + 2

    # hacky stuff
    #    totcov = np.zeros((6, cls.shape[1]))
    totcov = np.zeros((6, 3999))
    totcov[:3] += 1e12  # fill diagonal
    totcov[:4, :cls.shape[1]] = cls

    F.get_bins(**bin_opts)

    radii = F.get_updated_radii()

    F.get_beta(radii=radii, **beta_opts)

    chunks = np.array_split(radii, num_chunks)
    fisher_r = np.zeros(len(chunks))
    for cidx, chunk in enumerate(chunks):
        F.get_binned_bispec('equilateral',
                            radii_sub=chunk,
                            load=True,
                            tag=str(cidx))

        print(F.bispec['bispec'].size)
        print(F.bispec['bispec'][F.bins['num_pass'].astype(bool)].size)
        exit()

        #        amp = get_prim_amp('equilateral') # not needed anymore
        #        F.bispec['bispec'] *= amp
        fisher, _ = F.naive_fisher(2, lmax, totcov, fsky=1)
        fisher_r[cidx] = fisher
        print(fisher)

    # save in fisher
    np.save(opj(F.subdirs['fisher'], 'fisher_r.npy'), fisher_r)
    np.save(opj(F.subdirs['fisher'], 'chunks.npy'), chunks)
    return fisher_r, chunks
Example #4
0
import numpy as np
from sst import Fisher

test_dir = '/mn/stornext/d8/ITA/spider/adri/analysis/20181025_sst/test'
camb_dir = '/mn/stornext/d8/ITA/spider/adri/analysis/20180911_sst/camb_output/lensed_r0_4000'

F = Fisher(test_dir)
F.get_camb_output(camb_out_dir=camb_dir)
radii = F.get_updated_radii()
radii = radii[::10]
F.get_bins(lmin=2, lmax=10, load=True, verbose=True)
F.get_beta(func='equilateral', load=True, verbose=True, radii=radii)
F.barrier()

b = F.bins['num_pass'].astype(bool)
F.get_binned_bispec('equilateral', load=True)

#if F.mpi_rank == 0:
#    print F.bispec['bispec'][4,:,:,0]
#for rank in xrange(F.mpi_size):
#    if F.mpi_rank == rank:
#        print 'beta'
#        print F.beta['beta_s'].shape
#        print F.beta['beta_s'][20,0,0,0,0,:]
# bins needs to be recomputed when kwargs dont match
# beta needs to be recomputed when kwargs, radii, ks, transfers do not match
# bispec only needs to be recomputed when template doesnt match.

#beta = F.beta
#print beta
#beta_s = F.beta['beta_s']
Example #5
0
def run_fisher(template,
               ana_dir,
               camb_dir,
               totcov,
               ells,
               lmin=2,
               lmax=4999,
               fsky=0.03,
               plot_tag='',
               tag=None):

    F = Fisher(ana_dir)
    camb_opts = dict(camb_out_dir=camb_dir,
                     tag='r0',
                     lensed=False,
                     high_ell=True)

    F.get_camb_output(**camb_opts)
    radii = F.get_updated_radii()
    radii = radii[::2]
    F.get_bins(lmin=lmin,
               lmax=lmax,
               load=True,
               verbose=False,
               parity='odd',
               tag=tag)
    #   F.get_beta(func='equilateral', load=True, verbose=False, radii=radii, tag=tag)
    F.get_beta(func='equilateral',
               load=True,
               verbose=True,
               radii=radii,
               tag=tag,
               interp_factor=10)
    #    F.get_binned_bispec(template, load=True, tag=tag)
    F.get_binned_bispec(template, load=True, tag=tag)
    bin_invcov, bin_cov = F.get_binned_invcov(ells,
                                              totcov,
                                              return_bin_cov=True)

    # Plot invcov, cov
    plot_opts = dict(lmin=2)
    bins = F.bins['bins']
    plot_tools.cls_matrix(plot_tag,
                          bins,
                          bin_invcov,
                          log=False,
                          plot_dell=False,
                          inv=True,
                          **plot_opts)
    plot_tools.cls_matrix(plot_tag.replace('invcov', 'cov_dell'),
                          bins,
                          bin_cov,
                          log=False,
                          plot_dell=True,
                          **plot_opts)
    print(lmin, lmax)
    fisher = F.naive_fisher(bin_invcov, lmin=lmin, lmax=lmax, fsky=fsky)
    sigma = 1 / np.sqrt(fisher)

    return fisher, sigma

    ###### OLD

    amp = get_prim_amp(template)
    F.bispec['bispec'] *= amp

    F.get_binned_invcov(nls=totcov)
    bin_invcov = F.bin_invcov
    bin_cov = F.bin_cov

    bin_size = F.bins['bins'].size
    bins = F.bins['bins']
    num_pass = F.bins['num_pass_full']
    bispec = F.bispec['bispec']

    # Plot invcov, cov
    plot_opts = dict(lmin=2)
    plot_tools.cls_matrix(plot_tag,
                          bins,
                          bin_invcov,
                          log=False,
                          plot_dell=False,
                          inv=True,
                          **plot_opts)
    plot_tools.cls_matrix(plot_tag.replace('invcov', 'cov_dell'),
                          bins,
                          bin_cov,
                          log=False,
                          plot_dell=True,
                          **plot_opts)
    plot_tools.cls_matrix(plot_tag.replace('invcov', 'cov'),
                          bins,
                          bin_cov,
                          log=False,
                          plot_dell=False,
                          **plot_opts)

    # allocate bin-sized fisher matrix (same size as outer loop)
    fisher_per_bin = np.ones(bin_size) * np.nan

    # allocate 12 x 12 cov for use in inner loop
    invcov = np.zeros((F.bispec['pol_trpl'].size, F.bispec['pol_trpl'].size))

    # create (binned) inverse cov matrix for each ell
    # i.e. use the fact that 12x12 pol invcov can be factored
    # as (Cl-1)_l1^ip (Cl-1)_l2^jq (Cl-1)_l3^kr
    invcov1 = np.ones((bin_size, 12, 12))
    invcov2 = np.ones((bin_size, 12, 12))
    invcov3 = np.ones((bin_size, 12, 12))

    f_check = 0

    for tidx_a, ptrp_a in enumerate(F.bispec['pol_trpl']):
        # ptrp_a = ijk
        for tidx_b, ptrp_b in enumerate(F.bispec['pol_trpl']):
            # ptrp_a = pqr
            # a is first bispectrum, b second one
            # ptrp = pol triplet

            ptrp_a1 = ptrp_a[0]
            ptrp_a2 = ptrp_a[1]
            ptrp_a3 = ptrp_a[2]

            ptrp_b1 = ptrp_b[0]
            ptrp_b2 = ptrp_b[1]
            ptrp_b3 = ptrp_b[2]

            invcov1[:, tidx_a, tidx_b] = bin_invcov[:, ptrp_a1, ptrp_b1]
            invcov2[:, tidx_a, tidx_b] = bin_invcov[:, ptrp_a2, ptrp_b2]
            invcov3[:, tidx_a, tidx_b] = bin_invcov[:, ptrp_a3, ptrp_b3]

    # Depending on lmin, start outer loop not at first bin.
    start_bidx = np.where(bins >= lmin)[0][0]

    end_bidx = np.where(bins >= min(lmax, bins[-1]))[0][0] + 1

    # loop same loop as in binned_bispectrum
    for idx1, i1 in enumerate(bins[start_bidx:end_bidx]):
        idx1 += start_bidx
        cl1 = invcov1[idx1, :, :]  # 12x12

        # init
        fisher_per_bin[idx1] = 0.

        for idx2, i2 in enumerate(bins[idx1:end_bidx]):
            idx2 += idx1
            cl2 = invcov2[idx1, :, :]  # 12x12

            cl12 = cl1 * cl2

            for idx3, i3 in enumerate(bins[idx2:end_bidx]):
                idx3 += idx2

                num = num_pass[idx1, idx2, idx3]
                if num == 0:
                    continue

                cl123 = cl12 * invcov3[idx3, :, :]  #12x12

                B = bispec[idx1, idx2, idx3, :]

                f = np.einsum("i,ij,j", B, cl123, B)
                #                f0 = np.einsum("i,i", B, B)
                #                b0 = np.einsum("ij,ij", cl123, cl123)

                # both B's have num
                f /= float(num)

                if i1 == i2 == i3:
                    f /= 6.
                elif i1 != i2 != i3:
                    pass
                else:
                    f /= 2.

                fisher_per_bin[idx1] += f
                f_check += f

    fisher_per_bin *= fsky
    f_check *= fsky

    min_f = []

    #    print 'fisher_check:', f_check * (4*np.pi / np.sqrt(8))**2
    #    print 'sigma:', 1/np.sqrt(f_check) * (np.sqrt(8)/4./np.pi)

    fisher_check = f_check * (4 * np.pi / np.sqrt(8))**2
    sigma = 1 / np.sqrt(f_check) * (np.sqrt(8) / 4. / np.pi)

    return fisher_check, sigma
Example #6
0
def run(out_dir,
        tag,
        prim_template='local',
        add_noise_opts={},
        get_cls_opts={},
        interp_fisher_opts={},
        dry=False):
    '''    
    Calculate and save bins, beta and bispec. Then calculate fisher.

    Arguments
    ---------
    out_dir : str
        Output directory for Fisher.
    tag : str
        Fisher tag for output file.

    Keyword Arguments
    ---------
    prim_template : str
    dry : bool
        Do not run fisher estimate (but do everything else).
    save_cov : bool
        Save covariance and inverse convariance.

    kwargs : {add_noise_opts}, {get_cls_opts}, {interp_fisher_opts}
    '''

    F = Fisher(out_dir)

    if F.mpi_rank == 0:
        print('working on {}'.format(tag))

    if F.mpi_rank == 0:
        print(tag)

    beta_tag = 'r1_i1_l5200_16_7'
    bins_tag = '5200'
    cosmo_tag = '5200_16_7'
    bispec_tag = '5200_16_7'

    F.get_cosmo(load=True, tag=cosmo_tag, verbose=True)

    F.get_bins(load=True,
               parity='odd',
               verbose=True,
               tag=bins_tag,
               lmin=2,
               lmax=5000)

    F.get_beta(load=True, tag=beta_tag, verbose=True)

    F.get_binned_bispec(prim_template, load=True, tag=bispec_tag)

    cls, ells = get_cls(F.cosmo, **get_cls_opts)

    add_noise(cls, ells, **add_noise_opts)

    invcov, cov = F.get_invcov(ells,
                               cls,
                               return_cov=True,
                               write=True,
                               write_tag=tag)

    lmax_outer = 200

    if not dry:
        f_i = F.interp_fisher(invcov,
                              ells,
                              lmin=2,
                              lmax_outer=lmax_outer,
                              verbose=False,
                              **interp_fisher_opts)

        if F.mpi_rank == 0:
            print(f_i)

        r = get_cls_opts['r']
        F.save_fisher(f_i, r=r, tag=tag)

    return
Example #7
0
def run(prim_template='equilateral',
        out_dir=None,
        camb_dir=None,
        lmin=2,
        lmax=5000):
    '''    
    Calculate and save bins, beta and bispec. Then calculate fisher.

    Keyword Arguments
    ---------
    prim_template : str
    out_dir : str
    camb_dir : str
    lmin : int
    lmax : int
    '''

    tag = 'l{}'.format(lmax)

    camb_opts = dict(camb_out_dir=camb_dir,
                     tag='',
                     lensed=False,
                     high_ell=False,
                     interp_factor=None)

    F = Fisher(out_dir)

    F.get_camb_output(**camb_opts)
    F.get_bins(lmin=lmin,
               lmax=lmax,
               load=True,
               parity='odd',
               verbose=True,
               tag=tag)

    interp_factor = 1
    radii_factor = 10
    beta_tag = 'r{}_i{}_l{}'.format(radii_factor, interp_factor, lmax)

    radii = F.get_updated_radii()
    radii = radii[::radii_factor]

    F.get_beta(func=prim_template,
               radii=radii,
               verbose=True,
               optimize=True,
               interp_factor=interp_factor,
               load=True,
               sparse=True,
               tag=beta_tag)

    F.get_binned_bispec('local', load=True, tag=tag)

    cls, ells = get_cls(camb_dir, lmax, A_lens=1, no_ee=False, no_tt=False)

    # NOTE NOTE
    #    cls[:3,:18] += 1e20
    #    cls[3,:] *= 3

    invcov, cov = F.get_invcov(ells, cls, return_cov=True)
    f_i = F.interp_fisher(invcov, ells, lmin=2, lmax=lmax, verbose=2)
    if F.mpi_rank == 0:
        print(f_i)

    F.barrier()

    b_invcov, b_cov = F.get_binned_invcov(ells, cls, return_bin_cov=True)

    if F.mpi_rank == 0:
        f_n = F.naive_fisher(b_invcov, lmin=2, lmax=lmax)
        print('naive')
        print(f_n)