コード例 #1
0
def deltaShear(zspec, zphot, zcluster = 0.2):

    ginfpredict = nfwmodeltools.NFWShear(ones(1), 4.0, 1., zcluster)

    beta_s_spec = nfwutils.beta_s(zspec, zcluster)
    beta_s_phot = nfwutils.beta_s(zphot, zcluster)

    return (beta_s_spec - beta_s_phot)*ginfpredict
コード例 #2
0
def create_nfw_realsim(ngals,
                       concentration,
                       scale_radius,
                       zcluster,
                       shape_sigma,
                       zsigma,
                       pdf_range=np.arange(0, 5.0, 0.01),
                       doping=None,
                       pixscale=__DEFAULT_PIXSCALE__):

    radii_pix = np.random.uniform(100, 5000, size=ngals)

    radii_mpc = pix2mpc(radii_pix, pixscale, zcluster)

    actualZs, pdz = create_realistic_zdata(ngals,
                                           zsigma=zsigma,
                                           pdf_range=pdf_range)

    if doping:
        doping(actualZs)

    ghats = create_nfwmodel_shapedata(concentration, scale_radius , zcluster,
                                      ngals, actualZs, radii_mpc) + \
                                      shape_sigma*np.random.standard_normal(ngals)

    betas = nfwutils.beta_s(actualZs, zcluster)

    dz = pdf_range[1] - pdf_range[0]

    inRange = np.max(pdz, axis=-1) > 1e-3

    return radii_pix[inRange], radii_mpc[inRange], ghats[inRange], actualZs[
        inRange], betas[inRange], pdz[inRange], dz
コード例 #3
0
def betasplot():

    fig = pylab.figure()
    xmarg = 0.11
    ymarg = 0.15
    ax = pylab.axes([xmarg, ymarg, 0.95 - xmarg, 0.95 - ymarg])

    z = np.arange(0, 2.5, 0.002)

    pylab.plot(z, nfwutils.beta_s(z, 0.2), label=r'$Z_c = 0.2$')
    pylab.plot(z, nfwutils.beta_s(z, 0.5), label=r'$Z_c = 0.5$')
    pylab.plot(z, nfwutils.beta_s(z, 0.7), label=r'$Z_c = 0.7$')
    pylab.legend(loc='lower right', numpoints=1)
    pylab.xlabel(r'$Z_b$ : Source Galaxy Redshift')
    pylab.ylabel(r'$\beta_s(Z_b; Z_c)$')

    return fig
コード例 #4
0
def createNLikelihood(mcluster,
                      zcluster,
                      z0,
                      sigz0,
                      rmin=0.75,
                      rmax=3.0,
                      c=4.0,
                      massrad=1.5,
                      gsig=0.25,
                      npoints=200000):

    rs = nfwutils.RsMassInsideR(mcluster, c, zcluster, massrad)

    linmin = rmin / np.sqrt(2)
    linmax = rmax / np.sqrt(2)
    r_points = np.sqrt(
        np.random.uniform(linmin, linmax, size=npoints)**2 +
        np.random.uniform(linmin, linmax, size=npoints)**2)

    z_points = z0 + sigz0 * np.random.standard_normal(npoints)

    gamma_inf = nfwmodeltools.NFWShear(r_points, c, rs, zcluster)
    kappa_inf = nfwmodeltools.NFWKappa(r_points, c, rs, zcluster)
    beta_s = nfwutils.beta_s(z_points, zcluster)

    g0 = beta_s * gamma_inf / (1 - beta_s * kappa_inf)

    ghat_points = g0 + gsig * np.random.standard_normal(npoints)

    dzt = 0.01
    pdzrange = np.arange(z0 - 5 * sigz0, z0 + 5 * sigz0, dzt)
    pdz = stats.Gaussian(pdzrange, z0, sigz0) * dzt

    betas = np.array(nfwutils.beta_s(pdzrange, zcluster))

    def nloglike(Mguess):
        #units of 1e14
        Mguess = Mguess * 1e14

        rs_guess = nfwutils.RsMassInsideR(Mguess, c, zcluster, massrad)

        return -pdzperturbtools.nloglike_loop(r_points, z_points, ghat_points,
                                              betas, pdz, gsig, rs_guess, c,
                                              zcluster)

    return nloglike
