Skip to content

numpy.nan_to_num usage broken in numpy v2

Report

I've started getting errors like below with numpy v2. I think this has not been doing the intended thing for a while, as the second argument is copy, not nan which is presumably the target

This is related to https://github.com/numpy/numpy/issues/29045, the current behaviour is specifying the copy=0.

    obj.__init__(
  File "/usr/local/lib/python3.11/site-packages/pesummary/gw/conversions/__init__.py", line 582, in __init__
    self.generate_all_posterior_samples()
  File "/usr/local/lib/python3.11/site-packages/pesummary/gw/conversions/__init__.py", line 2105, in generate_all_posterior_samples
    self._rho_p()
  File "/usr/local/lib/python3.11/site-packages/pesummary/gw/conversions/__init__.py", line 1250, in _rho_p
    [rho_p, b_bar, overlap, snrs], data_used = precessing_snr(
                                               ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pesummary/utils/decorators.py", line 220, in wrapper_function
    output = func(*new_args, **new_kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pesummary/gw/conversions/snr.py", line 726, in precessing_snr
    _dict[key] = np.nan_to_num(
                 ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/numpy/lib/_type_check_impl.py", line 468, in nan_to_num
    x = _nx.array(x, subok=True, copy=copy)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Unable to avoid copy while creating an array as requested.
If using `np.array(obj, copy=False)` replace it with `np.asarray(obj)` to allow a copy when needed (no behavior change in NumPy 1.x).
For more details, see https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword.

Fix

Change all numpy.nan_to_num calls to use keyword arguments beyond the first argument.

@charlie.hoy I'm happy to put in an MR changing these instances.

Edited by Colm Talbot