32 PyErr_SetString(PyExc_RuntimeError,
"Object not convertible: Invalid Python object.");
38 if (pyinterface == NULL)
47 auto pyshape = PyDict_GetItemString(pyinterface,
"shape");
49 PyErr_SetString(PyExc_RuntimeError,
"Object not convertible: __array_interface__['shape'] does not exist.");
52 std::vector<std::size_t> shape;
53 for (
Py_ssize_t i = 0; i < PyTuple_Size(pyshape); i++) {
54 const auto s = PyLong_AsLong(PyTuple_GetItem(pyshape, i));
60 if (typestr.compare(
"") == 0)
66 const auto dtype = typestr.substr(1, typestr.size());
68 if (cppdtype.compare(
"") == 0)
72 if (!PyObject_HasAttrString(obj,
"strides")) {
73 PyErr_SetString(PyExc_RuntimeError,
"Object not convertible: Object does not have method 'strides'.");
76 auto pystrides = PyObject_GetAttrString(obj,
"strides");
77 std::vector<std::size_t> strides;
78 for (
Py_ssize_t i = 0; i < PyTuple_Size(pystrides); i++) {
79 strides.push_back(PyInt_AsLong(PyTuple_GetItem(pystrides, i)) / dtypesize);
85 if (strides.size() > 1) {
86 if (strides.front() < strides.back()) rowMajor =
false;
90 const std::string klassname =
"TMVA::Experimental::RTensor<" + cppdtype +
",std::vector<" + cppdtype +
">>";
91 std::stringstream code;
92 code <<
"new " << klassname <<
"(reinterpret_cast<" << cppdtype <<
"*>(" << std::hex << std::showbase <<
data <<
"),{";
93 for (
auto s: shape) code << s <<
",";
95 for (
auto s: strides) code << s <<
",";
98 code <<
"TMVA::Experimental::MemoryLayout::RowMajor";
101 code <<
"TMVA::Experimental::MemoryLayout::ColumnMajor";
104 const auto codestr = code.str();
105 auto address = (
void*)
gInterpreter->Calc(codestr.c_str());
115 if (PyObject_SetAttrString(pyobj,
"__adopted__", obj)) {
116 PyErr_SetString(PyExc_RuntimeError,
"Object not convertible: Failed to set Python object as attribute __adopted__.");
121 Py_DECREF(pyinterface);
unsigned long long GetDataPointerFromArrayInterface(PyObject *obj)
Get data pointer from Numpy array interface and perform error handling.
unsigned int GetDatatypeSizeFromTypestr(const std::string &typestr)
Get size of data type in bytes from Numpy type string.
std::string GetCppTypeFromNumpyType(const std::string &dtype)
Convert Numpy data-type string to the according C++ data-type string.
std::string GetTypestrFromArrayInterface(PyObject *obj)
Get type string from Numpy array interface and perform error handling.
bool CheckEndianessFromTypestr(const std::string &typestr)
Check whether endianess in type string matches the endianess of ROOT.
PyObject * GetArrayInterface(PyObject *obj)
Get Numpy array interface and perform error handling.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
PyObject * BindCppObject(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, const unsigned flags=0)
RPY_EXPORTED TCppScope_t GetScope(const std::string &scope_name)
PyObject * AsRTensor(PyObject *self, PyObject *obj)
Adopt memory of a Python object with array interface using an RTensor.