Example #1
0
    def NN3DCorrelation(self, min_sep=1, max_sep=200, bin_size=0.5):
        """
        Caclulates 3D correlation function of objects using Catalog's ra, dec, r
        Requires randcatalog to exist. Distance units are Mpc/h.

        Returns tuple (logr, meanlogr, xi, xivar)
        
        """
        catS = treecorr.Catalog(ra=self.catalog["ra"], dec=self.catalog["dec"], 
                                r=self.catalog["r"],
                                ra_units="radians", dec_units="radians")
        if (self.randcatalog):
            catR = treecorr.Catalog(ra=self.randcatalog["ra"], 
                                    dec=self.randcatalog["dec"], 
                                    r=self.randcatalog["r"], ra_units="radians", 
                                    dec_units="radians")
        else:
            print ("Need random catalog for NN")
            stop()
        dd=treecorr.NNCorrelation(min_sep=min_sep, bin_size=bin_size, 
                                  max_sep=max_sep)
        dr=treecorr.NNCorrelation(min_sep=min_sep, bin_size=bin_size, 
                                  max_sep=max_sep)
        rr=treecorr.NNCorrelation(min_sep=min_sep, bin_size=bin_size, 
                                  max_sep=max_sep)
            
        dd.process(catS)
        dr.process(catS, catR)
        rr.process(catR)
        xi, xivar=dd.calculateXi(rr,dr)
        logr = dd.logr 
        meanlogr= dd.meanlogr
        return (logr, meanlogr, xi, xivar)
Example #2
0
def pos_shear_corr(pos_lens,pos_source,shear_source,k_source=None,w_lense=None,w_source=None,same_cell=False,num_threads=0):

	nbins   = 6
	min_sep = 0.05 # 3 arcmin
	max_sep = 3.0 # 180 arcmin 
	bin_size = (max_sep-min_sep)/nbins # roughly
	bin_slop = 0.05/bin_size # 0.1 -> 0.05 # 2pt_pipeline for des used bin_slop: 0.01 here: https://github.com/des-science/2pt_pipeline/blob/master/pipeline/twopt_pipeline.yaml
	logger = None

	ra_lens, dec_lens = pos_lens
	ra_source, dec_source = pos_source
	g1_source, g2_source = shear_source

	# foreground (lens)
	cat_lens = treecorr.Catalog(ra=ra_lens, dec=dec_lens, w=w_lense, ra_units='degrees', dec_units='degrees') 
	
	# background (source)
	cat_source = treecorr.Catalog(ra=ra_source, dec=dec_source, w=w_source, g1=g1_source, g2=g2_source, k=k_source, ra_units='degrees', dec_units='degrees')

	ng = treecorr.NGCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins, bin_slop=bin_slop, sep_units='degrees', logger=logger)
	ng.process_cross(cat_lens,cat_source,num_threads=num_threads)  # there's no process_auto for this object

	# one shear variance per pixel per source zbin
	varg = treecorr.calculateVarG(cat_source) if same_cell else np.nan 

	return ng, varg
Example #3
0
def shear_shear_corr(pos1,pos2,shear1,shear2,k1=None,k2=None,w1=None,w2=None,same_zshell=False,same_cell=False,unique_encounter=False,num_threads=0):

	nbins = 6
	min_sep = 0.05 # 3 arcmin
	max_sep = 3.0 # 180 arcmin
	bin_size = (max_sep-min_sep)/nbins # roughly
	bin_slop = 0.05/bin_size # 0.1 -> 0.05 # 2pt_pipeline for des used bin_slop: 0.01 here: https://github.com/des-science/2pt_pipeline/blob/master/pipeline/twopt_pipeline.yaml
	# num_threads = 5 #None #0
	logger = None

	if same_zshell and same_cell: # auto
		ra, dec = pos1 # either 1 or 2 works
		g1, g2 = shear1
		k = k1
		w = w1
		cat = treecorr.Catalog(g1=g1, g2=g2, k=k, ra=ra, dec=dec, w=w, ra_units='degrees', dec_units='degrees')
	elif same_zshell: # just wanted to distrubute the workload fairly for two encounters (didn't want to make one of the cores idle)
		ra1, dec1 = np.array_split(pos1[0], 2), np.array_split(pos1[1], 2) # split in half
		ra2, dec2 = np.array_split(pos2[0], 2), np.array_split(pos2[1], 2)
		g1_1st, g2_1st = np.array_split(shear1[0], 2), np.array_split(shear1[1], 2)
		g1_2nd, g2_2nd = np.array_split(shear2[0], 2), np.array_split(shear2[1], 2)
		k1 = np.array_split(k1, 2) if (k1 is not None) else [None,None]
		k2 = np.array_split(k2, 2) if (k2 is not None) else [None,None]
		w1 = np.array_split(w1, 2) if (w1 is not None) else [None,None]
		w2 = np.array_split(w2, 2) if (w2 is not None) else [None,None]
		cat1 = [treecorr.Catalog(g1=g1_1st[h], g2=g2_1st[h], k=k1[h], ra=ra1[h], dec=dec1[h], w=w1[h], ra_units='degrees', dec_units='degrees') for h in [0,1]]
		cat2 = [treecorr.Catalog(g1=g1_2nd[h], g2=g2_2nd[h], k=k2[h], ra=ra2[h], dec=dec2[h], w=w2[h], ra_units='degrees', dec_units='degrees') for h in [0,1]]
	else:
		ra1, dec1 = pos1
		ra2, dec2 = pos2
		g1_1st, g2_1st = shear1
		g1_2nd, g2_2nd = shear2
		cat1 = treecorr.Catalog(g1=g1_1st, g2=g2_1st, k=k1, ra=ra1, dec=dec1, w=w1, ra_units='degrees', dec_units='degrees')
		cat2 = treecorr.Catalog(g1=g1_2nd, g2=g2_2nd, k=k2, ra=ra2, dec=dec2, w=w2, ra_units='degrees', dec_units='degrees')

	gg = treecorr.GGCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins, bin_slop=bin_slop, sep_units='degrees', logger=logger)
	
	if same_zshell and same_cell:
		gg.process_auto(cat,num_threads=num_threads)
	elif same_zshell: # just wanted to distrubute the workload fairly for two encounters (didn't want to make one of the cores idle)
		if unique_encounter: # the following two counts shouldn't be doubled up cuz they're the same in both directions
			gg.process_cross(cat1[0],cat2[0],num_threads=num_threads)
			gg.process_cross(cat1[1],cat2[1],num_threads=num_threads)
		else: # in the other encounter cat1 and cat2 are switched but does not matter anyway
			gg.process_cross(cat2[0],cat1[1],num_threads=num_threads)
			gg.process_cross(cat2[1],cat1[0],num_threads=num_threads)
	else:
		gg.process_cross(cat1,cat2,num_threads=num_threads)

	if same_zshell and same_cell:
		varg1 = treecorr.calculateVarG(cat)
		varg2 = varg1
	elif same_cell:
		varg1 = treecorr.calculateVarG(cat1)
		varg2 = treecorr.calculateVarG(cat2)
	else:
		varg1 = np.nan
		varg2 = np.nan

	return gg, varg1, varg2
