Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
for i in range(nrand):
fid.write(('%.20f %.20f\n')%(rx1[i],ry1[i]))
rand_file_name2 = os.path.join('data','nn_periodic_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, xperiod=Lx, yperiod=Ly)
rr.process(rcat1,rcat2, metric='Periodic')
xi, varxi = dd.calculateXi(rr)
print('xi = ',xi)
# Do this via the corr2 function.
config = treecorr.config.read_config('configs/nn_periodic.yaml')
logger = treecorr.config.setup_logger(2)
treecorr.corr2(config, logger)
corr2_output = np.genfromtxt(os.path.join('output','nn_periodic.out'), names=True,
skip_header=1)
np.testing.assert_allclose(corr2_output['r_nom'], dd.rnom, rtol=1.e-3)
np.testing.assert_allclose(corr2_output['DD'], dd.npairs, rtol=1.e-3)
np.testing.assert_allclose(corr2_output['npairs'], dd.npairs, rtol=1.e-3)
np.testing.assert_allclose(corr2_output['RR'], rr.npairs, rtol=1.e-3)
np.testing.assert_allclose(corr2_output['xi'], xi, rtol=1.e-3)
# If don't give a period, then an error.
rr = treecorr.NNCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins)
with assert_raises(ValueError):
rr.process(rcat1,rcat2, metric='Periodic')
# Or if only give one kind of period
rr = treecorr.NNCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins, xperiod=3)
with assert_raises(ValueError):
print('true_xi = ',true_xi)
print('kk.xi = ',kk.xi)
np.testing.assert_allclose(kk.xi, true_xi, rtol=1.e-4, atol=1.e-8)
try:
import fitsio
except ImportError:
print('Skipping FITS tests, since fitsio is not installed')
return
# Check that running via the corr2 script works correctly.
config = treecorr.config.read_config('configs/kk_direct_spherical.yaml')
cat1.write(config['file_name'])
cat2.write(config['file_name2'])
treecorr.corr2(config)
data = fitsio.read(config['kk_file_name'])
np.testing.assert_allclose(data['r_nom'], kk.rnom)
np.testing.assert_allclose(data['npairs'], kk.npairs)
np.testing.assert_allclose(data['weight'], kk.weight)
np.testing.assert_allclose(data['xi'], kk.xi, rtol=1.e-3)
# Repeat with binslop = 0
# And don't do any top-level recursion so we actually test not going to the leaves.
kk = treecorr.KKCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins,
sep_units='deg', bin_slop=0, max_top=0)
kk.process(cat1, cat2)
np.testing.assert_array_equal(kk.npairs, true_npairs)
np.testing.assert_allclose(kk.weight, true_weight, rtol=1.e-5, atol=1.e-8)
np.testing.assert_allclose(kk.xi, true_xi, rtol=1.e-3, atol=1.e-6)
print('true_xi = ',true_xi)
print('kk.xi = ',kk.xi)
np.testing.assert_allclose(kk.xi, true_xi, rtol=1.e-4, atol=1.e-8)
try:
import fitsio
except ImportError:
print('Skipping FITS tests, since fitsio is not installed')
return
# Check that running via the corr2 script works correctly.
config = treecorr.config.read_config('configs/kk_direct.yaml')
cat1.write(config['file_name'])
cat2.write(config['file_name2'])
treecorr.corr2(config)
data = fitsio.read(config['kk_file_name'])
np.testing.assert_allclose(data['r_nom'], kk.rnom)
np.testing.assert_allclose(data['npairs'], kk.npairs)
np.testing.assert_allclose(data['weight'], kk.weight)
np.testing.assert_allclose(data['xi'], kk.xi, rtol=1.e-3)
# Repeat with binslop = 0
# And don't do any top-level recursion so we actually test not going to the leaves.
kk = treecorr.KKCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins, bin_slop=0,
max_top=0)
kk.process(cat1, cat2)
np.testing.assert_array_equal(kk.npairs, true_npairs)
np.testing.assert_allclose(kk.weight, true_weight, rtol=1.e-5, atol=1.e-8)
np.testing.assert_allclose(kk.xi, true_xi, rtol=1.e-4, atol=1.e-8)
# Check a few basic operations with a KKCorrelation object.
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 function:
lens_cat.write(os.path.join('data','gg_oldrperp_lens.dat'))
source_cat.write(os.path.join('data','gg_oldrperp_source.dat'))
config = treecorr.read_config('configs/gg_oldrperp.yaml')
config['verbose'] = 0
treecorr.corr2(config)
corr2_output = np.genfromtxt(os.path.join('output','gg_oldrperp.out'),names=True,
skip_header=1)
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)
np.testing.assert_allclose(corr2_output['xim'], gg.xim, rtol=1.e-3)
np.testing.assert_allclose(corr2_output['xim_im'], gg.xim_im, rtol=1.e-3)
np.testing.assert_allclose(corr2_output['xip'], gg.xip, rtol=1.e-3)
np.testing.assert_allclose(corr2_output['xip_im'], gg.xip_im, rtol=1.e-3)
assert max(abs(gg.xip_im)) < 3.e-7 * tol_factor
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)) < 2.e-7 * tol_factor
assert max(abs(gg.xim_im)) < 2.e-7 * tol_factor
# Check that we get the same result using the corr2 function
cat.write(os.path.join('data','gg_spherical.dat'))
config = treecorr.read_config('configs/gg_spherical.yaml')
config['verbose'] = 0
treecorr.corr2(config)
corr2_output = np.genfromtxt(os.path.join('output','gg_spherical.out'), names=True,
skip_header=1)
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)
np.testing.assert_allclose(corr2_output['xip'], gg.xip, rtol=1.e-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)
np.testing.assert_allclose(corr2_output['xim'], gg.xim, rtol=1.e-3)
print('xip_im from corr2 output = ',corr2_output['xip_im'])
assert max(abs(corr2_output['xip_im'])) < 3.e-7 * tol_factor
corr2_output2 = np.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)
np.testing.assert_allclose(corr2_output2['Mapsq'], mapsq, rtol=1.e-4)
print('mxsq = ',mxsq)
print('from corr2 output = ',corr2_output2['Mxsq'])
print('ratio = ',corr2_output2['Mxsq']/mxsq)
print('diff = ',corr2_output2['Mxsq']-mxsq)
np.testing.assert_allclose(corr2_output2['Mxsq'], mxsq, rtol=1.e-4)
# OK to have m2 output, but not gg
del config['gg_file_name']
treecorr.corr2(config)
corr2_output2 = np.genfromtxt(os.path.join('output','gg_m2.out'), names=True)
np.testing.assert_allclose(corr2_output2['Mapsq'], mapsq, rtol=1.e-4)
np.testing.assert_allclose(corr2_output2['Mxsq'], mxsq, rtol=1.e-4)
try:
import fitsio
except ImportError:
print('Skipping FITS tests, since fitsio is not installed')
return
# 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)
np.testing.assert_allclose(data['r_nom'], np.exp(gg.logr))
np.testing.assert_allclose(data['meanr'], gg.meanr)
print('diff = ',kk.xi - true_xi)
print('max diff = ',max(abs(kk.xi - true_xi)))
print('max rel diff = ',max(abs((kk.xi - true_xi)/true_xi)))
np.testing.assert_allclose(kk.xi, true_xi, rtol=0.1*tol_factor)
# It should also work as a cross-correlation of this cat with itself
kk.process(cat,cat)
np.testing.assert_allclose(kk.meanlogr, np.log(kk.meanr), atol=1.e-3)
np.testing.assert_allclose(kk.xi, true_xi, rtol=0.1*tol_factor)
# Check that we get the same result using the corr2 function
cat.write(os.path.join('data','kk.dat'))
config = treecorr.read_config('configs/kk.yaml')
config['verbose'] = 0
config['precision'] = 8
treecorr.corr2(config)
corr2_output = np.genfromtxt(os.path.join('output','kk.out'), names=True, skip_header=1)
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)
np.testing.assert_allclose(corr2_output['xi'], kk.xi, rtol=1.e-3)
try:
import fitsio
except ImportError:
print('Skipping FITS tests, since fitsio is not installed')
return
# Check the fits write option
out_file_name = os.path.join('output','kk_out.fits')
kk.write(out_file_name)
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 function
lens_cat.write(os.path.join('data','gg_rperp_lens.dat'))
source_cat.write(os.path.join('data','gg_rperp_source.dat'))
config = treecorr.read_config('configs/gg_rperp.yaml')
config['verbose'] = 0
treecorr.corr2(config)
corr2_output = np.genfromtxt(os.path.join('output','gg_rperp.out'),names=True,
skip_header=1)
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)
np.testing.assert_allclose(corr2_output['xim'], gg.xim, rtol=1.e-3)
np.testing.assert_allclose(corr2_output['xim_im'], gg.xim_im, rtol=1.e-3)
np.testing.assert_allclose(corr2_output['xip'], gg.xip, rtol=1.e-3)
np.testing.assert_allclose(corr2_output['xip_im'], gg.xip_im, rtol=1.e-3)
treecorr.corr2(config)
corr2_output = np.genfromtxt(os.path.join('output','nk_single.out'), names=True,
skip_header=1)
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)
np.testing.assert_allclose(corr2_output['kappa'], nk.xi, rtol=1.e-3)
# There is special handling for single-row catalogs when using np.genfromtxt rather
# than pandas. So mock it up to make sure we test it.
if sys.version_info < (3,): return # mock only available on python 3
from unittest import mock
with mock.patch.dict(sys.modules, {'pandas':None}):
with CaptureLog() as cl:
treecorr.corr2(config, logger=cl.logger)
assert "Unable to import pandas" in cl.output
corr2_output = np.genfromtxt(os.path.join('output','nk_single.out'), names=True,
skip_header=1)
np.testing.assert_allclose(corr2_output['kappa'], nk.xi, rtol=1.e-3)