GeezerVison is showing github.com — numpy ufunc change · Issue #18045 · astropy/astropy
numpy ufunc change #18045Description
Description
Noticed in dev dep CI test See Logs.
This is arising in astropy.cosmology, but since nothing's changed there, that's not the source of the issue (though it may be where a change is required).
Expected behavior
The ufunc works!
@mhvk are you aware of any recent change to numpy ufuncs? Or maybe to astropy.units treatment of ufuncs?
How to Reproduce
Run the CI. Activitypllim commented on Apr 23, 2025 Hmm I see that on main as well here: https://github.com/astropy/astropy/actions/runs/14623471952/job/41029218545
But it is from some cosmology test that is expected to fail anyway.
________________________ test_z_at_value_numpyvectorize ________________________
@pytest.mark.skipif(not HAS_SCIPY, reason="test requires scipy")
def test_z_at_value_numpyvectorize():
"""Test that numpy vectorize fails on Quantities.
If this test starts failing then numpy vectorize can be used instead of
the home-brewed vectorization. Please submit a PR making the change.
"""
z_at_value = np.vectorize(
_z_at_scalar_value, excluded=["func", "method", "verbose"]
)
with pytest.raises(u.UnitConversionError, match="dimensionless quantities"):
> z_at_value(Planck15.age, 10 * u.Gyr)
...
E TypeError: unknown ufunc func (vectorized). If you believe this ufunc should be supported, please raise an issue on https://github.com/astropy/astropy
astropy/units/quantity_helper/converters.py:95
pllim commented on Apr 23, 2025 What does "ufunc func" even mean? nstarman commented on Apr 23, 2025 Maybe np.vectorize now turns the given function into a ufunc, but doesn't do a good job transferring the name? pllim commented on Apr 23, 2025 Hmm there is a note in the test docstring, "If this test starts failing then numpy vectorize can be used instead of the home-brewed vectorization. Please submit a PR making the change." Reacted by Nathaniel Starkman mhvk commented on Apr 23, 2025 Indeed, perhaps this is simply good news... Note that any ufunc would still need to be registered with UFUNC_HELPERS for quantities to be supported. But don't really know if that is worth it without looking at the context (and no time right now...). nstarman commented on Apr 24, 2025 Excellent. I'll be able to take a look in the next few days. seberg commented on May 26, 2025 Scratch that. Just fix the tests!
You never supported vectorize. Nothing significant there changed at all. The only change is that we stopped calling asarray() rather than asanyarray() (i.e. something you like) and because of that the error changed.
And it only changed if otypes= isn't used anyway. mhvk commented on May 26, 2025 Sorry to have been absent from this discussion: I agree with @seberg that the problem here is not numpy -- vectorize didn't work before and now fails in a slightly different manner, but really that cannot be helped.
The main problem is that we're trying to do something that simply cannot work: the whole idea behind overriding a ufunc via Quantity.__array_ufunc__ is that we turn the quantities into arrays of given units and then pass those arrays on to a function, i.e., that function should takes regular arrays, not quantities. If we vectorize a function that takes in Quantity, however, that necessarily breaks.
I suggest we remove the test and instead open an issue if there is a specific goal that ideally would be attained if vectorization were possible -- if the case is strong, we could create a Quantity specific version, say, u.vectorize. Reacted by Nathaniel Starkman nstarman commented on May 29, 2025 Just wondering, can we not generalize how quantity operates with a ufunc by trying to pass it through? Particularly if the ufunc is defined via vectorize which wraps a non-elemental function that might be able to handle quantity ops? mhvk commented on May 30, 2025
Just wondering, can we not generalize how quantity operates with a ufunc by trying to pass it through? Particularly if the ufunc is defined via vectorize which wraps a non-elemental function that might be able to handle quantity ops?
I was wondering about this too, and before I wrote my comment above in fact tried to see whether we could recognize the vectorized ufunc in Quantity.__array_ufunc__. Which we can, but the problem is that there is no way we can call the ufunc with quantities: if we call directly into ndarray.__array_ufunc__, it notices that the arguments override regular arrays and returns NotImplemented. At this point, we also no longer can tell what the function is that is being wrapped. Hence, my comment that if we wanted this, we should create our own u.vectorize.
Actually, I just realized there is a way out: the wrapping in an object ufunc only happens for scalar arguments, for functions that take arrays, np.vectorize mimics iteration internally. One can abuse this difference in tbehaviour for gufuncs by passing in a signature valid for scalars. Your example above passes if modified as follows:
def func_with_units(x):
assert np.shape(x) == ()
return 1.0 / x**2
x = [2.0, 4.0] * u.Mpc
print(func_with_units(x[0])) # works
print(np.vectorize(func_with_units, signature="()->()")(x))
# 0.25 1 / Mpc2
# [0.25 0.0625] 1 / Mpc2
Reacted by Nathaniel Starkman nstarman commented on May 30, 2025 Amazing! Can we apply that signature trick automatically? Or would that essentially be what u.vectorize does. mhvk commented on May 31, 2025
Amazing! Can we apply that signature trick automatically? Or would that essentially be what u.vectorize does.
Now that we know this works, I think the main point would be to document it (perhaps in both numpy and astropy?) -- np.vectorize is slow, so generally if one needs it, it is better to think through whether a faster solution is possible. Reacted by Nathaniel Starkman neutrinoceros commented on Jun 16, 2025 Reacted by Nathaniel Starkman pllim commented on Jun 16, 2025 Oh yay. We can revert my xfail and close this issue? nstarman commented on Jun 16, 2025 I guess so? But if it's only in dev then it can easily come back pllim commented on Jun 16, 2025 No wait, it still fails horribly for everything else, except, the combo of devdeps without scipy installed. Does this make any sense to anyone?
See the CI runs in #18278 neutrinoceros commented on Jun 16, 2025 Numpy 2.3 is now stable and known to fail the test, NumPy dev is now 2.4, so I think this is to be expected
Maybe we shouldn't be testing behavior we do not control ? pllim commented on Jun 16, 2025 I think @mhvk also suggested removing test back in #18045 (comment) but then there were follow-up convo and I got confused if that was still desirable or not. 🤷♀ pllim commented on Jun 16, 2025 If you support removing the test:
Reacted by Clément Robert and Nathaniel Starkman
MetadataMetadataStatusShow more project fields
DevelopmentNo branches or pull requestsIssue actions
|