Example #4
0
def calc_3pt(data, randoms, config_fname, zvar, random_zvar, ra_var='RA', dec_var='DEC', random_ra_var='RA', random_dec_var='DEC'):
    config_3pt = load_config(config_fname)['3PCF']
    cat = treecorr.Catalog(ra=data[ra_var], dec=data[dec_var],
                           dec_units='degrees', ra_units='degrees',
                           r=datasets.buzzard_cosmo.comoving_distance(data[zvar]).value*datasets.buzzard_cosmo.h)
    random_cat = treecorr.Catalog(ra=randoms[random_ra_var], dec=randoms[random_dec_var],
                                  dec_units='degrees', ra_units='degrees',
                                  r=datasets.buzzard_cosmo.comoving_distance(randoms[random_zvar]).value*datasets.buzzard_cosmo.h)
    print config_3pt
    ddd = treecorr.NNNCorrelation(config=config_3pt)
    ddr = treecorr.NNNCorrelation(config=config_3pt)
    drd = treecorr.NNNCorrelation(config=config_3pt)
    rdd = treecorr.NNNCorrelation(config=config_3pt)
    rdr = treecorr.NNNCorrelation(config=config_3pt)
    rrd = treecorr.NNNCorrelation(config=config_3pt)
    drr = treecorr.NNNCorrelation(config=config_3pt)
    rrr = treecorr.NNNCorrelation(config=config_3pt)
    ddd.process(cat, metric=config_3pt['metric'])
    ddr.process(cat, cat,  random_cat, metric=config_3pt['metric'])
    drd.process(cat, random_cat, cat, metric=config_3pt['metric'])
    rdd.process(random_cat, cat, cat, metric=config_3pt['metric'])
    rdr.process(random_cat, cat,  random_cat, metric=config_3pt['metric'])
    rrd.process(random_cat, random_cat,  cat, metric=config_3pt['metric'])
    drr.process(cat, random_cat,  random_cat, metric=config_3pt['metric'])
    rrr.process(random_cat, random_cat,  random_cat,
                metric=config_3pt['metric'])
    zeta, varzeta = ddd.calculateZeta(
        ddr=ddr, drd=drd, rdd=rdd, rrd=rrd, rdr=rdr, drr=drr, rrr=rrr)
    return zeta
Example #5
0
    def NNCorrelation(self):
        """
        Caclulates 2D correlation function using Catalog's ra, dec.
        Requires randcatalog to exist.

        Returns tuple (logr, meanlogr, xi, xivar)
        
        """
        catS = treecorr.Catalog(ra=self.catalog["ra"], dec=self.catalog["dec"], 
                                ra_units="radians", dec_units="radians")
        if (self.randcatalog):
            catR = treecorr.Catalog(ra=self.randcatalog["ra"], 
                                    dec=self.randcatalog["dec"], ra_units="radians", 
                                    dec_units="radians")
        else:
            print ("Need random catalog for NN")
            stop()
        dd=treecorr.NNCorrelation(min_sep=self.min_sep, bin_size=self.bin_size, 
                                  max_sep=self.max_sep, sep_units='arcmin')
        dr=treecorr.NNCorrelation(min_sep=self.min_sep, bin_size=self.bin_size,
                                  max_sep=self.max_sep, sep_units='arcmin' )
        rr=treecorr.NNCorrelation(min_sep=self.min_sep, bin_size=self.bin_size,
                                  max_sep=self.max_sep, sep_units='arcmin')
        dd.process(catS)
        dr.process(catS, catR)
        rr.process(catR)
        xi, xivar=dd.calculateXi(rr,dr)
        logr = dd.logr 
        meanlogr= dd.meanlogr
        return (logr, meanlogr, xi, xivar)
Example #6
0
def xshear_lens_jk(jk_label):

    source_jk_mask = labels_source_jk == jk_label
    source_mask = (source_z>0.1)&(source_z<0.3)
    source_mask = (~source_jk_mask)&(source_mask)

    source_cat=treecorr.Catalog(x=source_ra[source_mask],y=source_dec[source_mask],g1=source_e1[source_mask],g2=-1.*source_e2[source_mask],w=source_w[source_mask],x_units='degree',y_units='degree')        
    
    mask_z_lens = (lens_z_jk>0.5)&(lens_z_jk<0.7)
    lens_mask = labels_lens_jk == jk_label
    lens_mask = (~lens_mask)&(mask_z_lens)
    
    lens_cat = treecorr.Catalog(x=lens_ra_jk[lens_mask], y=lens_dec_jk[lens_mask], x_units='degree', y_units='degree')
    
    ng = treecorr.NGCorrelation(nbins = 20, min_sep=0.5, max_sep=250, sep_units='arcmin', verbose=1)
    ng.process(lens_cat, source_cat)
    r_lens_h, xt_lens_h, xx_lens_h , w_lens_h = ng.meanr , ng.xi , ng.xi_im, ng.npairs
    w_lens_h = w_lens_h / np.sum(w_lens_h)
    #print "lens", xt_lens_h

    random_mask = labels_random_jk == jk_label
    random_cat = treecorr.Catalog(x=random_ra_jk[~random_mask], y=random_dec_jk[~random_mask], x_units='degree', y_units='degree')
    
    ng = treecorr.NGCorrelation(nbins = 20, min_sep=0.5, max_sep=250, sep_units='arcmin', verbose=1)
    ng.process(random_cat, source_cat)
    r_rand_h, xt_rand_h, xx_rand_h , w_rand_h = ng.meanr , ng.xi, ng.xi_im, ng.npairs
    w_rand_h = w_rand_h / np.sum(w_rand_h)
    #print "random", xt_rand_h 

    return r_lens_h, xt_lens_h, xx_lens_h , w_lens_h, r_rand_h, xt_rand_h, xx_rand_h , w_rand_h
Example #7
0
def test_direct():

    nobj = 5000
    numpy.random.seed(8675309)
    x = numpy.random.random_sample(nobj)
    y = numpy.random.random_sample(nobj)
    ra = numpy.random.random_sample(nobj)
    dec = numpy.random.random_sample(nobj)
    w = numpy.random.random_sample(nobj)
    g1 = numpy.random.random_sample(nobj)
    g2 = numpy.random.random_sample(nobj)
    k = numpy.random.random_sample(nobj)

    cat1 = treecorr.Catalog(x=x, y=y, w=w, g1=g1, g2=g2, k=k)
    numpy.testing.assert_almost_equal(cat1.x, x)
    numpy.testing.assert_almost_equal(cat1.y, y)
    numpy.testing.assert_almost_equal(cat1.w, w)
    numpy.testing.assert_almost_equal(cat1.g1, g1)
    numpy.testing.assert_almost_equal(cat1.g2, g2)
    numpy.testing.assert_almost_equal(cat1.k, k)

    cat2 = treecorr.Catalog(ra=ra, dec=dec, w=w, g1=g1, g2=g2, k=k,
                            ra_units='hours', dec_units='degrees')
    numpy.testing.assert_almost_equal(cat2.ra, ra * treecorr.hours)
    numpy.testing.assert_almost_equal(cat2.dec, dec * treecorr.degrees)
    numpy.testing.assert_almost_equal(cat2.w, w)
    numpy.testing.assert_almost_equal(cat2.g1, g1)
    numpy.testing.assert_almost_equal(cat2.g2, g2)
    numpy.testing.assert_almost_equal(cat2.k, k)
