コード例 #1
0
def map_stats(cosmo_tomo_cone):
    '''for fits file fn, generate ps, peaks, minima, pdf, MFs
    fn: input file name, including full path
    tomo=1, 2,..5: int, for tomographic bins
    cone=1, 2,..5: int, for light cones'''

    if len(cosmo_tomo_cone) == 3:
        cosmo, tomo, cone = cosmo_tomo_cone
        ipz = ''
    else:
        cosmo, tomo, cone, ipz = cosmo_tomo_cone

    ##################################
    ### generate random see, such that it is the same for all cosmology
    ### but different for tomo and cone
    ##################################

    if cosmo == 'cov':
        iseed = int(10000 + cone * 10 + tomo)
        out_dir = dir_cov
        fn = cov_fn_gen(tomo, cone)

    elif cosmo == 'bias':
        iseed = int(20000 + cone * 10 + tomo)  #20000
        out_dir = dir_bias
        fn = bias_fn_gen(tomo, ipz, cone)

    else:  ## all cosmologies
        if cosmo[-1] == 'a':
            iseed = int(cone * 10 +
                        tomo)  ## cone goes from 1 to 25, so 10 to 250
        #else:#
        elif cosmo[
                -1] == 'f':  ##'f' starts with a different seed from the 'a' cosmology
            iseed = int(1000 + cone * 10 + tomo)
        out_dir = dir_cosmos
        fn = cosmo_fn_gen(cosmo, tomo, cone)

    print(fn)

    ##################################
    #### check if the map and comoputed stats files are there
    ##################################

    ############ check fits file exist
    if not os.path.isfile(fn):
        print(fn, 'fits file does not exist \n')
        return 0

    out_fn_arr = [
        out_dir + cosmo + '_tomo%i_cone%s_s%i.npy' % (tomo, cone, theta_g)
        for theta_g in theta_g_arr
    ]

    ############# check if stats files exist; if yes, skip computation
    if np.prod(array([os.path.isfile(out_fn) for out_fn in out_fn_arr])):
        ### check if the product of boolean elements in the array = 1 (meaning for all smoothing scales)
        print(fn, 'stats files exist; skip computation.\n')
        return 0  ### all files already exist, no need to process

    ##################################
    ########## map operations
    ##################################

    imap = fits.open(fn)[0].data  ## open the file

    ### add noise
    seed(iseed)
    noise_map = np.random.normal(loc=0.0,
                                 scale=sigma_pix_arr[tomo - 1],
                                 size=(map_pix, map_pix))
    kappa_map = ConvergenceMap(data=imap + noise_map, angle=map_side_deg)
    noise_map = 0  ## release the memory

    ### compute stats
    ## 3 smoothing
    ## 9 cols: ell, ps, kappa, peak, minima, pdf, v0, v1, v2
    ps_noiseless = ConvergenceMap(data=imap,
                                  angle=map_side_deg).powerSpectrum(l_edges)
    ps_unsmoothed = kappa_map.powerSpectrum(
        l_edges)  ## power spectrum should be computed on unsmoothed maps

    s = 0
    for theta_g in theta_g_arr:
        out_fn = out_dir + cosmo + '%s_tomo%i_cone%s_s%i.npy' % (ipz, tomo,
                                                                 cone, theta_g)
        imap = kappa_map.smooth(theta_g * u.arcmin)
        out = zeros(shape=(11, Nbin))
        kappa_bins = kappa_bin_edges[s][tomo - 1]
        ps = imap.powerSpectrum(l_edges)
        peak = imap.peakCount(kappa_bins)
        minima = ConvergenceMap(data=-imap.data,
                                angle=map_side_deg).peakCount(kappa_bins)
        pdf = imap.pdf(kappa_bins)
        mfs = imap.minkowskiFunctionals(kappa_bins)
        out[0] = ps[0]
        out[1] = ps_noiseless[1]
        out[2] = ps_unsmoothed[1]
        out[3] = ps[1]
        out[4] = peak[0]
        out[5] = peak[1]
        out[6] = minima[1][::-1]
        out[7] = pdf[1]
        out[8] = mfs[1]
        out[9] = mfs[2]
        out[10] = mfs[3]
        save(out_fn, out)  ### save the file
        s += 1