コード例 #5
0
def create_nfwmodel_shapedata(concentration, scale_radius, zcluster, ngals, zs,
                              rs):

    realBetas = nfwutils.beta_s(zs, zcluster)

    gamma = realBetas * tools.NFWShear(rs, concentration, scale_radius,
                                       zcluster)
    kappa = realBetas * tools.NFWKappa(rs, concentration, scale_radius,
                                       zcluster)

    g = gamma / (1 - kappa)

    return g, gamma, kappa
コード例 #6
0
    def makeLikelihood(self, datamanager, parts):

        inputcat = datamanager.inputcat

        pdz = datamanager.pdz

        bin_selectors = self.bin_selectors(inputcat)

        parts.r_mpc = [
            np.ascontiguousarray(inputcat['r_mpc'][x].astype(np.float64))
            for x in bin_selectors
        ]
        parts.ghats = [
            np.ascontiguousarray(inputcat['ghats'][x].astype(np.float64))
            for x in bin_selectors
        ]
        parts.pdz = [
            np.ascontiguousarray(pdz[x].astype(np.float64))
            for x in bin_selectors
        ]

        parts.nshapebins = len(bin_selectors)
        parts.pdzrange = datamanager.pdzrange

        parts.betas = np.ascontiguousarray(
            nfwutils.beta_s(parts.pdzrange, parts.zcluster).astype(np.float64))
        parts.nzbins = len(parts.betas)

        parts.data = np.empty(parts.nshapebins, dtype=object)

        for i, cur_ghats, cur_shape_param, cur_r_mpc, cur_pdz in zip(
                np.arange(parts.nshapebins), parts.ghats, parts.shape_params,
                parts.r_mpc, parts.pdz):

            @pymc.stochastic(observed=True, name='data_%d' % i)
            def data(value=cur_ghats,
                     r_scale=parts.r_scale,
                     shape_params=cur_shape_param,
                     r_mpc=cur_r_mpc,
                     pdz=cur_pdz,
                     betas=parts.betas,
                     concentration=parts.concentration,
                     zcluster=parts.zcluster):

                return self.likelihood_func(r_scale, r_mpc, value, betas, pdz,
                                            shape_params, concentration,
                                            zcluster)

            parts.data[i] = data
コード例 #7
0
    def makeLikelihood(self, datamanager, parts):

        inputcat = datamanager.inputcat

        pdz = datamanager.pdz

        parts.r_mpc = np.ascontiguousarray(inputcat['r_mpc'].astype(
            np.float64))
        parts.ghats = np.ascontiguousarray(inputcat['ghats'].astype(
            np.float64))
        parts.pdz = np.ascontiguousarray(pdz.astype(np.float64))

        parts.pdzrange = datamanager.pdzrange

        parts.betas = np.ascontiguousarray(
            nfwutils.beta_s(parts.pdzrange, parts.zcluster).astype(np.float64))
        parts.nzbins = len(parts.betas)

        parts.data = None
        for i in range(10):

            try:

                @pymc.stochastic(observed=True, name='data_%d' % i)
                def data(value=parts.ghats,
                         r_scale=parts.r_scale,
                         shearcal_m=parts.shearcal_m,
                         shearcal_c=parts.shearcal_c,
                         sigma=parts.sigma,
                         gamma=parts.gamma,
                         r_mpc=parts.r_mpc,
                         pdz=parts.pdz,
                         betas=parts.betas,
                         concentration=parts.concentration,
                         zcluster=parts.zcluster):

                    return nfwtools.bentvoigt_like(r_scale, r_mpc, value,
                                                   betas, pdz, shearcal_m,
                                                   shearcal_c, sigma, gamma,
                                                   concentration, zcluster)

                parts.data = data

                break
            except pymc.ZeroProbability:
                pass

        if parts.data is None:
            raise ModelInitException
コード例 #8
0
def scaleShear(controller, rs, concen = 4., cosmology = nfwutils.std_cosmology):
    #assumes BentVoigt3Shapedistro 

    inputcat = controller.inputcat
    z_b = inputcat['z_b']
    zcluster = controller.zcluster

    cluster_exclusion = np.logical_and(zcluster-0.05 < z_b, z_b < zcluster + 0.1)
    inputcat = inputcat.filter(np.logical_not(cluster_exclusion))
    r_mpc = inputcat['r_mpc']
    ghats = inputcat['ghats']
    size = inputcat['size']
    z_b = inputcat['z_b']


    comovingdist = nfwutils.ComovingDistMemoization(cosmology)

    pivot, m_slope, m_b, m_cov, c = controller.modelbuilder.psfDependence('interp', controller.psfsize)

    betainf = nfwutils.beta([1e6], zcluster, comovingdist = comovingdist)

    #matching definitions with Taylor et al 2011 -> need to get rid of beta_inf in shear amp
    shear = nfwmodeltools.NFWShear(r_mpc, concen, rs, zcluster, comovingdist = comovingdist) / betainf
    
    kappa = nfwmodeltools.NFWKappa(r_mpc, concen, rs, zcluster,  comovingdist = comovingdist)
    


    m = np.zeros_like(size)
    m[size >= pivot] = m_b
    m[size < pivot] = m_slope*(size[size < pivot] - pivot) + m_b

    beta_s = nfwutils.beta_s(z_b, zcluster, comovingdist = comovingdist)


    
    estimators = (ghats - c)*(1-beta_s*kappa)/((1+m)*shear)



    comoving_cluster = comovingdist(zcluster)
    scaledZ = np.array([comovingdist(zi)/comoving_cluster for zi in z_b])

    estimators[scaledZ < 1] = ((ghats - c)/(1+m))[scaledZ < 1]

    

    return scaledZ, estimators
コード例 #9
0
def outputBeta(simdir, cluster):

    for catfile in glob.glob('%s/%s//cutout_*.cat' % (simdir, cluster)):

        root, ext = os.path.splitext(catfile)

        cat = ldac.openObjectFile(catfile)

        zcluster = cat.hdu.header['Z']

        output = open('%s.beta' % root, 'w')

        betas = nfwutils.beta_s(cat['z'], zcluster)

        aveBeta = np.mean(betas)
        aveBeta2 = np.mean(betas**2)

        output.write('%f %f\n' % (aveBeta, aveBeta2))

        output.close()
コード例 #10
0
def alteredCosmology(cat, om):

    cosmology = nfwutils.Cosmology(om, 1. - om)
    comovingdist = nfwutils.ComovingDistMemoization(cosmology)

    betas = nfwutils.beta_s(cat['z'], 0.3, comovingdist)
    dl = nfwutils.angulardist(0.3, comovingdist=comovingdist)

    r_mpc = cat['r_pix'] * 0.2 * (1. / 3600.) * (np.pi / 180.) * dl

    shears = tools.NFWShear(r_mpc,
                            4.0,
                            0.5,
                            0.3,
                            dl,
                            Omega_m=om,
                            Omega_l=1 - om)
    kappa = tools.NFWKappa(r_mpc,
                           4.0,
                           0.5,
                           0.3,
                           dl,
                           Omega_m=om,
                           Omega_l=1 - om)
    g = betas * shears / (1 - betas * kappa)
    scale_radii = beta_method(cat['r_pix'],
                              r_mpc,
                              g,
                              np.mean(betas),
                              np.mean(betas**2),
                              4,
                              0.3,
                              Omega_m=om,
                              Omega_l=1 - om)
    mass = nfwutils.massInsideR(scale_radii, 4.0, 0.3, 1, cosmology)

    return mass