Example #8
0
    def calc_pos_pos(self,i,j,verbose,num_threads):

        mask = self.lens_binning==i
        lenscat_i = treecorr.Catalog(w=self.lensweight[mask], ra=self.lens['ra'][mask], dec=self.lens['dec'][mask], ra_units='deg', dec_units='deg')

        mask = self.ran_binning==i
        rancat_i  = treecorr.Catalog(w=np.ones(np.sum(mask)), ra=self.randoms['ra'][mask], dec=self.randoms['dec'][mask], ra_units='deg', dec_units='deg')

        mask = self.lens_binning==j
        lenscat_j = treecorr.Catalog(w=self.lensweight[mask], ra=self.lens['ra'][mask], dec=self.lens['dec'][mask], ra_units='deg', dec_units='deg')

        mask = self.ran_binning==j
        rancat_j  = treecorr.Catalog(w=np.ones(np.sum(mask)), ra=self.randoms['ra'][mask], dec=self.randoms['dec'][mask], ra_units='deg', dec_units='deg')

        nn = treecorr.NNCorrelation(nbins=self.params['tbins'], min_sep=self.params['tbounds'][0], max_sep=self.params['tbounds'][1], sep_units='arcmin', bin_slop=self.params['slop'], verbose=verbose,num_threads=num_threads)
        rn = treecorr.NNCorrelation(nbins=self.params['tbins'], min_sep=self.params['tbounds'][0], max_sep=self.params['tbounds'][1], sep_units='arcmin', bin_slop=self.params['slop'], verbose=verbose,num_threads=num_threads)
        nr = treecorr.NNCorrelation(nbins=self.params['tbins'], min_sep=self.params['tbounds'][0], max_sep=self.params['tbounds'][1], sep_units='arcmin', bin_slop=self.params['slop'], verbose=verbose,num_threads=num_threads)
        rr = treecorr.NNCorrelation(nbins=self.params['tbins'], min_sep=self.params['tbounds'][0], max_sep=self.params['tbounds'][1], sep_units='arcmin', bin_slop=self.params['slop'], verbose=verbose,num_threads=num_threads)
        nn.process(lenscat_i,lenscat_j)
        rn.process(rancat_i,lenscat_j)
        nr.process(lenscat_i,rancat_j)
        rr.process(rancat_i,rancat_j)

        theta=np.exp(nn.meanlogr)
        wtheta,wthetaerr=nn.calculateXi(rr,dr=nr,rd=rn)
        wthetaerr=np.sqrt(wthetaerr)

        return theta, wtheta, wthetaerr
Example #9
0
def calcalate_hhcf_full(outpath,halopath,nbins,limits,Nh,randoms):
    """
    Calculate the halo-halo correlation function
    for the full volume.
    """
    redpath = halopath+"/reduced_halo_cats/reduced_halo_cat.txt"
    infile = open(redpath,"r")
    halos = np.zeros((int(Nh),3))
    i = 0
    for line in infile:
        if line[0] is "#": continue
        parts = line.split()
        halos[i,:] = float(parts[x_index]),float(parts[y_index]),float(parts[z_index])
        i+=1
    infile.close()
    #Interface with treecorr
    config = {'nbins':nbins,'min_sep':limits[0],'max_sep':limits[1]}
    halo_cat = treecorr.Catalog(x=halos[:,0],y=halos[:,1],z=halos[:,2],config=config)
    random_cat = treecorr.Catalog(x=randoms[:,0],y=randoms[:,1],z=randoms[:,2],config=config)
    DD = treecorr.NNCorrelation(config)
    DR = treecorr.NNCorrelation(config)
    RR = treecorr.NNCorrelation(config)
    DD.process(halo_cat)
    DR.process(halo_cat,random_cat)
    RR.process(random_cat)
    DD.write(outpath+"/halohalo_correlation_function/full_hhcf/full_hhcf.txt",RR,DR)

    print "\tFull halo-halo correlation function complete."
    return
Example #10
0
    def calc_xi_perp(self, data1, data2,  min_rpar, max_rpar, nbins=20, slop=0.1, randoms=True):
        # Build a catalogue of random points drawn from the same volume
        rx = np.random.random(size=data1['x'].size) * (data1['x'].max()-data1['x'].min()) + data1['x'].mean()
        ry = np.random.random(size=data1['x'].size) * (data1['y'].max()-data1['y'].min()) + data1['y'].mean()
        rz = np.random.random(size=data1['x'].size) * (data1['z'].max()-data1['z'].min()) + data1['z'].mean()

        # Create the catalogues
        cat_i = treecorr.Catalog(x=data1['x'], y=data1['y'], z=data1['z'])
        cat_j = treecorr.Catalog(x=data2['x'], y=data2['y'], z=data2['z'])
        rancat_i  = treecorr.Catalog(x=rx, y=ry, z=rz)
        rancat_j  = treecorr.Catalog(x=rx, y=ry, z=rz)

        nn = treecorr.NNCorrelation(nbins=nbins, min_rpar=min_rpar, max_rpar=max_rpar, min_sep=15, max_sep=10e3, bin_slop=slop)
        rn = treecorr.NNCorrelation(nbins=nbins, min_rpar=min_rpar, max_rpar=max_rpar, min_sep=15, max_sep=10e3, bin_slop=slop)
        nr = treecorr.NNCorrelation(nbins=nbins, min_rpar=min_rpar, max_rpar=max_rpar, min_sep=15, max_sep=10e3, bin_slop=slop)
        rr = treecorr.NNCorrelation(nbins=nbins, min_rpar=min_rpar, max_rpar=max_rpar, min_sep=15, max_sep=10e3, bin_slop=slop)
        nn.process(cat_i,cat_j, metric='Rperp') #, metric='Periodic')
        rn.process(rancat_i,cat_j, metric='Rperp') #, metric='Periodic')
        nr.process(cat_i,rancat_j, metric='Rperp') #, metric='Periodic')
        rr.process(rancat_i,rancat_j, metric='Rperp') #, metric='Periodic')

        R = np.exp(nn.meanlogr)
        if randoms:
            w, werr = nn.calculateXi(rr,dr=nr,rd=rn)
        else:
            w, werr = nn.calculateXi(rr,dr=None,rd=None)
        werr = np.sqrt(werr)

        return R, w, werr
Example #11
0
def CorrelationFunction(source, source_random, corrconfig=None, source_ra='ra', source_dec='dec', source_random_ra=None, source_random_dec=None):

    if corrconfig is None:
        corrconfig = {'sep_units': 'degrees',
                      'min_sep': 0.1,
                      'max_sep': 6,
                      'nbins': 25,
                      'bin_slop': 0.25,
                      'num_threads': 1}

    if source_random_ra is None:
        source_random_ra = source_ra
    if source_random_dec is None:
        source_random_dec = source_dec

    SourceCat = treecorr.Catalog(ra=source[source_ra], dec=source[source_dec], ra_units='degrees', dec_units='degrees')
    SourceRand = treecorr.Catalog(ra=source_random[source_random_ra], dec=source_random[source_random_dec], ra_units='degrees', dec_units='degrees')
    dd = treecorr.NNCorrelation(**corrconfig)
    dr = treecorr.NNCorrelation(**corrconfig)
    rr = treecorr.NNCorrelation(**corrconfig)

    dd.process(SourceCat)
    dr.process(SourceCat, SourceRand)
    rr.process(SourceRand)

    xi, varxi = dd.calculateXi(rr, dr=dr)
    r = np.exp(dd.logr)
    
    return [xi, r]
Example #12
0
    def get_lens_catalog(self, data, i):
        import treecorr

        mask = data['lens_bin'] == i

        if 'lens_ra' in data:
            ra = data['lens_ra'][mask]
            dec = data['lens_dec'][mask]
        else:
            ra = data['ra'][mask]
            dec = data['dec'][mask]

        cat = treecorr.Catalog(ra=ra,
                               dec=dec,
                               ra_units='degree',
                               dec_units='degree')

        if 'random_bin' in data:
            random_mask = data['random_bin'] == i
            rancat = treecorr.Catalog(ra=data['random_ra'][random_mask],
                                      dec=data['random_dec'][random_mask],
                                      ra_units='degree',
                                      dec_units='degree')
        else:
            rancat = None

        return cat, rancat
