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 test_single(): # Use gamma_t(r) = gamma0 exp(-r^2/2r0^2) around a single lens # i.e. gamma(r) = -gamma0 exp(-r^2/2r0^2) (x+iy)^2/r^2 nsource = 1000000 gamma0 = 0.05 kappa = 0.23 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) gammat = gamma0 * numpy.exp(-0.5*r2/r0**2) g1 = -gammat * (x**2-y**2)/r2 g2 = -gammat * (2.*x*y)/r2 lens_cat = treecorr.Catalog(x=[0], y=[0], k=[kappa], x_units='arcmin', y_units='arcmin') source_cat = treecorr.Catalog(x=x, y=y, g1=g1, g2=g2, x_units='arcmin', y_units='arcmin') kg = treecorr.KGCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin', verbose=1) kg.process(lens_cat, source_cat) # log(<R>) != <logR>, but it should be close: print('meanlogr - log(meanr) = ',kg.meanlogr - numpy.log(kg.meanr)) numpy.testing.assert_almost_equal(kg.meanlogr, numpy.log(kg.meanr), decimal=3) r = kg.meanr true_kgt = kappa * gamma0 * numpy.exp(-0.5*r**2/r0**2) print('kg.xi = ',kg.xi) print('kg.xi_im = ',kg.xi_im) print('true_gammat = ',true_kgt) print('ratio = ',kg.xi / true_kgt) print('diff = ',kg.xi - true_kgt) print('max diff = ',max(abs(kg.xi - true_kgt))) assert max(abs(kg.xi - true_kgt)) < 4.e-4 assert max(abs(kg.xi_im)) < 3.e-5 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': lens_cat.write(os.path.join('data','kg_single_lens.dat')) source_cat.write(os.path.join('data','kg_single_source.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"kg_single.params"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','kg_single.out'),names=True) print('kg.xi = ',kg.xi) print('from corr2 output = ',corr2_output['kgamT']) print('ratio = ',corr2_output['kgamT']/kg.xi) print('diff = ',corr2_output['kgamT']-kg.xi) numpy.testing.assert_almost_equal(corr2_output['kgamT']/kg.xi, 1., decimal=3) print('xi_im from corr2 output = ',corr2_output['kgamX']) assert max(abs(corr2_output['kgamX'])) < 3.e-5
def test_pairwise(): # Test the same profile, but with the pairwise calcualtion: nsource = 1000000 gamma0 = 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) gammat = gamma0 * numpy.exp(-0.5*r2/r0**2) g1 = -gammat * (x**2-y**2)/r2 g2 = -gammat * (2.*x*y)/r2 dx = (numpy.random.random_sample(nsource)-0.5) * L dx = (numpy.random.random_sample(nsource)-0.5) * L lens_cat = treecorr.Catalog(x=dx, y=dx, x_units='arcmin', y_units='arcmin') source_cat = treecorr.Catalog(x=x+dx, y=y+dx, g1=g1, g2=g2, x_units='arcmin', y_units='arcmin') ng = treecorr.NGCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin', verbose=1, pairwise=True) ng.process(lens_cat, source_cat) r = ng.meanr true_gt = gamma0 * numpy.exp(-0.5*r**2/r0**2) print('ng.xi = ',ng.xi) print('ng.xi_im = ',ng.xi_im) print('true_gammat = ',true_gt) print('ratio = ',ng.xi / true_gt) print('diff = ',ng.xi - true_gt) print('max diff = ',max(abs(ng.xi - true_gt))) # I don't really understand why this comes out slightly less accurate. # I would have thought it would be slightly more accurate because it doesn't use the # approximations intrinsic to the tree calculation. assert max(abs(ng.xi - true_gt)) < 4.e-4 assert max(abs(ng.xi_im)) < 3.e-5 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': lens_cat.write(os.path.join('data','ng_pairwise_lens.dat')) source_cat.write(os.path.join('data','ng_pairwise_source.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"ng_pairwise.yaml"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','ng_pairwise.out'),names=True) print('ng.xi = ',ng.xi) print('from corr2 output = ',corr2_output['gamT']) print('ratio = ',corr2_output['gamT']/ng.xi) print('diff = ',corr2_output['gamT']-ng.xi) numpy.testing.assert_almost_equal(corr2_output['gamT']/ng.xi, 1., decimal=3) print('xi_im from corr2 output = ',corr2_output['gamX']) assert max(abs(corr2_output['gamX'])) < 3.e-5
def test_single(): # Use gamma_t(r) = gamma0 exp(-r^2/2r0^2) around a single lens # i.e. gamma(r) = -gamma0 exp(-r^2/2r0^2) (x+iy)^2/r^2 nsource = 1000000 gamma0 = 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) gammat = gamma0 * numpy.exp(-0.5*r2/r0**2) g1 = -gammat * (x**2-y**2)/r2 g2 = -gammat * (2.*x*y)/r2 lens_cat = treecorr.Catalog(x=[0], y=[0], x_units='arcmin', y_units='arcmin') source_cat = treecorr.Catalog(x=x, y=y, g1=g1, g2=g2, x_units='arcmin', y_units='arcmin') ng = treecorr.NGCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin', verbose=1) ng.process(lens_cat, source_cat) # log(<R>) != <logR>, but it should be close: print('meanlogr - log(meanr) = ',ng.meanlogr - numpy.log(ng.meanr)) numpy.testing.assert_almost_equal(ng.meanlogr, numpy.log(ng.meanr), decimal=3) r = ng.meanr true_gt = gamma0 * numpy.exp(-0.5*r**2/r0**2) print('ng.xi = ',ng.xi) print('ng.xi_im = ',ng.xi_im) print('true_gammat = ',true_gt) print('ratio = ',ng.xi / true_gt) print('diff = ',ng.xi - true_gt) print('max diff = ',max(abs(ng.xi - true_gt))) assert max(abs(ng.xi - true_gt)) < 4.e-4 assert max(abs(ng.xi_im)) < 3.e-5 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': lens_cat.write(os.path.join('data','ng_single_lens.dat')) source_cat.write(os.path.join('data','ng_single_source.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"ng_single.yaml"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','ng_single.out'),names=True) print('ng.xi = ',ng.xi) print('from corr2 output = ',corr2_output['gamT']) print('ratio = ',corr2_output['gamT']/ng.xi) print('diff = ',corr2_output['gamT']-ng.xi) numpy.testing.assert_almost_equal(corr2_output['gamT']/ng.xi, 1., decimal=3) print('xi_im from corr2 output = ',corr2_output['gamX']) assert max(abs(corr2_output['gamX'])) < 3.e-5
def test_pairwise(): # Test the same profile, but with the pairwise calcualtion: nsource = 1000000 gamma0 = 0.05 kappa = 0.23 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) gammat = gamma0 * numpy.exp(-0.5*r2/r0**2) g1 = -gammat * (x**2-y**2)/r2 g2 = -gammat * (2.*x*y)/r2 dx = (numpy.random.random_sample(nsource)-0.5) * L dx = (numpy.random.random_sample(nsource)-0.5) * L k = kappa * numpy.ones(nsource) lens_cat = treecorr.Catalog(x=dx, y=dx, k=k, x_units='arcmin', y_units='arcmin') source_cat = treecorr.Catalog(x=x+dx, y=y+dx, g1=g1, g2=g2, x_units='arcmin', y_units='arcmin') kg = treecorr.KGCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin', verbose=1, pairwise=True) kg.process(lens_cat, source_cat) r = kg.meanr true_kgt = kappa * gamma0 * numpy.exp(-0.5*r**2/r0**2) print('kg.xi = ',kg.xi) print('kg.xi_im = ',kg.xi_im) print('true_gammat = ',true_kgt) print('ratio = ',kg.xi / true_kgt) print('diff = ',kg.xi - true_kgt) print('max diff = ',max(abs(kg.xi - true_kgt))) assert max(abs(kg.xi - true_kgt)) < 4.e-4 assert max(abs(kg.xi_im)) < 3.e-5 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': lens_cat.write(os.path.join('data','kg_pairwise_lens.dat')) source_cat.write(os.path.join('data','kg_pairwise_source.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"kg_pairwise.params"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','kg_pairwise.out'),names=True) print('kg.xi = ',kg.xi) print('from corr2 output = ',corr2_output['kgamT']) print('ratio = ',corr2_output['kgamT']/kg.xi) print('diff = ',corr2_output['kgamT']-kg.xi) numpy.testing.assert_almost_equal(corr2_output['kgamT']/kg.xi, 1., decimal=3) print('xi_im from corr2 output = ',corr2_output['kgamX']) assert max(abs(corr2_output['kgamX'])) < 3.e-5
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 test_3d(): # For this one, build a Gaussian cloud around some random point in 3D space and do the # correlation function in 3D. # # Use n(r) = (2pi s^2)^-3/2 exp(-r^2/2s^2) # # The 3D Fourier transform is: n~(k) = exp(-s^2 k^2/2) # P(k) = <|n~(k)|^2> = exp(-s^2 k^2) # xi(r) = 1/2pi^2 int( dk k^2 P(k) j0(kr) ) # = 1/(8 pi^3/2) 1/s^3 exp(-r^2/4s^2) # # And as before, we need to correct for the randoms, so the final xi(r) is # # xi(r) = 1/(8 pi^3/2) (L/s)^3 exp(-r^2/4s^2) - 1 xcen = 823 # Mpc maybe? ycen = 342 zcen = -672 s = 10. if __name__ == "__main__": ngal = 100000 nrand = 5 * ngal L = 50. * s # Not infinity, so this introduces some error. Our integrals were to infinity. req_factor = 1 else: ngal = 20000 nrand = 2 * ngal L = 20. * s req_factor = 3 numpy.random.seed(8675309) x = numpy.random.normal(xcen, s, (ngal,) ) y = numpy.random.normal(ycen, s, (ngal,) ) z = numpy.random.normal(zcen, s, (ngal,) ) r = numpy.sqrt(x*x+y*y+z*z) dec = numpy.arcsin(z/r) / treecorr.degrees ra = numpy.arctan2(y,x) / treecorr.degrees cat = treecorr.Catalog(ra=ra, dec=dec, r=r, ra_units='deg', dec_units='deg') dd = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., verbose=1) dd.process(cat) print('dd.npairs = ',dd.npairs) rx = (numpy.random.random_sample(nrand)-0.5) * L + xcen ry = (numpy.random.random_sample(nrand)-0.5) * L + ycen rz = (numpy.random.random_sample(nrand)-0.5) * L + zcen rr = numpy.sqrt(rx*rx+ry*ry+rz*rz) rdec = numpy.arcsin(rz/rr) / treecorr.degrees rra = numpy.arctan2(ry,rx) / treecorr.degrees rand = treecorr.Catalog(ra=rra, dec=rdec, r=rr, ra_units='deg', dec_units='deg') rr = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., verbose=1) rr.process(rand) print('rr.npairs = ',rr.npairs) dr = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., verbose=1) dr.process(cat,rand) print('dr.npairs = ',dr.npairs) r = dd.meanr true_xi = 1./(8.*numpy.pi**1.5) * (L/s)**3 * numpy.exp(-0.25*r**2/s**2) - 1. xi, varxi = dd.calculateXi(rr,dr) print('xi = ',xi) print('true_xi = ',true_xi) print('ratio = ',xi / true_xi) print('diff = ',xi - true_xi) print('max rel diff = ',max(abs((xi - true_xi)/true_xi))) assert max(abs(xi - true_xi)/true_xi)/req_factor < 0.1 numpy.testing.assert_almost_equal(numpy.log(numpy.abs(xi))/req_factor, numpy.log(numpy.abs(true_xi))/req_factor, decimal=1) simple_xi, varxi = dd.calculateXi(rr) print('simple xi = ',simple_xi) print('max rel diff = ',max(abs((simple_xi - true_xi)/true_xi))) assert max(abs(simple_xi - true_xi)/true_xi)/req_factor < 0.1 numpy.testing.assert_almost_equal(numpy.log(numpy.abs(simple_xi))/req_factor, numpy.log(numpy.abs(true_xi))/req_factor, decimal=1) # Check that we get the same result using the corr2 executable: if __name__ == '__main__': cat.write(os.path.join('data','nn_3d_data.dat')) rand.write(os.path.join('data','nn_3d_rand.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"nn_3d.yaml"] ) p.communicate() corr2_outfile = os.path.join('output','nn_3d.fits') corr2_output = fitsio.read(corr2_outfile) print('xi = ',xi) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi) print('diff = ',corr2_output['xi']-xi) numpy.testing.assert_almost_equal(corr2_output['R_nom'], numpy.exp(dd.logr)) numpy.testing.assert_almost_equal(corr2_output['meanR'], dd.meanr) numpy.testing.assert_almost_equal(corr2_output['meanlogR'], dd.meanlogr) numpy.testing.assert_almost_equal(corr2_output['xi'], xi) numpy.testing.assert_almost_equal(corr2_output['sigma_xi'], numpy.sqrt(varxi)) numpy.testing.assert_almost_equal(corr2_output['DD'], dd.npairs) numpy.testing.assert_almost_equal(corr2_output['RR'], rr.npairs * (dd.tot / rr.tot)) numpy.testing.assert_almost_equal(corr2_output['DR'], dr.npairs * (dd.tot / dr.tot)) header = fitsio.read_header(corr2_outfile, 1) numpy.testing.assert_almost_equal(header['tot'], dd.tot) # And repeat with Catalogs that use x,y,z cat = treecorr.Catalog(x=x, y=y, z=z) rand = treecorr.Catalog(x=rx, y=ry, z=rz) dd.process(cat) rr.process(rand) dr.process(cat,rand) xi, varxi = dd.calculateXi(rr,dr) assert max(abs(xi - true_xi)/true_xi)/req_factor < 0.1 numpy.testing.assert_almost_equal(numpy.log(numpy.abs(xi))/req_factor, numpy.log(numpy.abs(true_xi))/req_factor, decimal=1)
def test_direct_count(): # If the catalogs are small enough, we can do a direct count of the number of pairs # to see if comes out right. This should exactly match the treecorr code if bin_slop=0. ngal = 100 s = 10. numpy.random.seed(8675309) x1 = numpy.random.normal(0,s, (ngal,) ) y1 = numpy.random.normal(0,s, (ngal,) ) cat1 = treecorr.Catalog(x=x1, y=y1) x2 = numpy.random.normal(0,s, (ngal,) ) y2 = numpy.random.normal(0,s, (ngal,) ) cat2 = treecorr.Catalog(x=x2, y=y2) min_sep = 1. max_sep = 50. nbins = 50 dd = treecorr.NNCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins, bin_slop=0.) dd.process(cat1, cat2) print('dd.npairs = ',dd.npairs) log_min_sep = numpy.log(min_sep) log_max_sep = numpy.log(max_sep) true_npairs = numpy.zeros(nbins) bin_size = (log_max_sep - log_min_sep) / nbins for i in range(ngal): for j in range(ngal): rsq = (x1[i]-x2[j])**2 + (y1[i]-y2[j])**2 logr = 0.5 * numpy.log(rsq) k = int(numpy.floor( (logr-log_min_sep) / bin_size )) if k < 0: continue if k >= nbins: continue true_npairs[k] += 1 print('true_npairs = ',true_npairs) print('diff = ',dd.npairs - true_npairs) numpy.testing.assert_array_equal(dd.npairs, true_npairs) # Check that running via the corr2 script works correctly. file_name1 = os.path.join('data','nn_direct_data1.dat') with open(file_name1, 'w') as fid: for i in range(ngal): fid.write(('%.20f %.20f\n')%(x1[i],y1[i])) file_name2 = os.path.join('data','nn_direct_data2.dat') with open(file_name2, 'w') as fid: for i in range(ngal): fid.write(('%.20f %.20f\n')%(x2[i],y2[i])) L = 10*s nrand = ngal rx1 = (numpy.random.random_sample(nrand)-0.5) * L ry1 = (numpy.random.random_sample(nrand)-0.5) * L rx2 = (numpy.random.random_sample(nrand)-0.5) * L ry2 = (numpy.random.random_sample(nrand)-0.5) * L rcat1 = treecorr.Catalog(x=rx1, y=ry1) rcat2 = treecorr.Catalog(x=rx2, y=ry2) rand_file_name1 = os.path.join('data','nn_direct_rand1.dat') with open(rand_file_name1, 'w') as fid: for i in range(nrand): fid.write(('%.20f %.20f\n')%(rx1[i],ry1[i])) rand_file_name2 = os.path.join('data','nn_direct_rand2.dat') with open(rand_file_name2, 'w') as fid: for i in range(nrand): fid.write(('%.20f %.20f\n')%(rx2[i],ry2[i])) rr = treecorr.NNCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins, bin_slop=0., verbose=0) rr.process(rcat1,rcat2) xi, varxi = dd.calculateXi(rr) # First do this via the corr2 function. config = treecorr.config.read_config('nn_direct.yaml') logger = treecorr.config.setup_logger(0) treecorr.corr2(config, logger) corr2_output = numpy.genfromtxt(os.path.join('output','nn_direct.out'), names=True, skip_header=1) print('corr2_output = ',corr2_output) print('corr2_output.dtype = ',corr2_output.dtype) print('rnom = ',dd.rnom) print(' ',corr2_output['R_nom']) numpy.testing.assert_almost_equal(corr2_output['R_nom'], dd.rnom, decimal=3) print('DD = ',dd.npairs) print(' ',corr2_output['DD']) numpy.testing.assert_almost_equal(corr2_output['DD'], dd.npairs, decimal=3) numpy.testing.assert_almost_equal(corr2_output['npairs'], dd.npairs, decimal=3) print('RR = ',rr.npairs) print(' ',corr2_output['RR']) numpy.testing.assert_almost_equal(corr2_output['RR'], rr.npairs, decimal=3) print('xi = ',xi) print('from corr2 output = ',corr2_output['xi']) print('diff = ',corr2_output['xi']-xi) diff_index = numpy.where(numpy.abs(corr2_output['xi']-xi) > 1.e-5)[0] print('different at ',diff_index) print('xi[diffs] = ',xi[diff_index]) print('corr2.xi[diffs] = ',corr2_output['xi'][diff_index]) print('diff[diffs] = ',xi[diff_index] - corr2_output['xi'][diff_index]) numpy.testing.assert_almost_equal(corr2_output['xi'], xi, decimal=3) # Now calling out to the external corr2 executable. import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"nn_direct.yaml"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','nn_direct.out'), names=True, skip_header=1) numpy.testing.assert_almost_equal(corr2_output['xi'], xi, decimal=3) # Repeat with binslop not precisely 0, since the code flow is different for bin_slop == 0. dd = treecorr.NNCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins, bin_slop=1.e-16) dd.process(cat1, cat2) numpy.testing.assert_array_equal(dd.npairs, true_npairs) # And again with no top-level recursion dd = treecorr.NNCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins, bin_slop=1.e-16, max_top=0) dd.process(cat1, cat2) numpy.testing.assert_array_equal(dd.npairs, true_npairs)
def test_aardvark(): # Eric Suchyta did a brute force calculation of the Aardvark catalog, so it is useful to # compare the output from my code with that. get_from_wiki('Aardvark.fit') file_name = os.path.join('data', 'Aardvark.fit') config = treecorr.read_config('Aardvark.params') cat1 = treecorr.Catalog(file_name, config) gg = treecorr.GGCorrelation(config) gg.process(cat1) direct_file_name = os.path.join('data', 'Aardvark.direct') direct_data = numpy.genfromtxt(direct_file_name) direct_xip = direct_data[:, 3] direct_xim = direct_data[:, 4] #print('gg.xip = ',gg.xip) #print('direct.xip = ',direct_xip) xip_err = gg.xip - direct_xip print('xip_err = ', xip_err) print('max = ', max(abs(xip_err))) assert max(abs(xip_err)) < 2.e-7 print('xip_im = ', gg.xip_im) print('max = ', max(abs(gg.xip_im))) assert max(abs(gg.xip_im)) < 3.e-7 xim_err = gg.xim - direct_xim print('xim_err = ', xim_err) print('max = ', max(abs(xim_err))) assert max(abs(xim_err)) < 1.e-7 print('xim_im = ', gg.xim_im) print('max = ', max(abs(gg.xim_im))) assert max(abs(gg.xim_im)) < 1.e-7 # However, after some back and forth about the calculation, we concluded that Eric hadn't # done the spherical trig correctly to get the shears relative to the great circle joining # the two positions. So let's compare with my own brute force calculation (i.e. using # bin_slop = 0): # This also has the advantage that the radial bins are done the same way -- uniformly # spaced in log of the chord distance, rather than the great circle distance. bs0_file_name = os.path.join('data', 'Aardvark.bs0') bs0_data = numpy.genfromtxt(bs0_file_name) bs0_xip = bs0_data[:, 2] bs0_xim = bs0_data[:, 3] #print('gg.xip = ',gg.xip) #print('bs0.xip = ',bs0_xip) xip_err = gg.xip - bs0_xip print('xip_err = ', xip_err) print('max = ', max(abs(xip_err))) assert max(abs(xip_err)) < 1.e-7 xim_err = gg.xim - bs0_xim print('xim_err = ', xim_err) print('max = ', max(abs(xim_err))) assert max(abs(xim_err)) < 5.e-8 # Check that we get the same result using the corr2 executable: # Note: This is the only test of the corr2 executable that we do with nosetests. # The other similar tests are blocked out with: if __name__ == '__main__': import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen([corr2_exe, "Aardvark.params"]) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output', 'Aardvark.out'), names=True) print('gg.xip = ', gg.xip) print('from corr2 output = ', corr2_output['xip']) print('ratio = ', corr2_output['xip'] / gg.xip) print('diff = ', corr2_output['xip'] - gg.xip) numpy.testing.assert_almost_equal(corr2_output['xip'] / gg.xip, 1., decimal=3) print('gg.xim = ', gg.xim) print('from corr2 output = ', corr2_output['xim']) print('ratio = ', corr2_output['xim'] / gg.xim) print('diff = ', corr2_output['xim'] - gg.xim) numpy.testing.assert_almost_equal(corr2_output['xim'] / gg.xim, 1., decimal=3) print('xip_im from corr2 output = ', corr2_output['xip_im']) print('max err = ', max(abs(corr2_output['xip_im']))) assert max(abs(corr2_output['xip_im'])) < 3.e-7 print('xim_im from corr2 output = ', corr2_output['xim_im']) print('max err = ', max(abs(corr2_output['xim_im']))) assert max(abs(corr2_output['xim_im'])) < 1.e-7 # As bin_slop decreases, the agreement should get even better. # This test is slow, so only do it if running test_gg.py directly. if __name__ == '__main__': config['bin_slop'] = 0.2 gg = treecorr.GGCorrelation(config) gg.process(cat1) #print('gg.xip = ',gg.xip) #print('bs0.xip = ',bs0_xip) xip_err = gg.xip - bs0_xip print('xip_err = ', xip_err) print('max = ', max(abs(xip_err))) assert max(abs(xip_err)) < 1.e-8 xim_err = gg.xim - bs0_xim print('xim_err = ', xim_err) print('max = ', max(abs(xim_err))) assert max(abs(xim_err)) < 1.e-8
def test_gg(): # cf. http://adsabs.harvard.edu/abs/2002A%26A...389..729S for the basic formulae I use here. # # Use gamma_t(r) = gamma0 r^2/r0^2 exp(-r^2/2r0^2) # i.e. gamma(r) = -gamma0 exp(-r^2/2r0^2) (x+iy)^2 / r0^2 # # The Fourier transform is: gamma~(k) = -2 pi gamma0 r0^4 k^2 exp(-r0^2 k^2/2) / L^2 # P(k) = (1/2pi) <|gamma~(k)|^2> = 2 pi gamma0^2 r0^8 k^4 / L^4 exp(-r0^2 k^2) # xi+(r) = (1/2pi) int( dk k P(k) J0(kr) ) # = pi/16 gamma0^2 (r0/L)^2 exp(-r^2/4r0^2) (r^4 - 16r^2r0^2 + 32r0^4)/r0^4 # xi-(r) = (1/2pi) int( dk k P(k) J4(kr) ) # = pi/16 gamma0^2 (r0/L)^2 exp(-r^2/4r0^2) r^4/r0^4 # Note: I'm not sure I handled the L factors correctly, but the units at the end need # to be gamma^2, so it needs to be (r0/L)^2. gamma0 = 0.05 r0 = 10. if __name__ == "__main__": ngal = 1000000 L = 50. * r0 # Not infinity, so this introduces some error. Our integrals were to infinity. req_factor = 1 else: ngal = 200000 L = 50. * r0 req_factor = 3 numpy.random.seed(8675309) x = (numpy.random.random_sample(ngal) - 0.5) * L y = (numpy.random.random_sample(ngal) - 0.5) * L r2 = (x**2 + y**2) / r0**2 g1 = -gamma0 * numpy.exp(-r2 / 2.) * (x**2 - y**2) / r0**2 g2 = -gamma0 * numpy.exp(-r2 / 2.) * (2. * x * y) / r0**2 cat = treecorr.Catalog(x=x, y=y, g1=g1, g2=g2, x_units='arcmin', y_units='arcmin') gg = treecorr.GGCorrelation(bin_size=0.1, min_sep=1., max_sep=100., sep_units='arcmin', verbose=1) gg.process(cat) # log(<R>) != <logR>, but it should be close: print('meanlogr - log(meanr) = ', gg.meanlogr - numpy.log(gg.meanr)) numpy.testing.assert_almost_equal(gg.meanlogr, numpy.log(gg.meanr), decimal=3) r = gg.meanr temp = numpy.pi / 16. * gamma0**2 * (r0 / L)**2 * numpy.exp( -0.25 * r**2 / r0**2) true_xip = temp * (r**4 - 16. * r**2 * r0**2 + 32. * r0**4) / r0**4 true_xim = temp * r**4 / r0**4 print('gg.xip = ', gg.xip) print('true_xip = ', true_xip) print('ratio = ', gg.xip / true_xip) print('diff = ', gg.xip - true_xip) print('max diff = ', max(abs(gg.xip - true_xip))) assert max(abs(gg.xip - true_xip)) / req_factor < 3.e-7 print('xip_im = ', gg.xip_im) assert max(abs(gg.xip_im)) / req_factor < 2.e-7 print('gg.xim = ', gg.xim) print('true_xim = ', true_xim) print('ratio = ', gg.xim / true_xim) print('diff = ', gg.xim - true_xim) print('max diff = ', max(abs(gg.xim - true_xim))) assert max(abs(gg.xim - true_xim)) / req_factor < 3.e-7 print('xim_im = ', gg.xim_im) assert max(abs(gg.xim_im)) / req_factor < 1.e-7 # Should also work as a cross-correlation with itself gg.process(cat, cat) numpy.testing.assert_almost_equal(gg.meanlogr, numpy.log(gg.meanr), decimal=3) assert max(abs(gg.xip - true_xip)) / req_factor < 3.e-7 assert max(abs(gg.xip_im)) / req_factor < 2.e-7 assert max(abs(gg.xim - true_xim)) / req_factor < 3.e-7 assert max(abs(gg.xim_im)) / req_factor < 1.e-7 # Check MapSq calculation: # cf. http://adsabs.harvard.edu/abs/2004MNRAS.352..338J # Use Crittenden formulation, since the analytic result is simpler: # Map^2(R) = int 1/2 r/R^2 (T+(r/R) xi+(r) + T-(r/R) xi-(r)) dr # = 6 pi gamma0^2 r0^8 R^4 / (L^2 (r0^2+R^2)^5) # Mx^2(R) = int 1/2 r/R^2 (T+(r/R) xi+(r) - T-(r/R) xi-(r)) dr # = 0 # where T+(s) = (s^4-16s^2+32)/128 exp(-s^2/4) # T-(s) = s^4/128 exp(-s^2/4) true_mapsq = 6. * numpy.pi * gamma0**2 * r0**8 * r**4 / (L**2 * (r**2 + r0**2)**5) mapsq, mapsq_im, mxsq, mxsq_im, varmapsq = gg.calculateMapSq('Crittenden') print('mapsq = ', mapsq) print('true_mapsq = ', true_mapsq) print('ratio = ', mapsq / true_mapsq) print('diff = ', mapsq - true_mapsq) print('max diff = ', max(abs(mapsq - true_mapsq))) print('max diff[16:] = ', max(abs(mapsq[16:] - true_mapsq[16:]))) # It's pretty ratty near the start where the integral is poorly evaluated, but the # agreement is pretty good if we skip the first 16 elements. # Well, it gets bad again at the end, but those values are small enough that they still # pass this test. assert max(abs(mapsq[16:] - true_mapsq[16:])) / req_factor < 3.e-8 print('mxsq = ', mxsq) print('max = ', max(abs(mxsq))) print('max[16:] = ', max(abs(mxsq[16:]))) assert max(abs(mxsq[16:])) / req_factor < 3.e-8 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': cat.write(os.path.join('data', 'gg.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen([corr2_exe, "gg.params"]) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output', 'gg.out'), names=True) print('gg.xip = ', gg.xip) print('from corr2 output = ', corr2_output['xip']) print('ratio = ', corr2_output['xip'] / gg.xip) print('diff = ', corr2_output['xip'] - gg.xip) numpy.testing.assert_almost_equal(corr2_output['xip'] / gg.xip, 1., decimal=3) print('gg.xim = ', gg.xim) print('from corr2 output = ', corr2_output['xim']) print('ratio = ', corr2_output['xim'] / gg.xim) print('diff = ', corr2_output['xim'] - gg.xim) numpy.testing.assert_almost_equal(corr2_output['xim'] / gg.xim, 1., decimal=3) print('xip_im from corr2 output = ', corr2_output['xip_im']) print('max err = ', max(abs(corr2_output['xip_im']))) assert max(abs(corr2_output['xip_im'])) / req_factor < 2.e-7 print('xim_im from corr2 output = ', corr2_output['xim_im']) print('max err = ', max(abs(corr2_output['xim_im']))) assert max(abs(corr2_output['xim_im'])) / req_factor < 1.e-7 corr2_output2 = numpy.genfromtxt(os.path.join('output', 'gg_m2.out'), names=True) print('mapsq = ', mapsq) print('from corr2 output = ', corr2_output2['Mapsq']) print('ratio = ', corr2_output2['Mapsq'] / mapsq) print('diff = ', corr2_output2['Mapsq'] - mapsq) numpy.testing.assert_almost_equal(corr2_output2['Mapsq'] / mapsq, 1., decimal=3) print('mxsq = ', mxsq) print('from corr2 output = ', corr2_output2['Mxsq']) print('ratio = ', corr2_output2['Mxsq'] / mxsq) print('diff = ', corr2_output2['Mxsq'] - mxsq) numpy.testing.assert_almost_equal(corr2_output2['Mxsq'] / mxsq, 1., decimal=3) # Check the fits write option out_file_name = os.path.join('output', 'gg_out.fits') gg.write(out_file_name) try: import fitsio data = fitsio.read(out_file_name) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(gg.logr)) numpy.testing.assert_almost_equal(data['meanR'], gg.meanr) numpy.testing.assert_almost_equal(data['meanlogR'], gg.meanlogr) numpy.testing.assert_almost_equal(data['xip'], gg.xip) numpy.testing.assert_almost_equal(data['xim'], gg.xim) numpy.testing.assert_almost_equal(data['xip_im'], gg.xip_im) numpy.testing.assert_almost_equal(data['xim_im'], gg.xim_im) numpy.testing.assert_almost_equal(data['sigma_xi'], numpy.sqrt(gg.varxi)) numpy.testing.assert_almost_equal(data['weight'], gg.weight) numpy.testing.assert_almost_equal(data['npairs'], gg.npairs) except ImportError: print('Unable to import fitsio. Skipping fits tests.') # Check the read function gg2 = treecorr.GGCorrelation(bin_size=0.1, min_sep=1., max_sep=100., sep_units='arcmin') gg2.read(out_file_name) numpy.testing.assert_almost_equal(gg2.logr, gg.logr) numpy.testing.assert_almost_equal(gg2.meanr, gg.meanr) numpy.testing.assert_almost_equal(gg2.meanlogr, gg.meanlogr) numpy.testing.assert_almost_equal(gg2.xip, gg.xip) numpy.testing.assert_almost_equal(gg2.xim, gg.xim) numpy.testing.assert_almost_equal(gg2.xip_im, gg.xip_im) numpy.testing.assert_almost_equal(gg2.xim_im, gg.xim_im) numpy.testing.assert_almost_equal(gg2.varxi, gg.varxi) numpy.testing.assert_almost_equal(gg2.weight, gg.weight) numpy.testing.assert_almost_equal(gg2.npairs, gg.npairs) # Also check the Schneider version. The math isn't quite as nice here, but it is tractable # using a different formula than I used above: # Map^2(R) = int k P(k) W(kR) dk # = 576 pi gamma0^2 r0^6/(L^2 R^4) exp(-R^2/2r0^2) (I4(R^2/2r0^2) # where I4 is the modified Bessel function with nu=4. try: from scipy.special import iv x = 0.5 * r**2 / r0**2 true_mapsq = 144. * numpy.pi * gamma0**2 * r0**2 / ( L**2 * x**2) * numpy.exp(-x) * iv(4, x) mapsq, mapsq_im, mxsq, mxsq_im, varmapsq = gg.calculateMapSq( 'Schneider') print('Schneider mapsq = ', mapsq) print('true_mapsq = ', true_mapsq) print('ratio = ', mapsq / true_mapsq) print('diff = ', mapsq - true_mapsq) print('max diff = ', max(abs(mapsq - true_mapsq))) print('max diff[20:] = ', max(abs(mapsq[20:] - true_mapsq[20:]))) # This one stays ratty longer, so we need to skip the first 20 and also loosen the # test a bit. assert max(abs(mapsq[20:] - true_mapsq[20:])) < 7.e-8 print('mxsq = ', mxsq) print('max = ', max(abs(mxsq))) print('max[20:] = ', max(abs(mxsq[20:]))) assert max(abs(mxsq[20:])) < 7.e-8 except ImportError: # Don't require scipy if the user doesn't have it. print( 'Skipping tests of Schneider aperture mass, since scipy.special not available.' )
def test_kg(): # Use gamma_t(r) = gamma0 exp(-r^2/2r0^2) around a bunch of foreground lenses. # i.e. gamma(r) = -gamma0 exp(-r^2/2r0^2) (x+iy)^2/r^2 nlens = 1000 nsource = 100000 gamma0 = 0.05 r0 = 10. L = 50. * r0 numpy.random.seed(8675309) xl = (numpy.random.random_sample(nlens) - 0.5) * L yl = (numpy.random.random_sample(nlens) - 0.5) * L xs = (numpy.random.random_sample(nsource) - 0.5) * L ys = (numpy.random.random_sample(nsource) - 0.5) * L g1 = numpy.zeros((nsource, )) g2 = numpy.zeros((nsource, )) kl = numpy.random.normal(0.23, 0.05, (nlens, )) for x, y, k in zip(xl, yl, kl): dx = xs - x dy = ys - y r2 = dx**2 + dy**2 gammat = gamma0 * numpy.exp(-0.5 * r2 / r0**2) / k g1 += -gammat * (dx**2 - dy**2) / r2 g2 += -gammat * (2. * dx * dy) / r2 lens_cat = treecorr.Catalog(x=xl, y=yl, k=kl, x_units='arcmin', y_units='arcmin') source_cat = treecorr.Catalog(x=xs, y=ys, g1=g1, g2=g2, x_units='arcmin', y_units='arcmin') kg = treecorr.KGCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin', verbose=1) kg.process(lens_cat, source_cat) r = kg.meanr true_gt = gamma0 * numpy.exp(-0.5 * r**2 / r0**2) print('kg.xi = ', kg.xi) print('kg.xi_im = ', kg.xi_im) print('true_gammat = ', true_gt) print('ratio = ', kg.xi / true_gt) print('diff = ', kg.xi - true_gt) print('max diff = ', max(abs(kg.xi - true_gt))) assert max(abs(kg.xi - true_gt)) < 4.e-3 assert max(abs(kg.xi_im)) < 4.e-3 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': lens_cat.write(os.path.join('data', 'kg_lens.dat')) source_cat.write(os.path.join('data', 'kg_source.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen([corr2_exe, "kg.params"]) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output', 'kg.out'), names=True) print('kg.xi = ', kg.xi) print('from corr2 output = ', corr2_output['kgamT']) print('ratio = ', corr2_output['kgamT'] / kg.xi) print('diff = ', corr2_output['kgamT'] - kg.xi) numpy.testing.assert_almost_equal(corr2_output['kgamT'] / kg.xi, 1., decimal=3) print('xi_im from corr2 output = ', corr2_output['kgamX']) assert max(abs(corr2_output['kgamX'])) < 4.e-3 # Check the fits write option out_file_name1 = os.path.join('output', 'kg_out1.fits') kg.write(out_file_name1) try: import fitsio data = fitsio.read(out_file_name1) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(kg.logr)) numpy.testing.assert_almost_equal(data['meanR'], kg.meanr) numpy.testing.assert_almost_equal(data['meanlogR'], kg.meanlogr) numpy.testing.assert_almost_equal(data['kgamT'], kg.xi) numpy.testing.assert_almost_equal(data['kgamX'], kg.xi_im) numpy.testing.assert_almost_equal(data['sigma'], numpy.sqrt(kg.varxi)) numpy.testing.assert_almost_equal(data['weight'], kg.weight) numpy.testing.assert_almost_equal(data['npairs'], kg.npairs) except ImportError: print('Unable to import fitsio. Skipping fits tests.') # Check the read function kg2 = treecorr.KGCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin') kg2.read(out_file_name1) numpy.testing.assert_almost_equal(kg2.logr, kg.logr) numpy.testing.assert_almost_equal(kg2.meanr, kg.meanr) numpy.testing.assert_almost_equal(kg2.meanlogr, kg.meanlogr) numpy.testing.assert_almost_equal(kg2.xi, kg.xi) numpy.testing.assert_almost_equal(kg2.xi_im, kg.xi_im) numpy.testing.assert_almost_equal(kg2.varxi, kg.varxi) numpy.testing.assert_almost_equal(kg2.weight, kg.weight) numpy.testing.assert_almost_equal(kg2.npairs, kg.npairs)
def test_rperp(): # Same as above, but using Rperp. nlens = 100 nsource = 200000 gamma0 = 0.05 R0 = 10. L = 50. * R0 numpy.random.seed(8675309) # Lenses are randomly located with random shapes. xl = (numpy.random.random_sample(nlens)-0.5) * L # -500 < x < 500 zl = (numpy.random.random_sample(nlens)-0.5) * L # -500 < y < 500 yl = numpy.random.random_sample(nlens) * 4*L + 10*L # 5000 < z < 7000 rl = numpy.sqrt(xl**2 + yl**2 + zl**2) g1l = numpy.random.normal(0., 0.1, (nlens,)) g2l = numpy.random.normal(0., 0.1, (nlens,)) gl = g1l + 1j * g2l gl /= numpy.abs(gl) print('Made lenses') # For the signal, we'll do a pure quadrupole halo lens signal. cf. test_haloellip() xs = (numpy.random.random_sample(nsource)-0.5) * L zs = (numpy.random.random_sample(nsource)-0.5) * L ys = numpy.random.random_sample(nsource) * 8*L + 160*L # 80000 < z < 84000 rs = numpy.sqrt(xs**2 + ys**2 + zs**2) g1 = numpy.zeros( (nsource,) ) g2 = numpy.zeros( (nsource,) ) bin_size = 0.1 # min_sep is set so the first bin doesn't have 0 pairs. # Both this and max_sep need to be larger than what we used for Rlens. min_sep = 4.5*R0 # max_sep can't be too large, since the measured value starts to have shape noise for larger # values of separation. We're not adding any shape noise directly, but the shear from other # lenses is effectively a shape noise, and that comes to dominate the measurement above ~4R0. max_sep = 14.*R0 # Because the Rperp values are a lot larger than the Rlens values, use a larger scale radius # in the gaussian signal. R1 = 4. * R0 nbins = int(numpy.ceil(numpy.log(max_sep/min_sep)/bin_size)) true_gQ = numpy.zeros( (nbins,) ) true_gCr = numpy.zeros( (nbins,) ) true_gCi = numpy.zeros( (nbins,) ) true_npairs = numpy.zeros((nbins,), dtype=int) print('Making shear vectors') for x,y,z,r,g in zip(xl,yl,zl,rl,gl): dsq = (x-xs)**2 + (y-ys)**2 + (z-zs)**2 rparsq = (r-rs)**2 Rperp = numpy.sqrt(dsq - rparsq) gammaQ = gamma0 * numpy.exp(-0.5*Rperp**2/R1**2) dx = xs/rs-x/r dz = zs/rs-z/r expialpha = dx + 1j*dz expialpha /= numpy.abs(expialpha) gQ = gammaQ * expialpha**4 * numpy.conj(g) g1 += gQ.real g2 += gQ.imag index = numpy.floor( numpy.log(Rperp/min_sep) / bin_size).astype(int) mask = (index >= 0) & (index < nbins) numpy.add.at(true_gQ, index[mask], gammaQ[mask]) numpy.add.at(true_npairs, index[mask], 1) gC = gQ * numpy.conj(g) numpy.add.at(true_gCr, index[mask], gC[mask].real) numpy.add.at(true_gCi, index[mask], -gC[mask].imag) true_gQ /= true_npairs true_gCr /= true_npairs true_gCi /= true_npairs print('true_gQ = ',true_gQ) print('true_gCr = ',true_gCr) print('true_gCi = ',true_gCi) # Start with bin_slop == 0. With only 100 lenses, this still runs very fast. lens_cat = treecorr.Catalog(x=xl, y=yl, z=zl, g1=gl.real, g2=gl.imag) source_cat = treecorr.Catalog(x=xs, y=ys, z=zs, g1=g1, g2=g2) gg = treecorr.GGCorrelation(bin_size=bin_size, min_sep=min_sep, max_sep=max_sep, verbose=1, metric='Rperp', bin_slop=0) gg.process(lens_cat, source_cat) Rperp = gg.meanr theory_gQ = gamma0 * numpy.exp(-0.5*Rperp**2/R1**2) print('Results with bin_slop = 0:') print('gg.npairs = ',gg.npairs) print('true_npairs = ',true_npairs) print('gg.xim = ',gg.xim) print('true_gammat = ',true_gQ) print('ratio = ',gg.xim / true_gQ) print('diff = ',gg.xim - true_gQ) print('max diff = ',max(abs(gg.xim - true_gQ))) assert max(abs(gg.xim - true_gQ)) < 1.e-5 print('gg.xim_im = ',gg.xim_im) assert max(abs(gg.xim_im)) < 1.e-5 print('gg.xip = ',gg.xip) print('true_gCr = ',true_gCr) print('diff = ',gg.xip - true_gCr) print('max diff = ',max(abs(gg.xip - true_gCr))) assert max(abs(gg.xip - true_gCr)) < 1.e-5 print('gg.xip_im = ',gg.xip_im) print('true_gCi = ',true_gCi) print('diff = ',gg.xip_im - true_gCi) print('max diff = ',max(abs(gg.xip_im - true_gCi))) assert max(abs(gg.xip_im - true_gCi)) < 1.e-5 print('gg.xim = ',gg.xim) print('theory_gammat = ',theory_gQ) print('ratio = ',gg.xim / theory_gQ) print('diff = ',gg.xim - theory_gQ) print('max diff = ',max(abs(gg.xim - theory_gQ))) assert max(abs(gg.xim - theory_gQ)) < 4.e-5 # Now use a more normal value for bin_slop. gg = treecorr.GGCorrelation(bin_size=bin_size, min_sep=min_sep, max_sep=max_sep, verbose=1, metric='Rperp', bin_slop=0.5) gg.process(lens_cat, source_cat) Rperp = gg.meanr theory_gQ = gamma0 * numpy.exp(-0.5*Rperp**2/R1**2) print('Results with bin_slop = 0.5') print('gg.npairs = ',gg.npairs) print('gg.xim = ',gg.xim) print('theory_gammat = ',theory_gQ) print('ratio = ',gg.xim / theory_gQ) print('diff = ',gg.xim - theory_gQ) print('max diff = ',max(abs(gg.xim - theory_gQ))) assert max(abs(gg.xim - theory_gQ)) < 4.e-5 print('gg.xim_im = ',gg.xim_im) assert max(abs(gg.xim_im)) < 1.e-5 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': lens_cat.write(os.path.join('data','gg_rperp_lens.dat')) source_cat.write(os.path.join('data','gg_rperp_source.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"gg_rperp.yaml"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','gg_rperp.out'),names=True) print('gg.xim = ',gg.xim) print('from corr2 output = ',corr2_output['xim']) print('ratio = ',corr2_output['xim']/gg.xim) print('diff = ',corr2_output['xim']-gg.xim) numpy.testing.assert_almost_equal(corr2_output['xim'], gg.xim, decimal=6) numpy.testing.assert_almost_equal(corr2_output['xim_im'], gg.xim_im, decimal=6) numpy.testing.assert_almost_equal(corr2_output['xip'], gg.xip, decimal=6) numpy.testing.assert_almost_equal(corr2_output['xip_im'], gg.xip_im, decimal=6)
def test_ggg(): # Use gamma_t(r) = gamma0 r^2/r0^2 exp(-r^2/2r0^2) # i.e. gamma(r) = -gamma0 exp(-r^2/2r0^2) (x+iy)^2 / r0^2 # # Rather than go through the bispectrum, I found it easier to just directly do the # integral: # # Gamma0 = int(int( g(x+x1,y+y1) g(x+x2,y+y2) g(x-x1-x2,y-y1-y2) (x1-Iy1)^2/(x1^2+y1^2) # (x2-Iy2)^2/(x2^2+y2^2) (x1+x2-I(y1+y2))^2/((x1+x2)^2+(y1+y2)^2))) # # where the positions are measured relative to the centroid (x,y). # If we call the positions relative to the centroid: # q1 = x1 + I y1 # q2 = x2 + I y2 # q3 = -(x1+x2) - I (y1+y2) # then the result comes out as # # Gamma0 = -2/3 gamma0^3/L^2r0^4 Pi |q1|^2 |q2|^2 |q3|^2 exp(-(|q1|^2+|q2|^2+|q3|^2)/2r0^2) # # The other three are a bit more complicated. # # Gamma1 = int(int( g(x+x1,y+y1)* g(x+x2,y+y2) g(x-x1-x2,y-y1-y2) (x1+Iy1)^2/(x1^2+y1^2) # (x2-Iy2)^2/(x2^2+y2^2) (x1+x2-I(y1+y2))^2/((x1+x2)^2+(y1+y2)^2))) # # = -2/3 gamma0^3/L^2r0^4 Pi exp(-(|q1|^2+|q2|^2+|q3|^2)/2r0^2) * # ( |q1|^2 |q2|^2 |q3|^2 - 8/3 r0^2 q1^2 q2* q3* # + 8/9 r0^4 (q1^2 q2*^2 q3*^2)/(|q1|^2 |q2|^2 |q3|^2) (2q1^2-q2^2-q3^2) ) # # Gamm2 and Gamma3 are found from cyclic rotations of q1,q2,q3. gamma0 = 0.05 r0 = 10. if __name__ == '__main__': ngal = 200000 L = 30.*r0 # Not infinity, so this introduces some error. Our integrals were to infinity. req_factor = 1 else: # Looser tests from nosetests that don't take so long to run. ngal = 10000 L = 10.*r0 req_factor = 5 numpy.random.seed(8675309) x = (numpy.random.random_sample(ngal)-0.5) * L y = (numpy.random.random_sample(ngal)-0.5) * L r2 = (x**2 + y**2)/r0**2 g1 = -gamma0 * numpy.exp(-r2/2.) * (x**2-y**2)/r0**2 g2 = -gamma0 * numpy.exp(-r2/2.) * (2.*x*y)/r0**2 min_sep = 11. max_sep = 15. nbins = 3 min_u = 0.7 max_u = 1.0 nubins = 3 min_v = -0.1 max_v = 0.3 nvbins = 4 cat = treecorr.Catalog(x=x, y=y, g1=g1, g2=g2, x_units='arcmin', y_units='arcmin') ggg = treecorr.GGGCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins, min_u=min_u, max_u=max_u, min_v=min_v, max_v=max_v, nubins=nubins, nvbins=nvbins, sep_units='arcmin', verbose=1) ggg.process(cat) # log(<d>) != <logd>, but it should be close: #print('meanlogd1 - log(meand1) = ',ggg.meanlogd1 - numpy.log(ggg.meand1)) #print('meanlogd2 - log(meand2) = ',ggg.meanlogd2 - numpy.log(ggg.meand2)) #print('meanlogd3 - log(meand3) = ',ggg.meanlogd3 - numpy.log(ggg.meand3)) #print('meanlogd3 - meanlogd2 - log(meanu) = ',ggg.meanlogd3 - ggg.meanlogd2 - numpy.log(ggg.meanu)) #print('log(meand1-meand2) - meanlogd3 - log(meanv) = ',numpy.log(ggg.meand1-ggg.meand2) - ggg.meanlogd3 - numpy.log(numpy.abs(ggg.meanv))) numpy.testing.assert_almost_equal(ggg.meanlogd1, numpy.log(ggg.meand1), decimal=3) numpy.testing.assert_almost_equal(ggg.meanlogd2, numpy.log(ggg.meand2), decimal=3) numpy.testing.assert_almost_equal(ggg.meanlogd3, numpy.log(ggg.meand3), decimal=3) numpy.testing.assert_almost_equal(ggg.meanlogd3-ggg.meanlogd2, numpy.log(ggg.meanu), decimal=3) numpy.testing.assert_almost_equal(numpy.log(ggg.meand1-ggg.meand2)-ggg.meanlogd3, numpy.log(numpy.abs(ggg.meanv)), decimal=3) d1 = ggg.meand1 d2 = ggg.meand2 d3 = ggg.meand3 #print('rnom = ',numpy.exp(ggg.logr)) #print('unom = ',ggg.u) #print('vnom = ',ggg.v) #print('d1 = ',d1) #print('d2 = ',d2) #print('d3 = ',d3) # For q1,q2,q3, we can choose an orientation where c1 is at the origin, and d2 is horizontal. # Then let s be the "complex vector" from c1 to c3, which is just real. s = d2 # And let t be from c1 to c2. t = |t| e^Iphi # |t| = d3 # cos(phi) = (d2^2+d3^2-d1^2)/(2d2 d3) # |t| cos(phi) = (d2^2+d3^2-d1^2)/2d2 # |t| sin(phi) = sqrt(|t|^2 - (|t|cos(phi))^2) tx = (d2**2 + d3**2 - d1**2)/(2.*d2) ty = numpy.sqrt(d3**2 - tx**2) # As arranged, if ty > 0, points 1,2,3 are clockwise, which is negative v. # So for bins with positive v, we need to flip the direction of ty. ty[ggg.meanv > 0] *= -1. t = tx + 1j * ty q1 = (s + t)/3. q2 = q1 - t q3 = q1 - s nq1 = numpy.abs(q1)**2 nq2 = numpy.abs(q2)**2 nq3 = numpy.abs(q3)**2 #print('q1 = ',q1) #print('q2 = ',q2) #print('q3 = ',q3) # The L^2 term in the denominator of true_zeta is the area over which the integral is done. # Since the centers of the triangles don't go to the edge of the box, we approximate the # correct area by subtracting off 2*mean(qi) from L, which should give a slightly better # estimate of the correct area to use here. (We used 2d2 for the kkk calculation, but this # is probably a slightly better estimate of the distance the triangles can get to the edges.) L = L - 2.*(q1 + q2 + q3)/3. # Gamma0 = -2/3 gamma0^3/L^2r0^4 Pi |q1|^2 |q2|^2 |q3|^2 exp(-(|q1|^2+|q2|^2+|q3|^2)/2r0^2) true_gam0 = ((-2.*numpy.pi * gamma0**3)/(3. * L**2 * r0**4) * numpy.exp(-(nq1+nq2+nq3)/(2.*r0**2)) * (nq1*nq2*nq3) ) # Gamma1 = -2/3 gamma0^3/L^2r0^4 Pi exp(-(|q1|^2+|q2|^2+|q3|^2)/2r0^2) * # ( |q1|^2 |q2|^2 |q3|^2 - 8/3 r0^2 q1^2 q2* q3* # + 8/9 r0^4 (q1^2 q2*^2 q3*^2)/(|q1|^2 |q2|^2 |q3|^2) (2q1^2-q2^2-q3^2) ) true_gam1 = ((-2.*numpy.pi * gamma0**3)/(3. * L**2 * r0**4) * numpy.exp(-(nq1+nq2+nq3)/(2.*r0**2)) * (nq1*nq2*nq3 - 8./3. * r0**2 * q1**2*nq2*nq3/(q2*q3) + (8./9. * r0**4 * (q1**2 * nq2 * nq3)/(nq1 * q2**2 * q3**2) * (2.*q1**2 - q2**2 - q3**2)) )) true_gam2 = ((-2.*numpy.pi * gamma0**3)/(3. * L**2 * r0**4) * numpy.exp(-(nq1+nq2+nq3)/(2.*r0**2)) * (nq1*nq2*nq3 - 8./3. * r0**2 * nq1*q2**2*nq3/(q1*q3) + (8./9. * r0**4 * (nq1 * q2**2 * nq3)/(q1**2 * nq2 * q3**2) * (2.*q2**2 - q1**2 - q3**2)) )) true_gam3 = ((-2.*numpy.pi * gamma0**3)/(3. * L**2 * r0**4) * numpy.exp(-(nq1+nq2+nq3)/(2.*r0**2)) * (nq1*nq2*nq3 - 8./3. * r0**2 * nq1*nq2*q3**2/(q1*q2) + (8./9. * r0**4 * (nq1 * nq2 * q3**2)/(q1**2 * q2**2 * nq3) * (2.*q3**2 - q1**2 - q2**2)) )) #print('ntri = ',ggg.ntri) print('gam0 = ',ggg.gam0) print('true_gam0 = ',true_gam0) #print('ratio = ',ggg.gam0 / true_gam0) #print('diff = ',ggg.gam0 - true_gam0) print('max rel diff = ',numpy.max(numpy.abs((ggg.gam0 - true_gam0)/true_gam0))) # The Gamma0 term is a bit worse than the others. The accurracy improves as I increase the # number of objects, so I think it's just because of the smallish number of galaxies being # not super accurate. assert numpy.max(numpy.abs((ggg.gam0 - true_gam0)/true_gam0))/req_factor < 0.2 numpy.testing.assert_almost_equal(numpy.log(numpy.abs(ggg.gam0))/2./req_factor, numpy.log(numpy.abs(true_gam0))/2./req_factor, decimal=1) print('gam1 = ',ggg.gam1) print('true_gam1 = ',true_gam1) #print('ratio = ',ggg.gam1 / true_gam1) #print('diff = ',ggg.gam1 - true_gam1) print('max rel diff = ',numpy.max(numpy.abs((ggg.gam1 - true_gam1)/true_gam1))) assert numpy.max(numpy.abs((ggg.gam1 - true_gam1)/true_gam1))/req_factor < 0.1 numpy.testing.assert_almost_equal(numpy.log(numpy.abs(ggg.gam1))/req_factor, numpy.log(numpy.abs(true_gam1))/req_factor, decimal=1) #print('gam2 = ',ggg.gam2) #print('true_gam2 = ',true_gam2) #print('ratio = ',ggg.gam2 / true_gam2) #print('diff = ',ggg.gam2 - true_gam2) #print('max rel diff = ',numpy.max(numpy.abs((ggg.gam2 - true_gam2)/true_gam2))) assert numpy.max(numpy.abs((ggg.gam2 - true_gam2)/true_gam2))/req_factor < 0.1 numpy.testing.assert_almost_equal(numpy.log(numpy.abs(ggg.gam2))/req_factor, numpy.log(numpy.abs(true_gam2))/req_factor, decimal=1) #print('gam3 = ',ggg.gam3) #print('true_gam3 = ',true_gam3) #print('ratio = ',ggg.gam3 / true_gam3) #print('diff = ',ggg.gam3 - true_gam3) #print('max rel diff = ',numpy.max(numpy.abs((ggg.gam3 - true_gam3)/true_gam3))) assert numpy.max(numpy.abs((ggg.gam3 - true_gam3)/true_gam3))/req_factor < 0.1 numpy.testing.assert_almost_equal(numpy.log(numpy.abs(ggg.gam3))/req_factor, numpy.log(numpy.abs(true_gam3))/req_factor, decimal=1) # Check that we get the same result using the corr3 executable: if __name__ == '__main__': cat.write(os.path.join('data','ggg_data.dat')) import subprocess corr3_exe = get_script_name('corr3') p = subprocess.Popen( [corr3_exe,"ggg.yaml"] ) p.communicate() corr3_output = numpy.genfromtxt(os.path.join('output','ggg.out'), names=True) #print('gam0r = ',ggg.gam0.real) #print('from corr3 output = ',corr3_output['gam0r']) #print('ratio = ',corr3_output['gam0r']/ggg.gam0r.flatten()) #print('diff = ',corr3_output['gam0r']-ggg.gam0r.flatten()) numpy.testing.assert_almost_equal(corr3_output['gam0r']/ggg.gam0r.flatten(), 1., decimal=3) #print('gam0i = ',ggg.gam0.imag) #print('from corr3 output = ',corr3_output['gam0i']) #print('ratio = ',corr3_output['gam0i']/ggg.gam0i.flatten()) #print('diff = ',corr3_output['gam0i']-ggg.gam0i.flatten()) numpy.testing.assert_almost_equal(corr3_output['gam0i']/ggg.gam0i.flatten(), 1., decimal=3) #print('gam1r = ',ggg.gam1.real) #print('from corr3 output = ',corr3_output['gam1r']) #print('ratio = ',corr3_output['gam1r']/ggg.gam1r.flatten()) #print('diff = ',corr3_output['gam1r']-ggg.gam1r.flatten()) numpy.testing.assert_almost_equal(corr3_output['gam1r']/ggg.gam1r.flatten(), 1., decimal=3) #print('gam1i = ',ggg.gam1.imag) #print('from corr3 output = ',corr3_output['gam1i']) #print('ratio = ',corr3_output['gam1i']/ggg.gam1i.flatten()) #print('diff = ',corr3_output['gam1i']-ggg.gam1i.flatten()) numpy.testing.assert_almost_equal(corr3_output['gam1i']/ggg.gam1i.flatten(), 1., decimal=3) #print('gam2r = ',ggg.gam2.real) #print('from corr3 output = ',corr3_output['gam2r']) #print('ratio = ',corr3_output['gam2r']/ggg.gam2r.flatten()) #print('diff = ',corr3_output['gam2r']-ggg.gam2r.flatten()) numpy.testing.assert_almost_equal(corr3_output['gam2r']/ggg.gam2r.flatten(), 1., decimal=3) #print('gam2i = ',ggg.gam2.imag) #print('from corr3 output = ',corr3_output['gam2i']) #print('ratio = ',corr3_output['gam2i']/ggg.gam2i.flatten()) #print('diff = ',corr3_output['gam2i']-ggg.gam2i.flatten()) numpy.testing.assert_almost_equal(corr3_output['gam2i']/ggg.gam2i.flatten(), 1., decimal=3) #print('gam3r = ',ggg.gam3.real) #print('from corr3 output = ',corr3_output['gam3r']) #print('ratio = ',corr3_output['gam3r']/ggg.gam3r.flatten()) #print('diff = ',corr3_output['gam3r']-ggg.gam3r.flatten()) numpy.testing.assert_almost_equal(corr3_output['gam3r']/ggg.gam3r.flatten(), 1., decimal=3) #print('gam3i = ',ggg.gam3.imag) #print('from corr3 output = ',corr3_output['gam3i']) #print('ratio = ',corr3_output['gam3i']/ggg.gam3i.flatten()) #print('diff = ',corr3_output['gam3i']-ggg.gam3i.flatten()) numpy.testing.assert_almost_equal(corr3_output['gam3i']/ggg.gam3i.flatten(), 1., decimal=3) # Check the fits write option out_file_name1 = os.path.join('output','ggg_out1.fits') ggg.write(out_file_name1) data = fitsio.read(out_file_name1) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(ggg.logr).flatten()) numpy.testing.assert_almost_equal(data['u_nom'], ggg.u.flatten()) numpy.testing.assert_almost_equal(data['v_nom'], ggg.v.flatten()) numpy.testing.assert_almost_equal(data['meand1'], ggg.meand1.flatten()) numpy.testing.assert_almost_equal(data['meanlogd1'], ggg.meanlogd1.flatten()) numpy.testing.assert_almost_equal(data['meand2'], ggg.meand2.flatten()) numpy.testing.assert_almost_equal(data['meanlogd2'], ggg.meanlogd2.flatten()) numpy.testing.assert_almost_equal(data['meand3'], ggg.meand3.flatten()) numpy.testing.assert_almost_equal(data['meanlogd3'], ggg.meanlogd3.flatten()) numpy.testing.assert_almost_equal(data['meanu'], ggg.meanu.flatten()) numpy.testing.assert_almost_equal(data['meanv'], ggg.meanv.flatten()) numpy.testing.assert_almost_equal(data['gam0r'], ggg.gam0.real.flatten()) numpy.testing.assert_almost_equal(data['gam1r'], ggg.gam1.real.flatten()) numpy.testing.assert_almost_equal(data['gam2r'], ggg.gam2.real.flatten()) numpy.testing.assert_almost_equal(data['gam3r'], ggg.gam3.real.flatten()) numpy.testing.assert_almost_equal(data['gam0i'], ggg.gam0.imag.flatten()) numpy.testing.assert_almost_equal(data['gam1i'], ggg.gam1.imag.flatten()) numpy.testing.assert_almost_equal(data['gam2i'], ggg.gam2.imag.flatten()) numpy.testing.assert_almost_equal(data['gam3i'], ggg.gam3.imag.flatten()) numpy.testing.assert_almost_equal(data['sigma_gam'], numpy.sqrt(ggg.vargam.flatten())) numpy.testing.assert_almost_equal(data['weight'], ggg.weight.flatten()) numpy.testing.assert_almost_equal(data['ntri'], ggg.ntri.flatten()) # Check the read function # Note: These don't need the flatten. The read function should reshape them to the right shape. ggg2 = treecorr.GGGCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins, min_u=min_u, max_u=max_u, min_v=min_v, max_v=max_v, nubins=nubins, nvbins=nvbins, sep_units='arcmin', verbose=1) ggg2.read(out_file_name1) numpy.testing.assert_almost_equal(ggg2.logr, ggg.logr) numpy.testing.assert_almost_equal(ggg2.u, ggg.u) numpy.testing.assert_almost_equal(ggg2.v, ggg.v) numpy.testing.assert_almost_equal(ggg2.meand1, ggg.meand1) numpy.testing.assert_almost_equal(ggg2.meanlogd1, ggg.meanlogd1) numpy.testing.assert_almost_equal(ggg2.meand2, ggg.meand2) numpy.testing.assert_almost_equal(ggg2.meanlogd2, ggg.meanlogd2) numpy.testing.assert_almost_equal(ggg2.meand3, ggg.meand3) numpy.testing.assert_almost_equal(ggg2.meanlogd3, ggg.meanlogd3) numpy.testing.assert_almost_equal(ggg2.meanu, ggg.meanu) numpy.testing.assert_almost_equal(ggg2.meanv, ggg.meanv) numpy.testing.assert_almost_equal(ggg2.gam0, ggg.gam0) numpy.testing.assert_almost_equal(ggg2.gam1, ggg.gam1) numpy.testing.assert_almost_equal(ggg2.gam2, ggg.gam2) numpy.testing.assert_almost_equal(ggg2.gam3, ggg.gam3) numpy.testing.assert_almost_equal(ggg2.vargam, ggg.vargam) numpy.testing.assert_almost_equal(ggg2.weight, ggg.weight) numpy.testing.assert_almost_equal(ggg2.ntri, ggg.ntri)
def test_spherical(): # This is the same profile we used for test_single, but put into spherical coords. # We do the spherical trig by hand using the obvious formulae, rather than the clever # optimizations that are used by the TreeCorr code, thus serving as a useful test of # the latter. nsource = 1000000 gamma0 = 0.05 r0 = 10. * treecorr.degrees 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) gammat = gamma0 * numpy.exp(-0.5*r2/r0**2) g1 = -gammat * (x**2-y**2)/r2 g2 = -gammat * (2.*x*y)/r2 r = numpy.sqrt(r2) theta = arctan2(y,x) ng = treecorr.NGCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='deg', verbose=1) r1 = numpy.exp(ng.logr) * treecorr.degrees true_gt = gamma0 * numpy.exp(-0.5*r1**2/r0**2) # Test this around several central points if __name__ == '__main__': ra0_list = [ 0., 1., 1.3, 232., 0. ] dec0_list = [ 0., -0.3, 1.3, -1.4, pi/2.-1.e-6 ] else: ra0_list = [ 232 ] dec0_list = [ -1.4 ] for ra0, dec0 in zip(ra0_list, dec0_list): # Use spherical triangle with A = point, B = (ra0,dec0), C = N. pole # a = Pi/2-dec0 # c = 2*asin(r/2) (lambert projection) # B = Pi/2 - theta c = 2.*arcsin(r/2.) a = pi/2. - dec0 B = pi/2. - theta B[x<0] *= -1. B[B<-pi] += 2.*pi B[B>pi] -= 2.*pi # Solve the rest of the triangle with spherical trig: cosb = cos(a)*cos(c) + sin(a)*sin(c)*cos(B) b = arccos(cosb) cosA = (cos(a) - cos(b)*cos(c)) / (sin(b)*sin(c)) #A = arccos(cosA) A = numpy.zeros_like(cosA) A[abs(cosA)<1] = arccos(cosA[abs(cosA)<1]) A[cosA<=-1] = pi cosC = (cos(c) - cos(a)*cos(b)) / (sin(a)*sin(b)) #C = arccos(cosC) C = numpy.zeros_like(cosC) C[abs(cosC)<1] = arccos(cosC[abs(cosC)<1]) C[cosC<=-1] = pi C[x<0] *= -1. ra = ra0 - C dec = pi/2. - b # Rotate shear relative to local west # gamma_sph = exp(2i beta) * gamma # where beta = pi - (A+B) is the angle between north and "up" in the tangent plane. beta = pi - (A+B) beta[x>0] *= -1. cos2beta = cos(2.*beta) sin2beta = sin(2.*beta) g1_sph = g1 * cos2beta - g2 * sin2beta g2_sph = g2 * cos2beta + g1 * sin2beta lens_cat = treecorr.Catalog(ra=[ra0], dec=[dec0], ra_units='rad', dec_units='rad') source_cat = treecorr.Catalog(ra=ra, dec=dec, g1=g1_sph, g2=g2_sph, ra_units='rad', dec_units='rad') ng.process(lens_cat, source_cat) print('ra0, dec0 = ',ra0,dec0) print('ng.xi = ',ng.xi) print('true_gammat = ',true_gt) print('ratio = ',ng.xi / true_gt) print('diff = ',ng.xi - true_gt) print('max diff = ',max(abs(ng.xi - true_gt))) # The 3rd and 4th centers are somewhat less accurate. Not sure why. # The math seems to be right, since the last one that gets all the way to the pole # works, so I'm not sure what is going on. It's just a few bins that get a bit less # accurate. Possibly worth investigating further at some point... assert max(abs(ng.xi - true_gt)) < 2.e-3 # One more center that can be done very easily. If the center is the north pole, then all # the tangential shears are pure (positive) g1. ra0 = 0 dec0 = pi/2. ra = theta dec = pi/2. - 2.*arcsin(r/2.) lens_cat = treecorr.Catalog(ra=[ra0], dec=[dec0], ra_units='rad', dec_units='rad') source_cat = treecorr.Catalog(ra=ra, dec=dec, g1=gammat, g2=numpy.zeros_like(gammat), ra_units='rad', dec_units='rad') ng.process(lens_cat, source_cat) print('ng.xi = ',ng.xi) print('ng.xi_im = ',ng.xi_im) print('true_gammat = ',true_gt) print('ratio = ',ng.xi / true_gt) print('diff = ',ng.xi - true_gt) print('max diff = ',max(abs(ng.xi - true_gt))) assert max(abs(ng.xi - true_gt)) < 1.e-3 assert max(abs(ng.xi_im)) < 3.e-5 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': lens_cat.write(os.path.join('data','ng_spherical_lens.dat')) source_cat.write(os.path.join('data','ng_spherical_source.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"ng_spherical.yaml"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','ng_spherical.out'),names=True) print('ng.xi = ',ng.xi) print('from corr2 output = ',corr2_output['gamT']) print('ratio = ',corr2_output['gamT']/ng.xi) print('diff = ',corr2_output['gamT']-ng.xi) numpy.testing.assert_almost_equal(corr2_output['gamT']/ng.xi, 1., decimal=3) print('xi_im from corr2 output = ',corr2_output['gamX']) assert max(abs(corr2_output['gamX'])) < 3.e-5
def test_kkk(): # Use kappa(r) = A exp(-r^2/2s^2) # # The Fourier transform is: kappa~(k) = 2 pi A s^2 exp(-s^2 k^2/2) / L^2 # # B(k1,k2) = <k~(k1) k~(k2) k~(-k1-k2)> # = (2 pi A (s/L)^2)^3 exp(-s^2 (|k1|^2 + |k2|^2 - k1.k2)) # = (2 pi A (s/L)^2)^3 exp(-s^2 (|k1|^2 + |k2|^2 + |k3|^2)/2) # # zeta(r1,r2) = (1/2pi)^4 int(d^2k1 int(d^2k2 exp(ik1.x1) exp(ik2.x2) B(k1,k2) )) # = 2/3 pi A^3 (s/L)^2 exp(-(x1^2 + y1^2 + x2^2 + y2^2 - x1x2 - y1y2)/3s^2) # = 2/3 pi A^3 (s/L)^2 exp(-(d1^2 + d2^2 + d3^2)/6s^2) A = 0.05 s = 10. if __name__ == '__main__': ngal = 200000 L = 30. * s # Not infinity, so this introduces some error. Our integrals were to infinity. req_factor = 1 else: # Looser tests from nosetests that don't take so long to run. ngal = 5000 L = 10. * s req_factor = 5 numpy.random.seed(8675309) x = (numpy.random.random_sample(ngal) - 0.5) * L y = (numpy.random.random_sample(ngal) - 0.5) * L r2 = (x**2 + y**2) / s**2 kappa = A * numpy.exp(-r2 / 2.) min_sep = 11. max_sep = 15. nbins = 3 min_u = 0.7 max_u = 1.0 nubins = 3 min_v = -0.1 max_v = 0.3 nvbins = 4 cat = treecorr.Catalog(x=x, y=y, k=kappa, x_units='arcmin', y_units='arcmin') kkk = treecorr.KKKCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins, min_u=min_u, max_u=max_u, min_v=min_v, max_v=max_v, nubins=nubins, nvbins=nvbins, sep_units='arcmin', verbose=1) kkk.process(cat) # log(<d>) != <logd>, but it should be close: #print('meanlogd1 - log(meand1) = ',kkk.meanlogd1 - numpy.log(kkk.meand1)) #print('meanlogd2 - log(meand2) = ',kkk.meanlogd2 - numpy.log(kkk.meand2)) #print('meanlogd3 - log(meand3) = ',kkk.meanlogd3 - numpy.log(kkk.meand3)) #print('meanlogd3 - meanlogd2 - log(meanu) = ',kkk.meanlogd3 - kkk.meanlogd2 - numpy.log(kkk.meanu)) #print('log(meand1-meand2) - meanlogd3 - log(meanv) = ',numpy.log(kkk.meand1-kkk.meand2) - kkk.meanlogd3 - numpy.log(numpy.abs(kkk.meanv))) numpy.testing.assert_almost_equal(kkk.meanlogd1, numpy.log(kkk.meand1), decimal=3) numpy.testing.assert_almost_equal(kkk.meanlogd2, numpy.log(kkk.meand2), decimal=3) numpy.testing.assert_almost_equal(kkk.meanlogd3, numpy.log(kkk.meand3), decimal=3) numpy.testing.assert_almost_equal(kkk.meanlogd3 - kkk.meanlogd2, numpy.log(kkk.meanu), decimal=3) numpy.testing.assert_almost_equal(numpy.log(kkk.meand1 - kkk.meand2) - kkk.meanlogd3, numpy.log(numpy.abs(kkk.meanv)), decimal=3) d1 = kkk.meand1 d2 = kkk.meand2 d3 = kkk.meand3 #print('rnom = ',numpy.exp(kkk.logr)) #print('unom = ',kkk.u) #print('vnom = ',kkk.v) #print('d1 = ',d1) #print('d2 = ',d2) #print('d3 = ',d3) # The L^2 term in the denominator of true_zeta is the area over which the integral is done. # Since the centers of the triangles don't go to the edge of the box, we approximate the # correct area by subtracting off 2d2 from L, which should give a slightly better estimate # of the correct area to use here. L = L - 2. * d2 true_zeta = (2. * numpy.pi / 3) * A**3 * (s / L)**2 * numpy.exp( -(d1**2 + d2**2 + d3**2) / (6. * s**2)) #print('ntri = ',kkk.ntri) print('zeta = ', kkk.zeta) print('true_zeta = ', true_zeta) #print('ratio = ',kkk.zeta / true_zeta) #print('diff = ',kkk.zeta - true_zeta) print('max rel diff = ', numpy.max(numpy.abs((kkk.zeta - true_zeta) / true_zeta))) assert numpy.max(numpy.abs( (kkk.zeta - true_zeta) / true_zeta)) / req_factor < 0.1 numpy.testing.assert_almost_equal( numpy.log(numpy.abs(kkk.zeta)) / req_factor, numpy.log(numpy.abs(true_zeta)) / req_factor, decimal=1) # Check that we get the same result using the corr3 executable: if __name__ == '__main__': cat.write(os.path.join('data', 'kkk_data.dat')) import subprocess corr3_exe = get_script_name('corr3') p = subprocess.Popen([corr3_exe, "kkk.yaml"]) p.communicate() corr3_output = numpy.genfromtxt(os.path.join('output', 'kkk.out'), names=True) #print('zeta = ',kkk.zeta) #print('from corr3 output = ',corr3_output['zeta']) #print('ratio = ',corr3_output['zeta']/kkk.zeta.flatten()) #print('diff = ',corr3_output['zeta']-kkk.zeta.flatten()) numpy.testing.assert_almost_equal(corr3_output['zeta'] / kkk.zeta.flatten(), 1., decimal=3) # Check the fits write option out_file_name = os.path.join('output', 'kkk_out.fits') kkk.write(out_file_name) data = fitsio.read(out_file_name) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(kkk.logr).flatten()) numpy.testing.assert_almost_equal(data['u_nom'], kkk.u.flatten()) numpy.testing.assert_almost_equal(data['v_nom'], kkk.v.flatten()) numpy.testing.assert_almost_equal(data['meand1'], kkk.meand1.flatten()) numpy.testing.assert_almost_equal(data['meanlogd1'], kkk.meanlogd1.flatten()) numpy.testing.assert_almost_equal(data['meand2'], kkk.meand2.flatten()) numpy.testing.assert_almost_equal(data['meanlogd2'], kkk.meanlogd2.flatten()) numpy.testing.assert_almost_equal(data['meand3'], kkk.meand3.flatten()) numpy.testing.assert_almost_equal(data['meanlogd3'], kkk.meanlogd3.flatten()) numpy.testing.assert_almost_equal(data['meanu'], kkk.meanu.flatten()) numpy.testing.assert_almost_equal(data['meanv'], kkk.meanv.flatten()) numpy.testing.assert_almost_equal(data['zeta'], kkk.zeta.flatten()) numpy.testing.assert_almost_equal(data['sigma_zeta'], numpy.sqrt(kkk.varzeta.flatten())) numpy.testing.assert_almost_equal(data['weight'], kkk.weight.flatten()) numpy.testing.assert_almost_equal(data['ntri'], kkk.ntri.flatten()) # Check the read function # Note: These don't need the flatten. The read function should reshape them to the right shape. kkk2 = treecorr.KKKCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins, min_u=min_u, max_u=max_u, min_v=min_v, max_v=max_v, nubins=nubins, nvbins=nvbins, sep_units='arcmin', verbose=1) kkk2.read(out_file_name) numpy.testing.assert_almost_equal(kkk2.logr, kkk.logr) numpy.testing.assert_almost_equal(kkk2.u, kkk.u) numpy.testing.assert_almost_equal(kkk2.v, kkk.v) numpy.testing.assert_almost_equal(kkk2.meand1, kkk.meand1) numpy.testing.assert_almost_equal(kkk2.meanlogd1, kkk.meanlogd1) numpy.testing.assert_almost_equal(kkk2.meand2, kkk.meand2) numpy.testing.assert_almost_equal(kkk2.meanlogd2, kkk.meanlogd2) numpy.testing.assert_almost_equal(kkk2.meand3, kkk.meand3) numpy.testing.assert_almost_equal(kkk2.meanlogd3, kkk.meanlogd3) numpy.testing.assert_almost_equal(kkk2.meanu, kkk.meanu) numpy.testing.assert_almost_equal(kkk2.meanv, kkk.meanv) numpy.testing.assert_almost_equal(kkk2.zeta, kkk.zeta) numpy.testing.assert_almost_equal(kkk2.varzeta, kkk.varzeta) numpy.testing.assert_almost_equal(kkk2.weight, kkk.weight) numpy.testing.assert_almost_equal(kkk2.ntri, kkk.ntri)
def test_ggg(): # Use gamma_t(r) = gamma0 r^2/r0^2 exp(-r^2/2r0^2) # i.e. gamma(r) = -gamma0 exp(-r^2/2r0^2) (x+iy)^2 / r0^2 # # Rather than go through the bispectrum, I found it easier to just directly do the # integral: # # Gamma0 = int(int( g(x+x1,y+y1) g(x+x2,y+y2) g(x-x1-x2,y-y1-y2) (x1-Iy1)^2/(x1^2+y1^2) # (x2-Iy2)^2/(x2^2+y2^2) (x1+x2-I(y1+y2))^2/((x1+x2)^2+(y1+y2)^2))) # # where the positions are measured relative to the centroid (x,y). # If we call the positions relative to the centroid: # q1 = x1 + I y1 # q2 = x2 + I y2 # q3 = -(x1+x2) - I (y1+y2) # then the result comes out as # # Gamma0 = -2/3 gamma0^3/L^2r0^4 Pi |q1|^2 |q2|^2 |q3|^2 exp(-(|q1|^2+|q2|^2+|q3|^2)/2r0^2) # # The other three are a bit more complicated. # # Gamma1 = int(int( g(x+x1,y+y1)* g(x+x2,y+y2) g(x-x1-x2,y-y1-y2) (x1+Iy1)^2/(x1^2+y1^2) # (x2-Iy2)^2/(x2^2+y2^2) (x1+x2-I(y1+y2))^2/((x1+x2)^2+(y1+y2)^2))) # # = -2/3 gamma0^3/L^2r0^4 Pi exp(-(|q1|^2+|q2|^2+|q3|^2)/2r0^2) * # ( |q1|^2 |q2|^2 |q3|^2 - 8/3 r0^2 q1^2 q2* q3* # + 8/9 r0^4 (q1^2 q2*^2 q3*^2)/(|q1|^2 |q2|^2 |q3|^2) (2q1^2-q2^2-q3^2) ) # # Gamm2 and Gamma3 are found from cyclic rotations of q1,q2,q3. gamma0 = 0.05 r0 = 10. if __name__ == '__main__': ngal = 200000 L = 30. * r0 # Not infinity, so this introduces some error. Our integrals were to infinity. req_factor = 1 else: # Looser tests from nosetests that don't take so long to run. ngal = 10000 L = 10. * r0 req_factor = 5 numpy.random.seed(8675309) x = (numpy.random.random_sample(ngal) - 0.5) * L y = (numpy.random.random_sample(ngal) - 0.5) * L r2 = (x**2 + y**2) / r0**2 g1 = -gamma0 * numpy.exp(-r2 / 2.) * (x**2 - y**2) / r0**2 g2 = -gamma0 * numpy.exp(-r2 / 2.) * (2. * x * y) / r0**2 min_sep = 11. max_sep = 15. nbins = 3 min_u = 0.7 max_u = 1.0 nubins = 3 min_v = -0.1 max_v = 0.3 nvbins = 4 cat = treecorr.Catalog(x=x, y=y, g1=g1, g2=g2, x_units='arcmin', y_units='arcmin') ggg = treecorr.GGGCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins, min_u=min_u, max_u=max_u, min_v=min_v, max_v=max_v, nubins=nubins, nvbins=nvbins, sep_units='arcmin', verbose=1) ggg.process(cat) # log(<d>) != <logd>, but it should be close: #print('meanlogd1 - log(meand1) = ',ggg.meanlogd1 - numpy.log(ggg.meand1)) #print('meanlogd2 - log(meand2) = ',ggg.meanlogd2 - numpy.log(ggg.meand2)) #print('meanlogd3 - log(meand3) = ',ggg.meanlogd3 - numpy.log(ggg.meand3)) #print('meanlogd3 - meanlogd2 - log(meanu) = ',ggg.meanlogd3 - ggg.meanlogd2 - numpy.log(ggg.meanu)) #print('log(meand1-meand2) - meanlogd3 - log(meanv) = ',numpy.log(ggg.meand1-ggg.meand2) - ggg.meanlogd3 - numpy.log(numpy.abs(ggg.meanv))) numpy.testing.assert_almost_equal(ggg.meanlogd1, numpy.log(ggg.meand1), decimal=3) numpy.testing.assert_almost_equal(ggg.meanlogd2, numpy.log(ggg.meand2), decimal=3) numpy.testing.assert_almost_equal(ggg.meanlogd3, numpy.log(ggg.meand3), decimal=3) numpy.testing.assert_almost_equal(ggg.meanlogd3 - ggg.meanlogd2, numpy.log(ggg.meanu), decimal=3) numpy.testing.assert_almost_equal(numpy.log(ggg.meand1 - ggg.meand2) - ggg.meanlogd3, numpy.log(numpy.abs(ggg.meanv)), decimal=3) d1 = ggg.meand1 d2 = ggg.meand2 d3 = ggg.meand3 #print('rnom = ',numpy.exp(ggg.logr)) #print('unom = ',ggg.u) #print('vnom = ',ggg.v) #print('d1 = ',d1) #print('d2 = ',d2) #print('d3 = ',d3) # For q1,q2,q3, we can choose an orientation where c1 is at the origin, and d2 is horizontal. # Then let s be the "complex vector" from c1 to c3, which is just real. s = d2 # And let t be from c1 to c2. t = |t| e^Iphi # |t| = d3 # cos(phi) = (d2^2+d3^2-d1^2)/(2d2 d3) # |t| cos(phi) = (d2^2+d3^2-d1^2)/2d2 # |t| sin(phi) = sqrt(|t|^2 - (|t|cos(phi))^2) tx = (d2**2 + d3**2 - d1**2) / (2. * d2) ty = numpy.sqrt(d3**2 - tx**2) # As arranged, if ty > 0, points 1,2,3 are clockwise, which is negative v. # So for bins with positive v, we need to flip the direction of ty. ty[ggg.meanv > 0] *= -1. t = tx + 1j * ty q1 = (s + t) / 3. q2 = q1 - t q3 = q1 - s nq1 = numpy.abs(q1)**2 nq2 = numpy.abs(q2)**2 nq3 = numpy.abs(q3)**2 #print('q1 = ',q1) #print('q2 = ',q2) #print('q3 = ',q3) # The L^2 term in the denominator of true_zeta is the area over which the integral is done. # Since the centers of the triangles don't go to the edge of the box, we approximate the # correct area by subtracting off 2*mean(qi) from L, which should give a slightly better # estimate of the correct area to use here. (We used 2d2 for the kkk calculation, but this # is probably a slightly better estimate of the distance the triangles can get to the edges.) L = L - 2. * (q1 + q2 + q3) / 3. # Gamma0 = -2/3 gamma0^3/L^2r0^4 Pi |q1|^2 |q2|^2 |q3|^2 exp(-(|q1|^2+|q2|^2+|q3|^2)/2r0^2) true_gam0 = ((-2. * numpy.pi * gamma0**3) / (3. * L**2 * r0**4) * numpy.exp(-(nq1 + nq2 + nq3) / (2. * r0**2)) * (nq1 * nq2 * nq3)) # Gamma1 = -2/3 gamma0^3/L^2r0^4 Pi exp(-(|q1|^2+|q2|^2+|q3|^2)/2r0^2) * # ( |q1|^2 |q2|^2 |q3|^2 - 8/3 r0^2 q1^2 q2* q3* # + 8/9 r0^4 (q1^2 q2*^2 q3*^2)/(|q1|^2 |q2|^2 |q3|^2) (2q1^2-q2^2-q3^2) ) true_gam1 = ((-2. * numpy.pi * gamma0**3) / (3. * L**2 * r0**4) * numpy.exp(-(nq1 + nq2 + nq3) / (2. * r0**2)) * (nq1 * nq2 * nq3 - 8. / 3. * r0**2 * q1**2 * nq2 * nq3 / (q2 * q3) + (8. / 9. * r0**4 * (q1**2 * nq2 * nq3) / (nq1 * q2**2 * q3**2) * (2. * q1**2 - q2**2 - q3**2)))) true_gam2 = ((-2. * numpy.pi * gamma0**3) / (3. * L**2 * r0**4) * numpy.exp(-(nq1 + nq2 + nq3) / (2. * r0**2)) * (nq1 * nq2 * nq3 - 8. / 3. * r0**2 * nq1 * q2**2 * nq3 / (q1 * q3) + (8. / 9. * r0**4 * (nq1 * q2**2 * nq3) / (q1**2 * nq2 * q3**2) * (2. * q2**2 - q1**2 - q3**2)))) true_gam3 = ((-2. * numpy.pi * gamma0**3) / (3. * L**2 * r0**4) * numpy.exp(-(nq1 + nq2 + nq3) / (2. * r0**2)) * (nq1 * nq2 * nq3 - 8. / 3. * r0**2 * nq1 * nq2 * q3**2 / (q1 * q2) + (8. / 9. * r0**4 * (nq1 * nq2 * q3**2) / (q1**2 * q2**2 * nq3) * (2. * q3**2 - q1**2 - q2**2)))) #print('ntri = ',ggg.ntri) print('gam0 = ', ggg.gam0) print('true_gam0 = ', true_gam0) #print('ratio = ',ggg.gam0 / true_gam0) #print('diff = ',ggg.gam0 - true_gam0) print('max rel diff = ', numpy.max(numpy.abs((ggg.gam0 - true_gam0) / true_gam0))) # The Gamma0 term is a bit worse than the others. The accurracy improves as I increase the # number of objects, so I think it's just because of the smallish number of galaxies being # not super accurate. assert numpy.max(numpy.abs( (ggg.gam0 - true_gam0) / true_gam0)) / req_factor < 0.2 numpy.testing.assert_almost_equal( numpy.log(numpy.abs(ggg.gam0)) / 2. / req_factor, numpy.log(numpy.abs(true_gam0)) / 2. / req_factor, decimal=1) print('gam1 = ', ggg.gam1) print('true_gam1 = ', true_gam1) #print('ratio = ',ggg.gam1 / true_gam1) #print('diff = ',ggg.gam1 - true_gam1) print('max rel diff = ', numpy.max(numpy.abs((ggg.gam1 - true_gam1) / true_gam1))) assert numpy.max(numpy.abs( (ggg.gam1 - true_gam1) / true_gam1)) / req_factor < 0.1 numpy.testing.assert_almost_equal( numpy.log(numpy.abs(ggg.gam1)) / req_factor, numpy.log(numpy.abs(true_gam1)) / req_factor, decimal=1) #print('gam2 = ',ggg.gam2) #print('true_gam2 = ',true_gam2) #print('ratio = ',ggg.gam2 / true_gam2) #print('diff = ',ggg.gam2 - true_gam2) #print('max rel diff = ',numpy.max(numpy.abs((ggg.gam2 - true_gam2)/true_gam2))) assert numpy.max(numpy.abs( (ggg.gam2 - true_gam2) / true_gam2)) / req_factor < 0.1 numpy.testing.assert_almost_equal( numpy.log(numpy.abs(ggg.gam2)) / req_factor, numpy.log(numpy.abs(true_gam2)) / req_factor, decimal=1) #print('gam3 = ',ggg.gam3) #print('true_gam3 = ',true_gam3) #print('ratio = ',ggg.gam3 / true_gam3) #print('diff = ',ggg.gam3 - true_gam3) #print('max rel diff = ',numpy.max(numpy.abs((ggg.gam3 - true_gam3)/true_gam3))) assert numpy.max(numpy.abs( (ggg.gam3 - true_gam3) / true_gam3)) / req_factor < 0.1 numpy.testing.assert_almost_equal( numpy.log(numpy.abs(ggg.gam3)) / req_factor, numpy.log(numpy.abs(true_gam3)) / req_factor, decimal=1) # Check that we get the same result using the corr3 executable: if __name__ == '__main__': cat.write(os.path.join('data', 'ggg_data.dat')) import subprocess corr3_exe = get_script_name('corr3') p = subprocess.Popen([corr3_exe, "ggg.yaml"]) p.communicate() corr3_output = numpy.genfromtxt(os.path.join('output', 'ggg.out'), names=True) #print('gam0r = ',ggg.gam0.real) #print('from corr3 output = ',corr3_output['gam0r']) #print('ratio = ',corr3_output['gam0r']/ggg.gam0r.flatten()) #print('diff = ',corr3_output['gam0r']-ggg.gam0r.flatten()) numpy.testing.assert_almost_equal(corr3_output['gam0r'] / ggg.gam0r.flatten(), 1., decimal=3) #print('gam0i = ',ggg.gam0.imag) #print('from corr3 output = ',corr3_output['gam0i']) #print('ratio = ',corr3_output['gam0i']/ggg.gam0i.flatten()) #print('diff = ',corr3_output['gam0i']-ggg.gam0i.flatten()) numpy.testing.assert_almost_equal(corr3_output['gam0i'] / ggg.gam0i.flatten(), 1., decimal=3) #print('gam1r = ',ggg.gam1.real) #print('from corr3 output = ',corr3_output['gam1r']) #print('ratio = ',corr3_output['gam1r']/ggg.gam1r.flatten()) #print('diff = ',corr3_output['gam1r']-ggg.gam1r.flatten()) numpy.testing.assert_almost_equal(corr3_output['gam1r'] / ggg.gam1r.flatten(), 1., decimal=3) #print('gam1i = ',ggg.gam1.imag) #print('from corr3 output = ',corr3_output['gam1i']) #print('ratio = ',corr3_output['gam1i']/ggg.gam1i.flatten()) #print('diff = ',corr3_output['gam1i']-ggg.gam1i.flatten()) numpy.testing.assert_almost_equal(corr3_output['gam1i'] / ggg.gam1i.flatten(), 1., decimal=3) #print('gam2r = ',ggg.gam2.real) #print('from corr3 output = ',corr3_output['gam2r']) #print('ratio = ',corr3_output['gam2r']/ggg.gam2r.flatten()) #print('diff = ',corr3_output['gam2r']-ggg.gam2r.flatten()) numpy.testing.assert_almost_equal(corr3_output['gam2r'] / ggg.gam2r.flatten(), 1., decimal=3) #print('gam2i = ',ggg.gam2.imag) #print('from corr3 output = ',corr3_output['gam2i']) #print('ratio = ',corr3_output['gam2i']/ggg.gam2i.flatten()) #print('diff = ',corr3_output['gam2i']-ggg.gam2i.flatten()) numpy.testing.assert_almost_equal(corr3_output['gam2i'] / ggg.gam2i.flatten(), 1., decimal=3) #print('gam3r = ',ggg.gam3.real) #print('from corr3 output = ',corr3_output['gam3r']) #print('ratio = ',corr3_output['gam3r']/ggg.gam3r.flatten()) #print('diff = ',corr3_output['gam3r']-ggg.gam3r.flatten()) numpy.testing.assert_almost_equal(corr3_output['gam3r'] / ggg.gam3r.flatten(), 1., decimal=3) #print('gam3i = ',ggg.gam3.imag) #print('from corr3 output = ',corr3_output['gam3i']) #print('ratio = ',corr3_output['gam3i']/ggg.gam3i.flatten()) #print('diff = ',corr3_output['gam3i']-ggg.gam3i.flatten()) numpy.testing.assert_almost_equal(corr3_output['gam3i'] / ggg.gam3i.flatten(), 1., decimal=3) # Check the fits write option out_file_name1 = os.path.join('output', 'ggg_out1.fits') ggg.write(out_file_name1) data = fitsio.read(out_file_name1) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(ggg.logr).flatten()) numpy.testing.assert_almost_equal(data['u_nom'], ggg.u.flatten()) numpy.testing.assert_almost_equal(data['v_nom'], ggg.v.flatten()) numpy.testing.assert_almost_equal(data['meand1'], ggg.meand1.flatten()) numpy.testing.assert_almost_equal(data['meanlogd1'], ggg.meanlogd1.flatten()) numpy.testing.assert_almost_equal(data['meand2'], ggg.meand2.flatten()) numpy.testing.assert_almost_equal(data['meanlogd2'], ggg.meanlogd2.flatten()) numpy.testing.assert_almost_equal(data['meand3'], ggg.meand3.flatten()) numpy.testing.assert_almost_equal(data['meanlogd3'], ggg.meanlogd3.flatten()) numpy.testing.assert_almost_equal(data['meanu'], ggg.meanu.flatten()) numpy.testing.assert_almost_equal(data['meanv'], ggg.meanv.flatten()) numpy.testing.assert_almost_equal(data['gam0r'], ggg.gam0.real.flatten()) numpy.testing.assert_almost_equal(data['gam1r'], ggg.gam1.real.flatten()) numpy.testing.assert_almost_equal(data['gam2r'], ggg.gam2.real.flatten()) numpy.testing.assert_almost_equal(data['gam3r'], ggg.gam3.real.flatten()) numpy.testing.assert_almost_equal(data['gam0i'], ggg.gam0.imag.flatten()) numpy.testing.assert_almost_equal(data['gam1i'], ggg.gam1.imag.flatten()) numpy.testing.assert_almost_equal(data['gam2i'], ggg.gam2.imag.flatten()) numpy.testing.assert_almost_equal(data['gam3i'], ggg.gam3.imag.flatten()) numpy.testing.assert_almost_equal(data['sigma_gam'], numpy.sqrt(ggg.vargam.flatten())) numpy.testing.assert_almost_equal(data['weight'], ggg.weight.flatten()) numpy.testing.assert_almost_equal(data['ntri'], ggg.ntri.flatten()) # Check the read function # Note: These don't need the flatten. The read function should reshape them to the right shape. ggg2 = treecorr.GGGCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins, min_u=min_u, max_u=max_u, min_v=min_v, max_v=max_v, nubins=nubins, nvbins=nvbins, sep_units='arcmin', verbose=1) ggg2.read(out_file_name1) numpy.testing.assert_almost_equal(ggg2.logr, ggg.logr) numpy.testing.assert_almost_equal(ggg2.u, ggg.u) numpy.testing.assert_almost_equal(ggg2.v, ggg.v) numpy.testing.assert_almost_equal(ggg2.meand1, ggg.meand1) numpy.testing.assert_almost_equal(ggg2.meanlogd1, ggg.meanlogd1) numpy.testing.assert_almost_equal(ggg2.meand2, ggg.meand2) numpy.testing.assert_almost_equal(ggg2.meanlogd2, ggg.meanlogd2) numpy.testing.assert_almost_equal(ggg2.meand3, ggg.meand3) numpy.testing.assert_almost_equal(ggg2.meanlogd3, ggg.meanlogd3) numpy.testing.assert_almost_equal(ggg2.meanu, ggg.meanu) numpy.testing.assert_almost_equal(ggg2.meanv, ggg.meanv) numpy.testing.assert_almost_equal(ggg2.gam0, ggg.gam0) numpy.testing.assert_almost_equal(ggg2.gam1, ggg.gam1) numpy.testing.assert_almost_equal(ggg2.gam2, ggg.gam2) numpy.testing.assert_almost_equal(ggg2.gam3, ggg.gam3) numpy.testing.assert_almost_equal(ggg2.vargam, ggg.vargam) numpy.testing.assert_almost_equal(ggg2.weight, ggg.weight) numpy.testing.assert_almost_equal(ggg2.ntri, ggg.ntri)
def test_kkk(): # Use kappa(r) = A exp(-r^2/2s^2) # # The Fourier transform is: kappa~(k) = 2 pi A s^2 exp(-s^2 k^2/2) / L^2 # # B(k1,k2) = <k~(k1) k~(k2) k~(-k1-k2)> # = (2 pi A (s/L)^2)^3 exp(-s^2 (|k1|^2 + |k2|^2 - k1.k2)) # = (2 pi A (s/L)^2)^3 exp(-s^2 (|k1|^2 + |k2|^2 + |k3|^2)/2) # # zeta(r1,r2) = (1/2pi)^4 int(d^2k1 int(d^2k2 exp(ik1.x1) exp(ik2.x2) B(k1,k2) )) # = 2/3 pi A^3 (s/L)^2 exp(-(x1^2 + y1^2 + x2^2 + y2^2 - x1x2 - y1y2)/3s^2) # = 2/3 pi A^3 (s/L)^2 exp(-(d1^2 + d2^2 + d3^2)/6s^2) A = 0.05 s = 10. if __name__ == '__main__': ngal = 200000 L = 30. * s # Not infinity, so this introduces some error. Our integrals were to infinity. req_factor = 1 else: # Looser tests from nosetests that don't take so long to run. ngal = 5000 L = 10. * s req_factor = 5 numpy.random.seed(8675309) x = (numpy.random.random_sample(ngal)-0.5) * L y = (numpy.random.random_sample(ngal)-0.5) * L r2 = (x**2 + y**2)/s**2 kappa = A * numpy.exp(-r2/2.) min_sep = 11. max_sep = 15. nbins = 3 min_u = 0.7 max_u = 1.0 nubins = 3 min_v = -0.1 max_v = 0.3 nvbins = 4 cat = treecorr.Catalog(x=x, y=y, k=kappa, x_units='arcmin', y_units='arcmin') kkk = treecorr.KKKCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins, min_u=min_u, max_u=max_u, min_v=min_v, max_v=max_v, nubins=nubins, nvbins=nvbins, sep_units='arcmin', verbose=1) kkk.process(cat) # log(<d>) != <logd>, but it should be close: #print('meanlogd1 - log(meand1) = ',kkk.meanlogd1 - numpy.log(kkk.meand1)) #print('meanlogd2 - log(meand2) = ',kkk.meanlogd2 - numpy.log(kkk.meand2)) #print('meanlogd3 - log(meand3) = ',kkk.meanlogd3 - numpy.log(kkk.meand3)) #print('meanlogd3 - meanlogd2 - log(meanu) = ',kkk.meanlogd3 - kkk.meanlogd2 - numpy.log(kkk.meanu)) #print('log(meand1-meand2) - meanlogd3 - log(meanv) = ',numpy.log(kkk.meand1-kkk.meand2) - kkk.meanlogd3 - numpy.log(numpy.abs(kkk.meanv))) numpy.testing.assert_almost_equal(kkk.meanlogd1, numpy.log(kkk.meand1), decimal=3) numpy.testing.assert_almost_equal(kkk.meanlogd2, numpy.log(kkk.meand2), decimal=3) numpy.testing.assert_almost_equal(kkk.meanlogd3, numpy.log(kkk.meand3), decimal=3) numpy.testing.assert_almost_equal(kkk.meanlogd3-kkk.meanlogd2, numpy.log(kkk.meanu), decimal=3) numpy.testing.assert_almost_equal(numpy.log(kkk.meand1-kkk.meand2)-kkk.meanlogd3, numpy.log(numpy.abs(kkk.meanv)), decimal=3) d1 = kkk.meand1 d2 = kkk.meand2 d3 = kkk.meand3 #print('rnom = ',numpy.exp(kkk.logr)) #print('unom = ',kkk.u) #print('vnom = ',kkk.v) #print('d1 = ',d1) #print('d2 = ',d2) #print('d3 = ',d3) # The L^2 term in the denominator of true_zeta is the area over which the integral is done. # Since the centers of the triangles don't go to the edge of the box, we approximate the # correct area by subtracting off 2d2 from L, which should give a slightly better estimate # of the correct area to use here. L = L - 2.*d2 true_zeta = (2.*numpy.pi/3) * A**3 * (s/L)**2 * numpy.exp(-(d1**2+d2**2+d3**2)/(6.*s**2)) #print('ntri = ',kkk.ntri) print('zeta = ',kkk.zeta) print('true_zeta = ',true_zeta) #print('ratio = ',kkk.zeta / true_zeta) #print('diff = ',kkk.zeta - true_zeta) print('max rel diff = ',numpy.max(numpy.abs((kkk.zeta - true_zeta)/true_zeta))) assert numpy.max(numpy.abs((kkk.zeta - true_zeta)/true_zeta)) / req_factor < 0.1 numpy.testing.assert_almost_equal(numpy.log(numpy.abs(kkk.zeta)) / req_factor, numpy.log(numpy.abs(true_zeta)) / req_factor, decimal=1) # Check that we get the same result using the corr3 executable: if __name__ == '__main__': cat.write(os.path.join('data','kkk_data.dat')) import subprocess corr3_exe = get_script_name('corr3') p = subprocess.Popen( [corr3_exe,"kkk.yaml"] ) p.communicate() corr3_output = numpy.genfromtxt(os.path.join('output','kkk.out'), names=True) #print('zeta = ',kkk.zeta) #print('from corr3 output = ',corr3_output['zeta']) #print('ratio = ',corr3_output['zeta']/kkk.zeta.flatten()) #print('diff = ',corr3_output['zeta']-kkk.zeta.flatten()) numpy.testing.assert_almost_equal(corr3_output['zeta']/kkk.zeta.flatten(), 1., decimal=3) # Check the fits write option out_file_name = os.path.join('output','kkk_out.fits') kkk.write(out_file_name) data = fitsio.read(out_file_name) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(kkk.logr).flatten()) numpy.testing.assert_almost_equal(data['u_nom'], kkk.u.flatten()) numpy.testing.assert_almost_equal(data['v_nom'], kkk.v.flatten()) numpy.testing.assert_almost_equal(data['meand1'], kkk.meand1.flatten()) numpy.testing.assert_almost_equal(data['meanlogd1'], kkk.meanlogd1.flatten()) numpy.testing.assert_almost_equal(data['meand2'], kkk.meand2.flatten()) numpy.testing.assert_almost_equal(data['meanlogd2'], kkk.meanlogd2.flatten()) numpy.testing.assert_almost_equal(data['meand3'], kkk.meand3.flatten()) numpy.testing.assert_almost_equal(data['meanlogd3'], kkk.meanlogd3.flatten()) numpy.testing.assert_almost_equal(data['meanu'], kkk.meanu.flatten()) numpy.testing.assert_almost_equal(data['meanv'], kkk.meanv.flatten()) numpy.testing.assert_almost_equal(data['zeta'], kkk.zeta.flatten()) numpy.testing.assert_almost_equal(data['sigma_zeta'], numpy.sqrt(kkk.varzeta.flatten())) numpy.testing.assert_almost_equal(data['weight'], kkk.weight.flatten()) numpy.testing.assert_almost_equal(data['ntri'], kkk.ntri.flatten()) # Check the read function # Note: These don't need the flatten. The read function should reshape them to the right shape. kkk2 = treecorr.KKKCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins, min_u=min_u, max_u=max_u, min_v=min_v, max_v=max_v, nubins=nubins, nvbins=nvbins, sep_units='arcmin', verbose=1) kkk2.read(out_file_name) numpy.testing.assert_almost_equal(kkk2.logr, kkk.logr) numpy.testing.assert_almost_equal(kkk2.u, kkk.u) numpy.testing.assert_almost_equal(kkk2.v, kkk.v) numpy.testing.assert_almost_equal(kkk2.meand1, kkk.meand1) numpy.testing.assert_almost_equal(kkk2.meanlogd1, kkk.meanlogd1) numpy.testing.assert_almost_equal(kkk2.meand2, kkk.meand2) numpy.testing.assert_almost_equal(kkk2.meanlogd2, kkk.meanlogd2) numpy.testing.assert_almost_equal(kkk2.meand3, kkk.meand3) numpy.testing.assert_almost_equal(kkk2.meanlogd3, kkk.meanlogd3) numpy.testing.assert_almost_equal(kkk2.meanu, kkk.meanu) numpy.testing.assert_almost_equal(kkk2.meanv, kkk.meanv) numpy.testing.assert_almost_equal(kkk2.zeta, kkk.zeta) numpy.testing.assert_almost_equal(kkk2.varzeta, kkk.varzeta) numpy.testing.assert_almost_equal(kkk2.weight, kkk.weight) numpy.testing.assert_almost_equal(kkk2.ntri, kkk.ntri)
def test_des_image(): """Test the whole process with a DES CCD. """ import os import fitsio image_file = 'y1_test/DECam_00241238_01.fits.fz' cat_file = 'y1_test/DECam_00241238_01_psfcat_tb_maxmag_17.0_magcut_3.0_findstars.fits' orig_image = galsim.fits.read(image_file) psf_file = os.path.join('output','pixel_des_psf.fits') if __name__ == '__main__': # These match what Gary used in fit_des.py nstars = None scale = 0.15 size = 41 else: # These are faster and good enough for the unit tests. nstars = 25 scale = 0.2 size = 21 np.random.seed(1234) stamp_size = 51 # The configuration dict with the right input fields for the file we're using. start_sigma = 1.0/2.355 # TODO: Need to make this automatic somehow. config = { 'input' : { 'images' : image_file, 'image_hdu' : 1, 'weight_hdu' : 3, 'badpix_hdu' : 2, 'cats' : cat_file, 'cat_hdu' : 2, 'x_col' : 'XWIN_IMAGE', 'y_col' : 'YWIN_IMAGE', 'sky_col' : 'BACKGROUND', 'stamp_size' : stamp_size, 'ra' : 'TELRA', 'dec' : 'TELDEC', 'gain' : 'GAINA', }, 'output' : { 'file_name' : psf_file, }, 'psf' : { 'model' : { 'type' : 'PixelGrid', 'scale' : scale, 'size' : size, 'start_sigma' : start_sigma, }, 'interp' : { 'type' : 'BasisPolynomial', 'order' : 2, }, }, } if __name__ == '__main__': config['verbose'] = 3 # These tests are slow, and it's really just doing the same thing three times, so # only do the first one when running via nosetests. if True: # Start by doing things manually: if __name__ == '__main__': logger = piff.config.setup_logger(2) else: logger = None # Largely copied from Gary's fit_des.py, but using the Piff input_handler to # read the input files. stars, wcs, pointing = piff.Input.process(config['input'], logger=logger) if nstars is not None: stars = stars[:nstars] # Make model, force PSF centering model = piff.PixelGrid(scale=scale, size=size, interp=piff.Lanczos(3), force_model_center=True, start_sigma=start_sigma, logger=logger) # Interpolator will be zero-order polynomial. # Find u, v ranges interp = piff.BasisPolynomial(order=2, logger=logger) # Make a psf psf = piff.SimplePSF(model, interp) psf.fit(stars, wcs, pointing, logger=logger) # The difference between the images of the fitted stars and the originals should be # consistent with noise. Keep track of how many don't meet that goal. n_bad = 0 # chisq/dof > 2 n_marginal = 0 # chisq/dof > 1.1 n_good = 0 # chisq/dof <= 1.1 # Note: The 2 and 1.1 values here are very arbitrary! for s in psf.stars: fitted = psf.drawStar(s) orig_stamp = orig_image[fitted.image.bounds] - s['sky'] fit_stamp = fitted.image x0 = int(s['x']+0.5) y0 = int(s['y']+0.5) b = galsim.BoundsI(x0-3,x0+3,y0-3,y0+3) #print('orig center = ',orig_stamp[b].array) #print('flux = ',orig_stamp.array.sum()) #print('fit center = ',fit_stamp[b].array) #print('flux = ',fit_stamp.array.sum()) flux = fitted.fit.flux #print('max diff/flux = ',np.max(np.abs(orig_stamp.array-fit_stamp.array))/flux) #np.testing.assert_almost_equal(fit_stamp.array/flux, orig_stamp.array/flux, decimal=2) weight = s.weight # These should be 1/var_pix resid = fit_stamp - orig_stamp chisq = np.sum(resid.array**2 * weight.array) print('chisq = ',chisq) print('cf. star.chisq, dof = ',s.fit.chisq, s.fit.dof) assert abs(chisq - s.fit.chisq) < 1.e-3 * chisq if chisq > 2. * s.fit.dof: n_bad += 1 elif chisq > 1.1 * s.fit.dof: n_marginal += 1 else: n_good += 1 # Check the convenience function that an end user would typically use offset = s.center_to_offset(s.fit.center) image = psf.draw(x=s['x'], y=s['y'], stamp_size=stamp_size, flux=s.fit.flux, offset=offset) np.testing.assert_almost_equal(image.array, fit_stamp.array, decimal=4) print('n_good, marginal, bad = ',n_good,n_marginal,n_bad) # The real counts are 10 and 2. So this says make sure any updates to the code don't make # things much worse. assert n_marginal <= 12 assert n_bad <= 3 # Use piffify function if __name__ == '__main__': print('start piffify') piff.piffify(config) print('read stars') stars, wcs, pointing = piff.Input.process(config['input']) print('read psf') psf = piff.read(psf_file) stars = [psf.model.initialize(s) for s in stars] flux = stars[0].fit.flux offset = stars[0].center_to_offset(stars[0].fit.center) fit_stamp = psf.draw(x=stars[0]['x'], y=stars[0]['y'], stamp_size=stamp_size, flux=flux, offset=offset) orig_stamp = orig_image[stars[0].image.bounds] - stars[0]['sky'] # The first star happens to be a good one, so go ahead and test the arrays directly. np.testing.assert_almost_equal(fit_stamp.array/flux, orig_stamp.array/flux, decimal=2) # Test using the piffify executable with open('pixel_des.yaml','w') as f: f.write(yaml.dump(config, default_flow_style=False)) if __name__ == '__main__': if os.path.exists(psf_file): os.remove(psf_file) piffify_exe = get_script_name('piffify') print('start piffify executable') p = subprocess.Popen( [piffify_exe, 'pixel_des.yaml'] ) p.communicate() print('read stars') stars, wcs, pointing = piff.Input.process(config['input']) print('read psf') psf = piff.read(psf_file) stars = [psf.model.initialize(s) for s in stars] flux = stars[0].fit.flux offset = stars[0].center_to_offset(stars[0].fit.center) fit_stamp = psf.draw(x=stars[0]['x'], y=stars[0]['y'], stamp_size=stamp_size, flux=flux, offset=offset) orig_stamp = orig_image[stars[0].image.bounds] - stars[0]['sky'] np.testing.assert_almost_equal(fit_stamp.array/flux, orig_stamp.array/flux, decimal=2)
def test_single_image(): """Test the whole process with a single image. Note: This test is based heavily on test_single_image in test_simple.py. """ import os import fitsio np.random.seed(1234) # Make the image image = galsim.Image(2048, 2048, scale=0.2) # The (x,y) values will be on a grid 5 x 5 stars with a random sub-pixel offset. xvals = np.linspace(50., 1950., 5) yvals = np.linspace(50., 1950., 5) x_list, y_list = np.meshgrid(xvals, yvals) x_list = x_list.flatten() y_list = y_list.flatten() x_list = x_list + (np.random.rand(len(x_list)) - 0.5) y_list = y_list + (np.random.rand(len(x_list)) - 0.5) print('x_list = ',x_list) print('y_list = ',y_list) # Range of fluxes from 100 to 15000 flux_list = 100. * np.exp(5. * np.random.rand(len(x_list))) print('fluxes range from ',np.min(flux_list),np.max(flux_list)) # Draw a Moffat PSF at each location on the image. # Have the truth values vary quadratically across the image. beta_fn = lambda x,y: 3.5 - 0.1*(x/1000) + 0.08*(y/1000)**2 fwhm_fn = lambda x,y: 0.9 + 0.05*(x/1000) - 0.03*(y/1000) + 0.02*(x/1000)*(y/1000) e1_fn = lambda x,y: 0.02 - 0.01*(x/1000) e2_fn = lambda x,y: -0.03 + 0.02*(x/1000)**2 - 0.01*(y/1000)*2 for x,y,flux in zip(x_list, y_list, flux_list): beta = beta_fn(x,y) fwhm = fwhm_fn(x,y) e1 = e1_fn(x,y) e2 = e2_fn(x,y) print(x,y,beta,fwhm,e1,e2) moffat = galsim.Moffat(fwhm=fwhm, beta=beta, flux=flux).shear(e1=e1, e2=e2) bounds = galsim.BoundsI(int(x-31), int(x+32), int(y-31), int(y+32)) offset = galsim.PositionD( x-int(x)-0.5 , y-int(y)-0.5 ) moffat.drawImage(image=image[bounds], offset=offset, method='no_pixel') print('drew image') # Write out the image to a file image_file = os.path.join('data','pixel_moffat_image.fits') image.write(image_file) print('wrote image') # Write out the catalog to a file dtype = [ ('x','f8'), ('y','f8') ] data = np.empty(len(x_list), dtype=dtype) data['x'] = x_list data['y'] = y_list cat_file = os.path.join('data','pixel_moffat_cat.fits') fitsio.write(cat_file, data, clobber=True) print('wrote catalog') # Use InputFiles to read these back in input = piff.InputFiles(image_file, cat_file, stamp_size=32) assert input.image_files == [ image_file ] assert input.cat_files == [ cat_file ] assert input.x_col == 'x' assert input.y_col == 'y' # Check image input.readImages() assert len(input.images) == 1 np.testing.assert_equal(input.images[0].array, image.array) # Check catalog input.readStarCatalogs() assert len(input.cats) == 1 np.testing.assert_equal(input.cats[0]['x'], x_list) np.testing.assert_equal(input.cats[0]['y'], y_list) # Make stars orig_stars = input.makeStars() assert len(orig_stars) == len(x_list) assert orig_stars[0].image.array.shape == (32,32) # Make a test star, not at the location of any of the model stars to use for each of the # below tests. x0 = 1024 # Some random position, not where a star was originally. y0 = 133 beta = beta_fn(x0,y0) fwhm = fwhm_fn(x0,y0) e1 = e1_fn(x0,y0) e2 = e2_fn(x0,y0) moffat = galsim.Moffat(fwhm=fwhm, beta=beta).shear(e1=e1, e2=e2) target_star = piff.Star.makeTarget(x=x0, y=y0, scale=image.scale) test_im = galsim.ImageD(bounds=target_star.image.bounds, scale=image.scale) moffat.drawImage(image=test_im, method='no_pixel', use_true_center=False) print('made test star') # These tests are slow, and it's really just doing the same thing three times, so # only do the first one when running via nosetests. if True: # Process the star data model = piff.PixelGrid(0.2, 16, start_sigma=0.9/2.355) interp = piff.BasisPolynomial(order=2) if __name__ == '__main__': logger = piff.config.setup_logger(2) else: logger = None pointing = None # wcs is not Celestial here, so pointing needs to be None. psf = piff.SimplePSF(model, interp) psf.fit(orig_stars, {0:input.images[0].wcs}, pointing, logger=logger) # Check that the interpolation is what it should be print('target.flux = ',target_star.fit.flux) test_star = psf.drawStar(target_star) #print('test_im center = ',test_im[b].array) #print('flux = ',test_im.array.sum()) #print('interp_im center = ',test_star.image[b].array) #print('flux = ',test_star.image.array.sum()) #print('max diff = ',np.max(np.abs(test_star.image.array-test_im.array))) np.testing.assert_almost_equal(test_star.image.array, test_im.array, decimal=3) # Check the convenience function that an end user would typically use image = psf.draw(x=x0, y=y0) np.testing.assert_almost_equal(image.array, test_im.array, decimal=3) # Round trip through a file psf_file = os.path.join('output','pixel_psf.fits') psf.write(psf_file, logger) psf = piff.read(psf_file, logger) assert type(psf.model) is piff.PixelGrid assert type(psf.interp) is piff.BasisPolynomial test_star = psf.drawStar(target_star) np.testing.assert_almost_equal(test_star.image.array, test_im.array, decimal=3) # Check the convenience function that an end user would typically use image = psf.draw(x=x0, y=y0) np.testing.assert_almost_equal(image.array, test_im.array, decimal=3) # Do the whole thing with the config parser config = { 'input' : { 'images' : image_file, 'cats' : cat_file, 'x_col' : 'x', 'y_col' : 'y', 'stamp_size' : 48 # Bigger than we drew, but should still work. }, 'output' : { 'file_name' : psf_file }, 'psf' : { 'model' : { 'type' : 'PixelGrid', 'scale' : 0.2, 'size' : 16, # Much smaller than the input stamps, but this is plenty here. 'start_sigma' : 0.9/2.355 }, 'interp' : { 'type' : 'BasisPolynomial', 'order' : 2 }, }, } if __name__ == '__main__': print("Running piffify function") piff.piffify(config) psf = piff.read(psf_file) test_star = psf.drawStar(target_star) np.testing.assert_almost_equal(test_star.image.array, test_im.array, decimal=3) # Test using the piffify executable with open('pixel_moffat.yaml','w') as f: f.write(yaml.dump(config, default_flow_style=False)) if __name__ == '__main__': print("Running piffify executable") if os.path.exists(psf_file): os.remove(psf_file) piffify_exe = get_script_name('piffify') p = subprocess.Popen( [piffify_exe, 'pixel_moffat.yaml'] ) p.communicate() psf = piff.read(psf_file) test_star = psf.drawStar(target_star) np.testing.assert_almost_equal(test_star.image.array, test_im.array, decimal=3)
def test_kk(): # cf. http://adsabs.harvard.edu/abs/2002A%26A...389..729S for the basic formulae I use here. # # Use kappa(r) = A exp(-r^2/2s^2) # # The Fourier transform is: kappa~(k) = 2 pi A s^2 exp(-s^2 k^2/2) / L^2 # P(k) = (1/2pi) <|kappa~(k)|^2> = 2 pi A^2 (s/L)^4 exp(-s^2 k^2) # xi(r) = (1/2pi) int( dk k P(k) J0(kr) ) # = pi A^2 (s/L)^2 exp(-r^2/2s^2/4) # Note: I'm not sure I handled the L factors correctly, but the units at the end need # to be kappa^2, so it needs to be (s/L)^2. ngal = 1000000 A = 0.05 s = 10. L = 30. * s # Not infinity, so this introduces some error. Our integrals were to infinity. numpy.random.seed(8675309) x = (numpy.random.random_sample(ngal)-0.5) * L y = (numpy.random.random_sample(ngal)-0.5) * L r2 = (x**2 + y**2)/s**2 kappa = A * numpy.exp(-r2/2.) cat = treecorr.Catalog(x=x, y=y, k=kappa, x_units='arcmin', y_units='arcmin') kk = treecorr.KKCorrelation(bin_size=0.1, min_sep=1., max_sep=50., sep_units='arcmin', verbose=1) kk.process(cat) # log(<R>) != <logR>, but it should be close: print('meanlogr - log(meanr) = ',kk.meanlogr - numpy.log(kk.meanr)) numpy.testing.assert_almost_equal(kk.meanlogr, numpy.log(kk.meanr), decimal=3) r = kk.meanr true_xi = numpy.pi * A**2 * (s/L)**2 * numpy.exp(-0.25*r**2/s**2) print('kk.xi = ',kk.xi) print('true_xi = ',true_xi) print('ratio = ',kk.xi / true_xi) print('diff = ',kk.xi - true_xi) print('max diff = ',max(abs(kk.xi - true_xi))) assert max(abs(kk.xi - true_xi)) < 5.e-7 # It should also work as a cross-correlation of this cat with itself kk.process(cat,cat) numpy.testing.assert_almost_equal(kk.meanlogr, numpy.log(kk.meanr), decimal=3) assert max(abs(kk.xi - true_xi)) < 5.e-7 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': cat.write(os.path.join('data','kk.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"kk.yaml"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','kk.out'), names=True) print('kk.xi = ',kk.xi) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/kk.xi) print('diff = ',corr2_output['xi']-kk.xi) numpy.testing.assert_almost_equal(corr2_output['xi']/kk.xi, 1., decimal=3) # Check the fits write option out_file_name = os.path.join('output','kk_out.fits') kk.write(out_file_name) data = fitsio.read(out_file_name) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(kk.logr)) numpy.testing.assert_almost_equal(data['meanR'], kk.meanr) numpy.testing.assert_almost_equal(data['meanlogR'], kk.meanlogr) numpy.testing.assert_almost_equal(data['xi'], kk.xi) numpy.testing.assert_almost_equal(data['sigma_xi'], numpy.sqrt(kk.varxi)) numpy.testing.assert_almost_equal(data['weight'], kk.weight) numpy.testing.assert_almost_equal(data['npairs'], kk.npairs) # Check the read function kk2 = treecorr.KKCorrelation(bin_size=0.1, min_sep=1., max_sep=100., sep_units='arcmin') kk2.read(out_file_name) numpy.testing.assert_almost_equal(kk2.logr, kk.logr) numpy.testing.assert_almost_equal(kk2.meanr, kk.meanr) numpy.testing.assert_almost_equal(kk2.meanlogr, kk.meanlogr) numpy.testing.assert_almost_equal(kk2.xi, kk.xi) numpy.testing.assert_almost_equal(kk2.varxi, kk.varxi) numpy.testing.assert_almost_equal(kk2.weight, kk.weight) numpy.testing.assert_almost_equal(kk2.npairs, kk.npairs)
def test_single_image(): """Test the simple case of one image and one catalog. """ # Make the image image = galsim.Image(2048, 2048, scale=0.26) # Where to put the stars. Include some flagged and not used locations. x_list = [ 123.12, 345.98, 567.25, 1094.94, 924.15, 1532.74, 1743.11, 888.39, 1033.29, 1409.31 ] y_list = [ 345.43, 567.45, 1094.32, 924.29, 1532.92, 1743.83, 888.83, 1033.19, 1409.20, 123.11 ] flag_list = [ 0, 0, 12, 0, 0, 1, 0, 0, 0, 0 ] use_list = [ 1, 1, 1, 1, 1, 0, 1, 1, 0, 1 ] # Draw a Gaussian PSF at each location on the image. sigma = 1.3 g1 = 0.23 g2 = -0.17 psf = galsim.Gaussian(sigma=sigma).shear(g1=g1, g2=g2) for x,y,flag,use in zip(x_list, y_list, flag_list, use_list): bounds = galsim.BoundsI(int(x-31), int(x+32), int(y-31), int(y+32)) offset = galsim.PositionD( x-int(x)-0.5 , y-int(y)-0.5 ) psf.drawImage(image=image[bounds], method='no_pixel', offset=offset) # corrupt the ones that are marked as flagged if flag: print('corrupting star at ',x,y) ar = image[bounds].array im_max = np.max(ar) * 0.2 ar[ar > im_max] = im_max # Write out the image to a file image_file = os.path.join('data','simple_image.fits') image.write(image_file) # Write out the catalog to a file dtype = [ ('x','f8'), ('y','f8'), ('flag','i2'), ('use','i2') ] data = np.empty(len(x_list), dtype=dtype) data['x'] = x_list data['y'] = y_list data['flag'] = flag_list data['use'] = use_list cat_file = os.path.join('data','simple_cat.fits') fitsio.write(cat_file, data, clobber=True) # Use InputFiles to read these back in input = piff.InputFiles(image_file, cat_file) assert input.image_files == [ image_file ] assert input.cat_files == [ cat_file ] assert input.x_col == 'x' assert input.y_col == 'y' # Check image input.readImages() assert len(input.images) == 1 np.testing.assert_equal(input.images[0].array, image.array) # Check catalog input.readStarCatalogs() assert len(input.cats) == 1 np.testing.assert_equal(input.cats[0]['x'], x_list) np.testing.assert_equal(input.cats[0]['y'], y_list) # Repeat, using flag and use columns this time. input = piff.InputFiles(image_file, cat_file, flag_col='flag', use_col='use', stamp_size=48) assert input.flag_col == 'flag' assert input.use_col == 'use' input.readImages() input.readStarCatalogs() assert len(input.cats[0]) == 7 # Make star data orig_stars = input.makeStars() assert len(orig_stars) == 7 assert orig_stars[0].image.array.shape == (48,48) # Process the star data model = piff.Gaussian() interp = piff.Mean() fitted_stars = [ model.fit(star) for star in orig_stars ] interp.solve(fitted_stars) print('mean = ',interp.mean) # Check that the interpolation is what it should be target = piff.Star.makeTarget(x=1024, y=123) # Any position would work here. true_params = [ sigma, g1, g2 ] test_star = interp.interpolate(target) np.testing.assert_almost_equal(test_star.fit.params, true_params, decimal=5) # Now test running it via the config parser psf_file = os.path.join('output','simple_psf.fits') config = { 'input' : { 'images' : image_file, 'cats' : cat_file, 'flag_col' : 'flag', 'use_col' : 'use', 'stamp_size' : 48 }, 'psf' : { 'model' : { 'type' : 'Gaussian' }, 'interp' : { 'type' : 'Mean' }, }, 'output' : { 'file_name' : psf_file }, } if __name__ == '__main__': logger = piff.config.setup_logger(verbose=3) else: logger = piff.config.setup_logger(verbose=1) orig_stars, wcs, pointing = piff.Input.process(config['input'], logger) # Use a SimplePSF to process the stars data this time. psf = piff.SimplePSF(model, interp) psf.fit(orig_stars, wcs, pointing, logger=logger) test_star = psf.interp.interpolate(target) np.testing.assert_almost_equal(test_star.fit.params, true_params, decimal=5) # Round trip to a file psf.write(psf_file, logger) psf = piff.read(psf_file, logger) assert type(psf.model) is piff.Gaussian assert type(psf.interp) is piff.Mean test_star = psf.interp.interpolate(target) np.testing.assert_almost_equal(test_star.fit.params, true_params, decimal=5) # Do the whole thing with the config parser os.remove(psf_file) piff.piffify(config, logger) psf = piff.read(psf_file) test_star = psf.interp.interpolate(target) np.testing.assert_almost_equal(test_star.fit.params, true_params, decimal=5) # Test using the piffify executable os.remove(psf_file) with open('simple.yaml','w') as f: f.write(yaml.dump(config, default_flow_style=False)) piffify_exe = get_script_name('piffify') p = subprocess.Popen( [piffify_exe, 'simple.yaml'] ) p.communicate() psf = piff.read(psf_file) test_star = psf.interp.interpolate(target) np.testing.assert_almost_equal(test_star.fit.params, true_params, decimal=5) # Test that we can make rho statistics min_sep = 1 max_sep = 100 bin_size = 0.1 stats = piff.RhoStats(min_sep=min_sep, max_sep=max_sep, bin_size=bin_size) stats.compute(psf, orig_stars) rhos = [stats.rho1, stats.rho2, stats.rho3, stats.rho4, stats.rho5] for rho in rhos: # Test the range of separations radius = np.exp(rho.logr) # last bin can be one bigger than max_sep np.testing.assert_array_less(radius, np.exp(np.log(max_sep) + bin_size)) np.testing.assert_array_less(min_sep, radius) np.testing.assert_array_almost_equal(np.diff(rho.logr), bin_size, decimal=5) # Test that the max absolute value of each rho isn't crazy np.testing.assert_array_less(np.abs(rho.xip), 1) # Check that each rho isn't precisely zero. This means the sum of abs > 0 np.testing.assert_array_less(0, np.sum(np.abs(rho.xip))) # Test the plotting and writing rho_psf_file = os.path.join('output','simple_psf_rhostats.png') stats.write(rho_psf_file) # Test that we can make summary shape statistics, using HSM shapeStats = piff.ShapeHistogramsStats() shapeStats.compute(psf, orig_stars) # test their characteristics np.testing.assert_array_almost_equal(sigma, shapeStats.T, decimal=4) np.testing.assert_array_almost_equal(sigma, shapeStats.T_model, decimal=3) np.testing.assert_array_almost_equal(g1, shapeStats.g1, decimal=4) np.testing.assert_array_almost_equal(g1, shapeStats.g1_model, decimal=3) np.testing.assert_array_almost_equal(g2, shapeStats.g2, decimal=4) np.testing.assert_array_almost_equal(g2, shapeStats.g2_model, decimal=3) shape_psf_file = os.path.join('output','simple_psf_shapestats.png') shapeStats.write(shape_psf_file) # Test that we can use the config parser for both RhoStats and ShapeHistogramsStats config['output']['stats'] = [ { 'type': 'ShapeHistograms', 'file_name': shape_psf_file }, { 'type': 'Rho', 'file_name': rho_psf_file }, ] os.remove(psf_file) os.remove(rho_psf_file) os.remove(shape_psf_file) piff.piffify(config) # Test using the piffify executable os.remove(psf_file) os.remove(rho_psf_file) os.remove(shape_psf_file) with open('simple.yaml','w') as f: f.write(yaml.dump(config, default_flow_style=False)) piffify_exe = get_script_name('piffify') p = subprocess.Popen( [piffify_exe, 'simple.yaml'] ) p.communicate()
def test_nn(): # Use a simple probability distribution for the galaxies: # # n(r) = (2pi s^2)^-1 exp(-r^2/2s^2) # # The Fourier transform is: n~(k) = exp(-s^2 k^2/2) # P(k) = <|n~(k)|^2> = exp(-s^2 k^2) # xi(r) = (1/2pi) int( dk k P(k) J0(kr) ) # = 1/(4 pi s^2) exp(-r^2/4s^2) # # However, we need to correct for the uniform density background, so the real result # is this minus 1/L^2 divided by 1/L^2. So: # # xi(r) = 1/4pi (L/s)^2 exp(-r^2/4s^2) - 1 s = 10. if __name__ == "__main__": ngal = 1000000 nrand = 5 * ngal L = 50. * s # Not infinity, so this introduces some error. Our integrals were to infinity. req_factor = 1 else: ngal = 100000 nrand = 2 * ngal L = 20. * s req_factor = 3 numpy.random.seed(8675309) x = numpy.random.normal(0,s, (ngal,) ) y = numpy.random.normal(0,s, (ngal,) ) cat = treecorr.Catalog(x=x, y=y, x_units='arcmin', y_units='arcmin') dd = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin', verbose=1) dd.process(cat) print('dd.npairs = ',dd.npairs) # log(<R>) != <logR>, but it should be close: print('meanlogr - log(meanr) = ',dd.meanlogr - numpy.log(dd.meanr)) numpy.testing.assert_almost_equal(dd.meanlogr, numpy.log(dd.meanr), decimal=3) rx = (numpy.random.random_sample(nrand)-0.5) * L ry = (numpy.random.random_sample(nrand)-0.5) * L rand = treecorr.Catalog(x=rx,y=ry, x_units='arcmin', y_units='arcmin') rr = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin', verbose=1) rr.process(rand) print('rr.npairs = ',rr.npairs) dr = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin', verbose=1) dr.process(cat,rand) print('dr.npairs = ',dr.npairs) r = dd.meanr true_xi = 0.25/numpy.pi * (L/s)**2 * numpy.exp(-0.25*r**2/s**2) - 1. xi, varxi = dd.calculateXi(rr,dr) print('xi = ',xi) print('true_xi = ',true_xi) print('ratio = ',xi / true_xi) print('diff = ',xi - true_xi) print('max rel diff = ',max(abs((xi - true_xi)/true_xi))) # This isn't super accurate. But the agreement improves as L increase, so I think it is # merely a matter of the finite field and the integrals going to infinity. (Sort of, since # we still have L in there.) assert max(abs(xi - true_xi)/true_xi)/req_factor < 0.1 numpy.testing.assert_almost_equal(numpy.log(numpy.abs(xi))/req_factor, numpy.log(numpy.abs(true_xi))/req_factor, decimal=1) simple_xi, simple_varxi = dd.calculateXi(rr) print('simple xi = ',simple_xi) print('max rel diff = ',max(abs((simple_xi - true_xi)/true_xi))) # The simple calculation (i.e. dd/rr-1, rather than (dd-2dr+rr)/rr as above) is only # slightly less accurate in this case. Probably because the mask is simple (a box), so # the difference is relatively minor. The error is slightly higher in this case, but testing # that it is everywhere < 0.1 is still appropriate. assert max(abs(simple_xi - true_xi)/true_xi)/req_factor < 0.1 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': cat.write(os.path.join('data','nn_data.dat')) rand.write(os.path.join('data','nn_rand.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"nn.params"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','nn.out'),names=True) print('xi = ',xi) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi) print('diff = ',corr2_output['xi']-xi) numpy.testing.assert_almost_equal(corr2_output['xi']/xi, 1., decimal=3) # Check the fits write option out_file_name1 = os.path.join('output','nn_out1.fits') dd.write(out_file_name1) try: import fitsio data = fitsio.read(out_file_name1) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(dd.logr)) numpy.testing.assert_almost_equal(data['meanR'], dd.meanr) numpy.testing.assert_almost_equal(data['meanlogR'], dd.meanlogr) numpy.testing.assert_almost_equal(data['npairs'], dd.npairs) except ImportError: print('Unable to import fitsio. Skipping fits tests.') out_file_name2 = os.path.join('output','nn_out2.fits') dd.write(out_file_name2, rr) try: import fitsio data = fitsio.read(out_file_name2) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(dd.logr)) numpy.testing.assert_almost_equal(data['meanR'], dd.meanr) numpy.testing.assert_almost_equal(data['meanlogR'], dd.meanlogr) numpy.testing.assert_almost_equal(data['xi'], simple_xi) numpy.testing.assert_almost_equal(data['sigma_xi'], numpy.sqrt(simple_varxi)) numpy.testing.assert_almost_equal(data['DD'], dd.npairs) numpy.testing.assert_almost_equal(data['RR'], rr.npairs * (dd.tot / rr.tot)) except ImportError: print('Unable to import fitsio. Skipping fits tests.') out_file_name3 = os.path.join('output','nn_out3.fits') dd.write(out_file_name3, rr, dr) try: import fitsio data = fitsio.read(out_file_name3) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(dd.logr)) numpy.testing.assert_almost_equal(data['meanR'], dd.meanr) numpy.testing.assert_almost_equal(data['meanlogR'], dd.meanlogr) numpy.testing.assert_almost_equal(data['xi'], xi) numpy.testing.assert_almost_equal(data['sigma_xi'], numpy.sqrt(varxi)) numpy.testing.assert_almost_equal(data['DD'], dd.npairs) numpy.testing.assert_almost_equal(data['RR'], rr.npairs * (dd.tot / rr.tot)) numpy.testing.assert_almost_equal(data['DR'], dr.npairs * (dd.tot / dr.tot)) except ImportError: print('Unable to import fitsio. Skipping fits tests.') # Check the read function dd2 = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin') dd2.read(out_file_name1) numpy.testing.assert_almost_equal(dd2.logr, dd.logr) numpy.testing.assert_almost_equal(dd2.meanr, dd.meanr) numpy.testing.assert_almost_equal(dd2.meanlogr, dd.meanlogr) numpy.testing.assert_almost_equal(dd2.npairs, dd.npairs) dd2.read(out_file_name3) numpy.testing.assert_almost_equal(dd2.logr, dd.logr) numpy.testing.assert_almost_equal(dd2.meanr, dd.meanr) numpy.testing.assert_almost_equal(dd2.meanlogr, dd.meanlogr) numpy.testing.assert_almost_equal(dd2.npairs, dd.npairs)
def test_list(): # Test that we can use a list of files for either data or rand or both. nobj = 5000 numpy.random.seed(8675309) ncats = 3 data_cats = [] rand_cats = [] s = 10. L = 50. * s numpy.random.seed(8675309) x = numpy.random.normal(0,s, (nobj,ncats) ) y = numpy.random.normal(0,s, (nobj,ncats) ) data_cats = [ treecorr.Catalog(x=x[:,k],y=y[:,k]) for k in range(ncats) ] rx = (numpy.random.random_sample((nobj,ncats))-0.5) * L ry = (numpy.random.random_sample((nobj,ncats))-0.5) * L rand_cats = [ treecorr.Catalog(x=rx[:,k],y=ry[:,k]) for k in range(ncats) ] dd = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., verbose=1) dd.process(data_cats) print('dd.npairs = ',dd.npairs) rr = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., verbose=1) rr.process(rand_cats) print('rr.npairs = ',rr.npairs) xi, varxi = dd.calculateXi(rr) print('xi = ',xi) # Now do the same thing with one big catalog for each. ddx = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., verbose=1) rrx = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., verbose=1) data_catx = treecorr.Catalog(x=x.reshape( (nobj*ncats,) ), y=y.reshape( (nobj*ncats,) )) rand_catx = treecorr.Catalog(x=rx.reshape( (nobj*ncats,) ), y=ry.reshape( (nobj*ncats,) )) ddx.process(data_catx) rrx.process(rand_catx) xix, varxix = ddx.calculateXi(rrx) print('ddx.npairs = ',ddx.npairs) print('rrx.npairs = ',rrx.npairs) print('xix = ',xix) print('ratio = ',xi/xix) print('diff = ',xi-xix) numpy.testing.assert_almost_equal(xix/xi, 1., decimal=2) # Check that we get the same result using the corr2 executable: file_list = [] rand_file_list = [] for k in range(ncats): file_name = os.path.join('data','nn_list_data%d.dat'%k) with open(file_name, 'w') as fid: for i in range(nobj): fid.write(('%.8f %.8f\n')%(x[i,k],y[i,k])) file_list.append(file_name) rand_file_name = os.path.join('data','nn_list_rand%d.dat'%k) with open(rand_file_name, 'w') as fid: for i in range(nobj): fid.write(('%.8f %.8f\n')%(rx[i,k],ry[i,k])) rand_file_list.append(rand_file_name) list_name = os.path.join('data','nn_list_data_files.txt') with open(list_name, 'w') as fid: for file_name in file_list: fid.write('%s\n'%file_name) rand_list_name = os.path.join('data','nn_list_rand_files.txt') with open(rand_list_name, 'w') as fid: for file_name in rand_file_list: fid.write('%s\n'%file_name) file_namex = os.path.join('data','nn_list_datax.dat') with open(file_namex, 'w') as fid: for k in range(ncats): for i in range(nobj): fid.write(('%.8f %.8f\n')%(x[i,k],y[i,k])) rand_file_namex = os.path.join('data','nn_list_randx.dat') with open(rand_file_namex, 'w') as fid: for k in range(ncats): for i in range(nobj): fid.write(('%.8f %.8f\n')%(rx[i,k],ry[i,k])) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"nn_list1.params"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','nn_list1.out'),names=True) print('xi = ',xi) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi) print('diff = ',corr2_output['xi']-xi) numpy.testing.assert_almost_equal(corr2_output['xi']/xi, 1., decimal=3) import subprocess p = subprocess.Popen( [corr2_exe,"nn_list2.params"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','nn_list2.out'),names=True) print('xi = ',xi) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi) print('diff = ',corr2_output['xi']-xi) numpy.testing.assert_almost_equal(corr2_output['xi']/xi, 1., decimal=2) import subprocess p = subprocess.Popen( [corr2_exe,"nn_list3.params"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','nn_list3.out'),names=True) print('xi = ',xi) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi) print('diff = ',corr2_output['xi']-xi) numpy.testing.assert_almost_equal(corr2_output['xi']/xi, 1., decimal=2) import subprocess p = subprocess.Popen( [corr2_exe,"nn_list4.params"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','nn_list4.out'),names=True) print('xi = ',xi) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi) print('diff = ',corr2_output['xi']-xi) numpy.testing.assert_almost_equal(corr2_output['xi']/xi, 1., decimal=2) import subprocess p = subprocess.Popen( [corr2_exe,"nn_list5.params"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','nn_list5.out'),names=True) print('xi = ',xi) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi) print('diff = ',corr2_output['xi']-xi) numpy.testing.assert_almost_equal(corr2_output['xi']/xi, 1., decimal=2) import subprocess p = subprocess.Popen( [corr2_exe,"nn_list6.params"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','nn_list6.out'),names=True) print('xi = ',xi) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi) print('diff = ',corr2_output['xi']-xi) numpy.testing.assert_almost_equal(corr2_output['xi']/xi, 1., decimal=2)
def test_rlens(): # Similar to test_rlens in test_ng.py, but we give the lenses a shape and do a GG correlation. # Use gamma_t(r) = gamma0 exp(-R^2/2R0^2) around a bunch of foreground lenses. nlens = 100 nsource = 200000 gamma0 = 0.05 R0 = 10. L = 50. * R0 numpy.random.seed(8675309) # Lenses are randomly located with random shapes. xl = (numpy.random.random_sample(nlens)-0.5) * L # -500 < x < 500 zl = (numpy.random.random_sample(nlens)-0.5) * L # -500 < y < 500 yl = numpy.random.random_sample(nlens) * 4*L + 10*L # 5000 < z < 7000 rl = numpy.sqrt(xl**2 + yl**2 + zl**2) g1l = numpy.random.normal(0., 0.1, (nlens,)) g2l = numpy.random.normal(0., 0.1, (nlens,)) gl = g1l + 1j * g2l gl /= numpy.abs(gl) print('Made lenses') # For the signal, we'll do a pure quadrupole halo lens signal. cf. test_haloellip() xs = (numpy.random.random_sample(nsource)-0.5) * L zs = (numpy.random.random_sample(nsource)-0.5) * L ys = numpy.random.random_sample(nsource) * 8*L + 160*L # 80000 < z < 84000 rs = numpy.sqrt(xs**2 + ys**2 + zs**2) g1 = numpy.zeros( (nsource,) ) g2 = numpy.zeros( (nsource,) ) bin_size = 0.1 # min_sep is set so the first bin doesn't have 0 pairs. min_sep = 1.3*R0 # max_sep can't be too large, since the measured value starts to have shape noise for larger # values of separation. We're not adding any shape noise directly, but the shear from other # lenses is effectively a shape noise, and that comes to dominate the measurement above ~4R0. max_sep = 4.*R0 nbins = int(numpy.ceil(numpy.log(max_sep/min_sep)/bin_size)) true_gQ = numpy.zeros( (nbins,) ) true_gCr = numpy.zeros( (nbins,) ) true_gCi = numpy.zeros( (nbins,) ) true_npairs = numpy.zeros((nbins,), dtype=int) print('Making shear vectors') for x,y,z,r,g in zip(xl,yl,zl,rl,gl): # Use |r1 x r2| = |r1| |r2| sin(theta) xcross = ys * z - zs * y ycross = zs * x - xs * z zcross = xs * y - ys * x sintheta = numpy.sqrt(xcross**2 + ycross**2 + zcross**2) / (rs * r) Rlens = 2. * r * numpy.sin(numpy.arcsin(sintheta)/2) gammaQ = gamma0 * numpy.exp(-0.5*Rlens**2/R0**2) # For the alpha angle, approximate that the x,z coords are approx the perpendicular plane. # So just normalize back to the unit sphere and do the 2d projection calculation. # It's not exactly right, but it should be good enough for this unit test. dx = xs/rs-x/r dz = zs/rs-z/r expialpha = dx + 1j*dz expialpha /= numpy.abs(expialpha) # In frame where halo is along x axis, # g_source = gammaQ exp(4itheta) # In real frame, theta = alpha - phi, and we need to rotate the shear an extra exp(2iphi) # g_source = gammaQ exp(4ialpha) exp(-2iphi) gQ = gammaQ * expialpha**4 * numpy.conj(g) g1 += gQ.real g2 += gQ.imag index = numpy.floor( numpy.log(Rlens/min_sep) / bin_size).astype(int) mask = (index >= 0) & (index < nbins) numpy.add.at(true_gQ, index[mask], gammaQ[mask]) numpy.add.at(true_npairs, index[mask], 1) # We aren't intentionally making a constant term, but there will be some C signal due to # the finite number of pairs being rendered. So let's figure out how much there is. gC = gQ * numpy.conj(g) numpy.add.at(true_gCr, index[mask], gC[mask].real) numpy.add.at(true_gCi, index[mask], -gC[mask].imag) true_gQ /= true_npairs true_gCr /= true_npairs true_gCi /= true_npairs print('true_gQ = ',true_gQ) print('true_gCr = ',true_gCr) print('true_gCi = ',true_gCi) # Start with bin_slop == 0. With only 100 lenses, this still runs very fast. lens_cat = treecorr.Catalog(x=xl, y=yl, z=zl, g1=gl.real, g2=gl.imag) source_cat = treecorr.Catalog(x=xs, y=ys, z=zs, g1=g1, g2=g2) gg0 = treecorr.GGCorrelation(bin_size=bin_size, min_sep=min_sep, max_sep=max_sep, verbose=1, metric='Rlens', bin_slop=0) gg0.process(lens_cat, source_cat) Rlens = gg0.meanr theory_gQ = gamma0 * numpy.exp(-0.5*Rlens**2/R0**2) print('Results with bin_slop = 0:') print('gg.npairs = ',gg0.npairs) print('true_npairs = ',true_npairs) print('gg.xim = ',gg0.xim) print('true_gammat = ',true_gQ) print('ratio = ',gg0.xim / true_gQ) print('diff = ',gg0.xim - true_gQ) print('max diff = ',max(abs(gg0.xim - true_gQ))) assert max(abs(gg0.xim - true_gQ)) < 2.e-6 print('gg.xim_im = ',gg0.xim_im) assert max(abs(gg0.xim_im)) < 2.e-6 print('gg.xip = ',gg0.xip) print('true_gCr = ',true_gCr) print('diff = ',gg0.xip - true_gCr) print('max diff = ',max(abs(gg0.xip - true_gCr))) assert max(abs(gg0.xip - true_gCr)) < 2.e-6 print('gg.xip_im = ',gg0.xip_im) print('true_gCi = ',true_gCi) print('diff = ',gg0.xip_im - true_gCi) print('max diff = ',max(abs(gg0.xip_im - true_gCi))) assert max(abs(gg0.xip_im - true_gCi)) < 2.e-6 print('gg.xim = ',gg0.xim) print('theory_gammat = ',theory_gQ) print('ratio = ',gg0.xim / theory_gQ) print('diff = ',gg0.xim - theory_gQ) print('max diff = ',max(abs(gg0.xim - theory_gQ))) assert max(abs(gg0.xim - theory_gQ)) < 4.e-5 # Now use a more normal value for bin_slop. gg1 = treecorr.GGCorrelation(bin_size=bin_size, min_sep=min_sep, max_sep=max_sep, verbose=1, metric='Rlens', bin_slop=0.5) gg1.process(lens_cat, source_cat) Rlens = gg1.meanr theory_gQ = gamma0 * numpy.exp(-0.5*Rlens**2/R0**2) print('Results with bin_slop = 0.5') print('gg.npairs = ',gg1.npairs) print('gg.xim = ',gg1.xim) print('theory_gammat = ',theory_gQ) print('ratio = ',gg1.xim / theory_gQ) print('diff = ',gg1.xim - theory_gQ) print('max diff = ',max(abs(gg1.xim - theory_gQ))) assert max(abs(gg1.xim - theory_gQ)) < 4.e-5 print('gg.xim_im = ',gg1.xim_im) assert max(abs(gg1.xim_im)) < 7.e-6 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': lens_cat.write(os.path.join('data','gg_rlens_lens.dat')) source_cat.write(os.path.join('data','gg_rlens_source.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"gg_rlens.yaml"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','gg_rlens.out'),names=True) print('gg.xim = ',gg1.xim) print('from corr2 output = ',corr2_output['xim']) print('ratio = ',corr2_output['xim']/gg1.xim) print('diff = ',corr2_output['xim']-gg1.xim) numpy.testing.assert_almost_equal(corr2_output['xim'], gg1.xim, decimal=6) numpy.testing.assert_almost_equal(corr2_output['xim_im'], gg1.xim_im, decimal=6) numpy.testing.assert_almost_equal(corr2_output['xip'], gg1.xip, decimal=6) numpy.testing.assert_almost_equal(corr2_output['xip_im'], gg1.xip_im, decimal=6) # Repeat with the sources being given as RA/Dec only. ral, decl = treecorr.CelestialCoord.xyz_to_radec(xl,yl,zl) ras, decs = treecorr.CelestialCoord.xyz_to_radec(xs,ys,zs) lens_cat = treecorr.Catalog(ra=ral, dec=decl, ra_units='radians', dec_units='radians', r=rl, g1=gl.real, g2=gl.imag) source_cat = treecorr.Catalog(ra=ras, dec=decs, ra_units='radians', dec_units='radians', g1=g1, g2=g2) gg0s = treecorr.GGCorrelation(bin_size=bin_size, min_sep=min_sep, max_sep=max_sep, verbose=1, metric='Rlens', bin_slop=0) gg0s.process(lens_cat, source_cat) Rlens = gg0s.meanr theory_gQ = gamma0 * numpy.exp(-0.5*Rlens**2/R0**2) print('Results with bin_slop = 0:') print('gg.npairs = ',gg0s.npairs) print('true_npairs = ',true_npairs) print('gg.xim = ',gg0s.xim) print('true_gammat = ',true_gQ) print('ratio = ',gg0s.xim / true_gQ) print('diff = ',gg0s.xim - true_gQ) print('max diff = ',max(abs(gg0s.xim - true_gQ))) assert max(abs(gg0s.xim - true_gQ)) < 2.e-6 print('gg.xim_im = ',gg0s.xim_im) assert max(abs(gg0s.xim_im)) < 2.e-6 print('gg.xip = ',gg0s.xip) print('true_gCr = ',true_gCr) print('diff = ',gg0s.xip - true_gCr) print('max diff = ',max(abs(gg0s.xip - true_gCr))) assert max(abs(gg0s.xip - true_gCr)) < 2.e-6 print('gg.xip_im = ',gg0s.xip_im) print('true_gCi = ',true_gCi) print('diff = ',gg0s.xip_im - true_gCi) print('max diff = ',max(abs(gg0s.xip_im - true_gCi))) assert max(abs(gg0s.xip_im - true_gCi)) < 2.e-6 print('gg.xim = ',gg0s.xim) print('theory_gammat = ',theory_gQ) print('ratio = ',gg0s.xim / theory_gQ) print('diff = ',gg0s.xim - theory_gQ) print('max diff = ',max(abs(gg0s.xim - theory_gQ))) assert max(abs(gg0s.xim - theory_gQ)) < 4.e-5 # This should be identical to the 3d version, since going all the way to leaves. # (The next test with bin_slop = 1 will be different, since tree creation is different.) assert max(abs(gg0s.xim - gg0.xim)) < 1.e-7 assert max(abs(gg0s.xip - gg0.xip)) < 1.e-7 assert max(abs(gg0s.xim_im - gg0.xim_im)) < 1.e-7 assert max(abs(gg0s.xip_im - gg0.xip_im)) < 1.e-7 assert max(abs(gg0s.npairs - gg0.npairs)) < 1.e-7 # Now use a more normal value for bin_slop. gg1s = treecorr.GGCorrelation(bin_size=bin_size, min_sep=min_sep, max_sep=max_sep, verbose=1, metric='Rlens', bin_slop=0.5) gg1s.process(lens_cat, source_cat) Rlens = gg1s.meanr theory_gQ = gamma0 * numpy.exp(-0.5*Rlens**2/R0**2) print('Results with bin_slop = 0.5') print('gg.npairs = ',gg1s.npairs) print('gg.xim = ',gg1s.xim) print('theory_gammat = ',theory_gQ) print('ratio = ',gg1s.xim / theory_gQ) print('diff = ',gg1s.xim - theory_gQ) print('max diff = ',max(abs(gg1s.xim - theory_gQ))) # Not quite as accurate as above, since the cells that get used tend to be larger, so more # slop happens in the binning. assert max(abs(gg1s.xim - theory_gQ)) < 4.e-5 print('gg.xim_im = ',gg1s.xim_im) assert max(abs(gg1s.xim_im)) < 7.e-6
def test_ng(): # Use gamma_t(r) = gamma0 exp(-r^2/2r0^2) around a bunch of foreground lenses. # i.e. gamma(r) = -gamma0 exp(-r^2/2r0^2) (x+iy)^2/r^2 nlens = 1000 nsource = 100000 gamma0 = 0.05 r0 = 10. L = 50. * r0 numpy.random.seed(8675309) xl = (numpy.random.random_sample(nlens)-0.5) * L yl = (numpy.random.random_sample(nlens)-0.5) * L xs = (numpy.random.random_sample(nsource)-0.5) * L ys = (numpy.random.random_sample(nsource)-0.5) * L g1 = numpy.zeros( (nsource,) ) g2 = numpy.zeros( (nsource,) ) for x,y in zip(xl,yl): dx = xs-x dy = ys-y r2 = dx**2 + dy**2 gammat = gamma0 * numpy.exp(-0.5*r2/r0**2) g1 += -gammat * (dx**2-dy**2)/r2 g2 += -gammat * (2.*dx*dy)/r2 lens_cat = treecorr.Catalog(x=xl, y=yl, x_units='arcmin', y_units='arcmin') source_cat = treecorr.Catalog(x=xs, y=ys, g1=g1, g2=g2, x_units='arcmin', y_units='arcmin') ng = treecorr.NGCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin', verbose=1) ng.process(lens_cat, source_cat) r = ng.meanr true_gt = gamma0 * numpy.exp(-0.5*r**2/r0**2) print('ng.xi = ',ng.xi) print('ng.xi_im = ',ng.xi_im) print('true_gammat = ',true_gt) print('ratio = ',ng.xi / true_gt) print('diff = ',ng.xi - true_gt) print('max diff = ',max(abs(ng.xi - true_gt))) assert max(abs(ng.xi - true_gt)) < 4.e-3 assert max(abs(ng.xi_im)) < 4.e-3 nrand = nlens * 13 xr = (numpy.random.random_sample(nrand)-0.5) * L yr = (numpy.random.random_sample(nrand)-0.5) * L rand_cat = treecorr.Catalog(x=xr, y=yr, x_units='arcmin', y_units='arcmin') rg = treecorr.NGCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin', verbose=1) rg.process(rand_cat, source_cat) print('rg.xi = ',rg.xi) xi, xi_im, varxi = ng.calculateXi(rg) print('compensated xi = ',xi) print('compensated xi_im = ',xi_im) print('true_gammat = ',true_gt) print('ratio = ',xi / true_gt) print('diff = ',xi - true_gt) print('max diff = ',max(abs(xi - true_gt))) # It turns out this doesn't come out much better. I think the imprecision is mostly just due # to the smallish number of lenses, not to edge effects assert max(abs(xi - true_gt)) < 4.e-3 assert max(abs(xi_im)) < 4.e-3 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': lens_cat.write(os.path.join('data','ng_lens.dat')) source_cat.write(os.path.join('data','ng_source.dat')) rand_cat.write(os.path.join('data','ng_rand.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"ng.yaml"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','ng.out'),names=True) print('ng.xi = ',ng.xi) print('xi = ',xi) print('from corr2 output = ',corr2_output['gamT']) print('ratio = ',corr2_output['gamT']/xi) print('diff = ',corr2_output['gamT']-xi) numpy.testing.assert_almost_equal(corr2_output['gamT']/xi, 1., decimal=3) print('xi_im from corr2 output = ',corr2_output['gamX']) assert max(abs(corr2_output['gamX'])) < 4.e-3 # Check the fits write option out_file_name1 = os.path.join('output','ng_out1.fits') ng.write(out_file_name1) data = fitsio.read(out_file_name1) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(ng.logr)) numpy.testing.assert_almost_equal(data['meanR'], ng.meanr) numpy.testing.assert_almost_equal(data['meanlogR'], ng.meanlogr) numpy.testing.assert_almost_equal(data['gamT'], ng.xi) numpy.testing.assert_almost_equal(data['gamX'], ng.xi_im) numpy.testing.assert_almost_equal(data['sigma'], numpy.sqrt(ng.varxi)) numpy.testing.assert_almost_equal(data['weight'], ng.weight) numpy.testing.assert_almost_equal(data['npairs'], ng.npairs) out_file_name2 = os.path.join('output','ng_out2.fits') ng.write(out_file_name2, rg) data = fitsio.read(out_file_name2) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(ng.logr)) numpy.testing.assert_almost_equal(data['meanR'], ng.meanr) numpy.testing.assert_almost_equal(data['meanlogR'], ng.meanlogr) numpy.testing.assert_almost_equal(data['gamT'], xi) numpy.testing.assert_almost_equal(data['gamX'], xi_im) numpy.testing.assert_almost_equal(data['sigma'], numpy.sqrt(varxi)) numpy.testing.assert_almost_equal(data['weight'], ng.weight) numpy.testing.assert_almost_equal(data['npairs'], ng.npairs) # Check the read function ng2 = treecorr.NGCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin') ng2.read(out_file_name1) numpy.testing.assert_almost_equal(ng2.logr, ng.logr) numpy.testing.assert_almost_equal(ng2.meanr, ng.meanr) numpy.testing.assert_almost_equal(ng2.meanlogr, ng.meanlogr) numpy.testing.assert_almost_equal(ng2.xi, ng.xi) numpy.testing.assert_almost_equal(ng2.xi_im, ng.xi_im) numpy.testing.assert_almost_equal(ng2.varxi, ng.varxi) numpy.testing.assert_almost_equal(ng2.weight, ng.weight) numpy.testing.assert_almost_equal(ng2.npairs, ng.npairs)
def test_kk(): # cf. http://adsabs.harvard.edu/abs/2002A%26A...389..729S for the basic formulae I use here. # # Use kappa(r) = A exp(-r^2/2s^2) # # The Fourier transform is: kappa~(k) = 2 pi A s^2 exp(-s^2 k^2/2) / L^2 # P(k) = (1/2pi) <|kappa~(k)|^2> = 2 pi A^2 (s/L)^4 exp(-s^2 k^2) # xi(r) = (1/2pi) int( dk k P(k) J0(kr) ) # = pi A^2 (s/L)^2 exp(-r^2/2s^2/4) # Note: I'm not sure I handled the L factors correctly, but the units at the end need # to be kappa^2, so it needs to be (s/L)^2. ngal = 1000000 A = 0.05 s = 10. L = 30. * s # Not infinity, so this introduces some error. Our integrals were to infinity. numpy.random.seed(8675309) x = (numpy.random.random_sample(ngal) - 0.5) * L y = (numpy.random.random_sample(ngal) - 0.5) * L r2 = (x**2 + y**2) / s**2 kappa = A * numpy.exp(-r2 / 2.) cat = treecorr.Catalog(x=x, y=y, k=kappa, x_units='arcmin', y_units='arcmin') kk = treecorr.KKCorrelation(bin_size=0.1, min_sep=1., max_sep=50., sep_units='arcmin', verbose=1) kk.process(cat) # log(<R>) != <logR>, but it should be close: print('meanlogr - log(meanr) = ', kk.meanlogr - numpy.log(kk.meanr)) numpy.testing.assert_almost_equal(kk.meanlogr, numpy.log(kk.meanr), decimal=3) r = kk.meanr true_xi = numpy.pi * A**2 * (s / L)**2 * numpy.exp(-0.25 * r**2 / s**2) print('kk.xi = ', kk.xi) print('true_xi = ', true_xi) print('ratio = ', kk.xi / true_xi) print('diff = ', kk.xi - true_xi) print('max diff = ', max(abs(kk.xi - true_xi))) assert max(abs(kk.xi - true_xi)) < 5.e-7 # It should also work as a cross-correlation of this cat with itself kk.process(cat, cat) numpy.testing.assert_almost_equal(kk.meanlogr, numpy.log(kk.meanr), decimal=3) assert max(abs(kk.xi - true_xi)) < 5.e-7 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': cat.write(os.path.join('data', 'kk.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen([corr2_exe, "kk.params"]) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output', 'kk.out'), names=True) print('kk.xi = ', kk.xi) print('from corr2 output = ', corr2_output['xi']) print('ratio = ', corr2_output['xi'] / kk.xi) print('diff = ', corr2_output['xi'] - kk.xi) numpy.testing.assert_almost_equal(corr2_output['xi'] / kk.xi, 1., decimal=3) # Check the fits write option out_file_name = os.path.join('output', 'kk_out.fits') kk.write(out_file_name) try: import fitsio data = fitsio.read(out_file_name) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(kk.logr)) numpy.testing.assert_almost_equal(data['meanR'], kk.meanr) numpy.testing.assert_almost_equal(data['meanlogR'], kk.meanlogr) numpy.testing.assert_almost_equal(data['xi'], kk.xi) numpy.testing.assert_almost_equal(data['sigma_xi'], numpy.sqrt(kk.varxi)) numpy.testing.assert_almost_equal(data['weight'], kk.weight) numpy.testing.assert_almost_equal(data['npairs'], kk.npairs) except ImportError: print('Unable to import fitsio. Skipping fits tests.') # Check the read function kk2 = treecorr.KKCorrelation(bin_size=0.1, min_sep=1., max_sep=100., sep_units='arcmin') kk2.read(out_file_name) numpy.testing.assert_almost_equal(kk2.logr, kk.logr) numpy.testing.assert_almost_equal(kk2.meanr, kk.meanr) numpy.testing.assert_almost_equal(kk2.meanlogr, kk.meanlogr) numpy.testing.assert_almost_equal(kk2.xi, kk.xi) numpy.testing.assert_almost_equal(kk2.varxi, kk.varxi) numpy.testing.assert_almost_equal(kk2.weight, kk.weight) numpy.testing.assert_almost_equal(kk2.npairs, kk.npairs)
def test_rlens(): # Same as above, except use R_lens for separation. # Use gamma_t(r) = gamma0 exp(-R^2/2R0^2) around a bunch of foreground lenses. nlens = 100 nsource = 200000 gamma0 = 0.05 R0 = 10. L = 50. * R0 numpy.random.seed(8675309) xl = (numpy.random.random_sample(nlens)-0.5) * L # -250 < x < 250 zl = (numpy.random.random_sample(nlens)-0.5) * L # -250 < y < 250 yl = numpy.random.random_sample(nlens) * 4*L + 10*L # 5000 < z < 7000 rl = numpy.sqrt(xl**2 + yl**2 + zl**2) xs = (numpy.random.random_sample(nsource)-0.5) * L zs = (numpy.random.random_sample(nsource)-0.5) * L ys = numpy.random.random_sample(nsource) * 8*L + 160*L # 80000 < z < 84000 rs = numpy.sqrt(xs**2 + ys**2 + zs**2) g1 = numpy.zeros( (nsource,) ) g2 = numpy.zeros( (nsource,) ) bin_size = 0.1 # min_sep is set so the first bin doesn't have 0 pairs. min_sep = 1.3*R0 # max_sep can't be too large, since the measured value starts to have shape noise for larger # values of separation. We're not adding any shape noise directly, but the shear from other # lenses is effectively a shape noise, and that comes to dominate the measurement above ~4R0. max_sep = 4.*R0 nbins = int(numpy.ceil(numpy.log(max_sep/min_sep)/bin_size)) true_gt = numpy.zeros( (nbins,) ) true_npairs = numpy.zeros((nbins,), dtype=int) print('Making shear vectors') for x,y,z,r in zip(xl,yl,zl,rl): # Use |r1 x r2| = |r1| |r2| sin(theta) xcross = ys * z - zs * y ycross = zs * x - xs * z zcross = xs * y - ys * x sintheta = numpy.sqrt(xcross**2 + ycross**2 + zcross**2) / (rs * r) Rlens = 2. * r * numpy.sin(numpy.arcsin(sintheta)/2) gammat = gamma0 * numpy.exp(-0.5*Rlens**2/R0**2) # For the rotation, approximate that the x,z coords are approx the perpendicular plane. # So just normalize back to the unit sphere and do the 2d projection calculation. # It's not exactly right, but it should be good enough for this unit test. dx = xs/rs-x/r dz = zs/rs-z/r drsq = dx**2 + dz**2 g1 += -gammat * (dx**2-dz**2)/drsq g2 += -gammat * (2.*dx*dz)/drsq index = numpy.floor( numpy.log(Rlens/min_sep) / bin_size).astype(int) mask = (index >= 0) & (index < nbins) numpy.add.at(true_gt, index[mask], gammat[mask]) numpy.add.at(true_npairs, index[mask], 1) true_gt /= true_npairs # Start with bin_slop == 0. With only 100 lenses, this still runs very fast. lens_cat = treecorr.Catalog(x=xl, y=yl, z=zl) source_cat = treecorr.Catalog(x=xs, y=ys, z=zs, g1=g1, g2=g2) ng0 = treecorr.NGCorrelation(bin_size=bin_size, min_sep=min_sep, max_sep=max_sep, verbose=1, metric='Rlens', bin_slop=0) ng0.process(lens_cat, source_cat) Rlens = ng0.meanr theory_gt = gamma0 * numpy.exp(-0.5*Rlens**2/R0**2) print('Results with bin_slop = 0:') print('ng.npairs = ',ng0.npairs) print('true_npairs = ',true_npairs) print('ng.xi = ',ng0.xi) print('true_gammat = ',true_gt) print('ratio = ',ng0.xi / true_gt) print('diff = ',ng0.xi - true_gt) print('max diff = ',max(abs(ng0.xi - true_gt))) assert max(abs(ng0.xi - true_gt)) < 2.e-6 print('ng.xi_im = ',ng0.xi_im) assert max(abs(ng0.xi_im)) < 1.e-6 print('ng.xi = ',ng0.xi) print('theory_gammat = ',theory_gt) print('ratio = ',ng0.xi / theory_gt) print('diff = ',ng0.xi - theory_gt) print('max diff = ',max(abs(ng0.xi - theory_gt))) assert max(abs(ng0.xi - theory_gt)) < 4.e-5 # Now use a more normal value for bin_slop. ng1 = treecorr.NGCorrelation(bin_size=bin_size, min_sep=min_sep, max_sep=max_sep, verbose=1, metric='Rlens', bin_slop=0.5) ng1.process(lens_cat, source_cat) Rlens = ng1.meanr theory_gt = gamma0 * numpy.exp(-0.5*Rlens**2/R0**2) print('Results with bin_slop = 0.5') print('ng.npairs = ',ng1.npairs) print('ng.xi = ',ng1.xi) print('theory_gammat = ',theory_gt) print('ratio = ',ng1.xi / theory_gt) print('diff = ',ng1.xi - theory_gt) print('max diff = ',max(abs(ng1.xi - theory_gt))) assert max(abs(ng1.xi - theory_gt)) < 5.e-5 print('ng.xi_im = ',ng1.xi_im) assert max(abs(ng1.xi_im)) < 3.e-6 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': lens_cat.write(os.path.join('data','ng_rlens_lens.dat')) source_cat.write(os.path.join('data','ng_rlens_source.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"ng_rlens.yaml"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','ng_rlens.out'),names=True) print('ng.xi = ',ng1.xi) print('from corr2 output = ',corr2_output['gamT']) print('ratio = ',corr2_output['gamT']/ng1.xi) print('diff = ',corr2_output['gamT']-ng1.xi) numpy.testing.assert_almost_equal(corr2_output['gamT'], ng1.xi, decimal=6) numpy.testing.assert_almost_equal(corr2_output['gamX'], ng1.xi_im, decimal=6) # Repeat with the sources being given as RA/Dec only. ral, decl = treecorr.CelestialCoord.xyz_to_radec(xl,yl,zl) ras, decs = treecorr.CelestialCoord.xyz_to_radec(xs,ys,zs) lens_cat = treecorr.Catalog(ra=ral, dec=decl, ra_units='radians', dec_units='radians', r=rl) source_cat = treecorr.Catalog(ra=ras, dec=decs, ra_units='radians', dec_units='radians', g1=g1, g2=g2) # Again, start with bin_slop == 0. # This version should be identical to the 3D version. When bin_slop != 0, it won't be # exactly identical, since the tree construction will have different decisions along the # way (since everything is at the same radius here), but the results are consistent. ng0s = treecorr.NGCorrelation(bin_size=bin_size, min_sep=min_sep, max_sep=max_sep, verbose=1, metric='Rlens', bin_slop=0) ng0s.process(lens_cat, source_cat) Rlens = ng0s.meanr theory_gt = gamma0 * numpy.exp(-0.5*Rlens**2/R0**2) print('Results when sources have no radius information, first bin_slop=0') print('ng.npairs = ',ng0s.npairs) print('true_npairs = ',true_npairs) print('ng.xi = ',ng0s.xi) print('true_gammat = ',true_gt) print('ratio = ',ng0s.xi / true_gt) print('diff = ',ng0s.xi - true_gt) print('max diff = ',max(abs(ng0s.xi - true_gt))) assert max(abs(ng0s.xi - true_gt)) < 2.e-6 print('ng.xi_im = ',ng0s.xi_im) assert max(abs(ng0s.xi_im)) < 1.e-6 print('ng.xi = ',ng0s.xi) print('theory_gammat = ',theory_gt) print('ratio = ',ng0s.xi / theory_gt) print('diff = ',ng0s.xi - theory_gt) print('max diff = ',max(abs(ng0s.xi - theory_gt))) assert max(abs(ng0s.xi - theory_gt)) < 4.e-5 assert max(abs(ng0s.xi - ng0.xi)) < 1.e-7 assert max(abs(ng0s.xi_im - ng0.xi_im)) < 1.e-7 assert max(abs(ng0s.npairs - ng0.npairs)) < 1.e-7 # Now use a more normal value for bin_slop. ng1s = treecorr.NGCorrelation(bin_size=bin_size, min_sep=min_sep, max_sep=max_sep, verbose=1, metric='Rlens', bin_slop=0.5) ng1s.process(lens_cat, source_cat) Rlens = ng1s.meanr theory_gt = gamma0 * numpy.exp(-0.5*Rlens**2/R0**2) print('Results with bin_slop = 0.5') print('ng.npairs = ',ng1s.npairs) print('ng.xi = ',ng1s.xi) print('theory_gammat = ',theory_gt) print('ratio = ',ng1s.xi / theory_gt) print('diff = ',ng1s.xi - theory_gt) print('max diff = ',max(abs(ng1s.xi - theory_gt))) assert max(abs(ng1s.xi - theory_gt)) < 5.e-5 print('ng.xi_im = ',ng1s.xi_im) assert max(abs(ng1s.xi_im)) < 3.e-6
def test_pairwise(): # Test the same profile, but with the pairwise calcualtion: nsource = 1000000 gamma0 = 0.05 kappa = 0.23 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) gammat = gamma0 * numpy.exp(-0.5 * r2 / r0**2) g1 = -gammat * (x**2 - y**2) / r2 g2 = -gammat * (2. * x * y) / r2 dx = (numpy.random.random_sample(nsource) - 0.5) * L dx = (numpy.random.random_sample(nsource) - 0.5) * L k = kappa * numpy.ones(nsource) lens_cat = treecorr.Catalog(x=dx, y=dx, k=k, x_units='arcmin', y_units='arcmin') source_cat = treecorr.Catalog(x=x + dx, y=y + dx, g1=g1, g2=g2, x_units='arcmin', y_units='arcmin') kg = treecorr.KGCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin', verbose=1, pairwise=True) kg.process(lens_cat, source_cat) r = kg.meanr true_kgt = kappa * gamma0 * numpy.exp(-0.5 * r**2 / r0**2) print('kg.xi = ', kg.xi) print('kg.xi_im = ', kg.xi_im) print('true_gammat = ', true_kgt) print('ratio = ', kg.xi / true_kgt) print('diff = ', kg.xi - true_kgt) print('max diff = ', max(abs(kg.xi - true_kgt))) assert max(abs(kg.xi - true_kgt)) < 4.e-4 assert max(abs(kg.xi_im)) < 3.e-5 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': lens_cat.write(os.path.join('data', 'kg_pairwise_lens.dat')) source_cat.write(os.path.join('data', 'kg_pairwise_source.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen([corr2_exe, "kg_pairwise.params"]) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output', 'kg_pairwise.out'), names=True) print('kg.xi = ', kg.xi) print('from corr2 output = ', corr2_output['kgamT']) print('ratio = ', corr2_output['kgamT'] / kg.xi) print('diff = ', corr2_output['kgamT'] - kg.xi) numpy.testing.assert_almost_equal(corr2_output['kgamT'] / kg.xi, 1., decimal=3) print('xi_im from corr2 output = ', corr2_output['kgamX']) assert max(abs(corr2_output['kgamX'])) < 3.e-5
def test_rlens_bkg(): # Same as above, except limit the sources to be in the background of the lens. nlens = 100 nsource = 200000 gamma0 = 0.05 R0 = 10. L = 50. * R0 numpy.random.seed(8675309) xl = (numpy.random.random_sample(nlens)-0.5) * L # -250 < x < 250 zl = (numpy.random.random_sample(nlens)-0.5) * L # -250 < y < 250 yl = numpy.random.random_sample(nlens) * 4*L + 10*L # 5000 < z < 7000 rl = numpy.sqrt(xl**2 + yl**2 + zl**2) xs = (numpy.random.random_sample(nsource)-0.5) * L zs = (numpy.random.random_sample(nsource)-0.5) * L ys = numpy.random.random_sample(nsource) * 12*L + 8*L # 4000 < z < 10000 rs = numpy.sqrt(xs**2 + ys**2 + zs**2) print('xl = ',numpy.min(xl),numpy.max(xl)) print('yl = ',numpy.min(yl),numpy.max(yl)) print('zl = ',numpy.min(zl),numpy.max(zl)) print('xs = ',numpy.min(xs),numpy.max(xs)) print('ys = ',numpy.min(ys),numpy.max(ys)) print('zs = ',numpy.min(zs),numpy.max(zs)) g1 = numpy.zeros( (nsource,) ) g2 = numpy.zeros( (nsource,) ) bin_size = 0.1 # min_sep is set so the first bin doesn't have 0 pairs. min_sep = 1.3*R0 # max_sep can't be too large, since the measured value starts to have shape noise for larger # values of separation. We're not adding any shape noise directly, but the shear from other # lenses is effectively a shape noise, and that comes to dominate the measurement above ~4R0. max_sep = 4.*R0 nbins = int(numpy.ceil(numpy.log(max_sep/min_sep)/bin_size)) print('Making shear vectors') for x,y,z,r in zip(xl,yl,zl,rl): # This time, only give the true shear to the background galaxies. bkg = (rs > r) # Use |r1 x r2| = |r1| |r2| sin(theta) xcross = ys[bkg] * z - zs[bkg] * y ycross = zs[bkg] * x - xs[bkg] * z zcross = xs[bkg] * y - ys[bkg] * x sintheta = numpy.sqrt(xcross**2 + ycross**2 + zcross**2) / (rs[bkg] * r) Rlens = 2. * r * numpy.sin(numpy.arcsin(sintheta)/2) gammat = gamma0 * numpy.exp(-0.5*Rlens**2/R0**2) # For the rotation, approximate that the x,z coords are approx the perpendicular plane. # So just normalize back to the unit sphere and do the 2d projection calculation. # It's not exactly right, but it should be good enough for this unit test. dx = (xs/rs)[bkg]-x/r dz = (zs/rs)[bkg]-z/r drsq = dx**2 + dz**2 g1[bkg] += -gammat * (dx**2-dz**2)/drsq g2[bkg] += -gammat * (2.*dx*dz)/drsq # Slight subtlety in this test vs the previous one. We need to build up the full g1,g2 # arrays first before calculating the true_gt value, since we need to include the background # galaxies for each lens regardless of whether they had signal or not. true_gt = numpy.zeros( (nbins,) ) true_npairs = numpy.zeros((nbins,), dtype=int) for x,y,z,r in zip(xl,yl,zl,rl): # Use |r1 x r2| = |r1| |r2| sin(theta) xcross = ys * z - zs * y ycross = zs * x - xs * z zcross = xs * y - ys * x sintheta = numpy.sqrt(xcross**2 + ycross**2 + zcross**2) / (rs * r) Rlens = 2. * r * numpy.sin(numpy.arcsin(sintheta)/2) dx = xs/rs-x/r dz = zs/rs-z/r drsq = dx**2 + dz**2 gt = -g1 * (dx**2-dz**2)/drsq - g2 * (2.*dx*dz)/drsq bkg = (rs > r) index = numpy.floor( numpy.log(Rlens/min_sep) / bin_size).astype(int) mask = (index >= 0) & (index < nbins) & bkg numpy.add.at(true_gt, index[mask], gt[mask]) numpy.add.at(true_npairs, index[mask], 1) true_gt /= true_npairs # Start with bin_slop == 0. With only 100 lenses, this still runs very fast. lens_cat = treecorr.Catalog(x=xl, y=yl, z=zl) source_cat = treecorr.Catalog(x=xs, y=ys, z=zs, g1=g1, g2=g2) ng0 = treecorr.NGCorrelation(bin_size=bin_size, min_sep=min_sep, max_sep=max_sep, verbose=1, metric='Rlens', bin_slop=0, min_rpar=0) ng0.process(lens_cat, source_cat) Rlens = ng0.meanr theory_gt = gamma0 * numpy.exp(-0.5*Rlens**2/R0**2) print('Results with bin_slop = 0:') print('ng.npairs = ',ng0.npairs) print('true_npairs = ',true_npairs) print('ng.xi = ',ng0.xi) print('true_gammat = ',true_gt) print('ratio = ',ng0.xi / true_gt) print('diff = ',ng0.xi - true_gt) print('max diff = ',max(abs(ng0.xi - true_gt))) assert max(abs(ng0.xi - true_gt)) < 2.e-6 print('ng.xi = ',ng0.xi) print('theory_gammat = ',theory_gt) print('ratio = ',ng0.xi / theory_gt) print('diff = ',ng0.xi - theory_gt) print('max diff = ',max(abs(ng0.xi - theory_gt))) assert max(abs(ng0.xi - theory_gt)) < 1.e-3 print('ng.xi_im = ',ng0.xi_im) assert max(abs(ng0.xi_im)) < 1.e-3 # Without min_rpar, this should fail. lens_cat = treecorr.Catalog(x=xl, y=yl, z=zl) source_cat = treecorr.Catalog(x=xs, y=ys, z=zs, g1=g1, g2=g2) ng0 = treecorr.NGCorrelation(bin_size=bin_size, min_sep=min_sep, max_sep=max_sep, verbose=1, metric='Rlens', bin_slop=0) ng0.process(lens_cat, source_cat) Rlens = ng0.meanr print('Results without min_rpar') print('ng.xi = ',ng0.xi) print('true_gammat = ',true_gt) print('max diff = ',max(abs(ng0.xi - true_gt))) assert max(abs(ng0.xi - true_gt)) > 5.e-3 # Now use a more normal value for bin_slop. ng1 = treecorr.NGCorrelation(bin_size=bin_size, min_sep=min_sep, max_sep=max_sep, verbose=1, metric='Rlens', bin_slop=0.5, min_rpar=0) ng1.process(lens_cat, source_cat) Rlens = ng1.meanr theory_gt = gamma0 * numpy.exp(-0.5*Rlens**2/R0**2) print('Results with bin_slop = 0.5') print('ng.npairs = ',ng1.npairs) print('ng.xi = ',ng1.xi) print('theory_gammat = ',theory_gt) print('ratio = ',ng1.xi / theory_gt) print('diff = ',ng1.xi - theory_gt) print('max diff = ',max(abs(ng1.xi - theory_gt))) assert max(abs(ng1.xi - theory_gt)) < 1.e-3 print('ng.xi_im = ',ng1.xi_im) assert max(abs(ng1.xi_im)) < 1.e-3 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': lens_cat.write(os.path.join('data','ng_rlens_bkg_lens.dat')) source_cat.write(os.path.join('data','ng_rlens_bkg_source.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"ng_rlens_bkg.yaml"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','ng_rlens_bkg.out'),names=True) print('ng.xi = ',ng1.xi) print('from corr2 output = ',corr2_output['gamT']) print('ratio = ',corr2_output['gamT']/ng1.xi) print('diff = ',corr2_output['gamT']-ng1.xi) numpy.testing.assert_almost_equal(corr2_output['gamT'], ng1.xi, decimal=6) numpy.testing.assert_almost_equal(corr2_output['gamX'], ng1.xi_im, decimal=6)
def test_spherical(): # This is the same field we used for test_gg, but put into spherical coords. # We do the spherical trig by hand using the obvious formulae, rather than the clever # optimizations that are used by the TreeCorr code, thus serving as a useful test of # the latter. gamma0 = 0.05 r0 = 10. * treecorr.arcmin if __name__ == "__main__": nsource = 1000000 L = 50. * r0 # Not infinity, so this introduces some error. Our integrals were to infinity. req_factor = 1 else: nsource = 200000 L = 50. * r0 req_factor = 3 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 g1 = -gamma0 * numpy.exp(-r2 / 2. / r0**2) * (x**2 - y**2) / r0**2 g2 = -gamma0 * numpy.exp(-r2 / 2. / r0**2) * (2. * x * y) / r0**2 r = numpy.sqrt(r2) theta = arctan2(y, x) gg = treecorr.GGCorrelation(bin_size=0.1, min_sep=1., max_sep=100., sep_units='arcmin', verbose=1) r1 = numpy.exp(gg.logr) * treecorr.arcmin temp = numpy.pi / 16. * gamma0**2 * (r0 / L)**2 * numpy.exp( -0.25 * r1**2 / r0**2) true_xip = temp * (r1**4 - 16. * r1**2 * r0**2 + 32. * r0**4) / r0**4 true_xim = temp * r1**4 / r0**4 # Test this around several central points if __name__ == '__main__': ra0_list = [0., 1., 1.3, 232., 0.] dec0_list = [0., -0.3, 1.3, -1.4, pi / 2. - 1.e-6] else: ra0_list = [232.] dec0_list = [-1.4] for ra0, dec0 in zip(ra0_list, dec0_list): # Use spherical triangle with A = point, B = (ra0,dec0), C = N. pole # a = Pi/2-dec0 # c = 2*asin(r/2) (lambert projection) # B = Pi/2 - theta c = 2. * arcsin(r / 2.) a = pi / 2. - dec0 B = pi / 2. - theta B[x < 0] *= -1. B[B < -pi] += 2. * pi B[B > pi] -= 2. * pi # Solve the rest of the triangle with spherical trig: cosb = cos(a) * cos(c) + sin(a) * sin(c) * cos(B) b = arccos(cosb) cosA = (cos(a) - cos(b) * cos(c)) / (sin(b) * sin(c)) #A = arccos(cosA) A = numpy.zeros_like(cosA) A[abs(cosA) < 1] = arccos(cosA[abs(cosA) < 1]) A[cosA <= -1] = pi cosC = (cos(c) - cos(a) * cos(b)) / (sin(a) * sin(b)) #C = arccos(cosC) C = numpy.zeros_like(cosC) C[abs(cosC) < 1] = arccos(cosC[abs(cosC) < 1]) C[cosC <= -1] = pi C[x < 0] *= -1. ra = ra0 - C dec = pi / 2. - b # Rotate shear relative to local west # gamma_sph = exp(2i beta) * gamma # where beta = pi - (A+B) is the angle between north and "up" in the tangent plane. beta = pi - (A + B) beta[x > 0] *= -1. cos2beta = cos(2. * beta) sin2beta = sin(2. * beta) g1_sph = g1 * cos2beta - g2 * sin2beta g2_sph = g2 * cos2beta + g1 * sin2beta cat = treecorr.Catalog(ra=ra, dec=dec, g1=g1_sph, g2=g2_sph, ra_units='rad', dec_units='rad') gg = treecorr.GGCorrelation(bin_size=0.1, min_sep=1., max_sep=100., sep_units='arcmin', verbose=1) gg.process(cat) print('ra0, dec0 = ', ra0, dec0) print('gg.xip = ', gg.xip) print('true_xip = ', true_xip) print('ratio = ', gg.xip / true_xip) print('diff = ', gg.xip - true_xip) print('max diff = ', max(abs(gg.xip - true_xip))) # The 3rd and 4th centers are somewhat less accurate. Not sure why. # The math seems to be right, since the last one that gets all the way to the pole # works, so I'm not sure what is going on. It's just a few bins that get a bit less # accurate. Possibly worth investigating further at some point... assert max(abs(gg.xip - true_xip)) / req_factor < 3.e-7 print('gg.xim = ', gg.xim) print('true_xim = ', true_xim) print('ratio = ', gg.xim / true_xim) print('diff = ', gg.xim - true_xim) print('max diff = ', max(abs(gg.xim - true_xim))) assert max(abs(gg.xim - true_xim)) / req_factor < 2.e-7 # One more center that can be done very easily. If the center is the north pole, then all # the tangential shears are pure (positive) g1. ra0 = 0 dec0 = pi / 2. ra = theta dec = pi / 2. - 2. * arcsin(r / 2.) gammat = -gamma0 * r2 / r0**2 * numpy.exp(-r2 / 2. / r0**2) cat = treecorr.Catalog(ra=ra, dec=dec, g1=gammat, g2=numpy.zeros_like(gammat), ra_units='rad', dec_units='rad') gg.process(cat) print('gg.xip = ', gg.xip) print('gg.xip_im = ', gg.xip_im) print('true_xip = ', true_xip) print('ratio = ', gg.xip / true_xip) print('diff = ', gg.xip - true_xip) print('max diff = ', max(abs(gg.xip - true_xip))) assert max(abs(gg.xip - true_xip)) / req_factor < 3.e-7 assert max(abs(gg.xip_im)) / req_factor < 3.e-7 print('gg.xim = ', gg.xim) print('gg.xim_im = ', gg.xim_im) print('true_xim = ', true_xim) print('ratio = ', gg.xim / true_xim) print('diff = ', gg.xim - true_xim) print('max diff = ', max(abs(gg.xim - true_xim))) assert max(abs(gg.xim - true_xim)) / req_factor < 2.e-7 assert max(abs(gg.xim_im)) / req_factor < 2.e-7 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': cat.write(os.path.join('data', 'gg_spherical.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen([corr2_exe, "gg_spherical.params"]) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output', 'gg_spherical.out'), names=True) print('gg.xip = ', gg.xip) print('from corr2 output = ', corr2_output['xip']) print('ratio = ', corr2_output['xip'] / gg.xip) print('diff = ', corr2_output['xip'] - gg.xip) numpy.testing.assert_almost_equal(corr2_output['xip'] / gg.xip, 1., decimal=3) print('gg.xim = ', gg.xim) print('from corr2 output = ', corr2_output['xim']) print('ratio = ', corr2_output['xim'] / gg.xim) print('diff = ', corr2_output['xim'] - gg.xim) numpy.testing.assert_almost_equal(corr2_output['xim'] / gg.xim, 1., decimal=3) print('xip_im from corr2 output = ', corr2_output['xip_im']) assert max(abs(corr2_output['xip_im'])) / req_factor < 3.e-7 print('xim_im from corr2 output = ', corr2_output['xim_im']) assert max(abs(corr2_output['xim_im'])) / req_factor < 2.e-7
def test_3d(): # For this one, build a Gaussian cloud around some random point in 3D space and do the # correlation function in 3D. # # Use n(r) = (2pi s^2)^-3/2 exp(-r^2/2s^2) # # The 3D Fourier transform is: n~(k) = exp(-s^2 k^2/2) # P(k) = <|n~(k)|^2> = exp(-s^2 k^2) # xi(r) = 1/2pi^2 int( dk k^2 P(k) j0(kr) ) # = 1/(8 pi^3/2) 1/s^3 exp(-r^2/4s^2) # # And as before, we need to correct for the randoms, so the final xi(r) is # # xi(r) = 1/(8 pi^3/2) (L/s)^3 exp(-r^2/4s^2) - 1 xcen = 823 # Mpc maybe? ycen = 342 zcen = -672 s = 10. if __name__ == "__main__": ngal = 100000 nrand = 5 * ngal L = 50. * s # Not infinity, so this introduces some error. Our integrals were to infinity. req_factor = 1 else: ngal = 20000 nrand = 2 * ngal L = 20. * s req_factor = 3 numpy.random.seed(8675309) x = numpy.random.normal(xcen, s, (ngal,) ) y = numpy.random.normal(ycen, s, (ngal,) ) z = numpy.random.normal(zcen, s, (ngal,) ) r = numpy.sqrt(x*x+y*y+z*z) dec = numpy.arcsin(z/r) / treecorr.degrees ra = numpy.arctan2(y,x) / treecorr.degrees cat = treecorr.Catalog(ra=ra, dec=dec, r=r, ra_units='deg', dec_units='deg') dd = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., verbose=1) dd.process(cat) print('dd.npairs = ',dd.npairs) rx = (numpy.random.random_sample(nrand)-0.5) * L + xcen ry = (numpy.random.random_sample(nrand)-0.5) * L + ycen rz = (numpy.random.random_sample(nrand)-0.5) * L + zcen rr = numpy.sqrt(rx*rx+ry*ry+rz*rz) rdec = numpy.arcsin(rz/rr) / treecorr.degrees rra = numpy.arctan2(ry,rx) / treecorr.degrees rand = treecorr.Catalog(ra=rra, dec=rdec, r=rr, ra_units='deg', dec_units='deg') rr = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., verbose=1) rr.process(rand) print('rr.npairs = ',rr.npairs) dr = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., verbose=1) dr.process(cat,rand) print('dr.npairs = ',dr.npairs) r = dd.meanr true_xi = 1./(8.*numpy.pi**1.5) * (L/s)**3 * numpy.exp(-0.25*r**2/s**2) - 1. xi, varxi = dd.calculateXi(rr,dr) print('xi = ',xi) print('true_xi = ',true_xi) print('ratio = ',xi / true_xi) print('diff = ',xi - true_xi) print('max rel diff = ',max(abs((xi - true_xi)/true_xi))) assert max(abs(xi - true_xi)/true_xi)/req_factor < 0.1 numpy.testing.assert_almost_equal(numpy.log(numpy.abs(xi))/req_factor, numpy.log(numpy.abs(true_xi))/req_factor, decimal=1) simple_xi, varxi = dd.calculateXi(rr) print('simple xi = ',simple_xi) print('max rel diff = ',max(abs((simple_xi - true_xi)/true_xi))) assert max(abs(simple_xi - true_xi)/true_xi)/req_factor < 0.1 numpy.testing.assert_almost_equal(numpy.log(numpy.abs(simple_xi))/req_factor, numpy.log(numpy.abs(true_xi))/req_factor, decimal=1) # Check that we get the same result using the corr2 executable: if __name__ == '__main__': cat.write(os.path.join('data','nn_3d_data.dat')) rand.write(os.path.join('data','nn_3d_rand.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"nn_3d.params"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','nn_3d.out'),names=True) print('xi = ',xi) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi) print('diff = ',corr2_output['xi']-xi) numpy.testing.assert_almost_equal(corr2_output['xi']/xi, 1., decimal=3) # And repeat with Catalogs that use x,y,z cat = treecorr.Catalog(x=x, y=y, z=z) rand = treecorr.Catalog(x=rx, y=ry, z=rz) dd.process(cat) rr.process(rand) dr.process(cat,rand) xi, varxi = dd.calculateXi(rr,dr) assert max(abs(xi - true_xi)/true_xi)/req_factor < 0.1 numpy.testing.assert_almost_equal(numpy.log(numpy.abs(xi))/req_factor, numpy.log(numpy.abs(true_xi))/req_factor, decimal=1)
def test_perp_minmax(): """This test is based on a bug report from Erika Wagoner where the lowest bins were getting spuriously high w(rp) values. It stemmed from a subtlety about how large rp can be compared to minsep. The maximum rp is more than just rp + s1 + s2. So this test checks that when the min and max are expanded a bit, the number of pairs doesn't change much in the bins that used to be the min and max. """ # Just use Erika's files for data and rand. config = { 'ra_col' : 1, 'dec_col' : 2, 'ra_units' : 'deg', 'dec_units' : 'deg', 'r_col' : 3, 'min_sep' : 20, 'bin_size' : 0.036652, 'nbins' : 50, 'verbose' : 1 } # Speed up for nosetests runs if __name__ != "__main__": config['nbins'] = 5 config['bin_size'] = 0.1 get_from_wiki('nn_perp_data.dat') dcat = treecorr.Catalog('data/nn_perp_data.dat', config) dd1 = treecorr.NNCorrelation(config) dd1.process(dcat, metric='Rperp') lower_min_sep = config['min_sep'] * numpy.exp(-2.*config['bin_size']) more_nbins = config['nbins'] + 4 dd2 = treecorr.NNCorrelation(config, min_sep=lower_min_sep, nbins=more_nbins) dd2.process(dcat, metric='Rperp') print('dd1 npairs = ',dd1.npairs) print('dd2 npairs = ',dd2.npairs[2:-2]) # First a basic sanity check. The values not near the edge should be identical. numpy.testing.assert_equal(dd1.npairs[2:-2], dd2.npairs[4:-4]) # The edge bins may differ slightly from the binning approximations (bin_slop and such), # but the differences should be very small. (When Erika reported the problem, the differences # were a few percent, which ended up making a bit difference in the correlation function.) numpy.testing.assert_almost_equal( dd1.npairs / dd2.npairs[2:-2], 1., decimal=4) if __name__ == '__main__': # If we're running from the command line, go ahead and finish the calculation # This catalog has 10^6 objects, which takes quite a while. I should really investigate # how to speed up the Rperp distance calculation. Probably by having a faster over- # and under-estimate first, and then only do the full calculation when it seems like we # will actually need it. # Anyway, until then, let's not take forever by using last_row=200000 get_from_wiki('nn_perp_rand.dat') rcat = treecorr.Catalog('data/nn_perp_rand.dat', config, last_row=200000) rr1 = treecorr.NNCorrelation(config) rr1.process(rcat, metric='Rperp') rr2 = treecorr.NNCorrelation(config, min_sep=lower_min_sep, nbins=more_nbins) rr2.process(rcat, metric='Rperp') print('rr1 npairs = ',rr1.npairs) print('rr2 npairs = ',rr2.npairs[2:-2]) numpy.testing.assert_almost_equal( rr1.npairs / rr2.npairs[2:-2], 1., decimal=4) dr1 = treecorr.NNCorrelation(config) dr1.process(dcat, rcat, metric='Rperp') dr2 = treecorr.NNCorrelation(config, min_sep=lower_min_sep, nbins=more_nbins) dr2.process(dcat, rcat, metric='Rperp') print('dr1 npairs = ',dr1.npairs) print('dr2 npairs = ',dr2.npairs[2:-2]) numpy.testing.assert_almost_equal( dr1.npairs / dr2.npairs[2:-2], 1., decimal=4) xi1, varxi1 = dd1.calculateXi(rr1, dr1) xi2, varxi2 = dd2.calculateXi(rr2, dr2) print('xi1 = ',xi1) print('xi2 = ',xi2[2:-2]) numpy.testing.assert_almost_equal( xi1 / xi2[2:-2], 1., decimal=2) # Check that we get the same result with the corr2 executable. import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"nn_rperp.yaml"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','nn_rperp.out'),names=True,skip_header=1) print('xi = ',xi1) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi1) print('diff = ',corr2_output['xi']-xi1) numpy.testing.assert_almost_equal(corr2_output['xi']/xi1, 1., decimal=3)
def test_nk(): # Use kappa(r) = kappa0 exp(-r^2/2r0^2) (1-r^2/2r0^2) around many lenses. nlens = 1000 nsource = 100000 kappa0 = 0.05 r0 = 10. L = 50. * r0 numpy.random.seed(8675309) xl = (numpy.random.random_sample(nlens) - 0.5) * L yl = (numpy.random.random_sample(nlens) - 0.5) * L xs = (numpy.random.random_sample(nsource) - 0.5) * L ys = (numpy.random.random_sample(nsource) - 0.5) * L k = numpy.zeros((nsource, )) for x, y in zip(xl, yl): dx = xs - x dy = ys - y r2 = dx**2 + dy**2 k += kappa0 * numpy.exp(-0.5 * r2 / r0**2) * (1. - 0.5 * r2 / r0**2) lens_cat = treecorr.Catalog(x=xl, y=yl, x_units='arcmin', y_units='arcmin') source_cat = treecorr.Catalog(x=xs, y=ys, 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) # log(<R>) != <logR>, but it should be close: print('meanlogr - log(meanr) = ', nk.meanlogr - numpy.log(nk.meanr)) numpy.testing.assert_almost_equal(nk.meanlogr, numpy.log(nk.meanr), decimal=3) 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)) < 5.e-3 nrand = nlens * 13 xr = (numpy.random.random_sample(nrand) - 0.5) * L yr = (numpy.random.random_sample(nrand) - 0.5) * L rand_cat = treecorr.Catalog(x=xr, y=yr, x_units='arcmin', y_units='arcmin') rk = treecorr.NKCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin', verbose=1) rk.process(rand_cat, source_cat) print('rk.xi = ', rk.xi) xi, varxi = nk.calculateXi(rk) print('compensated xi = ', xi) print('true_kappa = ', true_k) print('ratio = ', xi / true_k) print('diff = ', xi - true_k) print('max diff = ', max(abs(xi - true_k))) # It turns out this doesn't come out much better. I think the imprecision is mostly just due # to the smallish number of lenses, not to edge effects assert max(abs(xi - true_k)) < 5.e-3 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': lens_cat.write(os.path.join('data', 'nk_lens.dat')) source_cat.write(os.path.join('data', 'nk_source.dat')) rand_cat.write(os.path.join('data', 'nk_rand.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen([corr2_exe, "nk.yaml"]) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output', 'nk.out'), names=True) print('nk.xi = ', nk.xi) print('xi = ', xi) print('from corr2 output = ', corr2_output['kappa']) print('ratio = ', corr2_output['kappa'] / xi) print('diff = ', corr2_output['kappa'] - xi) numpy.testing.assert_almost_equal(corr2_output['kappa'] / xi, 1., decimal=3) # Check the fits write option out_file_name1 = os.path.join('output', 'nk_out1.fits') nk.write(out_file_name1) data = fitsio.read(out_file_name1) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(nk.logr)) numpy.testing.assert_almost_equal(data['meanR'], nk.meanr) numpy.testing.assert_almost_equal(data['meanlogR'], nk.meanlogr) numpy.testing.assert_almost_equal(data['kappa'], nk.xi) numpy.testing.assert_almost_equal(data['sigma'], numpy.sqrt(nk.varxi)) numpy.testing.assert_almost_equal(data['weight'], nk.weight) numpy.testing.assert_almost_equal(data['npairs'], nk.npairs) out_file_name2 = os.path.join('output', 'nk_out2.fits') nk.write(out_file_name2, rk) data = fitsio.read(out_file_name2) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(nk.logr)) numpy.testing.assert_almost_equal(data['meanR'], nk.meanr) numpy.testing.assert_almost_equal(data['meanlogR'], nk.meanlogr) numpy.testing.assert_almost_equal(data['kappa'], xi) numpy.testing.assert_almost_equal(data['sigma'], numpy.sqrt(varxi)) numpy.testing.assert_almost_equal(data['weight'], nk.weight) numpy.testing.assert_almost_equal(data['npairs'], nk.npairs) # Check the read function nk2 = treecorr.NKCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin') nk2.read(out_file_name1) numpy.testing.assert_almost_equal(nk2.logr, nk.logr) numpy.testing.assert_almost_equal(nk2.meanr, nk.meanr) numpy.testing.assert_almost_equal(nk2.meanlogr, nk.meanlogr) numpy.testing.assert_almost_equal(nk2.xi, nk.xi) numpy.testing.assert_almost_equal(nk2.varxi, nk.varxi) numpy.testing.assert_almost_equal(nk2.weight, nk.weight) numpy.testing.assert_almost_equal(nk2.npairs, nk.npairs)
def test_nn(): # Use a simple probability distribution for the galaxies: # # n(r) = (2pi s^2)^-1 exp(-r^2/2s^2) # # The Fourier transform is: n~(k) = exp(-s^2 k^2/2) # P(k) = <|n~(k)|^2> = exp(-s^2 k^2) # xi(r) = (1/2pi) int( dk k P(k) J0(kr) ) # = 1/(4 pi s^2) exp(-r^2/4s^2) # # However, we need to correct for the uniform density background, so the real result # is this minus 1/L^2 divided by 1/L^2. So: # # xi(r) = 1/4pi (L/s)^2 exp(-r^2/4s^2) - 1 s = 10. if __name__ == "__main__": ngal = 1000000 nrand = 5 * ngal L = 50. * s # Not infinity, so this introduces some error. Our integrals were to infinity. req_factor = 1 else: ngal = 100000 nrand = 2 * ngal L = 20. * s req_factor = 3 numpy.random.seed(8675309) x = numpy.random.normal(0,s, (ngal,) ) y = numpy.random.normal(0,s, (ngal,) ) cat = treecorr.Catalog(x=x, y=y, x_units='arcmin', y_units='arcmin') dd = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin', verbose=1) dd.process(cat) print('dd.npairs = ',dd.npairs) # log(<R>) != <logR>, but it should be close: print('meanlogr - log(meanr) = ',dd.meanlogr - numpy.log(dd.meanr)) numpy.testing.assert_almost_equal(dd.meanlogr, numpy.log(dd.meanr), decimal=3) rx = (numpy.random.random_sample(nrand)-0.5) * L ry = (numpy.random.random_sample(nrand)-0.5) * L rand = treecorr.Catalog(x=rx,y=ry, x_units='arcmin', y_units='arcmin') rr = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin', verbose=1) rr.process(rand) print('rr.npairs = ',rr.npairs) dr = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin', verbose=1) dr.process(cat,rand) print('dr.npairs = ',dr.npairs) r = dd.meanr true_xi = 0.25/numpy.pi * (L/s)**2 * numpy.exp(-0.25*r**2/s**2) - 1. xi, varxi = dd.calculateXi(rr,dr) print('xi = ',xi) print('true_xi = ',true_xi) print('ratio = ',xi / true_xi) print('diff = ',xi - true_xi) print('max rel diff = ',max(abs((xi - true_xi)/true_xi))) # This isn't super accurate. But the agreement improves as L increase, so I think it is # merely a matter of the finite field and the integrals going to infinity. (Sort of, since # we still have L in there.) assert max(abs(xi - true_xi)/true_xi)/req_factor < 0.1 numpy.testing.assert_almost_equal(numpy.log(numpy.abs(xi))/req_factor, numpy.log(numpy.abs(true_xi))/req_factor, decimal=1) simple_xi, simple_varxi = dd.calculateXi(rr) print('simple xi = ',simple_xi) print('max rel diff = ',max(abs((simple_xi - true_xi)/true_xi))) # The simple calculation (i.e. dd/rr-1, rather than (dd-2dr+rr)/rr as above) is only # slightly less accurate in this case. Probably because the mask is simple (a box), so # the difference is relatively minor. The error is slightly higher in this case, but testing # that it is everywhere < 0.1 is still appropriate. assert max(abs(simple_xi - true_xi)/true_xi)/req_factor < 0.1 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': cat.write(os.path.join('data','nn_data.dat')) rand.write(os.path.join('data','nn_rand.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"nn.yaml"] ) p.communicate() out_file_name = os.path.join('output','nn.out') corr2_output = numpy.genfromtxt(out_file_name, names=True, skip_header=1) print('xi = ',xi) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi) print('diff = ',corr2_output['xi']-xi) numpy.testing.assert_almost_equal(corr2_output['xi']/xi, 1., decimal=3) # Check the read function (not at very high accuracy for the ASCII I/O) dd2 = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin') dd2.read(out_file_name) numpy.testing.assert_almost_equal(dd2.logr/dd.logr, 1., decimal=3) numpy.testing.assert_almost_equal(dd2.meanr/dd.meanr, 1., decimal=3) numpy.testing.assert_almost_equal(dd2.meanlogr/dd.meanlogr, 1., decimal=3) numpy.testing.assert_almost_equal(dd2.npairs/dd.npairs, 1., decimal=3) numpy.testing.assert_almost_equal(dd2.tot/dd.tot, 1., decimal=3) # Check the fits write option out_file_name1 = os.path.join('output','nn_out1.fits') dd.write(out_file_name1) data = fitsio.read(out_file_name1) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(dd.logr)) numpy.testing.assert_almost_equal(data['meanR'], dd.meanr) numpy.testing.assert_almost_equal(data['meanlogR'], dd.meanlogr) numpy.testing.assert_almost_equal(data['npairs'], dd.npairs) header = fitsio.read_header(out_file_name1, 1) numpy.testing.assert_almost_equal(header['tot'], dd.tot) out_file_name2 = os.path.join('output','nn_out2.fits') dd.write(out_file_name2, rr) data = fitsio.read(out_file_name2) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(dd.logr)) numpy.testing.assert_almost_equal(data['meanR'], dd.meanr) numpy.testing.assert_almost_equal(data['meanlogR'], dd.meanlogr) numpy.testing.assert_almost_equal(data['xi'], simple_xi) numpy.testing.assert_almost_equal(data['sigma_xi'], numpy.sqrt(simple_varxi)) numpy.testing.assert_almost_equal(data['DD'], dd.npairs) numpy.testing.assert_almost_equal(data['RR'], rr.npairs * (dd.tot / rr.tot)) header = fitsio.read_header(out_file_name2, 1) numpy.testing.assert_almost_equal(header['tot'], dd.tot) out_file_name3 = os.path.join('output','nn_out3.fits') dd.write(out_file_name3, rr, dr) data = fitsio.read(out_file_name3) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(dd.logr)) numpy.testing.assert_almost_equal(data['meanR'], dd.meanr) numpy.testing.assert_almost_equal(data['meanlogR'], dd.meanlogr) numpy.testing.assert_almost_equal(data['xi'], xi) numpy.testing.assert_almost_equal(data['sigma_xi'], numpy.sqrt(varxi)) numpy.testing.assert_almost_equal(data['DD'], dd.npairs) numpy.testing.assert_almost_equal(data['RR'], rr.npairs * (dd.tot / rr.tot)) numpy.testing.assert_almost_equal(data['DR'], dr.npairs * (dd.tot / dr.tot)) header = fitsio.read_header(out_file_name3, 1) numpy.testing.assert_almost_equal(header['tot'], dd.tot) # Check the read function dd2 = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin') dd2.read(out_file_name1) numpy.testing.assert_almost_equal(dd2.logr, dd.logr) numpy.testing.assert_almost_equal(dd2.meanr, dd.meanr) numpy.testing.assert_almost_equal(dd2.meanlogr, dd.meanlogr) numpy.testing.assert_almost_equal(dd2.npairs, dd.npairs) numpy.testing.assert_almost_equal(dd2.tot, dd.tot) dd2.read(out_file_name3) numpy.testing.assert_almost_equal(dd2.logr, dd.logr) numpy.testing.assert_almost_equal(dd2.meanr, dd.meanr) numpy.testing.assert_almost_equal(dd2.meanlogr, dd.meanlogr) numpy.testing.assert_almost_equal(dd2.npairs, dd.npairs) numpy.testing.assert_almost_equal(dd2.tot, dd.tot)
def test_gg(): # cf. http://adsabs.harvard.edu/abs/2002A%26A...389..729S for the basic formulae I use here. # # Use gamma_t(r) = gamma0 r^2/r0^2 exp(-r^2/2r0^2) # i.e. gamma(r) = -gamma0 exp(-r^2/2r0^2) (x+iy)^2 / r0^2 # # The Fourier transform is: gamma~(k) = -2 pi gamma0 r0^4 k^2 exp(-r0^2 k^2/2) / L^2 # P(k) = (1/2pi) <|gamma~(k)|^2> = 2 pi gamma0^2 r0^8 k^4 / L^4 exp(-r0^2 k^2) # xi+(r) = (1/2pi) int( dk k P(k) J0(kr) ) # = pi/16 gamma0^2 (r0/L)^2 exp(-r^2/4r0^2) (r^4 - 16r^2r0^2 + 32r0^4)/r0^4 # xi-(r) = (1/2pi) int( dk k P(k) J4(kr) ) # = pi/16 gamma0^2 (r0/L)^2 exp(-r^2/4r0^2) r^4/r0^4 # Note: I'm not sure I handled the L factors correctly, but the units at the end need # to be gamma^2, so it needs to be (r0/L)^2. gamma0 = 0.05 r0 = 10. if __name__ == "__main__": ngal = 1000000 L = 50.*r0 # Not infinity, so this introduces some error. Our integrals were to infinity. req_factor = 1 else: ngal = 200000 L = 50.*r0 req_factor = 3 numpy.random.seed(8675309) x = (numpy.random.random_sample(ngal)-0.5) * L y = (numpy.random.random_sample(ngal)-0.5) * L r2 = (x**2 + y**2)/r0**2 g1 = -gamma0 * numpy.exp(-r2/2.) * (x**2-y**2)/r0**2 g2 = -gamma0 * numpy.exp(-r2/2.) * (2.*x*y)/r0**2 cat = treecorr.Catalog(x=x, y=y, g1=g1, g2=g2, x_units='arcmin', y_units='arcmin') gg = treecorr.GGCorrelation(bin_size=0.1, min_sep=1., max_sep=100., sep_units='arcmin', verbose=1) gg.process(cat) # log(<R>) != <logR>, but it should be close: print('meanlogr - log(meanr) = ',gg.meanlogr - numpy.log(gg.meanr)) numpy.testing.assert_almost_equal(gg.meanlogr, numpy.log(gg.meanr), decimal=3) r = gg.meanr temp = numpy.pi/16. * gamma0**2 * (r0/L)**2 * numpy.exp(-0.25*r**2/r0**2) true_xip = temp * (r**4 - 16.*r**2*r0**2 + 32.*r0**4)/r0**4 true_xim = temp * r**4/r0**4 print('gg.xip = ',gg.xip) print('true_xip = ',true_xip) print('ratio = ',gg.xip / true_xip) print('diff = ',gg.xip - true_xip) print('max diff = ',max(abs(gg.xip - true_xip))) assert max(abs(gg.xip - true_xip))/req_factor < 3.e-7 print('xip_im = ',gg.xip_im) assert max(abs(gg.xip_im))/req_factor < 2.e-7 print('gg.xim = ',gg.xim) print('true_xim = ',true_xim) print('ratio = ',gg.xim / true_xim) print('diff = ',gg.xim - true_xim) print('max diff = ',max(abs(gg.xim - true_xim))) assert max(abs(gg.xim - true_xim))/req_factor < 3.e-7 print('xim_im = ',gg.xim_im) assert max(abs(gg.xim_im))/req_factor < 1.e-7 # Should also work as a cross-correlation with itself gg.process(cat,cat) numpy.testing.assert_almost_equal(gg.meanlogr, numpy.log(gg.meanr), decimal=3) assert max(abs(gg.xip - true_xip))/req_factor < 3.e-7 assert max(abs(gg.xip_im))/req_factor < 2.e-7 assert max(abs(gg.xim - true_xim))/req_factor < 3.e-7 assert max(abs(gg.xim_im))/req_factor < 1.e-7 # Check MapSq calculation: # cf. http://adsabs.harvard.edu/abs/2004MNRAS.352..338J # Use Crittenden formulation, since the analytic result is simpler: # Map^2(R) = int 1/2 r/R^2 (T+(r/R) xi+(r) + T-(r/R) xi-(r)) dr # = 6 pi gamma0^2 r0^8 R^4 / (L^2 (r0^2+R^2)^5) # Mx^2(R) = int 1/2 r/R^2 (T+(r/R) xi+(r) - T-(r/R) xi-(r)) dr # = 0 # where T+(s) = (s^4-16s^2+32)/128 exp(-s^2/4) # T-(s) = s^4/128 exp(-s^2/4) true_mapsq = 6.*numpy.pi * gamma0**2 * r0**8 * r**4 / (L**2 * (r**2+r0**2)**5) mapsq, mapsq_im, mxsq, mxsq_im, varmapsq = gg.calculateMapSq('Crittenden') print('mapsq = ',mapsq) print('true_mapsq = ',true_mapsq) print('ratio = ',mapsq/true_mapsq) print('diff = ',mapsq-true_mapsq) print('max diff = ',max(abs(mapsq - true_mapsq))) print('max diff[16:] = ',max(abs(mapsq[16:] - true_mapsq[16:]))) # It's pretty ratty near the start where the integral is poorly evaluated, but the # agreement is pretty good if we skip the first 16 elements. # Well, it gets bad again at the end, but those values are small enough that they still # pass this test. assert max(abs(mapsq[16:]-true_mapsq[16:]))/req_factor < 3.e-8 print('mxsq = ',mxsq) print('max = ',max(abs(mxsq))) print('max[16:] = ',max(abs(mxsq[16:]))) assert max(abs(mxsq[16:]))/req_factor < 3.e-8 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': cat.write(os.path.join('data','gg.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"gg.yaml"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','gg.out'), names=True) print('gg.xip = ',gg.xip) print('from corr2 output = ',corr2_output['xip']) print('ratio = ',corr2_output['xip']/gg.xip) print('diff = ',corr2_output['xip']-gg.xip) numpy.testing.assert_almost_equal(corr2_output['xip']/gg.xip, 1., decimal=3) print('gg.xim = ',gg.xim) print('from corr2 output = ',corr2_output['xim']) print('ratio = ',corr2_output['xim']/gg.xim) print('diff = ',corr2_output['xim']-gg.xim) numpy.testing.assert_almost_equal(corr2_output['xim']/gg.xim, 1., decimal=3) print('xip_im from corr2 output = ',corr2_output['xip_im']) print('max err = ',max(abs(corr2_output['xip_im']))) assert max(abs(corr2_output['xip_im']))/req_factor < 2.e-7 print('xim_im from corr2 output = ',corr2_output['xim_im']) print('max err = ',max(abs(corr2_output['xim_im']))) assert max(abs(corr2_output['xim_im']))/req_factor < 1.e-7 corr2_output2 = numpy.genfromtxt(os.path.join('output','gg_m2.out'), names=True) print('mapsq = ',mapsq) print('from corr2 output = ',corr2_output2['Mapsq']) print('ratio = ',corr2_output2['Mapsq']/mapsq) print('diff = ',corr2_output2['Mapsq']-mapsq) numpy.testing.assert_almost_equal(corr2_output2['Mapsq']/mapsq, 1., decimal=3) print('mxsq = ',mxsq) print('from corr2 output = ',corr2_output2['Mxsq']) print('ratio = ',corr2_output2['Mxsq']/mxsq) print('diff = ',corr2_output2['Mxsq']-mxsq) numpy.testing.assert_almost_equal(corr2_output2['Mxsq']/mxsq, 1., decimal=3) # Check the fits write option out_file_name = os.path.join('output','gg_out.fits') gg.write(out_file_name) data = fitsio.read(out_file_name) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(gg.logr)) numpy.testing.assert_almost_equal(data['meanR'], gg.meanr) numpy.testing.assert_almost_equal(data['meanlogR'], gg.meanlogr) numpy.testing.assert_almost_equal(data['xip'], gg.xip) numpy.testing.assert_almost_equal(data['xim'], gg.xim) numpy.testing.assert_almost_equal(data['xip_im'], gg.xip_im) numpy.testing.assert_almost_equal(data['xim_im'], gg.xim_im) numpy.testing.assert_almost_equal(data['sigma_xi'], numpy.sqrt(gg.varxi)) numpy.testing.assert_almost_equal(data['weight'], gg.weight) numpy.testing.assert_almost_equal(data['npairs'], gg.npairs) # Check the read function gg2 = treecorr.GGCorrelation(bin_size=0.1, min_sep=1., max_sep=100., sep_units='arcmin') gg2.read(out_file_name) numpy.testing.assert_almost_equal(gg2.logr, gg.logr) numpy.testing.assert_almost_equal(gg2.meanr, gg.meanr) numpy.testing.assert_almost_equal(gg2.meanlogr, gg.meanlogr) numpy.testing.assert_almost_equal(gg2.xip, gg.xip) numpy.testing.assert_almost_equal(gg2.xim, gg.xim) numpy.testing.assert_almost_equal(gg2.xip_im, gg.xip_im) numpy.testing.assert_almost_equal(gg2.xim_im, gg.xim_im) numpy.testing.assert_almost_equal(gg2.varxi, gg.varxi) numpy.testing.assert_almost_equal(gg2.weight, gg.weight) numpy.testing.assert_almost_equal(gg2.npairs, gg.npairs) # Also check the Schneider version. The math isn't quite as nice here, but it is tractable # using a different formula than I used above: # Map^2(R) = int k P(k) W(kR) dk # = 576 pi gamma0^2 r0^6/(L^2 R^4) exp(-R^2/2r0^2) (I4(R^2/2r0^2) # where I4 is the modified Bessel function with nu=4. try: from scipy.special import iv x = 0.5*r**2/r0**2 true_mapsq = 144.*numpy.pi * gamma0**2 * r0**2 / (L**2 * x**2) * numpy.exp(-x) * iv(4,x) mapsq, mapsq_im, mxsq, mxsq_im, varmapsq = gg.calculateMapSq('Schneider') print('Schneider mapsq = ',mapsq) print('true_mapsq = ',true_mapsq) print('ratio = ',mapsq/true_mapsq) print('diff = ',mapsq-true_mapsq) print('max diff = ',max(abs(mapsq - true_mapsq))) print('max diff[20:] = ',max(abs(mapsq[20:] - true_mapsq[20:]))) # This one stays ratty longer, so we need to skip the first 20 and also loosen the # test a bit. assert max(abs(mapsq[20:]-true_mapsq[20:])) < 7.e-8 print('mxsq = ',mxsq) print('max = ',max(abs(mxsq))) print('max[20:] = ',max(abs(mxsq[20:]))) assert max(abs(mxsq[20:])) < 7.e-8 except ImportError: # Don't require scipy if the user doesn't have it. print('Skipping tests of Schneider aperture mass, since scipy.special not available.')
def test_list(): # Test that we can use a list of files for either data or rand or both. nobj = 5000 numpy.random.seed(8675309) ncats = 3 data_cats = [] rand_cats = [] s = 10. L = 50. * s numpy.random.seed(8675309) x = numpy.random.normal(0,s, (nobj,ncats) ) y = numpy.random.normal(0,s, (nobj,ncats) ) data_cats = [ treecorr.Catalog(x=x[:,k],y=y[:,k]) for k in range(ncats) ] rx = (numpy.random.random_sample((nobj,ncats))-0.5) * L ry = (numpy.random.random_sample((nobj,ncats))-0.5) * L rand_cats = [ treecorr.Catalog(x=rx[:,k],y=ry[:,k]) for k in range(ncats) ] dd = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., verbose=1) dd.process(data_cats) print('dd.npairs = ',dd.npairs) rr = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., verbose=1) rr.process(rand_cats) print('rr.npairs = ',rr.npairs) xi, varxi = dd.calculateXi(rr) print('xi = ',xi) # Now do the same thing with one big catalog for each. ddx = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., verbose=1) rrx = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., verbose=1) data_catx = treecorr.Catalog(x=x.reshape( (nobj*ncats,) ), y=y.reshape( (nobj*ncats,) )) rand_catx = treecorr.Catalog(x=rx.reshape( (nobj*ncats,) ), y=ry.reshape( (nobj*ncats,) )) ddx.process(data_catx) rrx.process(rand_catx) xix, varxix = ddx.calculateXi(rrx) print('ddx.npairs = ',ddx.npairs) print('rrx.npairs = ',rrx.npairs) print('xix = ',xix) print('ratio = ',xi/xix) print('diff = ',xi-xix) numpy.testing.assert_almost_equal(xix/xi, 1., decimal=2) # Check that we get the same result using the corr2 executable: file_list = [] rand_file_list = [] for k in range(ncats): file_name = os.path.join('data','nn_list_data%d.dat'%k) with open(file_name, 'w') as fid: for i in range(nobj): fid.write(('%.8f %.8f\n')%(x[i,k],y[i,k])) file_list.append(file_name) rand_file_name = os.path.join('data','nn_list_rand%d.dat'%k) with open(rand_file_name, 'w') as fid: for i in range(nobj): fid.write(('%.8f %.8f\n')%(rx[i,k],ry[i,k])) rand_file_list.append(rand_file_name) list_name = os.path.join('data','nn_list_data_files.txt') with open(list_name, 'w') as fid: for file_name in file_list: fid.write('%s\n'%file_name) rand_list_name = os.path.join('data','nn_list_rand_files.txt') with open(rand_list_name, 'w') as fid: for file_name in rand_file_list: fid.write('%s\n'%file_name) file_namex = os.path.join('data','nn_list_datax.dat') with open(file_namex, 'w') as fid: for k in range(ncats): for i in range(nobj): fid.write(('%.8f %.8f\n')%(x[i,k],y[i,k])) rand_file_namex = os.path.join('data','nn_list_randx.dat') with open(rand_file_namex, 'w') as fid: for k in range(ncats): for i in range(nobj): fid.write(('%.8f %.8f\n')%(rx[i,k],ry[i,k])) # First do this via the corr2 function. config = treecorr.config.read_config('nn_list1.yaml') logger = treecorr.config.setup_logger(0) treecorr.corr2(config, logger) corr2_output = numpy.genfromtxt(os.path.join('output','nn_list1.out'),names=True,skip_header=1) print('xi = ',xi) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi) print('diff = ',corr2_output['xi']-xi) numpy.testing.assert_almost_equal(corr2_output['xi']/xi, 1., decimal=3) # Now calling out to the external corr2 executable. import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"nn_list1.yaml"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','nn_list1.out'),names=True,skip_header=1) print('xi = ',xi) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi) print('diff = ',corr2_output['xi']-xi) numpy.testing.assert_almost_equal(corr2_output['xi']/xi, 1., decimal=3) import subprocess p = subprocess.Popen( [corr2_exe,"nn_list2.json"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','nn_list2.out'),names=True,skip_header=1) print('xi = ',xi) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi) print('diff = ',corr2_output['xi']-xi) numpy.testing.assert_almost_equal(corr2_output['xi']/xi, 1., decimal=2) import subprocess p = subprocess.Popen( [corr2_exe,"nn_list3.params"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','nn_list3.out'),names=True,skip_header=1) print('xi = ',xi) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi) print('diff = ',corr2_output['xi']-xi) numpy.testing.assert_almost_equal(corr2_output['xi']/xi, 1., decimal=2) import subprocess p = subprocess.Popen( [corr2_exe, "nn_list4.config", "-f", "yaml"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','nn_list4.out'),names=True,skip_header=1) print('xi = ',xi) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi) print('diff = ',corr2_output['xi']-xi) numpy.testing.assert_almost_equal(corr2_output['xi']/xi, 1., decimal=2) import subprocess p = subprocess.Popen( [corr2_exe, "nn_list5.config", "-f", "json"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','nn_list5.out'),names=True,skip_header=1) print('xi = ',xi) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi) print('diff = ',corr2_output['xi']-xi) numpy.testing.assert_almost_equal(corr2_output['xi']/xi, 1., decimal=2) # For this one, the output file is in the current directory, which used to give an error. import subprocess p = subprocess.Popen( [corr2_exe, "nn_list6.config", "-f", "params"] ) p.communicate() output_file = 'nn_list6.out' corr2_output = numpy.genfromtxt(output_file,names=True,skip_header=1) print('xi = ',xi) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi) print('diff = ',corr2_output['xi']-xi) numpy.testing.assert_almost_equal(corr2_output['xi']/xi, 1., decimal=2) # Move it to the output directory now to keep the current directory clean. mv_output_file = os.path.join('output',output_file) if os.path.exists(mv_output_file): os.remove(mv_output_file) os.rename(output_file, mv_output_file)
def test_spherical(): # This is the same field we used for test_gg, but put into spherical coords. # We do the spherical trig by hand using the obvious formulae, rather than the clever # optimizations that are used by the TreeCorr code, thus serving as a useful test of # the latter. gamma0 = 0.05 r0 = 10. * treecorr.arcmin if __name__ == "__main__": nsource = 1000000 L = 50.*r0 # Not infinity, so this introduces some error. Our integrals were to infinity. req_factor = 1 else: nsource = 200000 L = 50.*r0 req_factor = 3 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 g1 = -gamma0 * numpy.exp(-r2/2./r0**2) * (x**2-y**2)/r0**2 g2 = -gamma0 * numpy.exp(-r2/2./r0**2) * (2.*x*y)/r0**2 r = numpy.sqrt(r2) theta = arctan2(y,x) gg = treecorr.GGCorrelation(bin_size=0.1, min_sep=1., max_sep=100., sep_units='arcmin', verbose=1) r1 = numpy.exp(gg.logr) * treecorr.arcmin temp = numpy.pi/16. * gamma0**2 * (r0/L)**2 * numpy.exp(-0.25*r1**2/r0**2) true_xip = temp * (r1**4 - 16.*r1**2*r0**2 + 32.*r0**4)/r0**4 true_xim = temp * r1**4/r0**4 # Test this around several central points if __name__ == '__main__': ra0_list = [ 0., 1., 1.3, 232., 0. ] dec0_list = [ 0., -0.3, 1.3, -1.4, pi/2.-1.e-6 ] else: ra0_list = [ 232.] dec0_list = [ -1.4 ] for ra0, dec0 in zip(ra0_list, dec0_list): # Use spherical triangle with A = point, B = (ra0,dec0), C = N. pole # a = Pi/2-dec0 # c = 2*asin(r/2) (lambert projection) # B = Pi/2 - theta c = 2.*arcsin(r/2.) a = pi/2. - dec0 B = pi/2. - theta B[x<0] *= -1. B[B<-pi] += 2.*pi B[B>pi] -= 2.*pi # Solve the rest of the triangle with spherical trig: cosb = cos(a)*cos(c) + sin(a)*sin(c)*cos(B) b = arccos(cosb) cosA = (cos(a) - cos(b)*cos(c)) / (sin(b)*sin(c)) #A = arccos(cosA) A = numpy.zeros_like(cosA) A[abs(cosA)<1] = arccos(cosA[abs(cosA)<1]) A[cosA<=-1] = pi cosC = (cos(c) - cos(a)*cos(b)) / (sin(a)*sin(b)) #C = arccos(cosC) C = numpy.zeros_like(cosC) C[abs(cosC)<1] = arccos(cosC[abs(cosC)<1]) C[cosC<=-1] = pi C[x<0] *= -1. ra = ra0 - C dec = pi/2. - b # Rotate shear relative to local west # gamma_sph = exp(2i beta) * gamma # where beta = pi - (A+B) is the angle between north and "up" in the tangent plane. beta = pi - (A+B) beta[x>0] *= -1. cos2beta = cos(2.*beta) sin2beta = sin(2.*beta) g1_sph = g1 * cos2beta - g2 * sin2beta g2_sph = g2 * cos2beta + g1 * sin2beta cat = treecorr.Catalog(ra=ra, dec=dec, g1=g1_sph, g2=g2_sph, ra_units='rad', dec_units='rad') gg = treecorr.GGCorrelation(bin_size=0.1, min_sep=1., max_sep=100., sep_units='arcmin', verbose=1) gg.process(cat) print('ra0, dec0 = ',ra0,dec0) print('gg.xip = ',gg.xip) print('true_xip = ',true_xip) print('ratio = ',gg.xip / true_xip) print('diff = ',gg.xip - true_xip) print('max diff = ',max(abs(gg.xip - true_xip))) # The 3rd and 4th centers are somewhat less accurate. Not sure why. # The math seems to be right, since the last one that gets all the way to the pole # works, so I'm not sure what is going on. It's just a few bins that get a bit less # accurate. Possibly worth investigating further at some point... assert max(abs(gg.xip - true_xip))/req_factor < 3.e-7 print('gg.xim = ',gg.xim) print('true_xim = ',true_xim) print('ratio = ',gg.xim / true_xim) print('diff = ',gg.xim - true_xim) print('max diff = ',max(abs(gg.xim - true_xim))) assert max(abs(gg.xim - true_xim))/req_factor < 2.e-7 # One more center that can be done very easily. If the center is the north pole, then all # the tangential shears are pure (positive) g1. ra0 = 0 dec0 = pi/2. ra = theta dec = pi/2. - 2.*arcsin(r/2.) gammat = -gamma0 * r2/r0**2 * numpy.exp(-r2/2./r0**2) cat = treecorr.Catalog(ra=ra, dec=dec, g1=gammat, g2=numpy.zeros_like(gammat), ra_units='rad', dec_units='rad') gg.process(cat) print('gg.xip = ',gg.xip) print('gg.xip_im = ',gg.xip_im) print('true_xip = ',true_xip) print('ratio = ',gg.xip / true_xip) print('diff = ',gg.xip - true_xip) print('max diff = ',max(abs(gg.xip - true_xip))) assert max(abs(gg.xip - true_xip))/req_factor < 3.e-7 assert max(abs(gg.xip_im))/req_factor < 3.e-7 print('gg.xim = ',gg.xim) print('gg.xim_im = ',gg.xim_im) print('true_xim = ',true_xim) print('ratio = ',gg.xim / true_xim) print('diff = ',gg.xim - true_xim) print('max diff = ',max(abs(gg.xim - true_xim))) assert max(abs(gg.xim - true_xim))/req_factor < 2.e-7 assert max(abs(gg.xim_im))/req_factor < 2.e-7 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': cat.write(os.path.join('data','gg_spherical.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"gg_spherical.yaml"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','gg_spherical.out'), names=True) print('gg.xip = ',gg.xip) print('from corr2 output = ',corr2_output['xip']) print('ratio = ',corr2_output['xip']/gg.xip) print('diff = ',corr2_output['xip']-gg.xip) numpy.testing.assert_almost_equal(corr2_output['xip']/gg.xip, 1., decimal=3) print('gg.xim = ',gg.xim) print('from corr2 output = ',corr2_output['xim']) print('ratio = ',corr2_output['xim']/gg.xim) print('diff = ',corr2_output['xim']-gg.xim) numpy.testing.assert_almost_equal(corr2_output['xim']/gg.xim, 1., decimal=3) print('xip_im from corr2 output = ',corr2_output['xip_im']) assert max(abs(corr2_output['xip_im']))/req_factor < 3.e-7 print('xim_im from corr2 output = ',corr2_output['xim_im']) assert max(abs(corr2_output['xim_im']))/req_factor < 2.e-7
def test_kg(): # Use gamma_t(r) = gamma0 exp(-r^2/2r0^2) around a bunch of foreground lenses. # i.e. gamma(r) = -gamma0 exp(-r^2/2r0^2) (x+iy)^2/r^2 nlens = 1000 nsource = 100000 gamma0 = 0.05 r0 = 10. L = 50. * r0 numpy.random.seed(8675309) xl = (numpy.random.random_sample(nlens)-0.5) * L yl = (numpy.random.random_sample(nlens)-0.5) * L xs = (numpy.random.random_sample(nsource)-0.5) * L ys = (numpy.random.random_sample(nsource)-0.5) * L g1 = numpy.zeros( (nsource,) ) g2 = numpy.zeros( (nsource,) ) kl = numpy.random.normal(0.23, 0.05, (nlens,) ) for x,y,k in zip(xl,yl,kl): dx = xs-x dy = ys-y r2 = dx**2 + dy**2 gammat = gamma0 * numpy.exp(-0.5*r2/r0**2) / k g1 += -gammat * (dx**2-dy**2)/r2 g2 += -gammat * (2.*dx*dy)/r2 lens_cat = treecorr.Catalog(x=xl, y=yl, k=kl, x_units='arcmin', y_units='arcmin') source_cat = treecorr.Catalog(x=xs, y=ys, g1=g1, g2=g2, x_units='arcmin', y_units='arcmin') kg = treecorr.KGCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin', verbose=1) kg.process(lens_cat, source_cat) r = kg.meanr true_gt = gamma0 * numpy.exp(-0.5*r**2/r0**2) print('kg.xi = ',kg.xi) print('kg.xi_im = ',kg.xi_im) print('true_gammat = ',true_gt) print('ratio = ',kg.xi / true_gt) print('diff = ',kg.xi - true_gt) print('max diff = ',max(abs(kg.xi - true_gt))) assert max(abs(kg.xi - true_gt)) < 4.e-3 assert max(abs(kg.xi_im)) < 4.e-3 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': lens_cat.write(os.path.join('data','kg_lens.dat')) source_cat.write(os.path.join('data','kg_source.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"kg.params"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','kg.out'),names=True) print('kg.xi = ',kg.xi) print('from corr2 output = ',corr2_output['kgamT']) print('ratio = ',corr2_output['kgamT']/kg.xi) print('diff = ',corr2_output['kgamT']-kg.xi) numpy.testing.assert_almost_equal(corr2_output['kgamT']/kg.xi, 1., decimal=3) print('xi_im from corr2 output = ',corr2_output['kgamX']) assert max(abs(corr2_output['kgamX'])) < 4.e-3 # Check the fits write option out_file_name1 = os.path.join('output','kg_out1.fits') kg.write(out_file_name1) try: import fitsio data = fitsio.read(out_file_name1) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(kg.logr)) numpy.testing.assert_almost_equal(data['meanR'], kg.meanr) numpy.testing.assert_almost_equal(data['meanlogR'], kg.meanlogr) numpy.testing.assert_almost_equal(data['kgamT'], kg.xi) numpy.testing.assert_almost_equal(data['kgamX'], kg.xi_im) numpy.testing.assert_almost_equal(data['sigma'], numpy.sqrt(kg.varxi)) numpy.testing.assert_almost_equal(data['weight'], kg.weight) numpy.testing.assert_almost_equal(data['npairs'], kg.npairs) except ImportError: print('Unable to import fitsio. Skipping fits tests.') # Check the read function kg2 = treecorr.KGCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin') kg2.read(out_file_name1) numpy.testing.assert_almost_equal(kg2.logr, kg.logr) numpy.testing.assert_almost_equal(kg2.meanr, kg.meanr) numpy.testing.assert_almost_equal(kg2.meanlogr, kg.meanlogr) numpy.testing.assert_almost_equal(kg2.xi, kg.xi) numpy.testing.assert_almost_equal(kg2.xi_im, kg.xi_im) numpy.testing.assert_almost_equal(kg2.varxi, kg.varxi) numpy.testing.assert_almost_equal(kg2.weight, kg.weight) numpy.testing.assert_almost_equal(kg2.npairs, kg.npairs)
def test_aardvark(): # Eric Suchyta did a brute force calculation of the Aardvark catalog, so it is useful to # compare the output from my code with that. get_from_wiki('Aardvark.fit') file_name = os.path.join('data','Aardvark.fit') config = treecorr.read_config('Aardvark.yaml') cat1 = treecorr.Catalog(file_name, config) gg = treecorr.GGCorrelation(config) gg.process(cat1) direct_file_name = os.path.join('data','Aardvark.direct') direct_data = numpy.genfromtxt(direct_file_name) direct_xip = direct_data[:,3] direct_xim = direct_data[:,4] #print('gg.xip = ',gg.xip) #print('direct.xip = ',direct_xip) xip_err = gg.xip - direct_xip print('xip_err = ',xip_err) print('max = ',max(abs(xip_err))) assert max(abs(xip_err)) < 2.e-7 print('xip_im = ',gg.xip_im) print('max = ',max(abs(gg.xip_im))) assert max(abs(gg.xip_im)) < 3.e-7 xim_err = gg.xim - direct_xim print('xim_err = ',xim_err) print('max = ',max(abs(xim_err))) assert max(abs(xim_err)) < 1.e-7 print('xim_im = ',gg.xim_im) print('max = ',max(abs(gg.xim_im))) assert max(abs(gg.xim_im)) < 1.e-7 # However, after some back and forth about the calculation, we concluded that Eric hadn't # done the spherical trig correctly to get the shears relative to the great circle joining # the two positions. So let's compare with my own brute force calculation (i.e. using # bin_slop = 0): # This also has the advantage that the radial bins are done the same way -- uniformly # spaced in log of the chord distance, rather than the great circle distance. bs0_file_name = os.path.join('data','Aardvark.bs0') bs0_data = numpy.genfromtxt(bs0_file_name) bs0_xip = bs0_data[:,2] bs0_xim = bs0_data[:,3] #print('gg.xip = ',gg.xip) #print('bs0.xip = ',bs0_xip) xip_err = gg.xip - bs0_xip print('xip_err = ',xip_err) print('max = ',max(abs(xip_err))) assert max(abs(xip_err)) < 1.e-7 xim_err = gg.xim - bs0_xim print('xim_err = ',xim_err) print('max = ',max(abs(xim_err))) assert max(abs(xim_err)) < 5.e-8 # Check that we get the same result using the corr2 executable: # Note: This is the only test of the corr2 executable that we do with nosetests. # The other similar tests are blocked out with: if __name__ == '__main__': import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"Aardvark.yaml"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','Aardvark.out'), names=True) print('gg.xip = ',gg.xip) print('from corr2 output = ',corr2_output['xip']) print('ratio = ',corr2_output['xip']/gg.xip) print('diff = ',corr2_output['xip']-gg.xip) numpy.testing.assert_almost_equal(corr2_output['xip']/gg.xip, 1., decimal=3) print('gg.xim = ',gg.xim) print('from corr2 output = ',corr2_output['xim']) print('ratio = ',corr2_output['xim']/gg.xim) print('diff = ',corr2_output['xim']-gg.xim) numpy.testing.assert_almost_equal(corr2_output['xim']/gg.xim, 1., decimal=3) print('xip_im from corr2 output = ',corr2_output['xip_im']) print('max err = ',max(abs(corr2_output['xip_im']))) assert max(abs(corr2_output['xip_im'])) < 3.e-7 print('xim_im from corr2 output = ',corr2_output['xim_im']) print('max err = ',max(abs(corr2_output['xim_im']))) assert max(abs(corr2_output['xim_im'])) < 1.e-7 # As bin_slop decreases, the agreement should get even better. # This test is slow, so only do it if running test_gg.py directly. if __name__ == '__main__': config['bin_slop'] = 0.2 gg = treecorr.GGCorrelation(config) gg.process(cat1) #print('gg.xip = ',gg.xip) #print('bs0.xip = ',bs0_xip) xip_err = gg.xip - bs0_xip print('xip_err = ',xip_err) print('max = ',max(abs(xip_err))) assert max(abs(xip_err)) < 1.e-8 xim_err = gg.xim - bs0_xim print('xim_err = ',xim_err) print('max = ',max(abs(xim_err))) assert max(abs(xim_err)) < 1.e-8
def test_nk(): # Use kappa(r) = kappa0 exp(-r^2/2r0^2) (1-r^2/2r0^2) around many lenses. nlens = 1000 nsource = 100000 kappa0 = 0.05 r0 = 10. L = 50. * r0 numpy.random.seed(8675309) xl = (numpy.random.random_sample(nlens)-0.5) * L yl = (numpy.random.random_sample(nlens)-0.5) * L xs = (numpy.random.random_sample(nsource)-0.5) * L ys = (numpy.random.random_sample(nsource)-0.5) * L k = numpy.zeros( (nsource,) ) for x,y in zip(xl,yl): dx = xs-x dy = ys-y r2 = dx**2 + dy**2 k += kappa0 * numpy.exp(-0.5*r2/r0**2) * (1.-0.5*r2/r0**2) lens_cat = treecorr.Catalog(x=xl, y=yl, x_units='arcmin', y_units='arcmin') source_cat = treecorr.Catalog(x=xs, y=ys, 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) # log(<R>) != <logR>, but it should be close: print('meanlogr - log(meanr) = ',nk.meanlogr - numpy.log(nk.meanr)) numpy.testing.assert_almost_equal(nk.meanlogr, numpy.log(nk.meanr), decimal=3) 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)) < 5.e-3 nrand = nlens * 13 xr = (numpy.random.random_sample(nrand)-0.5) * L yr = (numpy.random.random_sample(nrand)-0.5) * L rand_cat = treecorr.Catalog(x=xr, y=yr, x_units='arcmin', y_units='arcmin') rk = treecorr.NKCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin', verbose=1) rk.process(rand_cat, source_cat) print('rk.xi = ',rk.xi) xi, varxi = nk.calculateXi(rk) print('compensated xi = ',xi) print('true_kappa = ',true_k) print('ratio = ',xi / true_k) print('diff = ',xi - true_k) print('max diff = ',max(abs(xi - true_k))) # It turns out this doesn't come out much better. I think the imprecision is mostly just due # to the smallish number of lenses, not to edge effects assert max(abs(xi - true_k)) < 5.e-3 # Check that we get the same result using the corr2 executable: if __name__ == '__main__': lens_cat.write(os.path.join('data','nk_lens.dat')) source_cat.write(os.path.join('data','nk_source.dat')) rand_cat.write(os.path.join('data','nk_rand.dat')) import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"nk.yaml"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','nk.out'), names=True) print('nk.xi = ',nk.xi) print('xi = ',xi) print('from corr2 output = ',corr2_output['kappa']) print('ratio = ',corr2_output['kappa']/xi) print('diff = ',corr2_output['kappa']-xi) numpy.testing.assert_almost_equal(corr2_output['kappa']/xi, 1., decimal=3) # Check the fits write option out_file_name1 = os.path.join('output','nk_out1.fits') nk.write(out_file_name1) data = fitsio.read(out_file_name1) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(nk.logr)) numpy.testing.assert_almost_equal(data['meanR'], nk.meanr) numpy.testing.assert_almost_equal(data['meanlogR'], nk.meanlogr) numpy.testing.assert_almost_equal(data['kappa'], nk.xi) numpy.testing.assert_almost_equal(data['sigma'], numpy.sqrt(nk.varxi)) numpy.testing.assert_almost_equal(data['weight'], nk.weight) numpy.testing.assert_almost_equal(data['npairs'], nk.npairs) out_file_name2 = os.path.join('output','nk_out2.fits') nk.write(out_file_name2, rk) data = fitsio.read(out_file_name2) numpy.testing.assert_almost_equal(data['R_nom'], numpy.exp(nk.logr)) numpy.testing.assert_almost_equal(data['meanR'], nk.meanr) numpy.testing.assert_almost_equal(data['meanlogR'], nk.meanlogr) numpy.testing.assert_almost_equal(data['kappa'], xi) numpy.testing.assert_almost_equal(data['sigma'], numpy.sqrt(varxi)) numpy.testing.assert_almost_equal(data['weight'], nk.weight) numpy.testing.assert_almost_equal(data['npairs'], nk.npairs) # Check the read function nk2 = treecorr.NKCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin') nk2.read(out_file_name1) numpy.testing.assert_almost_equal(nk2.logr, nk.logr) numpy.testing.assert_almost_equal(nk2.meanr, nk.meanr) numpy.testing.assert_almost_equal(nk2.meanlogr, nk.meanlogr) numpy.testing.assert_almost_equal(nk2.xi, nk.xi) numpy.testing.assert_almost_equal(nk2.varxi, nk.varxi) numpy.testing.assert_almost_equal(nk2.weight, nk.weight) numpy.testing.assert_almost_equal(nk2.npairs, nk.npairs)
def test_perp_minmax(): """This test is based on a bug report from Erika Wagoner where the lowest bins were getting spuriously high w(rp) values. It stemmed from a subtlety about how large rp can be compared to minsep. The maximum rp is more than just rp + s1 + s2. So this test checks that when the min and max are expanded a bit, the number of pairs doesn't change much in the bins that used to be the min and max. """ # Just use Erika's files for data and rand. config = { 'ra_col' : 1, 'dec_col' : 2, 'ra_units' : 'deg', 'dec_units' : 'deg', 'r_col' : 3, 'min_sep' : 40, 'bin_size' : 0.036652, 'nbins' : 50, 'verbose' : 1 } # Speed up for nosetests runs if __name__ != "__main__": config['nbins'] = 5 config['min_sep'] = 20 config['bin_size'] = 0.1 dcat = treecorr.Catalog('data/nn_perp_data.dat', config) dd1 = treecorr.NNCorrelation(config) dd1.process(dcat, metric='Rperp') lower_min_sep = config['min_sep'] * numpy.exp(-2.*config['bin_size']) more_nbins = config['nbins'] + 4 dd2 = treecorr.NNCorrelation(config, min_sep=lower_min_sep, nbins=more_nbins) dd2.process(dcat, metric='Rperp') print('dd1 npairs = ',dd1.npairs) print('dd2 npairs = ',dd2.npairs[2:-2]) # First a basic sanity check. The values not near the edge should be identical. numpy.testing.assert_equal(dd1.npairs[2:-2], dd2.npairs[4:-4]) # The edge bins may differ slightly from the binning approximations (bin_slop and such), # but the differences should be very small. (When Erika reported the problem, the differences # were a few percent, which ended up making a bit difference in the correlation function.) numpy.testing.assert_almost_equal( dd1.npairs / dd2.npairs[2:-2], 1., decimal=4) if __name__ == '__main__': # If we're running from the command line, go ahead and finish the calculation # This catalog has 10^6 objects, which takes quite a while. I should really investigate # how to speed up the Rperp distance calculation. Probably by having a faster over- # and under-estimate first, and then only do the full calculation when it seems like we # will actually need it. # Anyway, until then, let's not take forever by using last_row=200000 rcat = treecorr.Catalog('data/nn_perp_rand.dat', config, last_row=200000) rr1 = treecorr.NNCorrelation(config) rr1.process(rcat, metric='Rperp') rr2 = treecorr.NNCorrelation(config, min_sep=lower_min_sep, nbins=more_nbins) rr2.process(rcat, metric='Rperp') print('rr1 npairs = ',rr1.npairs) print('rr2 npairs = ',rr2.npairs[2:-2]) numpy.testing.assert_almost_equal( rr1.npairs / rr2.npairs[2:-2], 1., decimal=4) dr1 = treecorr.NNCorrelation(config) dr1.process(dcat, rcat, metric='Rperp') dr2 = treecorr.NNCorrelation(config, min_sep=lower_min_sep, nbins=more_nbins) dr2.process(dcat, rcat, metric='Rperp') print('dr1 npairs = ',dr1.npairs) print('dr2 npairs = ',dr2.npairs[2:-2]) numpy.testing.assert_almost_equal( dr1.npairs / dr2.npairs[2:-2], 1., decimal=4) xi1, varxi1 = dd1.calculateXi(rr1, dr1) xi2, varxi2 = dd2.calculateXi(rr2, dr2) print('xi1 = ',xi1) print('xi2 = ',xi2[2:-2]) numpy.testing.assert_almost_equal( xi1 / xi2[2:-2], 1., decimal=2) # Check that we get the same result with the corr2 executable. import subprocess corr2_exe = get_script_name('corr2') p = subprocess.Popen( [corr2_exe,"nn_rperp.params"] ) p.communicate() corr2_output = numpy.genfromtxt(os.path.join('output','nn_rperp.out'),names=True) print('xi = ',xi1) print('from corr2 output = ',corr2_output['xi']) print('ratio = ',corr2_output['xi']/xi1) print('diff = ',corr2_output['xi']-xi1) numpy.testing.assert_almost_equal(corr2_output['xi']/xi1, 1., decimal=3)