コード例 #11
0
def colorCutBias():

    fig = pylab.figure()
    xmarg = 0.12
    ymarg = 0.12
    ax = pylab.axes([xmarg, ymarg, 0.95 - xmarg, 0.95 - ymarg])

    cosmos = ldac.openObjectFile(
        '/u/ki/dapple/nfs12/cosmos/simulations/publication/highsn/APER/bpz.cat',
        'STDTAB')

    zclusters = np.arange(0.2, 0.8, 0.05)

    beta_true = np.array([np.mean(nfwutils.beta_s(cosmos['BPZ_Z_S'], z)) \
                              for z in zclusters])
    beta_measure = np.array([np.mean(nfwutils.beta_s(cosmos['BPZ_Z_B'], z)) \
                              for z in zclusters])

    pylab.plot(zclusters,
               beta_measure / beta_true,
               'b-',
               label='Limited Filters',
               linewidth=1.25)

    bpz = ldac.openObjectFile(
        '/u/ki/dapple/orange/cats_2011-09-08/MACS0018+16.W-J-V.bpz.tab',
        'STDTAB')
    night1 = ldac.openObjectFile(
        '/u/ki/dapple/orange/cats_2011-09-08/MACS0018+16.W-J-V.gabodsid1728.cut_lensing.cat'
    )
    night2 = ldac.openObjectFile(
        '/u/ki/dapple/orange/cats_2011-09-08/MACS0018+16.W-J-V.gabodsid1729.cut_lensing.cat'
    )
    bpz1 = bpz.matchById(night1)
    bpz2 = bpz.matchById(night2)

    aveBeta1 = np.array([np.mean(nfwutils.beta_s(bpz1['BPZ_Z_B'], x)) \
                          for x in zclusters])
    aveBeta2 = np.array([np.mean(nfwutils.beta_s(bpz2['BPZ_Z_B'], x)) \
                          for x in zclusters])

    pylab.plot(zclusters,
               aveBeta2 / aveBeta1,
               'r-',
               label='Different Seeing',
               linewidth=1.25)

    rhcut = night1['rh'] > np.min(night2['rh'])

    bpz1a = bpz1.filter(rhcut)
    aveBeta1a = np.array([np.mean(nfwutils.beta_s(bpz1a['BPZ_Z_B'], x)) \
                              for x in zclusters])
    pylab.plot(zclusters, aveBeta2 / aveBeta1a, 'r-.', linewidth=1.25)

    pylab.axhline(1.0, c='k', linewidth=1.5)

    pylab.grid()
    pylab.axis([0.15, 0.85, 0.95, 1.24])
    pylab.xlabel('Cluster Redshift')
    pylab.ylabel(r'$<\beta_{measured}> / <\beta_{true}>$')
    pylab.legend(loc='best')

    return fig