Example #13
0
def test_pairwise():
    # Test the pairwise option.

    ngal = 1000
    s = 10.
    np.random.seed(8675309)
    x1 = np.random.normal(0,s, (ngal,) )
    y1 = np.random.normal(0,s, (ngal,) )
    w1 = np.random.random(ngal)

    x2 = np.random.normal(0,s, (ngal,) )
    y2 = np.random.normal(0,s, (ngal,) )
    w2 = np.random.random(ngal)
    k2 = np.random.normal(0,3, (ngal,) )

    w1 = np.ones_like(w1)
    w2 = np.ones_like(w2)

    cat1 = treecorr.Catalog(x=x1, y=y1, w=w1)
    cat2 = treecorr.Catalog(x=x2, y=y2, w=w2, k=k2)

    min_sep = 5.
    max_sep = 50.
    nbins = 10
    bin_size = np.log(max_sep/min_sep) / nbins
    nk = treecorr.NKCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins)
    nk.process_pairwise(cat1, cat2)
    nk.finalize(cat2.vark)

    true_npairs = np.zeros(nbins, dtype=int)
    true_weight = np.zeros(nbins, dtype=float)
    true_xi = np.zeros(nbins, dtype=float)

    rsq = (x1-x2)**2 + (y1-y2)**2
    r = np.sqrt(rsq)
    logr = np.log(r)

    ww = w1 * w2
    xi = ww * k2

    index = np.floor(np.log(r/min_sep) / bin_size).astype(int)
    mask = (index >= 0) & (index < nbins)
    np.add.at(true_npairs, index[mask], 1)
    np.add.at(true_weight, index[mask], ww[mask])
    np.add.at(true_xi, index[mask], xi[mask])

    true_xi /= true_weight

    np.testing.assert_array_equal(nk.npairs, true_npairs)
    np.testing.assert_allclose(nk.weight, true_weight, rtol=1.e-5, atol=1.e-8)
    np.testing.assert_allclose(nk.xi, true_xi, rtol=1.e-4, atol=1.e-8)

    # If cats have names, then the logger will mention them.
    # Also, test running with optional args.
    cat1.name = "first"
    cat2.name = "second"
    with CaptureLog() as cl:
        nk.logger = cl.logger
        nk.process_pairwise(cat1, cat2, metric='Euclidean', num_threads=2)
    assert "for cats first, second" in cl.output
Example #14
0
    def test_getCF(self):
        """Test getCF() directly, without first processing by child classes."""
        stile_args = {'ra_units': 'degrees', 'dec_units': 'degrees', 'min_sep': 0.05, 'max_sep': 1,
                      'sep_units': 'degrees', 'nbins': 20}
        cf = stile.sys_tests.CorrelationFunctionSysTest()
        dh = temp_data_handler()
        lens_data = stile.ReadASCIITable('../examples/example_lens_catalog.dat',
                    fields={'id': 0, 'ra': 1, 'dec': 2, 'z': 3, 'g1': 4, 'g2': 5})
        source_data = stile.ReadASCIITable('../examples/example_source_catalog.dat',
                    fields={'id': 0, 'ra': 1, 'dec': 2, 'z': 3, 'g1': 4, 'g2': 5})
        lens_catalog = treecorr.Catalog(ra=numpy.array([lens_data['ra']]),
                                        dec=numpy.array([lens_data['dec']]),
                                        ra_units='degrees', dec_units='degrees')
        source_catalog = treecorr.Catalog(ra=source_data['ra'], dec=source_data['dec'],
                                          g1=source_data['g1'], g2=source_data['g2'],
                                          ra_units='degrees', dec_units='degrees')
        results = cf.getCF('ng', lens_catalog, source_catalog, **stile_args)
        numpy.testing.assert_array_equal(*helper.FormatSame(results, self.expected_result))
        results2 = cf.getCF('ng', lens_data, source_data, config=stile_args)
        self.assertEqual(self.expected_result.dtype.names, results.dtype.names)
        # Missing necessary data file
        numpy.testing.assert_equal(results, results2)
        self.assertRaises(TypeError,
                          cf.getCF, {}, 'ng',
                          file_name='../examples/example_lens_catalog.dat')
        # Nonsensical correlation type
        self.assertRaises(ValueError, cf.getCF, 'hello', lens_data, source_data, config=stile_args)

        # Then, test a test that uses .getCF().
        realshear = stile.sys_tests.GalaxyShearSysTest()
        results3 = realshear(lens_data, source_data, config=stile_args)
        numpy.testing.assert_equal(results, results3)
Example #15
0
def get_xi_window_norm(window=None, nside=None):
    window_norm = {corr: {} for corr in corrs}
    mask = {}
    for tracer in window.keys():
        # window[tracer]=kappa_class.z_bins[tracer][0]['window']
        mask[tracer] = window[tracer] == hp.UNSEEN
        # window[tracer]=window[tracer][~mask[tracer]]
    fsky = mask[tracer].mean()
    cat0 = {'fullsky': np.ones_like(mask)}
    tree_cat_args0 = get_treecorr_cat_args(cat0, masks=None, nside=nside)

    tree_cat0 = treecorr.Catalog(**tree_cat_args0['fullsky'])
    tree_corrs0 = treecorr.NNCorrelation(**corr_config)
    _ = tree_corrs0.process(tree_cat0, tree_cat0)
    npairs0 = tree_corrs0.npairs * fsky
    del cat0, tree_cat0, tree_corrs0

    tree_cat_args = get_treecorr_cat_args(window, masks=mask, nside=nside)
    tree_cat = {
        tracer: treecorr.Catalog(w=window[tracer][~mask[tracer]],
                                 **tree_cat_args[tracer])
        for tracer in window.keys()
    }
    del mask
    for corr in corrs:
        tree_corrs = treecorr.NNCorrelation(**corr_config)
        _ = tree_corrs.process(tree_cat[corr[0]], tree_cat[corr[1]])
        window_norm[corr]['weight'] = tree_corrs.weight
        window_norm[corr]['npairs'] = tree_corrs.npairs
        window_norm[corr]['npairs0'] = npairs0
    del tree_cat, tree_corrs
    return window_norm
