-
Notifications
You must be signed in to change notification settings - Fork 475
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
Handle array creation functions such as np.asanyarray #1406
Comments
This is unfortunately not well-defined for Pint Quantities, as a single |
I think it should be array type and units. You'd lose the point of pint if the units were forgotten: import numpy as np
import pint
import dask.array as da
def torque(r, F, like_r=None, like_F=None):
"""Low level numpy-like only implementation from another package."""
r = np.asanyarray(r, like=like_r) if like_r is not None else np.asanyarray(r)
F = np.asanyarray(F, like=like_F) if like_F is not None else np.asanyarray(F)
return r * F
def torque_pint(r, F):
"""High level wrapper adding units"""
like_r = pint.Quantity([], "m")
like_F = pint.Quantity([], "N")
return torque(r, F, like_r=like_r, like_F=like_F) # [Nm]
def torque_dask(r, F):
"""High level wrapper adding dask"""
like_r = da.array([])
like_F = da.array([])
return torque(r, F, like_r=like_r, like_F=like_F)
torque_pint(2, 3)
Traceback (most recent call last):
File "<ipython-input-44-2b30fd66181f>", line 1, in <module>
torque_pint(2, 3)
File "<ipython-input-42-815053e56d26>", line 18, in torque_pint
return torque(r, F, like_r=like_r, like_F=like_F) # [Nm]
File "<ipython-input-42-815053e56d26>", line 8, in torque
r = np.asanyarray(r, like=like_r) if like_r is not None else np.asanyarray(r)
TypeError: no implementation found for 'numpy.asanyarray' on types that implement __array_function__: [<class 'pint.quantity.Quantity'>]
torque_dask(2, 3)
Out[45]: dask.array<mul, shape=(), dtype=int32, chunksize=(), chunktype=numpy.ndarray> |
It's common for downstream packages to force inputs to numpy-likes using
np.asarray
,np.asanyarray
etc. These have alike
argument to handle duckarrays, it would be nice if pint handled these methods. Dask supports this if you want ideas how to implement it.The text was updated successfully, but these errors were encountered: