-
Notifications
You must be signed in to change notification settings - Fork 27
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: add constructors for VectorObject2D
and MomentumObject2D
#89
Conversation
Codecov ReportBase: 81.04% // Head: 81.08% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## main #89 +/- ##
==========================================
+ Coverage 81.04% 81.08% +0.03%
==========================================
Files 96 96
Lines 10624 10672 +48
==========================================
+ Hits 8610 8653 +43
- Misses 2014 2019 +5
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
src/vector/backends/object.py
Outdated
def __init__( | ||
self, azimuthal: typing.Optional[AzimuthalObject] = None, **kwargs: float | ||
) -> None: | ||
if not kwargs and azimuthal is not None: | ||
self.azimuthal = azimuthal | ||
elif kwargs and azimuthal is None: | ||
if set(kwargs) == {"px", "py"}: | ||
self.azimuthal = AzimuthalObjectXY(kwargs["px"], kwargs["py"]) | ||
elif set(kwargs) == {"pt", "phi"}: | ||
self.azimuthal = AzimuthalObjectRhoPhi(kwargs["pt"], kwargs["phi"]) | ||
else: | ||
raise TypeError("invalid arguments, must be px=, py= or pt=, phi=") | ||
else: | ||
raise TypeError("must give Azimuthal if not giving keyword arguments") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went through the overloads of vector.obj
and a user can indeed create a MomentumObject
by specifying a mixture of momentum and generic coordinates. I think this should be removed and the momentum coordinate names must be converted to generic in the __init__
method of VectorObject
classes, which will then be inherited by MomentumObject
classes.
src/vector/backends/object.py
Outdated
def __init__( | ||
self, azimuthal: typing.Optional[AzimuthalObject] = None, **kwargs: float | ||
) -> None: | ||
if not kwargs and azimuthal is not None: | ||
self.azimuthal = azimuthal | ||
elif kwargs and azimuthal is None: | ||
if set(kwargs) == {"x", "y"}: | ||
self.azimuthal = AzimuthalObjectXY(kwargs["x"], kwargs["y"]) | ||
elif set(kwargs) == {"rho", "phi"}: | ||
self.azimuthal = AzimuthalObjectRhoPhi(kwargs["rho"], kwargs["phi"]) | ||
else: | ||
raise TypeError("invalid arguments, must be x=, y= or rho=, phi=") | ||
else: | ||
raise TypeError("must give Azimuthal if not giving keyword arguments") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing the MomentumObject
constructor and changing the momentum
coordinates to generic
ones works!
def __init__( | |
self, azimuthal: typing.Optional[AzimuthalObject] = None, **kwargs: float | |
) -> None: | |
if not kwargs and azimuthal is not None: | |
self.azimuthal = azimuthal | |
elif kwargs and azimuthal is None: | |
if set(kwargs) == {"x", "y"}: | |
self.azimuthal = AzimuthalObjectXY(kwargs["x"], kwargs["y"]) | |
elif set(kwargs) == {"rho", "phi"}: | |
self.azimuthal = AzimuthalObjectRhoPhi(kwargs["rho"], kwargs["phi"]) | |
else: | |
raise TypeError("invalid arguments, must be x=, y= or rho=, phi=") | |
else: | |
raise TypeError("must give Azimuthal if not giving keyword arguments") | |
def __init__( | |
self, azimuthal: typing.Optional[AzimuthalObject] = None, **kwargs: float | |
) -> None: | |
for k, v in kwargs.copy().items(): | |
kwargs.pop(k) | |
kwargs[_repr_momentum_to_generic.get(k, k)] = v | |
if not kwargs and azimuthal is not None: | |
self.azimuthal = azimuthal | |
elif kwargs and azimuthal is None: | |
if set(kwargs) == {"x", "y"}: | |
self.azimuthal = AzimuthalObjectXY(kwargs["x"], kwargs["y"]) | |
elif set(kwargs) == {"rho", "phi"}: | |
self.azimuthal = AzimuthalObjectRhoPhi(kwargs["rho"], kwargs["phi"]) | |
else: | |
raise TypeError("invalid arguments, must be x=, y= or rho=, phi=") | |
else: | |
raise TypeError("must give Azimuthal if not giving keyword arguments") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A minor issue with this -
vector.obj(rho=1, phi=2)
right now constructs a VectorObject2D
object.
But the user would now also be able to do -
vector.MomentumObject2D(rho=1, phi=2)
I think this PR should work now, given that we are fine with passing generic coordinates into |
VectorObject2D
and MomentumObject2D
VectorObject2D
and MomentumObject2D
VectorObject2D
and MomentumObject2D
415539e
to
4fc6af4
Compare
Feel free to turn of the unreachable check for mypy if it triggers too much, and 1-2 type ignores need |
Done! I've also refactored out the type safety code from the |
I can't approve "my own" (according to GitHub) PR, but I reviewed it so verbally approving here! |
ea023ae
to
d9a4462
Compare
Merged! Thanks for the review! 🎉 |
Closes #88.
WIP, currently just 2D Object implemented.