Example #16
0
    def calc_pos_shear(self,i,j,verbose,num_threads):

        mask = self.lens_binning==i
        lenscat_i = treecorr.Catalog(w=self.lensweight[mask], ra=self.lens['ra'][mask], dec=self.lens['dec'][mask], ra_units='deg', dec_units='deg')

        mask = self.ran_binning==i
        rancat_i  = treecorr.Catalog(w=np.ones(np.sum(mask)), ra=self.randoms['ra'][mask], dec=self.randoms['dec'][mask], ra_units='deg', dec_units='deg')

        m1,m2,mask = self.get_m(j)
        if self.params['has_sheared']:
            cat_j = treecorr.Catalog(g1=self.shape['e1'][mask]/m1[mask], g2=self.shape['e2'][mask]/m2[mask], w=self.weight[mask], ra=self.shape['ra'][mask], dec=self.shape['dec'][mask], ra_units='deg', dec_units='deg')
        else:
            cat_j = treecorr.Catalog(g1=self.shape['e1'][mask], g2=self.shape['e2'][mask], w=self.weight[mask], ra=self.shape['ra'][mask], dec=self.shape['dec'][mask], ra_units='deg', dec_units='deg')
            biascat_j = treecorr.Catalog(k=np.sqrt(self.shape['m1'][mask]*self.shape['m2'][mask]), w=self.weight[mask], ra=self.shape['ra'][mask], dec=self.shape['dec'][mask], ra_units='deg', dec_units='deg')

        ng = treecorr.NGCorrelation(nbins=self.params['tbins'], min_sep=self.params['tbounds'][0], max_sep=self.params['tbounds'][1], sep_units='arcmin', bin_slop=self.params['slop'], verbose=verbose,num_threads=num_threads)
        rg = treecorr.NGCorrelation(nbins=self.params['tbins'], min_sep=self.params['tbounds'][0], max_sep=self.params['tbounds'][1], sep_units='arcmin', bin_slop=self.params['slop'], verbose=verbose,num_threads=num_threads)
        if self.params['has_sheared']:
            norm = 1.
        else:
            nk = treecorr.NKCorrelation(nbins=self.params['tbins'], min_sep=self.params['tbounds'][0], max_sep=self.params['tbounds'][1], sep_units='arcmin', bin_slop=self.params['slop'], verbose=verbose,num_threads=num_threads)
            nk.process(lenscat_i,biascat_j)
            norm,tmp=nk.calculateXi()
        ng.process(lenscat_i,cat_j)
        rg.process(rancat_i,cat_j)
        gammat,gammat_im,gammaterr=ng.calculateXi(rg)

        theta=np.exp(ng.meanlogr)
        if np.sum(norm)==0:
          norm=1.
        gammat/=norm
        gammat_im/=norm
        gammaterr=np.sqrt(gammaterr/norm)

        return theta, gammat, gammaterr
Example #17
0
def calc_2pt(data,
             randoms,
             config_fname,
             zvar,
             random_zvar,
             ra_var='RA',
             dec_var='DEC',
             random_ra_var='RA',
             random_dec_var='DEC'):
    config_2pt = load_config(config_fname)['2PCF']
    cat = treecorr.Catalog(
        ra=data[ra_var],
        dec=data[dec_var],
        dec_units='degrees',
        ra_units='degrees',
    )
    random_cat = treecorr.Catalog(
        ra=randoms[random_ra_var],
        dec=randoms[random_dec_var],
        dec_units='degrees',
        ra_units='degrees',
    )
    print config_2pt
    dd = treecorr.NNCorrelation(config=config_2pt)
    dr = treecorr.NNCorrelation(config=config_2pt)
    rr = treecorr.NNCorrelation(config=config_2pt)
    dd.process(cat, metric=config_2pt['metric'])
    dr.process(cat, random_cat, metric=config_2pt['metric'])
    rr.process(random_cat, metric=config_2pt['metric'])
    xi, varxi = dd.calculateXi(dr=dr, rr=rr)
    return xi
Example #18
0
    def correlate(self,group1=0,group2=0):
        print 'Will correlate galaxies in groups %d and %d'%(group1,group2)

        data1 =  self.get(table='subfind_halos' , fields='groupId, x, y, z', cond='subfind_halos.snapnum = %d AND subfind_halos.groupId = %d'%(self.snapshot, group1))
        data2 =  self.get(table='subfind_halos' , fields='groupId, x, y, z', cond='subfind_halos.snapnum = %d AND subfind_halos.groupId = %d'%(self.snapshot, group2))

        #Setup the correlation
        print 'Setting up correlation'
        corr = treecorr.NNCorrelation(nbins=15, min_sep=30, max_sep=4e3)
        cat1 = treecorr.Catalog(x=data1['x'],y=data1['y'],z=data1['z'])
        cat2 = treecorr.Catalog(x=data2['x'],y=data2['y'],z=data2['z'])
        print 'Calculating...'
        corr.process(cat1,cat2)

        print 'Random-Random'
        rr = treecorr.NNCorrelation(nbins=15, min_sep=30, max_sep=4e3)
        rx,ry,rz = np.random.choice(data1['x'], size=5000),np.random.choice(data1['y'], size=5000),np.random.choice(data1['z'], size=5000)
        rcat = treecorr.Catalog(x=rx, y=ry, z=rz)
        rr.process(rcat)

        print 'Data-Random'
        dr = treecorr.NNCorrelation(nbins=15, min_sep=30, max_sep=4e3)
        dr.process(rcat, cat2)

        print 'Random-Data'
        rd = treecorr.NNCorrelation(nbins=15, min_sep=30, max_sep=4e3)
        rd.process(cat1, rcat)

        xi,varxi = corr.calculateXi(rr,dr,rd)

        return np.exp(corr.logr), xi, varxi
Example #19
0
def test_constant():
    # A fairly trivial test is to use a constant value of kappa everywhere.

    ngal = 100000
    A = 0.05
    L = 100.
    numpy.random.seed(8675309)
    x = (numpy.random.random_sample(ngal) - 0.5) * L
    y = (numpy.random.random_sample(ngal) - 0.5) * L
    kappa = A * numpy.ones(ngal)

    cat = treecorr.Catalog(x=x,
                           y=y,
                           k=kappa,
                           x_units='arcmin',
                           y_units='arcmin')
    kk = treecorr.KKCorrelation(bin_size=0.1,
                                min_sep=0.1,
                                max_sep=10.,
                                sep_units='arcmin')
    kk.process(cat)
    print('kk.xi = ', kk.xi)
    numpy.testing.assert_almost_equal(kk.xi, A**2, decimal=10)

    # Now add some noise to the values. It should still work, but at slightly lower accuracy.
    kappa += 0.001 * (numpy.random.random_sample(ngal) - 0.5)
    cat = treecorr.Catalog(x=x,
                           y=y,
                           k=kappa,
                           x_units='arcmin',
                           y_units='arcmin')
    kk.process(cat)
    print('kk.xi = ', kk.xi)
    numpy.testing.assert_almost_equal(kk.xi, A**2, decimal=6)
Example #20
0
def compute_gg_treecorr(cat1, cat2, options, nbins, ijk):

	print('Using treecorr', treecorr.version)

	slop = 0.1

	# arrays to store the output
	r    = np.zeros(nbins)
	DD   = np.zeros_like(r)
	DR   = np.zeros_like(r)
	RR   = np.zeros_like(r)
	RD   = np.zeros_like(r)


	# Set up the catalogues
	rcat1, rcat2, _, _ = randoms(cat1, cat2, ijk, period=options['box_size'])
	#import pdb ; pdb.set_trace()

	cat1  = treecorr.Catalog(g1=None, g2=None, x=cat1['x'], y=cat1['y'], z=cat1['z'])
	cat2  = treecorr.Catalog(g1=None, g2=None, x=cat2['x'], y=cat2['y'], z=cat2['z'])

	NR1 = rcat1.x.size * 1.0
	NR2 = rcat2.x.size * 1.0
	ND1 = cat1.x.size * 1.0
	ND2 = cat2.x.size * 1.0

	f0 = (NR1*NR2)/(ND1*ND2)
	f1 = (NR1*NR2)/(ND1*NR2)
	f2 = (NR1*NR2)/(ND2*NR1)

	print('Processing DD')
	nn = treecorr.NNCorrelation(nbins=nbins, min_sep=options['rlim'][0], max_sep=options['rlim'][1], bin_slop=slop, verbose=0, period=options['box_size'])
	#nn.process(rcat1, rcat2, period=options['box_size'], metric='Periodic')
	nn.process(cat1, cat2, metric='Periodic')
	nn.finalize()
	DD = np.copy(nn.weight)
	rp_bins = np.copy(nn.rnom)
	nn.clear()

	print('Processing RD')
	nn = treecorr.NNCorrelation(nbins=nbins, min_sep=options['rlim'][0], max_sep=options['rlim'][1], bin_slop=slop, verbose=0, period=options['box_size'])
	nn.process(rcat1, cat2, metric='Periodic')
	RD = np.copy(nn.weight)
	nn.clear()

	print('Processing DR')
	nn = treecorr.NNCorrelation(nbins=nbins, min_sep=options['rlim'][0], max_sep=options['rlim'][1], bin_slop=slop, verbose=0, period=options['box_size'])
	nn.process(cat1, rcat2, metric='Periodic')
	DR = np.copy(nn.weight)
	nn.clear()

	print('Processing RR')
	nn = treecorr.NNCorrelation(nbins=nbins, min_sep=options['rlim'][0], max_sep=options['rlim'][1], bin_slop=slop, verbose=0, period=options['box_size'])
	nn.process(rcat1, rcat2, metric='Periodic')
	RR = np.copy(nn.weight)
	nn.clear()

	gg = (f0 * DD/RR) - (f1 * DR/RR) - (f2 * RD/RR) + 1.0	

	return gg
Example #21
0
    def correlate_data(self):
        cat_c = treecorr.Catalog(ra=self.c_rel[0].copy(),
                                 dec=self.c_rel[1].copy(),
                                 r=self.c_rel[3].copy(),
                                 ra_units='deg',
                                 dec_units='deg')
        self.nn = treecorr.NNCorrelation(min_sep=self.min_sep,
                                         max_sep=self.max_sep,
                                         nbins=self.nbins,
                                         metric='Rlens')

        self.cat_g = treecorr.Catalog(ra=self.b_g_rel[0].copy(),
                                      dec=self.b_g_rel[1].copy(),
                                      w=np.power(self.b_g_rel[2].copy(),
                                                 self.n),
                                      ra_units='deg',
                                      dec_units='deg')
        self.nn.process(cat_c, self.cat_g)

        z_bar = self.nn.weight.copy() / self.nn.npairs.copy()
        self.mean_z = np.average(np.power(self.b_g_rel[2], self.n))
        self.del_z_bar = z_bar - self.mean_z
        self.N = self.nn.npairs.copy()
        self.sep = self.nn.meanr.copy()

        return None
    def correlate_data(self):
        self.del_z_bars = np.zeros((self.n, self.nbins))
        self.N = np.zeros(self.nbins)
        self.seps = np.zeros((self.n, self.nbins))

        cat_c = treecorr.Catalog(ra=self.c_rel[0].copy(),
                                 dec=self.c_rel[1].copy(),
                                 r=self.c_rel[3].copy(),
                                 ra_units='deg',
                                 dec_units='deg')
        nn = treecorr.NNCorrelation(min_sep=self.min_sep,
                                    max_sep=self.max_sep,
                                    nbins=self.nbins,
                                    metric='Rlens')

        for i in range(self.n):
            cat_g = treecorr.Catalog(ra=self.b_g_rel[0].copy(),
                                     dec=self.b_g_rel[1].copy(),
                                     w=np.power(self.b_g_rel[2].copy(), i + 1),
                                     ra_units='deg',
                                     dec_units='deg')
            nn.process(cat_c, cat_g)

            z_bar = nn.weight.copy() / nn.npairs.copy()
            mean_z = np.average(np.power(self.b_g_rel[2], i + 1))
            self.del_z_bars[i, :] = z_bar - mean_z
            self.N = nn.npairs.copy()
            self.seps[i] = nn.meanr.copy()

        return None
Example #23
0
    def _get_corrs_nosep(self, data, min_sep=44, max_sep=1e6, binning='log', nbins=20, ctype=('s','s'), estimator='Landy-Szalay', verbosity=1, randoms=None, method='halotools'):

    	if verbosity>0:
    		print 'Will construct %s - %s correlation functions'%ctype
    		print 'Using %s estimator'%estimator

    	# Decide on an appropriate binning scheme
    	if (binning.lower()=='log'):
    		rbins = np.logspace(np.log10(min_sep), np.log10(max_sep), nbins )
    	elif (binning.lower()=='linear'):
    		rbins = np.linspace(min_sep, max_sep, nbins )

    	if verbosity>1:
    		print 'Will use %s binning:'%binning, rbins

    	# Parse the mask
    	mask1 = util.choose_cs_mask(data,ctype[0])
    	mask2 = util.choose_cs_mask(data,ctype[1])

    	pos1 = pretending.return_xyz_formatted_array(data['x'], data['y'], data['z'], mask = mask1)
    	pos2 = pretending.return_xyz_formatted_array(data['x'], data['y'], data['z'], mask = mask2)

    	# And do the randoms
    	if randoms is None:
            r1 = util.construct_random_cat(data, mask=mask1)
            r2 = util.construct_random_cat(data, mask=mask2)
        else:
            if verbosity>0:
                print 'Using random points provided for normalisation.'
            r1 = randoms

    	R = np.sqrt(np.array(rbins)[1:]*np.array(rbins)[:-1]) 

        print 'Using %s to calculate two-point correlations'%method
    	if method=='halotools':
            return R, pretending.tpcf(pos1, rbins, sample2=pos2, randoms=r1, period=info.Lbox, estimator=estimator )

        elif method=='treecorr':
            print 'Constructing catalogues...'
            cat_i = treecorr.Catalog(x=data['x'][mask1], y=data['y'][mask1], z=data['z'][mask1])
            cat_j = treecorr.Catalog(x=data['x'][mask2], y=data['y'][mask2], z=data['z'][mask2])
            rx_1 = (np.random.random(size=data['x'][mask1].size) - 0.5) * (data['x'][mask1].max()-data['x'][mask1].min()) + data['x'][mask1].mean()
            ry_1 = (np.random.random(size=data['x'][mask1].size) - 0.5) * (data['y'][mask1].max()-data['y'][mask1].min()) + data['y'][mask1].mean()
            rz_1 = (np.random.random(size=data['x'][mask1].size) - 0.5) * (data['z'][mask1].max()-data['z'][mask1].min()) + data['z'][mask1].mean()
            rancat_1  = treecorr.Catalog(x=rx_1, y=ry_1, z=rz_1)

            print 'Correlating...'
            nn = treecorr.NNCorrelation(nbins=nbins, min_sep=min_sep, max_sep=max_sep, bin_slop=0.1)
            nr = treecorr.NNCorrelation(nbins=nbins, min_sep=min_sep, max_sep=max_sep, bin_slop=0.1)
            rn = treecorr.NNCorrelation(nbins=nbins, min_sep=min_sep, max_sep=max_sep, bin_slop=0.1)
            rr = treecorr.NNCorrelation(nbins=nbins, min_sep=min_sep, max_sep=max_sep, bin_slop=0.1)

            nn.process(cat_i,cat_j) 
            nr.process(rancat_1,cat_i)
            rn.process(cat_j,rancat_1)
            rr.process(rancat_1,rancat_1) 
            R = np.exp(nn.meanlogr)
            w = (nn.weight - nr.weight -  rn.weight + rr.weight) / rr.weight
            return R, w
Example #24
0
def test_fits():
    try:
        import fitsio
    except ImportError:
        print('Skipping FITS tests, since fitsio is not installed')
        return

    get_from_wiki('Aardvark.fit')
    file_name = os.path.join('data','Aardvark.fit')
    config = treecorr.read_config('Aardvark.yaml')
    config['verbose'] = 1

    # Just test a few random particular values
    cat1 = treecorr.Catalog(file_name, config)
    np.testing.assert_equal(len(cat1.ra), 390935)
    np.testing.assert_equal(cat1.nobj, 390935)
    np.testing.assert_almost_equal(cat1.ra[0], 56.4195 * (pi/180.))
    np.testing.assert_almost_equal(cat1.ra[390934], 78.4782 * (pi/180.))
    np.testing.assert_almost_equal(cat1.dec[290333], 83.1579 * (pi/180.))
    np.testing.assert_almost_equal(cat1.g1[46392], 0.0005066675)
    np.testing.assert_almost_equal(cat1.g2[46392], -0.0001006742)
    np.testing.assert_almost_equal(cat1.k[46392], -0.0008628797)

    # The catalog doesn't have x, y, or w, but test that functionality as well.
    del config['ra_col']
    del config['dec_col']
    config['x_col'] = 'RA'
    config['y_col'] = 'DEC'
    config['w_col'] = 'MU'
    config['flag_col'] = 'INDEX'
    config['ignore_flag'] = 64
    cat2 = treecorr.Catalog(file_name, config)
    np.testing.assert_almost_equal(cat2.x[390934], 78.4782, decimal=4)
    np.testing.assert_almost_equal(cat2.y[290333], 83.1579, decimal=4)
    np.testing.assert_almost_equal(cat2.w[46392], 0.)        # index = 1200379
    np.testing.assert_almost_equal(cat2.w[46393], 0.9995946) # index = 1200386

    # Test using a limited set of rows
    config['first_row'] = 101
    config['last_row'] = 50000
    cat3 = treecorr.Catalog(file_name, config)
    np.testing.assert_equal(len(cat3.x), 49900)
    np.testing.assert_equal(cat3.ntot, 49900)
    np.testing.assert_equal(cat3.nobj, sum(cat3.w != 0))
    np.testing.assert_equal(cat3.sumw, sum(cat3.w))
    np.testing.assert_equal(cat3.sumw, sum(cat2.w[100:50000]))
    np.testing.assert_almost_equal(cat3.g1[46292], 0.0005066675)
    np.testing.assert_almost_equal(cat3.g2[46292], -0.0001006742)
    np.testing.assert_almost_equal(cat3.k[46292], -0.0008628797)

    cat4 = treecorr.read_catalogs(config, key='file_name', is_rand=True)[0]
    np.testing.assert_equal(len(cat4.x), 49900)
    np.testing.assert_equal(cat4.ntot, 49900)
    np.testing.assert_equal(cat4.nobj, sum(cat4.w != 0))
    np.testing.assert_equal(cat4.sumw, sum(cat4.w))
    np.testing.assert_equal(cat4.sumw, sum(cat2.w[100:50000]))
    assert cat4.g1 is None
    assert cat4.g2 is None
    assert cat4.k is None
def correlation_TreeCorr(data_ra, data_dec, data_r, rand_ra, rand_dec, rand_r, config) :
	import time
	import numpy as np
	import treecorr
	import sys

	# Begin timing
	start = time.time()

	# Make sure arrays match
	assert data_ra.size == data_dec.size, "Data must have both RA and DEC"
	assert rand_ra.size == rand_dec.size, "Randoms must have both RA and DEC"

	# Create TreeCorr catalog objects
	dcat = treecorr.Catalog(ra=data_ra, dec=data_dec, r=data_r, ra_units='deg', dec_units='deg')
	rcat = treecorr.Catalog(ra=rand_ra, dec=rand_dec, r=rand_r, ra_units='deg', dec_units='deg')
	print ('TreeCorr catalogs created')
	sys.stdout.flush()

	# Run TreeCorr processes for DD, DR, RD, and RR
	dd = treecorr.NNCorrelation(config)
	dr = treecorr.NNCorrelation(config)
	# rd = treecorr.NNCorrelation(config)
	rr = treecorr.NNCorrelation(config)
	dd.process(dcat)
	print ('DD done')
	sys.stdout.flush()
	# I also need to get the bin locations for plotting
	logr = dd.logr
	dr.process(dcat, rcat)
	print ('DR done')
	sys.stdout.flush()
	# rd.process(rcat, dcat)
	# print ('RD done')
	# sys.stdout.flush()
	rr.process(rcat)
	print ('RR done')
	sys.stdout.flush()

	# Find the correlation function and errors
	# xi, varxi = dd.calculateXi(rr, dr, rd)
	xi, varxi = dd.calculateXi(rr, dr)
	print ('Correlation function and errors calculated')
	sys.stdout.flush()

	# Find elapsed time
	runtime = time.time() - start
	del start
	## Print the time it took
	h = int(np.floor(runtime/(60.0*60.0)))
	m = int(np.floor((runtime - (60.0*60.0*h))/60.0))
	s = runtime - 60.0*60.0*h - 60.0*m
	print ('Elapsed time: {:>02d}:{:>02d}:{:>05.2f}'.format(h, m, s))
	sys.stdout.flush()
	del runtime, h, m, s

	# Return xi, varxi, and bin locations
	return (xi, varxi, logr)
Example #26
0
def measure_rho(ra, dec, e1, e2, s, m_e1, m_e2, m_s, max_sep, tag=None):
    """Compute the rho statistics
    """
    import treecorr

    de1 = e1 - m_e1
    de2 = e2 - m_e2
    dt = (s**2 - m_s**2) / s**2

    ecat = treecorr.Catalog(ra=ra,
                            dec=dec,
                            ra_units='deg',
                            dec_units='deg',
                            g1=e1,
                            g2=e2)
    decat = treecorr.Catalog(ra=ra,
                             dec=dec,
                             ra_units='deg',
                             dec_units='deg',
                             g1=de1,
                             g2=de2)
    dtcat = treecorr.Catalog(ra=ra,
                             dec=dec,
                             ra_units='deg',
                             dec_units='deg',
                             k=dt,
                             g1=dt * e1,
                             g2=dt * e2)

    ecat.name = 'ecat'
    decat.name = 'decat'
    dtcat.name = 'dtcat'
    if tag is not None:
        for cat in [ecat, decat, dtcat]:
            cat.name = tag + ":" + cat.name

    min_sep = 0.5
    bin_size = 0.5
    bin_slop = 0.1

    results = []
    for (cat1, cat2) in [(decat, decat), (ecat, decat), (dtcat, dtcat),
                         (decat, dtcat), (ecat, dtcat)]:
        print 'Doing correlation of %s vs %s' % (cat1.name, cat2.name)

        rho = treecorr.GGCorrelation(min_sep=min_sep,
                                     max_sep=max_sep,
                                     sep_units='arcmin',
                                     bin_size=bin_size,
                                     bin_slop=bin_slop,
                                     verbose=2)
        if cat1 is cat2:
            rho.process(cat1)
        else:
            rho.process(cat1, cat2)
        results.append(rho)

    return results
