示例#1
0
def get_bkgr_percentage(bkgr, bkgr_err, p_sample, p_data=None, best_fit=0):
    """
    Get the polarisation percentage of the background, and a histogram 
    of the percent:
    - p_samples/p_v. Need pv_err??

    Input:
    - bkgr (N_Gibbs, 2)
    - pv (N_Gibbs, N)
    - bkgr_err (2)
    """
    #print(np.shape(bkgr), np.shape(pv), np.shape(bkgr_err))
    p_bkgr = tools.MAS(tools.get_P(bkgr[:,0], bkgr[:,1]),\
                       tools.get_P_err(bkgr[:,0], bkgr[:,1], bkgr_err[0],\
                                       bkgr_err[1]))
    print(bkgr[best_fit], bkgr_err)
    bkgr_percent1 = 100*p_bkgr/np.mean(p_sample, axis=1)
    bkgr_best1 = bkgr_percent1[best_fit]
    bkgr_percent1a = 100*p_bkgr/np.mean(p_sample)
    bkgr_best1a = bkgr_percent1a[best_fit]
    print(p_bkgr[best_fit])
    print(bkgr_best1, np.std(bkgr_percent1))
    print(bkgr_best1a, np.std(bkgr_percent1a))
    if p_data is not None:
        bkgr_percent2 = 100*p_bkgr/np.mean(p_data)
        bkgr_best2 = bkgr_percent2[best_fit]
        print(bkgr_best2, np.std(bkgr_percent2))
    bkgr_err_percent = np.std(bkgr_percent1)
    # Histogram:
    plt.figure()
    plt.hist(bkgr_percent1, bins=20, color='g', alpha=0.7,\
             density=True, stacked=True)
    #plt.hist(bkgr[:,0], bins=20, histtype='step', color='k')
    #plt.hist(bkgr[:,1], bins=20, histtype='step', color='b')
    plt.axvline(bkgr_best1, linestyle=':', color='k',\
                label=r'Best fit: ${}\pm{}$ $\%$'.\
                format(round(bkgr_percent1[best_fit], 3),\
                       round(bkgr_err_percent, 3)))
    plt.xlabel(r'Background polarization $p_{{bkgr}}/p_v$ $\%$')
    plt.ylabel('Density')
    plt.legend()
