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

mocks: warn about small theta and large mu in float32 precision #299

Merged
merged 7 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 27 additions & 0 deletions Corrfunc/mocks/DDsmu_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from __future__ import (division, print_function, absolute_import,
unicode_literals)
import warnings

__author__ = ('Manodeep Sinha', 'Nick Hand')
__all__ = ('DDsmu_mocks', )
Expand Down Expand Up @@ -296,6 +297,12 @@ def DDsmu_mocks(autocorr, cosmology, nthreads, mu_max, nmu_bins, binfile,

integer_isa = translate_isa_string_to_enum(isa)
sbinfile, delete_after_use = return_file_with_rbins(binfile)

warn_large_mu(mu_max,
# RA and DEC are checked to be the same dtype
dtype=RA1.dtype,
)

with sys_pipes():
extn_results = DDsmu_extn(autocorr, cosmology, nthreads,
mu_max, nmu_bins, sbinfile,
Expand Down Expand Up @@ -344,6 +351,26 @@ def DDsmu_mocks(autocorr, cosmology, nthreads, mu_max, nmu_bins, binfile,
else:
return results, api_time


def warn_large_mu(mu_max, dtype):
'''
Small theta values (large mu) underfloat float32. Warn the user.
Context: https://github.com/manodeep/Corrfunc/pull/297
lgarrison marked this conversation as resolved.
Show resolved Hide resolved
'''
if dtype.itemsize > 4:
return

if mu_max >= 0.9800666: # cos(0.2)
warnings.warn("""
Be aware that small angular pair separations (mu near 1) will suffer from loss
of floating-point precision, as the input data is in float32 precision or
lower. In float32, the loss of precision is 1% in mu at separations of 0.2
degrees, and larger at smaller separations.
For more information, see: https://github.com/manodeep/Corrfunc/pull/297
"""
)


if __name__ == '__main__':
import doctest
doctest.testmod()
40 changes: 40 additions & 0 deletions Corrfunc/mocks/DDtheta_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from __future__ import (division, print_function, absolute_import,
unicode_literals)
import warnings

__author__ = ('Manodeep Sinha', 'Kris Akira Stern')
__all__ = ('DDtheta_mocks', 'find_fastest_DDtheta_mocks_bin_refs')
Expand Down Expand Up @@ -313,6 +314,11 @@ def DDtheta_mocks(autocorr, nthreads, binfile,

integer_isa = translate_isa_string_to_enum(isa)
rbinfile, delete_after_use = return_file_with_rbins(binfile)

warn_small_theta(rbinfile,
RA1.dtype, # RA and DEC are checked to be the same dtype
)

with sys_pipes():
extn_results = DDtheta_mocks_extn(autocorr, nthreads, rbinfile,
RA1, DEC1,
Expand Down Expand Up @@ -644,6 +650,40 @@ def find_fastest_DDtheta_mocks_bin_refs(autocorr, nthreads, binfile,
return ret


def warn_small_theta(thetabinfile, dtype):
'''
Small theta values underfloat float32. Warn the user.
Context: https://github.com/manodeep/Corrfunc/pull/297
'''
if dtype.itemsize > 4:
return

import numpy as np
try:
bins = np.loadtxt(thetabinfile)
except RuntimeError:
warnings.warn("""
Could not load binfile "{}". Be aware that small angular pair separations
will suffer from loss of floating-point precision, as the input data is in
float32 precision or lower. The loss of precision is 0.5% in theta at
separations of 0.2 degrees, and larger at smaller separations.
For more information, see: https://github.com/manodeep/Corrfunc/pull/297
""".format(thetabinfile)
)
return

if bins.min() <= 0.2:
warnings.warn("""
A binning with a minimum angular separation of 0.2 degrees or less was
requested, and the input data is in float32 precision or lower. Be aware that
small angular pair separations will suffer from loss of floating-point
precision. In float32, the loss of precision is 0.5% in theta at separations of
0.2 degrees, and larger at smaller separations.
For more information, see: https://github.com/manodeep/Corrfunc/pull/297
"""
)


if __name__ == '__main__':
import doctest

Expand Down