23 #include <pybind11/pybind11.h> 24 #include <pybind11/stl.h> 25 #include <pybind11/numpy.h> 27 #include "openPMD/Datatype.hpp" 36 dtype_from_numpy( pybind11::dtype
const dt )
40 if( dt.is(pybind11::dtype(
"b")) )
41 return Datatype::CHAR;
42 else if( dt.is(pybind11::dtype(
"B")) )
43 return Datatype::UCHAR;
44 else if( dt.is(pybind11::dtype(
"short")) )
45 return Datatype::SHORT;
46 else if( dt.is(pybind11::dtype(
"intc")) )
48 else if( dt.is(pybind11::dtype(
"int_")) )
49 return Datatype::LONG;
50 else if( dt.is(pybind11::dtype(
"longlong")) )
51 return Datatype::LONGLONG;
52 else if( dt.is(pybind11::dtype(
"ushort")) )
53 return Datatype::USHORT;
54 else if( dt.is(pybind11::dtype(
"uintc")) )
55 return Datatype::UINT;
56 else if( dt.is(pybind11::dtype(
"uint")) )
57 return Datatype::ULONG;
58 else if( dt.is(pybind11::dtype(
"ulonglong")) )
59 return Datatype::ULONGLONG;
60 else if( dt.is(pybind11::dtype(
"longdouble")) )
61 return Datatype::LONG_DOUBLE;
62 else if( dt.is(pybind11::dtype(
"double")) )
63 return Datatype::DOUBLE;
64 else if( dt.is(pybind11::dtype(
"single")) )
65 return Datatype::FLOAT;
66 else if( dt.is(pybind11::dtype(
"bool")) )
67 return Datatype::BOOL;
69 throw std::runtime_error(
"Datatype '...' not known in 'dtype_from_numpy'!");
84 if( fmt.find(
"?") != std::string::npos )
86 else if( fmt.find(
"b") != std::string::npos )
88 else if( fmt.find(
"h") != std::string::npos )
90 else if( fmt.find(
"i") != std::string::npos )
92 else if( fmt.find(
"l") != std::string::npos )
94 else if( fmt.find(
"q") != std::string::npos )
96 else if( fmt.find(
"B") != std::string::npos )
98 else if( fmt.find(
"H") != std::string::npos )
100 else if( fmt.find(
"I") != std::string::npos )
102 else if( fmt.find(
"L") != std::string::npos )
104 else if( fmt.find(
"Q") != std::string::npos )
105 return DT::ULONGLONG;
106 else if( fmt.find(
"f") != std::string::npos )
108 else if( fmt.find(
"d") != std::string::npos )
110 else if( fmt.find(
"g") != std::string::npos )
111 return DT::LONG_DOUBLE;
113 throw std::runtime_error(
"dtype_from_bufferformat: Unknown " 114 "Python type '" + fmt +
"'");
117 inline pybind11::dtype
127 return pybind11::dtype(
"b");
131 return pybind11::dtype(
"B");
139 return pybind11::dtype(
"short");
143 return pybind11::dtype(
"intc");
147 return pybind11::dtype(
"int_");
150 case DT::VEC_LONGLONG:
151 return pybind11::dtype(
"longlong");
155 return pybind11::dtype(
"ushort");
159 return pybind11::dtype(
"uintc");
163 return pybind11::dtype(
"uint");
166 case DT::VEC_ULONGLONG:
167 return pybind11::dtype(
"ulonglong");
171 return pybind11::dtype(
"single");
176 return pybind11::dtype(
"double");
178 case DT::LONG_DOUBLE:
179 case DT::VEC_LONG_DOUBLE:
180 return pybind11::dtype(
"longdouble");
183 return pybind11::dtype(
"bool");
188 throw std::runtime_error(
"dtype_to_numpy: Invalid Datatype '{...}'!");
Datatype dtype_from_bufferformat(std::string const &fmt)
Return openPMD::Datatype from py::buffer_info::format.
Definition: Numpy.hpp:75
Datatype
Concrete datatype of an object available at runtime.
Definition: Datatype.hpp:38
Public definitions of openPMD-api.
Definition: Date.cpp:28