Skip to content

Commit af675ea

Browse files
lgarrisonmanodeep
andauthored
mocks: warn about small theta and large mu in float32 precision (#299)
* mocks: warn about small theta and large mu in float32 precision * mocks: update small theta warning. Update changelog. * Update CHANGES.rst @lgarrison Re-worded the changelog - see if that works for you. * Update DDsmu_mocks.py Fixed typo * Update DDtheta_mocks.py Fixed typo * Update DDsmu_mocks.py Fixed typo * Update DDtheta_mocks.py Fixed typo --------- Co-authored-by: Manodeep Sinha <manodeep@gmail.com>
1 parent 1735f3d commit af675ea

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ Changes
2222
-------
2323
- Python >= 3.7 and numpy >= 1.16 are required for python extensions [#291]
2424

25+
Enhancements
26+
------------
27+
- Warn about loss of precision for float32 calculations involving small ``theta`` in ``DDtheta_mocks`` and large ``mu`` in ``DDsmu_mocks`` [#299]
28+
2529
2.5.0 (2022-12-23)
2630
================
2731

Corrfunc/mocks/DDsmu_mocks.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from __future__ import (division, print_function, absolute_import,
1010
unicode_literals)
11+
import warnings
1112

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

297298
integer_isa = translate_isa_string_to_enum(isa)
298299
sbinfile, delete_after_use = return_file_with_rbins(binfile)
300+
301+
warn_large_mu(mu_max,
302+
# RA and DEC are checked to be the same dtype
303+
dtype=RA1.dtype,
304+
)
305+
299306
with sys_pipes():
300307
extn_results = DDsmu_extn(autocorr, cosmology, nthreads,
301308
mu_max, nmu_bins, sbinfile,
@@ -344,6 +351,27 @@ def DDsmu_mocks(autocorr, cosmology, nthreads, mu_max, nmu_bins, binfile,
344351
else:
345352
return results, api_time
346353

354+
355+
def warn_large_mu(mu_max, dtype):
356+
'''
357+
Small theta values (large mu) underfloat float32. Warn the user.
358+
Context: https://github.com/manodeep/Corrfunc/issues/296 (see also #297)
359+
'''
360+
if dtype.itemsize > 4:
361+
return
362+
363+
if mu_max >= 0.9800666: # cos(0.2)
364+
warnings.warn("""
365+
Be aware that small angular pair separations (mu near 1) will suffer from loss
366+
of floating-point precision, as the input data is in float32 precision or
367+
lower. In float32, the loss of precision is 1% in mu at separations of 0.2
368+
degrees, and larger at smaller separations.
369+
For more information, see:
370+
https://github.com/manodeep/Corrfunc/issues/296 (see also #297)
371+
"""
372+
)
373+
374+
347375
if __name__ == '__main__':
348376
import doctest
349377
doctest.testmod()

Corrfunc/mocks/DDtheta_mocks.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from __future__ import (division, print_function, absolute_import,
1212
unicode_literals)
13+
import warnings
1314

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

314315
integer_isa = translate_isa_string_to_enum(isa)
315316
rbinfile, delete_after_use = return_file_with_rbins(binfile)
317+
318+
warn_small_theta(rbinfile,
319+
RA1.dtype, # RA and DEC are checked to be the same dtype
320+
)
321+
316322
with sys_pipes():
317323
extn_results = DDtheta_mocks_extn(autocorr, nthreads, rbinfile,
318324
RA1, DEC1,
@@ -644,6 +650,42 @@ def find_fastest_DDtheta_mocks_bin_refs(autocorr, nthreads, binfile,
644650
return ret
645651

646652

653+
def warn_small_theta(thetabinfile, dtype):
654+
'''
655+
Small theta values underfloat float32. Warn the user.
656+
Context: https://github.com/manodeep/Corrfunc/issues/296 (see also #297)
657+
'''
658+
if dtype.itemsize > 4:
659+
return
660+
661+
import numpy as np
662+
try:
663+
bins = np.loadtxt(thetabinfile)
664+
except RuntimeError:
665+
warnings.warn("""
666+
Could not load binfile "{}". Be aware that small angular pair separations
667+
will suffer from loss of floating-point precision, as the input data is in
668+
float32 precision or lower. The loss of precision is 0.5% in theta at
669+
separations of 0.2 degrees, and larger at smaller separations.
670+
For more information, see:
671+
https://github.com/manodeep/Corrfunc/issues/296 (see also #297)
672+
""".format(thetabinfile)
673+
)
674+
return
675+
676+
if bins.min() <= 0.2:
677+
warnings.warn("""
678+
A binning with a minimum angular separation of 0.2 degrees or less was
679+
requested, and the input data is in float32 precision or lower. Be aware that
680+
small angular pair separations will suffer from loss of floating-point
681+
precision. In float32, the loss of precision is 0.5% in theta at separations of
682+
0.2 degrees, and larger at smaller separations.
683+
For more information, see:
684+
https://github.com/manodeep/Corrfunc/issues/296 (see also #297)
685+
"""
686+
)
687+
688+
647689
if __name__ == '__main__':
648690
import doctest
649691

0 commit comments

Comments
 (0)