镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

feat: support to_2D/3D/4D, to_p* conversions and like() in Numba - #751

Open
Thanatos9404 wants to merge 1 commit into
scikit-hep:mainfrom
Thanatos9404:numba-object-conversions
Open

Thanatos9404 wants to merge 1 commit into
scikit-hep:mainfrom
Thanatos9404:numba-object-conversions

Conversation

@Thanatos9404

Copy link
Copy Markdown

AI usage: this PR is fully AI generated. An AI coding agent wrote the code, the tests and the description below, and ran the checks listed; a second AI pass reviewed the diff. I directed the work and chose to open this PR.

🤖 AI text below 🤖

Description

Fixes #750

vector.obj vectors in numba.njit raised a TypingError for three groups of methods that work in pure Python. All changes are in src/vector/backends/_numba_object.py:

  • to_2D, to_3D and to_4D are registered on the existing to_Vector2D/3D/4D overloads with a second overload_method decorator, the same way scale and scale2D share one overload.
  • add_coordinate_change registers the momentum spelling next to the generic one (pxpy, ptphi, pz, energy, mass; theta and eta unchanged), giving all 20 to_p* conversions. The names are built there rather than by looping over _repr_momentum_to_generic, because that mapping does not give energy and mass for the temporal part.
  • A new like() overload picks to_Vector2D/3D/4D from the Numba type of other at compile time. The result keeps the class of self, and a non-vector argument raises a TypingError saying it "is not a vector.Vector", as in Python.

As before, compiled conversions take no keyword arguments, so to_3D(z=...) still only works in pure Python.

import numba, vector

v = vector.obj(px=1.0, py=2.0, pz=3.0, E=10.0)
numba.njit(lambda v: v.to_2D())(v)  # MomentumObject2D(px=1.0, py=2.0)
numba.njit(lambda v: v.to_pxpy())(v)  # MomentumObject2D(px=1.0, py=2.0)
numba.njit(lambda v: v.like(v.to_Vector2D()))(v)  # MomentumObject2D(px=1.0, py=2.0)

Tests

test_conversion_aliases (all to_2D/3D/4D and to_p* names) and test_method_like (every pair of six generic and momentum inputs, plus the non-vector error) compare njit results with pure Python on class, coordinate classes and values. All 24 fail on main with UntypedAttributeError and pass here.

tests/backends/test_numba_object.py: 69 passed; full suite 4943 passed, 16 skipped (ROOT, CuPy, JAX, dask-awkward not installed); doctests 119 passed (Windows, Python 3.13, numba 0.68.0). Pre-commit hooks pass on the changed files; the local mypy hook environment reports [type-arg] errors only in untouched files.

Checklist

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't any other open Pull Requests for the required change?
  • Does your submission pass pre-commit? ($ pre-commit run --all-files or $ nox -s lint)
  • Does your submission pass tests? ($ pytest or $ nox -s tests)
  • Does the documentation build with your changes? ($ cd docs; make clean; make html or $ nox -s docs) Not run locally; this PR does not change the docs.
  • Does your submission pass the doctests? ($ pytest --doctest-plus src/vector/ or $ nox -s doctests)

Before Merging

  • Summarize the commit messages into a brief review of the Pull request.

The Numba object backend only knew the to_Vector*D and generic coordinate
spellings, so to_2D/to_3D/to_4D, the momentum-named conversions (to_pxpy,
to_ptphietamass, ...) and like() raised a TypingError inside njit even
though they work in pure Python.

Register to_2D/to_3D/to_4D on the existing to_Vector*D overloads, register
each coordinate change under its momentum name as well, and add a like()
overload that picks the projection from the dimension of the argument's
type at compile time.

Fixes scikit-hep#750

@Saransh-cpp Saransh-cpp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Thanatos9404, thanks for the PR. I unfortunately do not advocate for new contributors using AI for PRs. I use AI daily but only in the code bases I understand.

Moreover, this PR serves no purpose as any maintainer could have done this change (with or without AI). The entire point of onboarding new contributors is for them to learn the internals of a repository, which you don’t if you offload the work to AI.

I would be very happy if a maintainer creates this AI generated PR, but not if a new contributor does it. Partially also because I would like my “voluntary” reviews to benefit the repository and attract more human (and not AI) contributors.

I hope you understand. I’ll close this PR.

@Thanatos9404

Copy link
Copy Markdown
Author

Thanks for explaining, @Saransh-cpp. That's fair, and I understand the point about learning the internals first.

@Saransh-cpp

Copy link
Copy Markdown
Member

Hi @Thanatos9404, thanks for understanding! We are going through AI guidelines for this repository / for the org. The discussion was triggered by my comments + other activities in the org. I'll reopen this PR and wait until an AI policy is formalised (such that you are not punished unjustly for using and declaring AI usage).

@Saransh-cpp Saransh-cpp reopened this Oct 19, 2026
@Thanatos9404

Copy link
Copy Markdown
Author

Thanks, that's kind of you. Happy to wait for the policy.

@codecov

codecov Bot commented Oct 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.90909% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.95%. Comparing base (caf34cd) to head (d438ed3).

Files with missing lines Patch % Lines
src/vector/backends/_numba_object.py 90.90% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #751      +/-   ##
==========================================
+ Coverage   88.88%   88.95%   +0.06%     
==========================================
  Files          96       96              
  Lines       10783    10816      +33     
==========================================
+ Hits         9585     9621      +36     
+ Misses       1198     1195       -3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Numba object backend: missing to_2D/to_3D/to_4D, momentum-named conversions, and like()

2 participants