-
Notifications
You must be signed in to change notification settings - Fork 125
BUG: convert_to_lal_binary_black_hole_parameters gives NaNs when called twice on parameters with chi_1 = 0 or chi_2 = 0 #1051
Description
Describe the bug
When using convert_to_lal_binary_black_hole_parameters from bilby/gw/conversion.py on a dictionary of parameters that contains chi_1 = 0 or chi_2 = 0 twice, a NaN is produced that is not caught and treated correctly. I found the bug when doing an injection-recovery with zero spins where I specified the injected spin parameters by their chi_i values using the IMRPhenomXAS_NRTidalv3 waveform, but the bug can be isolated by only using this function. bilby manages to finish sampling correctly and saves the posterior samples to the result file correctly, but then throws an error in some postprocessing function when calling conversion_function twice on the injection parameters in the run_samplerfunction inside bilby/core/sampler/__init__.py.
The convert_to_lal_binary_black_hole_parameters function already catches parameter dicts where a_1 = 0 which would give a NaN from division by zero and corrects for that by setting converted_parameters[f"cos_tilt_{idx}"] = 1.0. However, this is only triggered on the first call to the conversion function. After a second call, the if-else block checking for chi_1_in_plane will execute the if branch, not the else branch (which catches the error). Since the code in the if block does not catch the zero division error, bilby crashes.
To Reproduce
This small test reproduces the error for me (found while testing for a BNS injection but the root cause is at the function described above)
from bilby.gw.conversion import generate_all_bns_parameters
# Some random injection parameters with chi_1 = chi_2 = 0.0, which triggers the bug
injection = {
'chirp_mass': 1.1975,
'mass_ratio': 0.9,
'chi_1': 0.0,
'chi_2': 0.0,
'luminosity_distance': 40.0,
'geocent_time': 1187008882.43,
'ra': 3.45,
'dec': -0.408,
'psi': 0.0,
'phase': 0.0,
'theta_jn': 2.63545,
'lambda_1': 217.03468294803295,
'lambda_2': 420.5944867773165,
}
# Reproduce the double-call that we have in the `run_sampler` function
out1 = generate_all_bns_parameters(dict(injection))
out2 = generate_all_bns_parameters(out1) # this will produce NaN output
for key, value in out2.items():
# Print if NaN
if isinstance(value, float) and (value != value): # NaN check
print(f"{key}: {value}")This is the output I get:
/Users/Woute029/miniconda3/envs/bilby/lib/python3.10/site-packages/bilby/gw/conversion.py:240: RuntimeWarning: invalid value encountered in scalar divide
converted_parameters[f"chi_{idx}"]
cos_tilt_1: nan
cos_tilt_2: nan
tilt_1: nan
tilt_2: nan
chi_eff: nanError message
While this produces a warning for the minimal example above, it will give an error when passing these parameters to a waveform call: this is the error I got from running with bilby_pipe:
19:18 bilby INFO : Starting to close worker pool.
19:18 bilby INFO : Finished closing worker pool.
19:18 bilby INFO : Rejection sampling nested samples to obtain 10957 posterior samples
19:18 bilby INFO : Sampling time: 1:51:20.935455
/work/wouters/jester-review/jester-review/.venv/lib/python3.11/site-packages/bilby/gw/conversion.py:241: RuntimeWarning: invalid value encountered in scalar divide
converted_parameters[f"chi_{idx}"]
XLAL Error - IMRPhenomX_Initialize_Powers (LALSimIMRPhenomX_internals.c:57): number must be non-negative
XLAL Error - IMRPhenomX_Initialize_Powers (LALSimIMRPhenomX_internals.c:57): Input domain error
XLAL Error - IMRPhenomX_Initialize_Powers (LALSimIMRPhenomX_internals.c:57): number must be non-negative
XLAL Error - IMRPhenomX_Initialize_Powers (LALSimIMRPhenomX_internals.c:57): Input domain error
XLAL Error - IMRPhenomX_Initialize_Powers (LALSimIMRPhenomX_internals.c:57): number must be non-negative
XLAL Error - IMRPhenomX_Initialize_Powers (LALSimIMRPhenomX_internals.c:57): Input domain error
XLAL Error - IMRPhenomX_Initialize_Powers (LALSimIMRPhenomX_internals.c:57): number must be non-negative
XLAL Error - IMRPhenomX_Initialize_Powers (LALSimIMRPhenomX_internals.c:57): Input domain error
XLAL Error - IMRPhenomX_Initialize_Powers (LALSimIMRPhenomX_internals.c:57): number must be non-negative
XLAL Error - IMRPhenomX_Initialize_Powers (LALSimIMRPhenomX_internals.c:57): Input domain error
XLAL Error - IMRPhenomX_Initialize_Powers (LALSimIMRPhenomX_internals.c:57): number must be non-negative
XLAL Error - IMRPhenomX_Initialize_Powers (LALSimIMRPhenomX_internals.c:57): Input domain error
Traceback (most recent call last):
File "/work/wouters/jester-review/jester-review/.venv/bin/bilby_pipe_analysis", line 10, in <module>
sys.exit(main())
^^^^^^
File "/work/wouters/jester-review/jester-review/.venv/lib/python3.11/site-packages/bilby_pipe/data_analysis.py", line 388, in main
analysis.run_sampler()
File "/work/wouters/jester-review/jester-review/.venv/lib/python3.11/site-packages/bilby_pipe/data_analysis.py", line 270, in run_sampler
self.result = bilby.run_sampler(
^^^^^^^^^^^^^^^^^^
File "/work/wouters/jester-review/jester-review/.venv/lib/python3.11/site-packages/bilby/core/sampler/__init__.py", line 341, in run_sampler
result.injection_parameters = conversion_function(
^^^^^^^^^^^^^^^^^^^^
File "/work/wouters/jester-review/jester-review/.venv/lib/python3.11/site-packages/bilby/gw/conversion.py", line 1775, in generate_all_bns_parameters
output_sample = _generate_all_cbc_parameters(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/wouters/jester-review/jester-review/.venv/lib/python3.11/site-packages/bilby/gw/conversion.py", line 1656, in _generate_all_cbc_parameters
compute_per_detector_log_likelihoods(
File "/work/wouters/jester-review/jester-review/.venv/lib/python3.11/site-packages/bilby/gw/conversion.py", line 2333, in compute_per_detector_log_likelihoods
samples = likelihood.compute_per_detector_log_likelihood()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/wouters/jester-review/jester-review/.venv/lib/python3.11/site-packages/bilby/gw/likelihood/base.py", line 460, in compute_per_detector_log_likelihood
self.waveform_generator.frequency_domain_strain(self.parameters)
File "/work/wouters/jester-review/jester-review/.venv/lib/python3.11/site-packages/bilby/gw/waveform_generator.py", line 131, in frequency_domain_strain
return self._calculate_strain(model=self.frequency_domain_source_model,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/wouters/jester-review/jester-review/.venv/lib/python3.11/site-packages/bilby/gw/waveform_generator.py", line 176, in _calculate_strain
model_strain = self._strain_from_model(model_data_points, model)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/wouters/jester-review/jester-review/.venv/lib/python3.11/site-packages/bilby/gw/waveform_generator.py", line 189, in _strain_from_model
return model(model_data_points, **self.parameters)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/wouters/jester-review/jester-review/.venv/lib/python3.11/site-packages/bilby/gw/source.py", line 1053, in binary_neutron_star_frequency_sequence
return _base_waveform_frequency_sequence(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/wouters/jester-review/jester-review/.venv/lib/python3.11/site-packages/bilby/gw/source.py", line 1117, in _base_waveform_frequency_sequence
h_plus, h_cross = lalsim_SimInspiralChooseFDWaveformSequence(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/wouters/jester-review/jester-review/.venv/lib/python3.11/site-packages/bilby/gw/utils.py", line 764, in lalsim_SimInspiralChooseFDWaveformSequence
return SimInspiralChooseFDWaveformSequence(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Input domain errorExpected behavior
Catch the zero spins everywhere in the conversion function and avoid zero division.
Environment (please complete the following information):
- Bilby version: v2.7.1
- Installation method [e.g. conda, pip, source]: pip