Skip to content

Commit

Permalink
Incorporated wp and tpcf results provided by @manodeep into the Halot…
Browse files Browse the repository at this point in the history
…ools

unit-testing suite.
  • Loading branch information
Andrew Hearin committed Jun 9, 2016
1 parent e18647e commit 9debd74
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions halotools/mock_observables/two_point_clustering/tests/test_tpcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

28 changes: 28 additions & 0 deletions halotools/mock_observables/two_point_clustering/tests/test_wp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -17,6 +18,8 @@

fixed_seed = 43

WP_CORRFUNC_FILES_EXIST = wp_corrfunc_comparison_files_exist()


def test_wp_auto_nonperiodic():
"""
Expand Down Expand Up @@ -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

0 comments on commit 9debd74

Please sign in to comment.