示例#2
0
def tomo_map(data, Nside=2048, starsel='all', part='all', distcut=360):
    """
    Make healpix map for the given Nside for the tomography data

    Parameters:
    -----------
    - data, ndarray.    All of the data array.
    - Nside, integer.   The resolution of the map to make, default is 2048

    Return:
    -------
    - p_map, array.     Array with the fractional polarization, p=sqrt(q^2+u^2)
    - q_map, array.     Array with the fractional q=Q/I polarization.
    - u_map, array.     Array with the fractional u=U/I polarization.
    - sigma_p, array.   Array with the uncertainty of p
    - sigma_q, array.   Array with the uncertainty of q.
    - sigma_u, array.   Array with the uncertainty of u.
    - pix, array.       The pixels where there exist data.
    """

    # Select stars
    if starsel == 'all':
        print('Use all areas')
        pass
    elif starsel == '1cloud':
        ii = select_stars(103.9, 21.97, 0.16)  # 1-cloud region
        data = data[ii, :]
        #ii = np.where(in 1 cloud)[0]     # 1 cloud region
    elif starsel == '2cloud':
        ii = select_stars(104.08, 22.31, 0.16)  # 2 cloud region
        data = data[ii, :]

    print(np.shape(data))
    jj = np.where(data[:, 11] > 360)[0]
    cut_intr = np.logical_and(data[:, 4] <= 0, data[:, 4])
    data = data[cut_intr, :]  # use stars with negative evpa
    print(np.shape(data))

    if part == 'LVC':
        if len(distcut) == 2:
            print('min and max distance for LVC', distcut)
            clean = np.logical_and(data[:,11] > distcut[0],\
                                   data[:,11] < distcut[1])
        else:
            print('LVC for dist >', distcut)
            clean = np.logical_and(data[:, 11] > distcut[0], data[:, 11])
    else:
        clean = np.logical_and(data[:, 11] > distcut[0], data[:, 11])

    data = data[clean, :]  # remove close stars, use r > 360 pc
    print(np.shape(data))
    # remove pol.angle outliers:
    mean = np.mean(data[:, 4])  #*180/np.pi
    sigma3 = 2.5 * np.std(data[:, 4])  #*180/np.pi
    clean_3sigma = np.logical_and(data[:, 4],
                                  data[:, 4] - data[:, 5] < mean + sigma3)
    data = data[clean_3sigma, :]
    print(np.shape(data))
    #s2n = data[:,2]/data[:,3] >= 3
    #data = data[s2n,:]
    #print(np.shape(data), '-')

    # convert to galactic coordinates:
    l, b = tools.convert2galactic(data[:, 0], data[:, 1])
    theta = np.pi / 2. - b * np.pi / 180.
    phi = l * np.pi / 180.
    #print(np.min(theta), np.max(theta))
    print(np.shape(data))

    # get pixel numbers
    pix = hp.pixelfunc.ang2pix(Nside, theta, phi, nest=False)

    # make cut regarding IVC, have only stars not affected by the IVC
    print('Remove stars affected by the IVC')
    if part != 'none':
        IVC_cut = tools.IVC_cut(pix, data[:,11], distcut[0], Nside=Nside,\
                                clouds=part)
        data = data[IVC_cut, :]
        pix = pix[IVC_cut]
    else:
        pass

    #cut_max_d = np.logical_and(data[:,11] < 1000, data[:,11])
    #data = data[cut_max_d,:]
    #pix = pix[cut_max_d]

    #sys.exit()

    # Debias p, since > 0.
    pmas = tools.MAS(data[:, 2], data[:, 3])

    # polariasation rotation (IAU convention):
    # compute pol. in galactic coordinates of q and u.
    print('Rotate polarisation angle from equitorial to galactic.')
    q_gal, u_gal, theta_gal = tools.rotate_pol(data[:,0], data[:,1],\
                                               pmas, data[:,6],\
                                               data[:,8], data[:,4])
    sq_gal, su_gal = tools.error_polangle(pmas, data[:,3],\
                                          theta_gal, np.radians(data[:,5]))

    # correct for extinction:
    #correction = tools.extinction_correction(l, b, data[:,10])
    #q_gal = q_gal*correction
    #u_gal = u_gal*correction
    q_err = sq_gal  #*correction
    u_err = su_gal  #*correction

    psi = 0.5 * np.arctan2(-u_gal, q_gal)

    # Create maps
    Npix = hp.nside2npix(Nside)
    p_map = np.full(Npix, hp.UNSEEN)
    q_map = np.full(Npix, hp.UNSEEN)
    u_map = np.full(Npix, hp.UNSEEN)
    r_map = np.full(Npix, hp.UNSEEN)
    psi_map = np.full(Npix, hp.UNSEEN)
    sigma_p = np.full(Npix, hp.UNSEEN)
    sigma_q = np.full(Npix, hp.UNSEEN)
    sigma_u = np.full(Npix, hp.UNSEEN)
    sigma_psi = np.full(Npix, hp.UNSEEN)
    err_psi = np.full(Npix, hp.UNSEEN)

    print(len(np.unique(pix)), len(pix))
    uniqpix = np.unique(pix)
    index = []
    for k, i in enumerate(uniqpix):
        ind = np.where(pix == i)[0]

        q, qerr = tools.weightedmean(q_gal[ind], q_err[ind])
        u, uerr = tools.weightedmean(u_gal[ind], u_err[ind])
        p, perr = tools.weightedmean(pmas[ind], data[ind, 3])
        #psi2, psierr = tools.weightedmean(psi[ind]*180/np.pi, data[ind,5])

        p_map[i] = p  #np.mean(data[ind, 2])
        q_map[i] = q  #np.mean(q_gal[ind])
        u_map[i] = u  #np.mean(u_gal[ind])

        sigma_p[i] = perr  #tools.sigma_x(data[ind, 3], len(ind))
        sigma_q[
            i] = qerr  #+ np.std(q_gal[ind]) #tools.sigma_x(q_err[ind], len(ind))#
        sigma_u[
            i] = uerr  #+ np.std(u_gal[ind]) #tools.sigma_x(u_err[ind], len(ind))#
        a = np.std(u_gal[ind])  #tools.sigma_x(u_err[ind], len(ind))

        sigma_psi[i] = tools.sigma_x(data[ind, 5], len(ind))  #np.std(psi[ind])
        r_map[i] = np.mean(data[ind, 10])

    #
    print(u_map[uniqpix])
    #sys.exit()
    return (p_map, q_map, u_map, [sigma_p, sigma_q, sigma_u,
                                  sigma_psi], r_map, pix)
