Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#85 Fixes to usage of randoms in correlation functions #111

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
7/13/18: Fix usage of randoms in correlation functions (issue #85)
7/11/18: Update code to python3
3/17/16: Update documentation to Sphinx standard and add documentation build files (issue #17)
2/17/16: Changes to correlation function plots & documentation (issue #77)
Expand Down
29 changes: 20 additions & 9 deletions stile/sys_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,16 @@ def makeCatalog(self, data, config=None, use_as_k=None, use_chip_coords=False):
catalog_kwargs['config'] = config
return treecorr.Catalog(**catalog_kwargs)

def hasObjects(self, cat):
if hasattr(cat, '__len__'):
try:
return len(cat)>0
except TypeError:
return len(numpy.atleast_1d(cat))>0
elif hasattr(cat, 'nobj'):
return cat.nobj>0
return False

def getCF(self, correlation_function_type, data, data2=None,
random=None, random2=None, use_as_k=None, use_chip_coords=False,
config=None, **kwargs):
Expand Down Expand Up @@ -429,11 +439,12 @@ def getCF(self, correlation_function_type, data, data2=None,

# First, pull out the TreeCorr-relevant parameters from the stile_args dict, and add
# anything passed as a kwarg to that dict.
if (random and len(random)) or (random2 and len(random2)):
treecorr_kwargs[correlation_function_type+'_statistic'] = \
treecorr_kwargs.get(correlation_function_type+'_statistic', 'compensated')
treecorr_kwargs = stile.treecorr_utils.PickTreeCorrKeys(config)
treecorr_kwargs.update(stile.treecorr_utils.PickTreeCorrKeys(kwargs))
if ((self.hasObjects(random) or self.hasObjects(random2))
and correlation_function_type in ['nn', 'ng', 'nk']):
treecorr_kwargs[correlation_function_type+'_statistic'] = \
treecorr_kwargs.get(correlation_function_type+'_statistic', 'compensated')
treecorr.config.check_config(treecorr_kwargs, corr2_valid_params)

if data is None:
Expand Down Expand Up @@ -500,11 +511,11 @@ def getCF(self, correlation_function_type, data, data2=None,
func_dr = None
elif correlation_function_type == 'nn':
func_random = treecorr_func_dict[correlation_function_type](treecorr_kwargs)
if len(random2):
if self.hasObjects(random2):
func_random.process(random, random2)
else:
func_random.process(random)
if not len(data2):
if not self.hasObjects(data2):
func_rr = treecorr_func_dict['nn'](treecorr_kwargs)
func_rr.process(data, random)
if treecorr_kwargs.get(['nn_statistic'],
Expand All @@ -519,7 +530,7 @@ def getCF(self, correlation_function_type, data, data2=None,
else:
func_rr = treecorr_func_dict['nn'](treecorr_kwargs)
func_rr.process(random, random2)
if treecorr_kwargs.get(['nn_statistic'],
if treecorr_kwargs.get('nn_statistic',
self.compensateDefault(data, data2, random, random2, both=True)
) == 'compensated':
func_dr = treecorr_func_dict['nn'](treecorr_kwargs)
Expand Down Expand Up @@ -555,10 +566,10 @@ def compensateDefault(self, data, data2, random, random2, both=False):
indicates that both data sets if present must have randoms; the default, False, means only
the first data set must have an associated random.
"""
if not random or (random and not len(random)): # No random
if not self.hasObjects(random): # No random
return 'simple'
elif both and data2 and len(data2): # Second data set exists and must have a random
if random2 and len(random2):
elif both and self.hasObjects(data2): # Second data set exists and must have a random
if self.hasObjects(random2):
return 'compensated'
else:
return 'simple'
Expand Down
15 changes: 15 additions & 0 deletions tests/test_correlation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ def test_generator(self):
self.assertEqual(type(stile.sys_tests.BaseCorrelationFunctionSysTest()),
type(stile.CorrelationFunctionSysTest()))

def test_randoms(self):
""" Run some correlation functions with random catalogs to make sure that also works. """
stile_args = {'ra_units': 'degrees', 'dec_units': 'degrees', 'min_sep': 0.05, 'max_sep': 1,
'sep_units': 'degrees', 'nbins': 20}
lens_data = stile.ReadASCIITable('../examples/example_lens_catalog.dat',
fields={'id': 0, 'ra': 1, 'dec': 2, 'z': 3, 'g1': 4, 'g2': 5})
source_data = stile.ReadASCIITable('../examples/example_source_catalog.dat',
fields={'id': 0, 'ra': 1, 'dec': 2, 'z': 3, 'g1': 4, 'g2': 5})

object_list = ['GalaxyShear', 'BrightStarShear', 'StarXGalaxyDensity', 'StarXGalaxyShear',
'StarXStarShear', 'GalaxyDensityCorrelation', 'StarDensityCorrelation']
for object_type in object_list:
object_1 = stile.CorrelationFunctionSysTest(object_type)
results = object_1(lens_data, source_data, lens_data, source_data, config=stile_args)


if __name__=='__main__':
unittest.main()
Expand Down