-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add NumPy Scalars #3544
base: master
Are you sure you want to change the base?
Add NumPy Scalars #3544
Conversation
Logging my thoughts after looking for a bit. That's a lot of code (cost). It makes sense to distinguish between ints of different sizes etc. for large arrays, but how often does it matter for scalars in real life? Passing a one-element array could do the trick, too. Probably a minor nuisance that's worth spending time and code on only if it becomes wide-spread clutter. I will wait for other maintainers to chime in. |
@rwgk I understand your thoughts, thanks for your suggestions. |
It is not sure whether the PR will be merged, so if you want to use this feature, you can directly use this patch. |
I won't speak to the cost/benefit ratio. But I thought I could mention that I have found myself writing a small wrapper function like this: template<typename T>
py::object create_numpy_scalar(T val) {
// usage requires initialized NumPy C-API (call _imoprt_array() before use)
py::object dt = py::dtype::of<T>();
PyObject * scal = PyArray_Scalar(&val, (PyArray_Descr*)dt.ptr(), py::int_(sizeof(T)).ptr());
return py::reinterpret_steal<py::object>(scal);
} to wrap e.g. a |
@bjodah, thank you very very much. My original idea was to transfer this part of the cost directly from the user to us. Whether it is |
I have been bitten by this in the past as well and the current inconsistency for 0-dim data (scalars) makes generic ND data handling on API interfaces pretty wild/complicated. As long as this goes in the separate |
I'm personally fine with following numpy more closely (that is, adding features already supported by numpy, within reason). |
Thanks for pointing out @ax3l, I hadn't given that enough weight previously. I see the CI is currently very unhappy (42 failing). @sun1638650145, after I see you got it back to green, I'll run the Google global testing with this PR. Could you please tag me with an |
OK, I'll try to fix the bug as soon as I can. |
FYI, we are still okay with this if you can fix the bug(s?). (Invalid buffer descriptor) |
Hello, I’m not very familiar with using |
I'll unsubscribe myself here for the time being. Taking this as an opportunity to explain how I approach looking at pybind11 issues and PRs in general: In the back of my mind: I only have so many minutes per day. How do I decide what's important, and what's not?
|
Description
Compared with using
py::buffer
to provide processingpy::numpy_scalar
is more concise and easier to use. I think PR #2060 is a very good solution, but it seems to be abandoned. So I made a simple modification and resubmitted a new PR.