Title: Support for custom types? · Issue #285 · xtensor-stack/xtensor-python · GitHub
Open Graph Title: Support for custom types? · Issue #285 · xtensor-stack/xtensor-python
X Title: Support for custom types? · Issue #285 · xtensor-stack/xtensor-python
Description: I'm trying to wrap a custom type using pybind11 and xtensor-python. Although I can create a numpy array of my wrapped type from Python, I cannot pass this numpy array to C++ functions or pass xt::xarray to Python. The C++ part is #includ...
Open Graph Description: I'm trying to wrap a custom type using pybind11 and xtensor-python. Although I can create a numpy array of my wrapped type from Python, I cannot pass this numpy array to C++ functions or pass xt::x...
X Description: I'm trying to wrap a custom type using pybind11 and xtensor-python. Although I can create a numpy array of my wrapped type from Python, I cannot pass this numpy array to C++ functions or pass x...
Opengraph URL: https://github.com/xtensor-stack/xtensor-python/issues/285
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support for custom types?","articleBody":"I'm trying to wrap a custom type using pybind11 and xtensor-python. Although I can create a numpy array of my wrapped type from Python, I cannot pass this numpy array to C++ functions or pass xt::xarray to Python.\r\n\r\nThe C++ part is\r\n\r\n```c++\r\n#include \u003cpybind11/numpy.h\u003e\r\n#include \u003cpybind11/pybind11.h\u003e\r\n\r\n#include \u003cxtensor/xarray.hpp\u003e\r\n#define FORCE_IMPORT_ARRAY\r\n#include \u003cxtensor-python/pyarray.hpp\u003e\r\n\r\nstruct Quantity\r\n{\r\n double q;\r\n \r\n Quantity(double q=0)\r\n : q(q)\r\n {\r\n }\r\n};\r\n\r\nPYBIND11_MODULE(test_array, test_array)\r\n{\r\n xt::import_numpy();\r\n \r\n pybind11::class_\u003cQuantity\u003e(test_array, \"Quantity\")\r\n .def(pybind11::init\u003cdouble\u003e())\r\n .def_readwrite(\"q\", \u0026Quantity::q);\r\n \r\n test_array.def(\"create\", []() { return xt::xarray\u003cQuantity\u003e{1.1, 2.2}; });\r\n test_array.def(\r\n \"consume\", [](xt::xarray\u003cQuantity\u003e const \u0026 array) {\r\n for(auto \u0026\u0026 item: array)\r\n {\r\n std::cout \u003c\u003c item.q \u003c\u003c \"\\n\";\r\n }\r\n });\r\n}\r\n```\r\n\r\nAnd the Python code\r\n```python\r\nimport numpy\r\nimport test_array\r\n\r\nq1 = test_array.Quantity(42)\r\nq2 = test_array.Quantity(3.14)\r\na = numpy.array([q1, q2])\r\n\r\nprint([x.q for x in a])\r\n\r\ntry:\r\n test_array.create()\r\nexcept Exception as e:\r\n print(e)\r\n\r\ntry:\r\n test_array.consume(a)\r\nexcept Exception as e:\r\n print(e)\r\n```\r\n\r\nBoth try/except blocks in Python yield a RuntimeError saying \"NumPy type info missing for 8Quantity\"\r\n\r\nIs this use case supported by xtensor-python? If so, what am I missing? If not, what alternative would you suggest?","author":{"url":"https://github.com/lamyj","@type":"Person","name":"lamyj"},"datePublished":"2022-12-17T14:59:28.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/285/xtensor-python/issues/285"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c5daec77-4c28-c055-ed46-6f57bd6444ec |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E8A0:211079:2BB5E53:3AA4867:696EF7A6 |
| html-safe-nonce | 1e86220e859d091e965bbeda1ebc017bcc68432ca716ef0becaf47c83c523565 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOEEwOjIxMTA3OToyQkI1RTUzOjNBQTQ4Njc6Njk2RUY3QTYiLCJ2aXNpdG9yX2lkIjoiOTAxNTY5NjM0MjQ5ODg2NzExMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b813e4f5412066993e6eb5c3fa2e58a9bfca82009e00b9f6145746e4bccbf4ae |
| hovercard-subject-tag | issue:1501428409 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/xtensor-stack/xtensor-python/285/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e78597e781c14f8b12319570a17f2026882408294030bf4bfacd710177d89e2c/xtensor-stack/xtensor-python/issues/285 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e78597e781c14f8b12319570a17f2026882408294030bf4bfacd710177d89e2c/xtensor-stack/xtensor-python/issues/285 |
| og:image:alt | I'm trying to wrap a custom type using pybind11 and xtensor-python. Although I can create a numpy array of my wrapped type from Python, I cannot pass this numpy array to C++ functions or pass xt::x... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lamyj |
| hostname | github.com |
| expected-hostname | github.com |
| None | b278ad162d35332b6de714dfb005de04386c4d92df6475522bef910f491a35ee |
| turbo-cache-control | no-preview |
| go-import | github.com/xtensor-stack/xtensor-python git https://github.com/xtensor-stack/xtensor-python.git |
| octolytics-dimension-user_id | 56542697 |
| octolytics-dimension-user_login | xtensor-stack |
| octolytics-dimension-repository_id | 73431892 |
| octolytics-dimension-repository_nwo | xtensor-stack/xtensor-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 73431892 |
| octolytics-dimension-repository_network_root_nwo | xtensor-stack/xtensor-python |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 39aed5006635ab6f45e6b77d23e73b08a00272a3 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width