-
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
How to create a numpy array of objects? #647
Comments
Do you really need np.array of objects, just wondering what's the use case? That being said, you can always get the |
As for |
As I understand it, yes (I've been tasked to do this, but I'm not the end user; @rdeits might have a more definitive answer). The use case is to be able to do vector/matrix operations on the data. Our current work-around is to call
Okay; that seems like it may be the best solution; I wasn't sure because currently it seems pybind11 never includes any Python (or at least numpy) headers itself, so I wasn't sure if that was "not kosher" or something...
That looks plausible (granting that I'm not an expert on the bowels of either pybind11 nor numpy); thanks! |
There are a couple of cases in which having an ndarray of objects is actually pretty useful. The particular case I'm working on right now is a symbolic Variable type that's used in the optimization tools in https://github.com/RobotLocomotion/drake . Having a numpy array of Variables (rather than a list) means that I can do matrix/vector math on those variables (for example, multiplying a matrix of coefficients by a vector of variables to get a vector of linear expressions). Of course, that's possible with lists, but it requires re-implementing matrix multiplication yourself, which seems like a waste. I've used the gurobi python interface in the past, and while it's quite nice, the fact that it returns its variables in a non-numpy container type means a lot of annoying re-implementation of basic things like Of course, as @mwoehlke-kitware said, the manual conversion to |
Maybe I should clarify my intent. I want to teach pybind11 how to apply its magic Eigen↔NumPy wrapping to not-so-scalar types. I don't plan that this would be 100% automatic, but that the user would write something like |
Take a look at PR #610, if you haven't noticed it already; it pretty much rewrites the Eigen support. It doesn't particularly help in your case (the main point is to let numpy and eigen reference each others' data, which is impossible here), but will save either you or me some major merge conflicts down the line. |
If I understood everything correctly, I think there's a very simple solution here: py::array convert(const EigenVectorWithCustomScalar& v) {
auto l = py::list();
for (int i = 0; i < v.size(); ++i) {
l.append(v[i]);
}
return py::array(l);
} The last line does the expected conversion from a Python list of objects to a numpy array of objects (just like |
"Noticed" is about the right word 😄... but thanks for the reminder anyway. I'll take another look when I get that far; right now I'm just trying to get this working locally. Since I need to convert between a NumPy array of
I'm not in love with creating a bunch of intermediary |
Assuming Going the other way doesn't seem possible, as far as I can see. |
You can do that by setting the |
Sure, makes sense. I just thought I'd offer an alternative which can work in a pinch without additional numpy headers. |
I assume that this is resolved and will close the ticket -- feel free to comment in case it isn't. |
Does closing this imply you will not accept patches to upstream this functionality? I'm waiting on #610, but my intent was to refactor after that lands and then try to get this feature upstreamed, which would close this issue in the process. To the extent that the issue is implicitly "...in stock pybind11", I don't think it should be closed unless it is fixed in upstream or is "wontfix". |
No, that was definitely not my intent. It seemed to me that the discussion had converged with a number of alternative proposals/workaround. If your plan is still to submit a PR for this functionality, then we'll definitely take a good look at it. (though I generally can't promise to accept things before they've gone through a code review) |
I've only known @wjakob to not accept something if it was overly intrusive or of very marginal value: this seems neither. But I think closing this issue makes sense as a housekeeping measure: it's going to require a PR (where further discussion can happen). |
I wouldn't expect you to 😄.
Well, obviously you are free to manage your project as you like. I usually leave feature request issues open until a PR provides the feature, and then the PR will close it. I think that's common practice, but... |
I wish github had a convert-this-bug-into-a-PR feature, to avoid that sort of thing, but sadly it does not. |
I'm closing this as a housekeeping measure, since this is not a bug. Further discussion can happen on a separate PR if @mwoehlke-kitware would like to contribute this functionality. |
(Is there a mailing list or better place for questions? I couldn't find one...)
I'm trying to wrap an API that returns Eigen vectors/matrices of stuff like "symbols" (for symbolic algebra). I think I have wrapping of the actual value types working, but trying to convert them to numpy objects is harder, because they aren't POD data; basically I need a numpy array of
object
s.I think I have a partly working solution, adapted from hacking together bits of
eigen.h
,numpy.h
andstl.h
:...however, I don't understand the way that the numpy C API is called. Apparently there is some dynamic function loading magic such that no numpy headers are ever actually included, but the "functions" I need to call for this to work (
PyArray_GETPTR1
,PyArray_SETITEM
) are actually macros. What's the right way in pybind11 to call these? Is there some other mechanism I don't know to fill the items in the newly created numpy array? Or is there some other approach than creating the numpy array and then filling it?For performance reasons, and because this needs to work with matrices also, I really want to go directly to numpy without an intermediate Python list.
(Note: the dtype is
PyArray_DescrFromType_(NPY_OBJECT_)
, which I believe is correct? Also, this is the first time I've ever tried to work with numpy; please go easy on my lack of knowledge / experience.)The text was updated successfully, but these errors were encountered: