Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
np.testing.assert_allclose(ng3.xi_im, ng.xi_im)
fits_name = 'output/ng_fits.fits'
ng.write(fits_name)
ng4 = treecorr.NGCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins)
ng4.read(fits_name)
np.testing.assert_allclose(ng4.npairs, ng.npairs)
np.testing.assert_allclose(ng4.weight, ng.weight)
np.testing.assert_allclose(ng4.meanr, ng.meanr)
np.testing.assert_allclose(ng4.meanlogr, ng.meanlogr)
np.testing.assert_allclose(ng4.xi, ng.xi)
np.testing.assert_allclose(ng4.xi_im, ng.xi_im)
with assert_raises(TypeError):
ng2 += config
ng4 = treecorr.NGCorrelation(min_sep=min_sep/2, max_sep=max_sep, nbins=nbins)
with assert_raises(ValueError):
ng2 += ng4
ng5 = treecorr.NGCorrelation(min_sep=min_sep, max_sep=max_sep*2, nbins=nbins)
with assert_raises(ValueError):
ng2 += ng5
ng6 = treecorr.NGCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins*2)
with assert_raises(ValueError):
ng2 += ng6
y=source_cat.y[source_cat.patch != i],
k=source_cat.k[source_cat.patch != i])
nk1 = treecorr.NKCorrelation(bin_size=0.3, min_sep=10., max_sep=30., brute=True)
nk1.process(lens_cat1, source_cat1)
rk1 = treecorr.NKCorrelation(bin_size=0.3, min_sep=10., max_sep=30., brute=True)
rk1.process(rand_lens_cat1, source_cat1)
nk1.calculateXi(rk1)
xi_list.append(nk1.xi)
xi_list = np.array(xi_list)
C = np.cov(xi_list.T, bias=True) * (len(xi_list)-1)
varxi = np.diagonal(C)
print('var = ',varxi)
np.testing.assert_allclose(nk.varxi, varxi)
# Repeat for NG, GG, KK, KG
ng = treecorr.NGCorrelation(bin_size=0.3, min_sep=10., max_sep=30., brute=True,
var_method='jackknife')
ng.process(lens_cat, source_cat)
gg = treecorr.GGCorrelation(bin_size=0.3, min_sep=10., max_sep=30., brute=True,
var_method='jackknife')
gg.process(source_cat)
kk = treecorr.KKCorrelation(bin_size=0.3, min_sep=10., max_sep=30., brute=True,
var_method='jackknife')
kk.process(source_cat)
kg = treecorr.KGCorrelation(bin_size=0.3, min_sep=10., max_sep=30., brute=True,
var_method='jackknife')
kg.process(lens_cat, source_cat)
ng_xi_list = []
gg_xip_list = []
gg_xim_list = []
kk_xi_list = []
print('ratio = ',cov_boot.diagonal() / var_xi)
np.testing.assert_allclose(cov_boot.diagonal(), var_xi, rtol=0.3*tol_factor)
# Check bootstrap covariance estimate.
t0 = time.time()
cov_boot = ng3.estimate_cov('bootstrap')
t1 = time.time()
print('Time to calculate bootstrap covariance = ',t1-t0)
print('varxi = ',cov_boot.diagonal())
print('ratio = ',cov_boot.diagonal() / var_xi)
np.testing.assert_allclose(cov_boot.diagonal(), var_xi, rtol=0.5*tol_factor)
# Use a random catalog
# In this case the locations of the source catalog are fine to use as our random catalog,
# since they fill the region where the lenses are allowed to be.
rg3 = treecorr.NGCorrelation(bin_size=0.4, min_sep=1., max_sep=20.)
t0 = time.time()
rg3.process(cat2p, cat2p)
t1 = time.time()
print('Time for processing RG = ',t1-t0)
ng3b = ng3.copy()
ng3b.calculateXi(rg3)
print('xi = ',ng3b.xi)
print('varxi = ',ng3b.varxi)
print('ratio = ',ng3b.varxi / var_xi)
np.testing.assert_allclose(ng3b.weight, ng3.weight, rtol=0.02*tol_factor)
np.testing.assert_allclose(ng3b.xi, ng3.xi, rtol=0.02*tol_factor)
np.testing.assert_allclose(ng3b.varxi, var_xi, rtol=0.3*tol_factor)
# Check only using patches for one of the two catalogs.
# Not as good as using patches for both, but not much worse.
ng4 = treecorr.NGCorrelation(bin_size=0.3, min_sep=10., max_sep=50., var_method='jackknife')
t0 = time.time()
ng4.process(cat1p, cat2)
t1 = time.time()
print('Time for only patches for cat1 processing = ',t1-t0)
print('weight = ',ng4.weight)
print('xi = ',ng4.xi)
print('varxi = ',ng4.varxi)
np.testing.assert_allclose(ng4.weight, ng1.weight, rtol=1.e-2*tol_factor)
np.testing.assert_allclose(ng4.xi, ng1.xi, rtol=3.e-2*tol_factor)
np.testing.assert_allclose(ng4.varxi, var_xi, rtol=0.5*tol_factor)
ng5 = treecorr.NGCorrelation(bin_size=0.3, min_sep=10., max_sep=50., var_method='jackknife')
t0 = time.time()
ng5.process(cat1, cat2p)
t1 = time.time()
print('Time for only patches for cat2 processing = ',t1-t0)
print('weight = ',ng5.weight)
print('xi = ',ng5.xi)
print('varxi = ',ng5.varxi)
np.testing.assert_allclose(ng5.weight, ng1.weight, rtol=1.e-2*tol_factor)
np.testing.assert_allclose(ng5.xi, ng1.xi, rtol=3.e-2*tol_factor)
np.testing.assert_allclose(ng5.varxi, var_xi, rtol=0.4*tol_factor)
# Check sample covariance estimate
t0 = time.time()
cov_sample = ng3.estimate_cov('sample')
t1 = time.time()
print('Time to calculate sample covariance = ',t1-t0)
np.testing.assert_allclose(data['gamT'], ng.xi, rtol=1.e-3)
np.testing.assert_allclose(data['gamX'], ng.xi_im, rtol=1.e-3)
# Invalid with only one file_name
del config['file_name2']
with assert_raises(TypeError):
treecorr.corr2(config)
config['file_name2'] = 'data/ng_direct_cat2.fits'
# Invalid to request compoensated if no rand_file
config['ng_statistic'] = 'compensated'
with assert_raises(TypeError):
treecorr.corr2(config)
# Repeat with binslop = 0
# And don't do any top-level recursion so we actually test not going to the leaves.
ng = treecorr.NGCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins, bin_slop=0,
max_top=0)
ng.process(cat1, cat2)
np.testing.assert_array_equal(ng.npairs, true_npairs)
np.testing.assert_allclose(ng.weight, true_weight, rtol=1.e-5, atol=1.e-8)
np.testing.assert_allclose(ng.xi, true_xi.real, rtol=1.e-3, atol=3.e-4)
np.testing.assert_allclose(ng.xi_im, true_xi.imag, atol=3.e-4)
# Check a few basic operations with a NGCorrelation object.
do_pickle(ng)
ng2 = ng.copy()
ng2 += ng
np.testing.assert_allclose(ng2.npairs, 2*ng.npairs)
np.testing.assert_allclose(ng2.weight, 2*ng.weight)
np.testing.assert_allclose(ng2.meanr, 2*ng.meanr)
np.testing.assert_allclose(ng2.meanlogr, 2*ng.meanlogr)
x3 = (rng.random_sample(nrand)-0.5) * L
y3 = (rng.random_sample(nrand)-0.5) * L
r2 = (x2**2 + y2**2)/r0**2
g1 = -gamma0 * np.exp(-r2/2.) * (x2**2-y2**2)/r0**2
g2 = -gamma0 * np.exp(-r2/2.) * (2.*x2*y2)/r0**2
# This time, add some shape noise (different each run).
g1 += rng.normal(0, 0.1, size=nsource)
g2 += rng.normal(0, 0.1, size=nsource)
# Varied weights are hard, but at least check that non-unit weights work correctly.
w = np.ones_like(x2) * 5
source = treecorr.Catalog(x=x2, y=y2, w=w, g1=g1, g2=g2)
rand = treecorr.Catalog(x=x3, y=y3)
ng = treecorr.NGCorrelation(bin_size=0.1, min_sep=5., max_sep=20.)
rg = treecorr.NGCorrelation(bin_size=0.1, min_sep=5., max_sep=20.)
ng.process(lens, source)
rg.process(rand, source)
all_ngs.append(ng)
all_rgs.append(rg)
print('Uncompensated:')
all_xis = [ng.calculateXi() for ng in all_ngs]
mean_wt = np.mean([ng.weight for ng in all_ngs], axis=0)
mean_xi = np.mean([xi[0] for xi in all_xis], axis=0)
var_xi = np.var([xi[0] for xi in all_xis], axis=0)
mean_varxi = np.mean([xi[2] for xi in all_xis], axis=0)
print('mean_xi = ',mean_xi)
print('mean_wt = ',mean_wt)
print('mean_varxi = ',mean_varxi)
ng0 = treecorr.NGCorrelation(max_sep=max_sep, nbins=nbins, bin_type='TwoD', brute=True)
t0 = time.time()
ng0.process(cat, cat)
t1 = time.time()
print('t for bs=0 = ',t1-t0)
ng1 = treecorr.NGCorrelation(max_sep=max_sep, nbins=nbins, bin_type='TwoD', bin_slop=0)
t0 = time.time()
ng1.process(cat, cat)
t1 = time.time()
print('t for bs=1.e-10 = ',t1-t0)
print('max abs diff ng.xi = ',np.max(np.abs(ng1.xi - ng0.xi)))
np.testing.assert_array_equal(ng1.npairs, ng0.npairs)
np.testing.assert_allclose(ng1.xi, ng0.xi, atol=2.e-4)
ng2 = treecorr.NGCorrelation(max_sep=max_sep, nbins=nbins, bin_type='TwoD', bin_slop=0.1)
t0 = time.time()
ng2.process(cat, cat)
t1 = time.time()
print('t for bs=0.1 = ',t1-t0)
print('max abs diff npairs = ',np.max(np.abs(ng2.npairs - ng0.npairs)))
print('max rel diff npairs = ',np.max(np.abs(ng2.npairs - ng0.npairs)/np.abs(ng0.npairs)))
print('max abs diff ng.xi = ',np.max(np.abs(ng2.xi - ng0.xi)))
np.testing.assert_allclose(ng2.npairs, ng0.npairs, rtol=1.e-2)
np.testing.assert_allclose(ng2.xi, ng0.xi, atol=5.e-4)
kg1 = treecorr.KGCorrelation(max_sep=max_sep, nbins=nbins, bin_type='TwoD', bin_slop=0)
t0 = time.time()
kg1.process(cat, cat)
t1 = time.time()
print('t for bs=1.e-10 = ',t1-t0)
print('ng0.xi = ',ng0.xi)
gg2 = treecorr.GGCorrelation(max_sep=max_sep, nbins=nbins, bin_type='TwoD', bin_slop=0.1,
max_top=4)
t0 = time.time()
gg2.process(cat)
t1 = time.time()
print('t for bs=0.1 = ',t1-t0)
print('max abs diff npairs = ',np.max(np.abs(gg2.npairs - gg0.npairs)))
print('max rel diff npairs = ',np.max(np.abs(gg2.npairs - gg0.npairs)/np.abs(gg0.npairs)))
print('max abs diff xip = ',np.max(np.abs(gg2.xip - gg0.xip)))
print('max abs diff xim = ',np.max(np.abs(gg2.xim - gg0.xim)))
np.testing.assert_allclose(gg2.npairs, gg0.npairs, rtol=3.e-3)
np.testing.assert_allclose(gg2.xip, gg0.xip, atol=1.e-4)
np.testing.assert_allclose(gg2.xim, gg0.xim, atol=1.e-4)
# Repeat with NG and KG so we can test those routines too.
ng0 = treecorr.NGCorrelation(max_sep=max_sep, nbins=nbins, bin_type='TwoD', brute=True)
t0 = time.time()
ng0.process(cat, cat)
t1 = time.time()
print('t for bs=0 = ',t1-t0)
ng1 = treecorr.NGCorrelation(max_sep=max_sep, nbins=nbins, bin_type='TwoD', bin_slop=0)
t0 = time.time()
ng1.process(cat, cat)
t1 = time.time()
print('t for bs=1.e-10 = ',t1-t0)
print('max abs diff ng.xi = ',np.max(np.abs(ng1.xi - ng0.xi)))
np.testing.assert_array_equal(ng1.npairs, ng0.npairs)
np.testing.assert_allclose(ng1.xi, ng0.xi, atol=2.e-4)
ng2 = treecorr.NGCorrelation(max_sep=max_sep, nbins=nbins, bin_type='TwoD', bin_slop=0.1)
t0 = time.time()
# Do NG correlation function if necessary
if 'ng_file_name' in config or 'nm_file_name' in config or 'norm_file_name' in config:
if cat2 is None:
raise TypeError("file_name2 is required for ng correlation")
logger.warning("Performing NG calculations...")
ng = treecorr.NGCorrelation(config,logger)
ng.process(cat1,cat2)
logger.info("Done NG calculation.")
# The default ng_statistic is compensated _iff_ rand files are given.
rg = None
if rand1 is None:
if config.get('ng_statistic',None) == 'compensated':
raise TypeError("rand_files is required for ng_statistic = compensated")
elif config.get('ng_statistic','compensated') == 'compensated':
rg = treecorr.NGCorrelation(config,logger)
rg.process(rand1,cat2)
logger.info("Done RG calculation.")
if 'ng_file_name' in config:
ng.write(config['ng_file_name'], rg)
logger.warning("Wrote NG correlation to %s",config['ng_file_name'])
if 'nm_file_name' in config:
ng.writeNMap(config['nm_file_name'], rg=rg, m2_uform=config['m2_uform'],
precision=config.get('precision',None))
logger.warning("Wrote NMap values to %s",config['nm_file_name'])
if 'norm_file_name' in config:
gg = treecorr.GGCorrelation(config,logger)
gg.process(cat2)
logger.info("Done GG calculation for norm")
dd = treecorr.NNCorrelation(config,logger)
gg = treecorr.GGCorrelation(config,logger)
gg.process(cat1,cat2)
logger.info("Done GG calculations.")
if 'gg_file_name' in config:
gg.write(config['gg_file_name'])
logger.warning("Wrote GG correlation to %s",config['gg_file_name'])
if 'm2_file_name' in config:
gg.writeMapSq(config['m2_file_name'], m2_uform=config['m2_uform'])
logger.warning("Wrote Mapsq values to %s",config['m2_file_name'])
# Do NG correlation function if necessary
if 'ng_file_name' in config or 'nm_file_name' in config or 'norm_file_name' in config:
if cat2 is None:
raise TypeError("file_name2 is required for ng correlation")
logger.warning("Performing NG calculations...")
ng = treecorr.NGCorrelation(config,logger)
ng.process(cat1,cat2)
logger.info("Done NG calculation.")
# The default ng_statistic is compensated _iff_ rand files are given.
rg = None
if rand1 is None:
if config.get('ng_statistic',None) == 'compensated':
raise TypeError("rand_files is required for ng_statistic = compensated")
elif config.get('ng_statistic','compensated') == 'compensated':
rg = treecorr.NGCorrelation(config,logger)
rg.process(rand1,cat2)
logger.info("Done RG calculation.")
if 'ng_file_name' in config:
ng.write(config['ng_file_name'], rg)
logger.warning("Wrote NG correlation to %s",config['ng_file_name'])