Skip to content

Commit 0a15fa1

Browse files
committed
Try converting from things like np.int_ to np.dtype
1 parent d80b5a6 commit 0a15fa1

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

include/openPMD/binding/python/Numpy.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ inline Datatype dtype_from_numpy(pybind11::dtype const dt)
9898
}
9999
}
100100

101+
inline Datatype dtype_from_numpy(pybind11::object const &dt)
102+
{
103+
if (py::isinstance<pybind11::dtype>(dt))
104+
{
105+
return dtype_from_numpy(py::cast<pybind11::dtype>(dt));
106+
}
107+
else
108+
{
109+
auto numpy = pybind11::module_::import("numpy");
110+
auto create_dtype = numpy.attr("dtype");
111+
return dtype_from_numpy(py::cast<pybind11::dtype>(create_dtype(dt)));
112+
}
113+
}
114+
101115
/** Return openPMD::Datatype from py::buffer_info::format
102116
*/
103117
inline Datatype dtype_from_bufferformat(std::string const &fmt)
@@ -239,4 +253,5 @@ inline pybind11::dtype dtype_to_numpy(Datatype const dt)
239253
throw std::runtime_error("dtype_to_numpy: Invalid Datatype!");
240254
}
241255
}
256+
242257
} // namespace openPMD

src/binding/python/Dataset.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ void init_Dataset(py::module &m)
3737
py::arg("extent"),
3838
py::arg("options") = "{}")
3939
.def(
40-
py::init([](py::dtype dt, Extent e, std::string options) {
41-
auto const d = dtype_from_numpy(std::move(dt));
42-
return new Dataset{d, std::move(e), std::move(options)};
43-
}),
40+
py::init(
41+
[](py::object const &dt, Extent e, std::string options) {
42+
auto const d = dtype_from_numpy(dt);
43+
return new Dataset{d, std::move(e), std::move(options)};
44+
}),
4445
py::arg("dtype"),
4546
py::arg("extent"),
4647
py::arg("options") = "{}")
@@ -54,8 +55,10 @@ void init_Dataset(py::module &m)
5455
py::arg("extent"),
5556
py::arg("options"))
5657
.def(
57-
py::init([](py::dtype dt, Extent e, py::object const &options) {
58-
auto const d = dtype_from_numpy(std::move(dt));
58+
py::init([](py::object const &dt,
59+
Extent e,
60+
py::object const &options) {
61+
auto const d = dtype_from_numpy(dt);
5962
auto resolved_options = ::auxiliary::json_dumps(options);
6063
return new Dataset{
6164
d, std::move(e), std::move(resolved_options)};

0 commit comments

Comments
 (0)