示例#3
0
def pix2star_tomo(data, Nside, starsel='all'):
    """
    Method where the pixels are asigned to a star. Remove stars closer 
    than 360 pc, since not polarised.
    """
    # Select stars
    if starsel == 'all':
        print('Use all areas')
        pass
    elif starsel == '1cloud':
        ii = select_stars(103.9, 21.97, 0.16)  # 1-cloud region
        data = data[ii, :]
        #ii = np.where(in 1 cloud)[0]     # 1 cloud region
    elif starsel == '2cloud':
        ii = select_stars(104.08, 22.31, 0.16)  # 2 cloud region
        data = data[ii, :]
    print(np.shape(data))

    jj = np.where(data[:, 11] > 360)[0]
    clean = np.logical_and(data[:, 11] > 360, data[:, 11])
    #print(clean)
    data = data[clean, :]  # remove close stars, use r > 360 pc
    # remove pol.angle outliers:
    mean = np.mean(data[:, 4])  #*180/np.pi
    sigma3 = 2.5 * np.std(data[:, 4])  #*180/np.pi
    clean_3sigma = np.logical_and(data[:, 4],
                                  data[:, 4] - data[:, 5] < mean + sigma3)
    data = data[clean_3sigma, :]

    #jj = np.where(data[:,10] > 360)[0]
    #data = data[jj,:] # remove close stars, use r > 360 pc
    print(Nside, np.shape(data))
    #sys.exit()

    # convert to galactic coordinates:
    l, b = tools.convert2galactic(data[:, 0], data[:, 1])
    theta = np.pi / 2. - b * np.pi / 180.  # in healpix
    phi = l * np.pi / 180.

    # get pixel numbers
    pix = hp.pixelfunc.ang2pix(Nside, theta, phi, nest=False)

    # clean for IVC:
    IVC_cut = tools.IVC_cut(pix,
                            data[:, 11],
                            distcut=900,
                            Nside=Nside,
                            clouds='LVC')
    data = data[IVC_cut, :]
    theta = theta[IVC_cut]
    phi = phi[IVC_cut]

    # Debias p, since > 0.
    pmas = tools.MAS(data[:, 2], data[:, 3])
    # polariasation rotation (IAU convention):
    print('Rotate polarisation angle from equitorial to galactic.')
    q_gal, u_gal, evpa = tools.rotate_pol(data[:,0], data[:,1], data[:,2],\
                                    data[:,6],data[:,8], data[:,4])
    #q_err, u_err, evpa0 = tools.rotate_pol(data[:,0], data[:,1], data[:,3],\
    #                                data[:,7], data[:,9], data[:,4])
    sq_gal, su_gal = tools.error_polangle(pmas, data[:,3],\
                                          evpa, np.radians(data[:,5]))
    #print(len(q_gal), len(u_gal), len(evpa), '.')
    # correct for extinction:
    #correction = tools.extinction_correction(l, b, data[:,10])
    #q_gal = q_gal*correction
    #u_gal = u_gal*correction
    j = np.where(u_gal == np.max(u_gal))[0]
    #print(j, u_gal[j], l[j], b[j], data[j,10], data[j,4])
    #print(np.mean(u_gal), np.mean(data[:,4]))
    q_err = sq_gal  #*correction
    u_err = su_gal  #*correction

    #q_err = data[:,7]
    #u_err = data[:,9]

    p_gal = np.sqrt(q_gal**2 + u_gal**2)
    sigma = [data[:, 3], q_err, u_err]
    r = data[:, 10]
    pix_stars = hp.ang2pix(Nside, theta, phi)
    #print(pix_stars)
    #print(len(pix_stars), len(u_gal))
    return (p_gal, q_gal, u_gal, sigma, r, pix_stars)
