diff --git a/halotools/mock_observables/two_point_clustering/tests/locate_external_unit_testing_data.py b/halotools/mock_observables/two_point_clustering/tests/locate_external_unit_testing_data.py new file mode 100644 index 000000000..842d65faa --- /dev/null +++ b/halotools/mock_observables/two_point_clustering/tests/locate_external_unit_testing_data.py @@ -0,0 +1,57 @@ +""" This module is used to search the user's disk to see whether +data files are present to conduct unit-tests in which Halotools results +are compared against results obtained from independently-written code bases. +""" +from __future__ import absolute_import, division, print_function, unicode_literals + +import os +from astropy.config.paths import _find_home +halotools_cache_dirname = os.path.join(_find_home(), '.astropy', 'cache', 'halotools') +halotool_unit_testing_dirname = os.path.join(halotools_cache_dirname, 'unit_testing_files') + + +__all__ = ('tpcf_corrfunc_comparison_files_exist', 'wp_corrfunc_comparison_files_exist') + + +def tpcf_corrfunc_comparison_files_exist(return_fnames=False): + """ + """ + aph_fname1 = os.path.join(halotool_unit_testing_dirname, 'sample1_position_array.npy') + aph_fname2 = os.path.join(halotool_unit_testing_dirname, 'sample2_position_array.npy') + aph_fname3 = os.path.join(halotool_unit_testing_dirname, 'rp_bins_array.npy') + + deep_fname1 = os.path.join(halotool_unit_testing_dirname, + 'sinha_corrfunc_results', 'sample1_position_array_xi.npy') + deep_fname2 = os.path.join(halotool_unit_testing_dirname, + 'sinha_corrfunc_results', 'sample2_position_array_xi.npy') + + all_files_exist = ( + os.path.isfile(aph_fname1) & os.path.isfile(aph_fname2) & os.path.isfile(aph_fname3) & + os.path.isfile(deep_fname1) & os.path.isfile(deep_fname2)) + + if return_fnames is False: + return all_files_exist + else: + return all_files_exist, aph_fname1, aph_fname2, aph_fname3, deep_fname1, deep_fname2 + + +def wp_corrfunc_comparison_files_exist(return_fnames=False): + """ + """ + aph_fname1 = os.path.join(halotool_unit_testing_dirname, 'sample1_position_array.npy') + aph_fname2 = os.path.join(halotool_unit_testing_dirname, 'sample2_position_array.npy') + aph_fname3 = os.path.join(halotool_unit_testing_dirname, 'rp_bins_array.npy') + + deep_fname1 = os.path.join(halotool_unit_testing_dirname, + 'sinha_corrfunc_results', 'sample1_position_array_wp.npy') + deep_fname2 = os.path.join(halotool_unit_testing_dirname, + 'sinha_corrfunc_results', 'sample2_position_array_wp.npy') + + all_files_exist = ( + os.path.isfile(aph_fname1) & os.path.isfile(aph_fname2) & os.path.isfile(aph_fname3) & + os.path.isfile(deep_fname1) & os.path.isfile(deep_fname2)) + + if return_fnames is False: + return all_files_exist + else: + return all_files_exist, aph_fname1, aph_fname2, aph_fname3, deep_fname1, deep_fname2 diff --git a/halotools/mock_observables/two_point_clustering/tests/test_tpcf.py b/halotools/mock_observables/two_point_clustering/tests/test_tpcf.py index 1da239d7c..1658f035c 100644 --- a/halotools/mock_observables/two_point_clustering/tests/test_tpcf.py +++ b/halotools/mock_observables/two_point_clustering/tests/test_tpcf.py @@ -8,6 +8,8 @@ from astropy.tests.helper import pytest from astropy.utils.misc import NumpyRNGContext +from .locate_external_unit_testing_data import tpcf_corrfunc_comparison_files_exist + from ..tpcf import tpcf from ....custom_exceptions import HalotoolsError @@ -19,6 +21,7 @@ 'test_tpcf_period_API', 'test_tpcf_cross_consistency_w_auto') fixed_seed = 43 +TPCF_CORRFUNC_FILES_EXIST = tpcf_corrfunc_comparison_files_exist() @slow @@ -576,3 +579,28 @@ def test_tpcf_raises_exception_for_unavailable_estimator(): estimator='Jose Canseco') substr = "is not in the list of available estimators:" assert substr in err.value.args[0] + + +@pytest.mark.skipif('not TPCF_CORRFUNC_FILES_EXIST') +def test_tpcf_vs_corrfunc(): + """ + """ + msg = ("This unit-test compares the tpcf results from halotools \n" + "against the results derived from the Corrfunc code managed by \n" + "Manodeep Sinha. ") + __, aph_fname1, aph_fname2, aph_fname3, deep_fname1, deep_fname2 = ( + tpcf_corrfunc_comparison_files_exist(return_fnames=True)) + + sinha_sample1_xi = np.load(deep_fname1)[:, 0] + sinha_sample2_xi = np.load(deep_fname2)[:, 0] + + sample1 = np.load(aph_fname1) + sample2 = np.load(aph_fname2) + rbins = np.load(aph_fname3) + + halotools_result1 = tpcf(sample1, rbins, period=250.0) + assert np.allclose(halotools_result1, sinha_sample1_xi, rtol=1e-5), msg + + halotools_result2 = tpcf(sample2, rbins, period=250.0) + assert np.allclose(halotools_result2, sinha_sample2_xi, rtol=1e-5), msg + diff --git a/halotools/mock_observables/two_point_clustering/tests/test_wp.py b/halotools/mock_observables/two_point_clustering/tests/test_wp.py index 9a29efc87..58a241b39 100755 --- a/halotools/mock_observables/two_point_clustering/tests/test_wp.py +++ b/halotools/mock_observables/two_point_clustering/tests/test_wp.py @@ -6,6 +6,7 @@ from astropy.utils.misc import NumpyRNGContext from astropy.tests.helper import pytest +from .locate_external_unit_testing_data import wp_corrfunc_comparison_files_exist from ..wp import wp __all__ = ('test_wp_auto_nonperiodic', 'test_wp_auto_periodic', 'test_wp_cross_periodic', @@ -17,6 +18,8 @@ fixed_seed = 43 +WP_CORRFUNC_FILES_EXIST = wp_corrfunc_comparison_files_exist() + def test_wp_auto_nonperiodic(): """ @@ -87,3 +90,28 @@ def test_wp_cross_nonperiodic(): assert result[0].ndim == 1, "dimension of auto incorrect" assert result[1].ndim == 1, "dimension of cross incorrect" assert result[2].ndim == 1, "dimension auto incorrect" + + +@pytest.mark.skipif('not WP_CORRFUNC_FILES_EXIST') +def test_wp_vs_corrfunc(): + """ + """ + msg = ("This unit-test compares the wp results from halotools \n" + "against the results derived from the Corrfunc code managed by \n" + "Manodeep Sinha. ") + __, aph_fname1, aph_fname2, aph_fname3, deep_fname1, deep_fname2 = ( + wp_corrfunc_comparison_files_exist(return_fnames=True)) + + sinha_sample1_wp = np.load(deep_fname1)[:, 0] + sinha_sample2_wp = np.load(deep_fname2)[:, 0] + + sample1 = np.load(aph_fname1) + sample2 = np.load(aph_fname2) + rp_bins = np.load(aph_fname3) + pi_max = 40.0 + + halotools_result1 = wp(sample1, rp_bins, pi_max, period=250.0) + assert np.allclose(halotools_result1, sinha_sample1_wp, rtol=1e-3), msg + + halotools_result2 = wp(sample2, rp_bins, pi_max, period=250.0) + assert np.allclose(halotools_result2, sinha_sample2_wp, rtol=1e-3), msg