Draft: Prevent Overflow in pAstro calculation
Motivation
During LLPIC, an event from the injection channel got uploaded with pAstro as NaNs - pAstro file. The logs were checked and they revealed that there was overflow in the pAstro calculation for this event.
2025-05-21 13:38:13,507 | ll_inspiral_pastro_uploader : DEBUG : Processing G2989487...
/usr/local/lib/python3.6/site-packages/pastro/pastro.py:355: RuntimeWarning: overflow encountered in exp
weights[c]=np.exp(np.nan_to_num(PPoly(self.template_weight_coefficients[c][:,:,index],self.template_weight_SNR)(snr)))
/usr/local/lib/python3.6/site-packages/pastro/pastro.py:356: RuntimeWarning: invalid value encountered in double_scalars
return {c:weights[c]/np.sum(list(weights.values())) for c in self.categories}
2025-05-21 13:38:14,223 | ll_inspiral_pastro_uploader : DEBUG : Successfully uploaded gstlal.p_astro.json to G2989487.
Fix
NaNs make no sense for pAstro values. This MR prevents NaNs from being uploaded. It changes the behaviour of the the function such that
- Clips SNR such that maximum value passed to
PPoly
is 80 - It fails out if
PPoly
returns+inf
-
NaN
is mapped to-inf
- The
PPoly
output is normalized usingscipy.special.logsumexp
Tests
Same Event
For the same event, the output is now more reasonable
SNR: 167.3338762340001
[WARNING] SNR=167.3338762340001 exceeds 80. Clipping to 80.
Poly Vals: {'BBH': -9.901634801062755, 'BNS': -3204.3943249001168, 'NSBH': -1981.145652845502}
Weights: {'BBH': 1.0, 'BNS': 0.0, 'NSBH': 0.0}
Total: 1.0
P(astro) = {"BBH": 1.0, "BNS": 0.0, "NSBH": 0.0, "Terrestrial": 0.0}
poly_val = inf
Manually set output of PPoly
to inf
AssertionError: poly_val for category 'BBH' is +inf for template_id=1809086.0, snr=80.0.
poly_val = NaN
Manually set output of PPoly
to NaN
for BBH
SNR: 167.3338762340001
[WARNING] SNR=167.3338762340001 exceeds 80. Clipping to 80.
Poly Vals: {'BBH': -inf, 'BNS': -3204.3943249001168, 'NSBH': -1981.145652845502}
Weights: {'BBH': 0.0, 'BNS': 0.0, 'NSBH': 1.0}
Total: 1.0
P(astro) = {"BBH": 0.0, "BNS": 0.0, "NSBH": 1.0, "Terrestrial": 0.0}
All -inf
Manually set output of PPoly
to NaN
for all
SNR: 167.3338762340001
[WARNING] SNR=167.3338762340001 exceeds 80. Clipping to 80.
Poly Vals: {'BBH': -inf, 'BNS': -inf, 'NSBH': -inf}
Traceback (most recent call last):
File "test.py", line 46, in <module>
p_astro_value = uploader.calculate_pastro(mock_event)
File "/ligo/home/ligo.org/shomik.adhicary/Projects/2025_Pastro/gstlal_ll_inspiral_pastro_uploader.py", line 226, in calculate_pastro
pa = self.model(data,inj=self.is_injection_job)
File "/usr/local/lib/python3.6/site-packages/pastro/pastro.py", line 402, in __call__
template_weights=self.calculate_template_weights(template_id,snr)
File "/usr/local/lib/python3.6/site-packages/pastro/pastro.py", line 382, in calculate_template_weights
f"logsumexp returned a non-finite value (lse = {lse}) for template_id={template_id}, snr={snr}"
AssertionError: logsumexp returned a non-finite value (lse = -inf) for template_id=1809086.0, snr=80.0
SNR below 80
SNR: 79.99999
Poly Vals: {'BBH': -9.901634652452396, 'BNS': -3204.3935259286513, 'NSBH': -1981.145539405018}
Weights: {'BBH': 1.0, 'BNS': 0.0, 'NSBH': 0.0}
Total: 1.0
P(astro) = {"BBH": 1.0, "BNS": 0.0, "NSBH": 0.0, "Terrestrial": 0.0}
Notes
Have noticed
/usr/lib64/python3.6/site-packages/scipy/integrate/quadpack.py:364: IntegrationWarning: The occurrence of roundoff error is detected, which prevents
the requested tolerance from being achieved. The error may be
underestimated.
warnings.warn(msg, IntegrationWarning)
might be an underlying issue causing overflow
Edited by Shomik Adhicary