示例#4
0
        #sys.exit()

        print('')
        #print(model_err3[:,mask])
        #print(err_mod1)
        print(err_mod3[0,:]/(np.sqrt(C_QQ[mask])*unit),\
              err_mod3[1,:]/(np.sqrt(C_UU[mask])*unit),\
              err_mod3[2,:]/(np.sqrt(C_QU[mask])*unit))
        err_model3 = np.full(np.shape(model_err3), np.nan)
        err_model3[:, mask] = err_mod3

        print('Residual polarisation:')
        P_bkgr = np.sqrt(QU_bkgr2[0]**2 + QU_bkgr2[1]**2)
        P_bkgr_err = np.sqrt((bkgr_err2[0]*QU_bkgr2[0])**2\
                             + (bkgr_err2[1]*QU_bkgr2[1])**2)/P_bkgr
        P_bkgr = tools.MAS(P_bkgr, P_bkgr_err)
        #print(P_bkgr, P_bkgr_err)
        #print(P_bkgr/Ps[mask])
        print(P_bkgr / np.mean(Ps[mask]), np.std(P_bkgr / Ps[mask]))
        # plot correlation with slopes:
        plotting.plot_corr2(QU_model3[0,:]/unit, QU_model3[1,:]/unit, q, u,\
                            sq, su, mask, dist, \
                            Nside=Nside, xlab=r'$q,u$', \
                            ylab=r'$Q,U_{{353}}$', title='QU-qu',\
                            save=save+'_model3', part=part, C_ij=err_model3)

        plotting.plot_corr2(QU_model2[0,:]/unit, QU_model2[1,:]/unit, q, u,\
                            sq, su, mask, dist, \
                            Nside=Nside, xlab=r'$q,u$', \
                            ylab=r'$Q,U_{{353}}$', title='QU-qu',\
                            save=save+'_model2', part=part, C_ij=err_model2)
