GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — numpy ufunc change · Issue #18045 · astropy/astropy

numpy ufunc change #18045

New issue
Copy link
New issue
Copy link
Closed
#18281
Closed
numpy ufunc change#18045
#18281
Copy link
Labels
Upstream Action RequiredWas: Upstream Fix Requiredcosmologynumpy-devtesting

Description

@nstarman
nstarman
opened on Apr 23, 2025
Issue body actions
  • Revert TST: xfail test_z_at_value_numpyvectorize for numpy 2.3.dev and later #18093

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.

Activity

Next
  1. nstarman
    added
    Bug
    on Apr 23, 2025
  2. pllim
    added
    units
    numpy-dev
    testing
    cosmology
    and removed
    Bug
    on Apr 23, 2025
  3. github-project-automation
    added this to Cosmology, the Expansionon Apr 23, 2025
  4. github-project-automation
    moved this to Triage in Cosmology, the Expansionon Apr 23, 2025
  5. pllim
    removed
    units
    on Apr 23, 2025
  6. pllim commented on Apr 23, 2025

    @pllim
    pllim
    on Apr 23, 2025
    Member
    More actions

    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
    
  7. pllim commented on Apr 23, 2025

    @pllim
    pllim
    on Apr 23, 2025
    Member
    More actions

    What does "ufunc func" even mean?

  8. nstarman commented on Apr 23, 2025

    @nstarman
    nstarman
    on Apr 23, 2025
    MemberAuthor
    More actions

    Maybe np.vectorize now turns the given function into a ufunc, but doesn't do a good job transferring the name?

  9. pllim commented on Apr 23, 2025

    @pllim
    pllim
    on Apr 23, 2025
    Member
    More actions

    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
  10. mhvk commented on Apr 23, 2025

    @mhvk
    mhvk
    on Apr 23, 2025
    Contributor
    More actions

    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...).

  11. nstarman commented on Apr 24, 2025

    @nstarman
    nstarman
    on Apr 24, 2025
    MemberAuthor
    More actions

    Excellent. I'll be able to take a look in the next few days.

  12. 8 remaining items

  13. seberg commented on May 26, 2025

    @seberg
    seberg
    on May 26, 2025
    Contributor
    More actions

    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.

  14. mhvk commented on May 26, 2025

    @mhvk
    mhvk
    on May 26, 2025
    Contributor
    More actions

    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
  15. nstarman commented on May 29, 2025

    @nstarman
    nstarman
    on May 29, 2025
    MemberAuthor
    More actions

    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?

  16. mhvk commented on May 30, 2025

    @mhvk
    mhvk
    on May 30, 2025
    Contributor
    More actions

    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
  17. nstarman commented on May 30, 2025

    @nstarman
    nstarman
    on May 30, 2025
    MemberAuthor
    More actions

    Amazing! Can we apply that signature trick automatically? Or would that essentially be what u.vectorize does.

  18. mhvk commented on May 31, 2025

    @mhvk
    mhvk
    on May 31, 2025
    Contributor
    More actions

    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
  19. neutrinoceros commented on Jun 16, 2025

    @neutrinoceros
    neutrinoceros
    on Jun 16, 2025
    Contributor
    More actions

    Hum, the test now xpasses on devdeps (weekly cron) https://github.com/astropy/astropy/actions/runs/15673215628/job/44147912314

    Reacted by Nathaniel Starkman
  20. pllim commented on Jun 16, 2025

    @pllim
    pllim
    on Jun 16, 2025
    Member
    More actions

    Oh yay. We can revert my xfail and close this issue?

  21. nstarman commented on Jun 16, 2025

    @nstarman
    nstarman
    on Jun 16, 2025
    MemberAuthor
    More actions

    I guess so? But if it's only in dev then it can easily come back

  22. pllim commented on Jun 16, 2025

    @pllim
    pllim
    on Jun 16, 2025
    Member
    More actions

    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

  23. neutrinoceros commented on Jun 16, 2025

    @neutrinoceros
    neutrinoceros
    on Jun 16, 2025
    Contributor
    More actions

    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 ?

  24. pllim commented on Jun 16, 2025

    @pllim
    pllim
    on Jun 16, 2025
    Member
    More actions

    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. 🤷‍♀

  25. pllim commented on Jun 16, 2025

    @pllim
    pllim
    on Jun 16, 2025
    Member
    More actions

    If you support removing the test:

    • TST: Remove test_z_at_value_numpyvectorize #18281
    Reacted by Clément Robert and Nathaniel Starkman
  26. pllim
    closed this as completedin #18281on Jun 16, 2025
  27. github-project-automation
    moved this from Triage to Done in Cosmology, the Expansionon Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Upstream Action RequiredWas: Upstream Fix Requiredcosmologynumpy-devtesting

    Type

    No type

    Projects

    • Cosmology, the Expansion
      Status
      Done
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.