Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: like method for projecting vector into the coordinate space of another vector + better type errors and hints #426

Merged

Conversation

Saransh-cpp
Copy link
Member

@Saransh-cpp Saransh-cpp commented Feb 21, 2024

Description

Xref: #422
Will rebase once #423 is merged.

Please describe the purpose of this pull request. Reference and link to any relevant issues or pull requests.

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

@Saransh-cpp Saransh-cpp marked this pull request as draft February 21, 2024 14:39
@Saransh-cpp
Copy link
Member Author

Hi @jpivarski, @nsmith-, @iasonkrom, @lgray

I've noticed that some operations require the vectors to be of different dimensions; for instance, boost_beta3 (the failing test).

Is there a specific set of operations that should error out when performed on vectors of different dimensionality, or maybe I should ask about a particular set of operations that should not error?

The like method and the TypeError are working well in this PR! (see tests)

@ikrommyd
Copy link

I would assume that those that require different dimensionality are fewer so.... the second option? Y'all may have better opinions though.

Copy link

codecov bot commented Feb 23, 2024

Codecov Report

Attention: Patch coverage is 72.28916% with 23 lines in your changes are missing coverage. Please review.

Project coverage is 82.81%. Comparing base (6574993) to head (ddad594).
Report is 1 commits behind head on main.

Files Patch % Lines
src/vector/_methods.py 72.28% 23 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #426      +/-   ##
==========================================
- Coverage   82.92%   82.81%   -0.12%     
==========================================
  Files          96       96              
  Lines       11533    11557      +24     
==========================================
+ Hits         9564     9571       +7     
- Misses       1969     1986      +17     

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

@Saransh-cpp Saransh-cpp force-pushed the user-friendly-dimension-conversions branch from 6025671 to 09b2de8 Compare February 26, 2024 12:34
@Saransh-cpp Saransh-cpp changed the title (WIP) feat: error out on operations on vectors of different geometric dimensions + new like method (WIP) feat: like method for projecting vector into the coordinate space of another vector + better type errors and hints Feb 26, 2024
@nsmith-
Copy link

nsmith- commented Feb 26, 2024

Some methods do require arguments of a particular type to be meaningful, such as LorentzVector.boost_beta3 should only accept a 3-component monentum vector.

@Saransh-cpp
Copy link
Member Author

Some methods do require arguments of a particular type to be meaningful, such as LorentzVector.boost_beta3 should only accept a 3-component monentum vector.

Ah, this can be easily highlighted by updating the type signature, so I think an error in such situations will be redundant?

--- a/src/vector/_methods.py
+++ b/src/vector/_methods.py
@@ -3818,7 +3818,7 @@ class Lorentz(Spatial, VectorProtocolLorentz):
         return boost_p4.dispatch(self, p4)
 
     def boost_beta3(
-        self: SameVectorType, beta3: VectorProtocolSpatial
+        self: SameVectorType, beta3: MomentumProtocolSpatial
     ) -> SameVectorType:
         from vector._compute.lorentz import boost_beta3

@Saransh-cpp
Copy link
Member Author

Thinking about this again, do we want strict checks in each method/operation or do we want strict checks only in the methods/operations that can accept every type of vector (v: VectorProtocol).

For instance, add has the following signature:

def add(self, other: VectorProtocol) -> VectorProtocol:

Therefore, I have added a type check in this method. But, cross has the following updated signature in this PR:

def cross(self, other: VectorProtocolSpatial) -> VectorProtocolSpatial:

and the following works:

In [1]: import vector

In [2]: vector.obj(x=0.1, y=0.2, z=0.3, t=10).cross(vector.obj(x=0.4, y=0.5, z=0.6, t=20))
Out[2]: VectorObject3D(x=-0.03, y=0.06, z=-0.030000000000000013)

From an API design point of view, should we check the type of an argument even after providing a type hint? It feels redundant to me. Maybe the implementation of cross should be changed such that it does not generalize to 4D vectors, instead of explicitly checking the type of the vectors before executing the logic.

cc: @jpivarski

@Saransh-cpp Saransh-cpp marked this pull request as ready for review February 29, 2024 13:27
Copy link
Member

@jpivarski jpivarski left a comment

Choose a reason for hiding this comment

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

The new like method is documented, but there should also be a short comment about it in the error messages that result from trying to add/subtract/compare vectors of different dimensionality, since the like method is how users would fix those errors.

The exclusion of implicit projection/embedding and inclusion of a new like method together form a single project of asking users for clarity about whether they want to project or embed. Some text like

x + y   →   x.like(y) + y   or   x + y.like(x)

would indicate where the user is being asked for their opinion.

README.md Outdated
Comment on lines 540 to 544
v1 - v2.like(v1), v1.like(
v2
) - v2, v1 - v2.to_xyz(), v1.to_xy() - v2, v1 - v2.to_Vector3D(
z=3
), v1.to_Vector2D() - v2
Copy link
Member

Choose a reason for hiding this comment

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

This might be required for black-compliant formatting, but it makes what would be easy to read into something difficult to read.

Do you need the commas?

Suggested change
v1 - v2.like(v1), v1.like(
v2
) - v2, v1 - v2.to_xyz(), v1.to_xy() - v2, v1 - v2.to_Vector3D(
z=3
), v1.to_Vector2D() - v2
v1 - v2.like(v1)
v1.like(v2) - v2
v1 - v2.to_xyz()
v1.to_xy() - v2
v1 - v2.to_Vector3D(z=3)
v1.to_Vector2D() - v2

Should there be some explanatory text between these?

Comment on lines 4183 to 4200
def _maybe_dimension_error(
v1: VectorProtocol, v2: VectorProtocol, operation: str
) -> None:
"""Raises an error if the vectors are not of the same dimension."""
if dim(v1) != dim(v2):
raise TypeError(
f"""{v1!r} and {v2!r} do not have the same dimension; use

a.like(b).{operation}(b)

or

a.{operation}(b.like(a))

or the binary operation equivalent to project or embed one of the vectors
to match the other's dimensionality
"""
)
Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the review, @jpivarski! The error message here displays the usage of the new like method. Should this be changed in any way?

@Saransh-cpp Saransh-cpp marked this pull request as draft March 1, 2024 15:14
@Saransh-cpp Saransh-cpp changed the title (WIP) feat: like method for projecting vector into the coordinate space of another vector + better type errors and hints feat: like method for projecting vector into the coordinate space of another vector + better type errors and hints Mar 5, 2024
@Saransh-cpp Saransh-cpp marked this pull request as ready for review March 5, 2024 14:38
@Saransh-cpp
Copy link
Member Author

Re-requesting your review on this too, @jpivarski!

Copy link
Member

@jpivarski jpivarski left a comment

Choose a reason for hiding this comment

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

This is great! I approve!

Having a utility function for that error message is a good idea; it seems to be needed in a lot of methods.

@Saransh-cpp
Copy link
Member Author

Great, thanks for the review!

@Saransh-cpp Saransh-cpp merged commit b1e1df4 into scikit-hep:main Mar 5, 2024
21 of 23 checks passed
@Saransh-cpp Saransh-cpp deleted the user-friendly-dimension-conversions branch March 5, 2024 18:00
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.

4 participants