コード例 #12
0
def createCatalog(bpz,
                  bpz_sizes,
                  bpz_snratios,
                  concentration,
                  scale_radius,
                  zcluster,
                  ngals,
                  shape_distro=__DEFAULT_SHAPE_DISTRO__,
                  shape_distro_args=[],
                  shape_distro_kw={},
                  maxpix=5000,
                  radii_pix=None,
                  contam=None,
                  contam_args=[],
                  contam_kw={},
                  idcol='ID'):

    # ngals == None -> no bootstrapping
    # bpz is ldac bpz output

    momento = {
        'concentration': concentration,
        'scale_radius': scale_radius,
        'zcluster': zcluster,
        'ngals': ngals,
        'shape_distro': shape_distro.func_name,
        'shape_distro_args': shape_distro_args,
        'shape_distro_kw': shape_distro_kw,
        'maxpix': maxpix,
        'radii_pix': radii_pix
    }

    bootstrap = True
    if ngals == None:
        bootstrap = False
        ngals = len(bpz)

    if radii_pix is None:
        x_pix = np.random.uniform(0, maxpix, ngals)
        y_pix = np.random.uniform(0, maxpix, ngals)
        radii_pix = np.sqrt(x_pix**2 + y_pix**2)

    radii_mpc = radii_pix * pixscale * (1. / 3600.) * (
        np.pi / 180.) * sp.angulardist(zcluster)

    chosenZs = bpz
    chosenSizes = bpz_sizes
    chosenSNratios = bpz_snratios
    if bootstrap:
        indices = np.random.randint(0, len(bpz), ngals)
        chosenZs = bpz.filter(indices)
        chosenSizes = bpz_sizes[indices]
        chosenSNratios = bpz_snratios[indices]

    print "adam-look: running createCatalog in cosmos_sim.py"
    z_id = chosenZs[idcol]
    zbins = numpy.arange(0, 6.01, .01)
    z_drawn = -1 * np.ones(len(chosenZs))
    print '!!!', len(z_drawn)
    for id_indx, id in enumerate(z_id):
        try:
            cdf = id2pz_cdf[id]
        except:
            if id in gone_ids:
                print "adam-look: gone id ", id
                continue
            else:
                raise
        x = numpy.random.rand()
        try:
            zval = (zbins[cdf <= x])[-1]
        except:
            if id in bad_ids:
                print "adam-look: bad id ", id
                continue
            else:
                raise
        z_drawn[id_indx] = zval

    good_draws = z_drawn > -1
    z_drawn = z_drawn[good_draws]
    radii_pix = radii_pix[good_draws]
    radii_mpc = radii_mpc[good_draws]
    chosenZs = chosenZs.filter(good_draws)
    chosenSizes = chosenSizes[good_draws]
    chosenSNratios = chosenSNratios[good_draws]
    ngals = len(z_drawn)

    true_shears, true_gamma, true_kappa = nfwsim.create_nfwmodel_shapedata(
        concentration, scale_radius, zcluster, ngals, z_drawn, radii_mpc)

    true_beta = nfwutils.beta_s(z_drawn, zcluster)

    ghats = shape_distro(true_shears,
                         np.column_stack([chosenSizes, chosenSNratios]),
                         *shape_distro_args, **shape_distro_kw)

    cols = [
        pyfits.Column(name='Seqnr', format='J', array=np.arange(ngals)),
        pyfits.Column(name='r_pix', format='E', array=radii_pix),
        pyfits.Column(name='r_mpc', format='E', array=radii_mpc),
        pyfits.Column(name='z', format='E', array=z_drawn),
        pyfits.Column(name='z_id', format='J', array=chosenZs[idcol]),
        pyfits.Column(name='ghats', format='E', array=ghats),
        pyfits.Column(name='true_shear', format='E', array=true_shears),
        pyfits.Column(name='true_z', format='E', array=z_drawn),
        pyfits.Column(name='true_beta', format='E', array=true_beta),
        pyfits.Column(name='true_gamma', format='E', array=true_gamma),
        pyfits.Column(name='true_kappa', format='E', array=true_kappa)
    ]

    simcat = pyfits.new_table(pyfits.ColDefs(cols))
    simcat.header.update('EXTNAME', 'OBJECTS')
    simcat.header.update('concen', concentration)
    simcat.header.update('r_s', scale_radius)
    simcat.header.update('z', zcluster)

    return ldac.LDACCat(simcat), momento
コード例 #13
0
bpz = ldac.openObjectFile(bpzfile, 'STDTAB').matchById(inputcat, 'z_id')

pdzmanager = pdzutils.PDZManager.open(pdzfile)

pdzrange, pdzs = pdzmanager.associatePDZ(inputcat['z_id'])

foregroundprob = pdzs.cumsum(axis=-1).T[pdzrange <= zcluster][-1]

goodObjs = np.logical_and(
    bpz['BPZ_ODDS'] > bpzcut,
    np.logical_and(bpz['BPZ_Z_B'] < 1.25, foregroundprob < 0.25))

#                                         bpz['z_b'] > zcluster + 0.1),

betas = nfwutils.beta_s(bpz['BPZ_Z_B'], zcluster)

sigma = 0.25

print '!!!!!!!!!!!!!!'
print len(inputcat.filter(goodObjs))

rss, nfails = bm.bootstrap_newman_method(inputcat['r_mpc'][goodObjs],
                                         inputcat['ghats'][goodObjs],
                                         betas[goodObjs],
                                         sigma,
                                         concentration,
                                         zcluster,
                                         nBootstraps,
                                         msigma=msigma)