示例#5
0
def correlation(qu, mod, sq, su, mod_err, mask, lab='', save='', QU=None,\
                QU_err=None, R_Pp=None, R_err=None):
    """
    Plot correlation plot of model vs visual polarisation. If QU is not none 
    include in plot.

    """
    unit = 287.45 * 1e-6
    unit2 = 287.45 * 1e-12
    qu_a = np.concatenate((qu[0, :], qu[1, :]))
    mod_a = np.concatenate((mod[0, :], mod[1, :]))

    Ps = tools.get_P(mod[0, :], mod[1, :])
    pv = tools.get_P(qu[0, :], qu[1, :])
    P_err = tools.get_P_err(mod[0, :], mod[1, :], mod_err[0, :], mod_err[1, :])
    p_err = tools.get_P_err(qu[0, :], qu[1, :], sq, su)
    if R_Pp is None:
        R_mod = np.mean(tools.MAS(Ps, P_err) / tools.MAS(pv, p_err))
        print(R_mod)
    else:
        R_Pp.append(np.mean(tools.MAS(Ps, P_err) / tools.MAS(pv, p_err)))
        print(R_Pp)

    # chi^2 estimate:
    print('Joint')
    param, sigma, chi2 = tools.Chi2(mod[0,:]/unit, mod[1,:]/unit, qu[0,:],\
                                    qu[1,:], (mod_err/unit)**2,\
                                    sq, su, sampler=True)
    print(np.corrcoef(qu_a, mod_a))
    print('Qq')
    param_q, sigma_q, chi2_q = tools.Chi2(mod[0,:]/unit, None, qu[0,:], None,\
                                          (mod_err/unit)**2, sq, \
                                          None, sampler=True)
    print(np.corrcoef(qu[0, :], mod[0, :]))
    print('Uu')
    param_u, sigma_u, chi2_u = tools.Chi2(None, mod[1,:]/unit, None, qu[1,:],\
                                          (mod_err/unit)**2, None,\
                                          su, sampler=True)
    print(np.corrcoef(qu[1, :], mod[1, :]))

    tools.delta_psi(mod[0, :], mod[1, :], qu[0, :], qu[1, :])

    x = np.linspace(np.min(qu_a), np.max(qu_a), 10)
    plt.figure('scatter {}'.format(lab))
    # points
    plt.scatter(qu[0, :], mod[0, :], marker='^', c='k')
    plt.scatter(qu[1, :], mod[1, :], marker='v', c='b')

    if QU is not None:
        plt.scatter(qu[0, :], QU[0, :], marker='.', c='grey')
        plt.scatter(qu[1, :], QU[1, :], marker='.', c='skyblue')
    # Slopes
    plt.plot(x, x*param_q[0]*unit + param_q[1]*unit, '-k',\
             label=r'$a_{{Qq}}$={}'.format(round(param_q[0]*unit,3)))
    plt.plot(x, x*param_u[0]*unit + param_u[1]*unit, '-b',\
             label=r'$a_{{Uu}}$={}'.format(round(param_u[0]*unit,3)))
    plt.plot(x, x*param[0]*unit + param[1]*unit, '-r',\
             label=r'$a_{{QU-qu}}$={}'.format(round(param[0]*unit,3)))

    if R_Pp is not None:
        print('Data R_Pp:', R_Pp[0])
        print('Model R_Pp:', R_Pp[1])
        plt.plot(x, -x*R_Pp[0], '--r', label=r'$R_{{Pp}}^{{data}}$={}'.\
                 format(round(R_Pp[0], 3)))
        plt.plot(x, -x*R_Pp[1], ':r', label=r'$R_{{Pp}}^{{max L}}$={}'.\
                 format(round(R_Pp[1], 3)))

    plt.xlabel(r'$q_v, u_v$')
    plt.ylabel(r'{} $Q_s, U_s$ [MJy/sr]'.format(lab))
    plt.grid(True)
    plt.legend()
    plt.savefig('Figures/Sampling/QUqu_{}_{}_corr.png'.format(save, lab))

    plt.figure('errorbar {}'.format(lab))
    x = np.linspace(np.min(qu), np.max(qu), 10)
    plt.errorbar(qu[0,:], mod[0,:], xerr=sq, yerr=mod_err[0,:], fmt='none',\
                 ecolor='k')
    plt.errorbar(qu[1,:], mod[1,:], xerr=su, yerr=mod_err[1,:], fmt='none',\
                 ecolor='b')

    # Slopes
    plt.plot(x, x*param_q[0]*unit + param_q[1]*unit, '-k',\
             label=r'$a_{{Qq}}={}\pm{}$'.format(round(param_q[0]*unit, 3),\
                                                round(sigma_q[0]*unit, 3)))
    plt.plot(x, x*param_u[0]*unit + param_u[1]*unit, '-b',\
             label=r'$a_{{Uu}}={}\pm{}$'.format(round(param_u[0]*unit, 3),\
                                           round(sigma_u[0]*unit, 3)))
    plt.plot(x, x*param[0]*unit + param[1]*unit, '-r',\
             label=r'$a_{{QUqu}}={}\pm{}$'.format(round(param[0]*unit, 3),\
                                              round(sigma[0]*unit, 3)))
    if R_Pp is not None:
        plt.plot(x, -x*R_Pp[0], '--r', label=r'$R_{{Pp}}^{{data}}={}\pm{}$'.\
                 format(round(R_Pp[0], 3), round(R_err[0], 3)))
        plt.plot(x, -x*R_Pp[0], ':r', label=r'$R_{{Pp}}^{{max L}}={}\pm{}$'.\
                 format(round(R_Pp[1], 3), round(R_err[1], 3)))

    plt.legend()
    plt.xlabel(r'$q_v, u_v$')
    plt.ylabel(r'{} $Q_s, U_s$ [MJy/sr]'.format(lab))
    plt.grid(True)
    plt.savefig('Figures/Sampling/QUqu_{}_{}_ebar.png'.format(save, lab))
示例#6
0
    if plot == 'mcmc':
        print('Sampling contribution to submm polarization')
        # Load C_ij from Planck:
        Cfile = 'Data/Planck_Cij_353_2048_full.h5'
        C_ij = load.C_ij(Cfile, Nside)
        C_II = C_ij[0, :] * 1e12
        C_IQ = C_ij[1, :] * 1e12
        C_IU = C_ij[2, :] * 1e12
        C_QQ = C_ij[3, :] * 1e12
        C_QU = C_ij[4, :] * 1e12
        C_UU = C_ij[5, :] * 1e12

        # input params to sampler: Ps, ps, psi_v, mask
        Ps = np.sqrt(Q**2 + U**2)
        err_P = np.sqrt(C_QQ * Q**2 + C_UU * U**2) / Ps
        Ps = tools.MAS(Ps, err_P)
        ps = Ps / T
        lon = coord[0]
        lat = coord[1]
        ps_err = err_P / T

        # Convert from uK_cmb to MJy/sr:
        unit = 287.45 * 1e-6
        Ps *= unit

        QU = np.array([Q, U]) * unit
        qu = np.array([q, u])
        print(QU[:, mask], np.shape(QU))

        R_Pp = Ps[mask] / p[mask]
        err_R = err_P[mask] / p[mask] - Ps[mask] * tomo[-1][mask] / p[mask]**2