Example #27
0
def test_single():
    # Use kappa(r) = kappa0 exp(-r^2/2r0^2) (1-r^2/2r0^2) around a single lens

    nsource = 1000000
    kappa0 = 0.05
    r0 = 10.
    L = 5. * r0
    numpy.random.seed(8675309)
    x = (numpy.random.random_sample(nsource) - 0.5) * L
    y = (numpy.random.random_sample(nsource) - 0.5) * L
    r2 = (x**2 + y**2)
    k = kappa0 * numpy.exp(-0.5 * r2 / r0**2) * (1. - 0.5 * r2 / r0**2)

    lens_cat = treecorr.Catalog(x=[0],
                                y=[0],
                                x_units='arcmin',
                                y_units='arcmin')
    source_cat = treecorr.Catalog(x=x,
                                  y=y,
                                  k=k,
                                  x_units='arcmin',
                                  y_units='arcmin')
    nk = treecorr.NKCorrelation(bin_size=0.1,
                                min_sep=1.,
                                max_sep=25.,
                                sep_units='arcmin',
                                verbose=1)
    nk.process(lens_cat, source_cat)

    r = nk.meanr
    true_k = kappa0 * numpy.exp(
        -0.5 * r**2 / r0**2) * (1. - 0.5 * r**2 / r0**2)

    print('nk.xi = ', nk.xi)
    print('true_kappa = ', true_k)
    print('ratio = ', nk.xi / true_k)
    print('diff = ', nk.xi - true_k)
    print('max diff = ', max(abs(nk.xi - true_k)))
    assert max(abs(nk.xi - true_k)) < 4.e-4

    # Check that we get the same result using the corr2 executable:
    if __name__ == '__main__':
        lens_cat.write(os.path.join('data', 'nk_single_lens.dat'))
        source_cat.write(os.path.join('data', 'nk_single_source.dat'))
        import subprocess
        corr2_exe = get_script_name('corr2')
        p = subprocess.Popen([corr2_exe, "nk_single.yaml"])
        p.communicate()
        corr2_output = numpy.genfromtxt(os.path.join('output',
                                                     'nk_single.out'),
                                        names=True)
        print('nk.xi = ', nk.xi)
        print('from corr2 output = ', corr2_output['kappa'])
        print('ratio = ', corr2_output['kappa'] / nk.xi)
        print('diff = ', corr2_output['kappa'] - nk.xi)
        numpy.testing.assert_almost_equal(corr2_output['kappa'] / nk.xi,
                                          1.,
                                          decimal=3)
def loadGalaxy():
    print 'loading SuperCosmos catalog and mask...'

    catalog = np.load('datalog/wisecatalog.npy')
    scosmask = healpy.read_map('WISExSCOSmask.fits', verbose=False)

    num = 30000000

    coord = SkyCoord(catalog[0], catalog[1], frame='icrs', unit='deg').galactic
    l, b = coord.l.deg, coord.b.deg

    catalog = catalog[:, scosmask[healpy.ang2pix(
        256, l, b, nest=False, lonlat=True)] == 1]

    cat_galaxy = treecorr.Catalog(ra=catalog[0],
                                  dec=catalog[1],
                                  ra_units='deg',
                                  dec_units='deg')

    print('Done!\n')

    print 'generating random galaxy catalog'
    #plt.scatter(catalog[0],catalog[1],s=0.01)
    #plt.xlabel('RA(deg)')
    #plt.ylabel('DEC(deg)')
    #plt.show()
    ra_min = np.min(cat_galaxy.ra)
    ra_max = np.max(cat_galaxy.ra)
    dec_min = np.min(cat_galaxy.dec)
    dec_max = np.max(cat_galaxy.dec)
    #print('ra range = %f .. %f' % (ra_min, ra_max))
    #print('dec range = %f .. %f' % (dec_min, dec_max))

    rand_ra = np.random.uniform(ra_min, ra_max, num)
    rand_sindec = np.random.uniform(np.sin(dec_min), np.sin(dec_max), num)
    rand_dec = np.arcsin(rand_sindec)

    coord = SkyCoord(rand_ra, rand_dec, frame='icrs', unit='rad').galactic
    l, b = coord.l.deg, coord.b.deg

    rand_ra = rand_ra[scosmask[healpy.ang2pix(
        256, l, b, nest=False, lonlat=True)] == 1]
    rand_dec = rand_dec[scosmask[healpy.ang2pix(
        256, l, b, nest=False, lonlat=True)] == 1]

    cat_rand = treecorr.Catalog(ra=rand_ra,
                                dec=rand_dec,
                                ra_units='radians',
                                dec_units='radians')

    #plt.scatter(np.rad2deg(rand_ra),np.rad2deg(rand_dec),s=0.01)
    #plt.xlabel('RA(deg)')
    #plt.ylabel('DEC(deg)')
    #plt.show()
    print('Done!\n')

    return cat_galaxy, cat_rand
Example #29
0
 def compute_statistic(self, i, ra, dec, q1, q2):
     n = len(ra)
     print(f"Computing Rowe statistic rho_{i} from {n} objects")
     import treecorr
     corr = treecorr.GGCorrelation(self.config)
     cat1 = treecorr.Catalog(ra=ra, dec=dec, g1=q1[0], g2=q1[1], ra_units='deg', dec_units='deg')
     cat2 = treecorr.Catalog(ra=ra, dec=dec, g1=q2[0], g2=q2[1], ra_units='deg', dec_units='deg')
     corr.process(cat1, cat2)
     return corr.meanr, corr.xip, corr.varxip**0.5
Example #30
0
def calculate_cross(config, all_halos, all_dms, halorandoms, dmrandoms, step,
                    ndivs, Njk):
    """
    Calcualte the DD, DR, RD and RR cross correlations
    """
    #Inititalize these objects
    DDc_all = []
    DRc_all = []
    RDc_all = []
    RRc_all = []
    for i in xrange(0, Njk):
        DDc_all.append(treecorr.NNCorrelation(config))
        DRc_all.append(treecorr.NNCorrelation(config))
        RDc_all.append(treecorr.NNCorrelation(config))
        RRc_all.append(treecorr.NNCorrelation(config))

    for index1 in xrange(0, Njk):
        halos = all_halos[index1]
        halo_cat = treecorr.Catalog(x=halos[:, 0],
                                    y=halos[:, 1],
                                    z=halos[:, 2],
                                    config=config)
        i1 = index1 % ndivs
        j1 = (index1 / ndivs) % ndivs
        k1 = index1 / ndivs**2
        dmrandom_cat = treecorr.Catalog(x=dmrandoms[:, 0] + i1 * step,
                                        y=dmrandoms[:, 1] + j1 * step,
                                        z=dmrandoms[:, 2] + k1 * step,
                                        config=config)
        for index2 in xrange(index1 + 1, Njk):
            dm = all_dms[index2]
            dm_cat = treecorr.Catalog(x=dm[:, 0],
                                      y=dm[:, 1],
                                      z=dm[:, 2],
                                      config=config)
            i2 = index2 % ndivs
            j2 = (index2 / ndivs) % ndivs
            k2 = index2 / ndivs**2
            halorandom_cat = treecorr.Catalog(x=halorandoms[:, 0] + i2 * step,
                                              y=halorandoms[:, 1] + j2 * step,
                                              z=halorandoms[:, 2] + k2 * step,
                                              config=config)
            DD = treecorr.NNCorrelation(config)
            DR = treecorr.NNCorrelation(config)
            RD = treecorr.NNCorrelation(config)
            RR = treecorr.NNCorrelation(config)
            DD.process(halo_cat, dm_cat)
            DR.process(halo_cat, halorandom_cat)
            RD.process(dm_cat, dmrandom_cat)
            RR.process(dmrandom_cat, halorandom_cat)
            DDc_all[index1] += DD
            DDc_all[index2] += DD
            DRc_all[index1] += DR
            RDc_all[index2] += RD
            RRc_all[index1] += RR
            RRc_all[index2] += RR
    return DDc_all, DRc_all, RDc_all, RRc_all