Skip to content
Merged
4 changes: 2 additions & 2 deletions examples/10_streaming_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
electronPositions.set_attribute("comment", "I'm a comment")

length = 10
local_data = np.arange(i * length, (i + 1) * length, dtype=np.dtype("double"))
local_data = np.arange(i * length, (i + 1) * length, dtype="double")
for dim in ["x", "y", "z"]:
pos = electronPositions[dim]
pos.reset_dataset(io.Dataset(local_data.dtype, [length]))
Expand All @@ -60,7 +60,7 @@
# temperature has no x,y,z components, so skip the last layer:
temperature_dataset = temperature
# let's say we are in a 3x3 mesh
temperature_dataset.reset_dataset(io.Dataset(np.dtype("double"), [3, 3]))
temperature_dataset.reset_dataset(io.Dataset(local_data.dtype, [3, 3]))
# temperature is constant
temperature_dataset.make_constant(273.15)

Expand Down
3 changes: 1 addition & 2 deletions examples/12_span_write.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import numpy as np
import openpmd_api as io


def span_write(filename):
series = io.Series(filename, io.Access_Type.create_linear)

datatype = np.dtype("double")
datatype = "double"
length = 10
extent = [length]
dataset = io.Dataset(datatype, extent)
Expand Down
6 changes: 3 additions & 3 deletions examples/13_write_dynamic_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def main():
electronPositions.set_attribute("comment", "I'm a comment")

length = 10
local_data = np.arange(i * length, (i + 1) * length, dtype=np.dtype("double"))
local_data = np.arange(i * length, (i + 1) * length, dtype="double")
for dim in ["x", "y", "z"]:
pos = electronPositions[dim]
pos.reset_dataset(io.Dataset(local_data.dtype, [length]))
Expand Down Expand Up @@ -110,10 +110,10 @@ def main():
# temperature has no x,y,z components, so skip the last layer:
temperature_dataset = temperature
# let's say we are in a 3x3 mesh
dataset = io.Dataset(np.dtype("double"), [3, 3], config)
dataset = io.Dataset(local_data.dtype, [3, 3], config)
temperature_dataset.reset_dataset(dataset)
# temperature is constant
local_data = np.arange(i * 9, (i + 1) * 9, dtype=np.dtype("double"))
local_data = np.arange(i * 9, (i + 1) * 9, dtype="double")
local_data = local_data.reshape([3, 3])
temperature_dataset[()] = local_data

Expand Down
10 changes: 5 additions & 5 deletions examples/15_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ def write(filename, config):
E.axis_labels = ["x", "y"]
for dim in ["x", "y"]:
component = E[dim]
component.reset_dataset(opmd.Dataset(np.dtype("float"), [10, 10]))
component.reset_dataset(opmd.Dataset("float", [10, 10]))
component[:, :] = np.reshape(
np.arange(i * 100, (i + 1) * 100, dtype=np.dtype("float")),
np.arange(i * 100, (i + 1) * 100, dtype="float"),
[10, 10],
)

Expand All @@ -57,12 +57,12 @@ def write(filename, config):
for dim in ["x", "y"]:
# Do not bother with a positionOffset
position_offset = e["positionOffset"][dim]
position_offset.reset_dataset(opmd.Dataset(np.dtype("int"), [100]))
position_offset.reset_dataset(opmd.Dataset("int", [100]))
position_offset.make_constant(0)

position = e["position"][dim]
position.reset_dataset(opmd.Dataset(np.dtype("float"), [100]))
position[:] = np.arange(i * 100, (i + 1) * 100, dtype=np.dtype("float"))
position.reset_dataset(opmd.Dataset("float", [100]))
position[:] = np.arange(i * 100, (i + 1) * 100, dtype="float")


def main():
Expand Down
8 changes: 4 additions & 4 deletions examples/7_extended_write_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@
electrons["displacement"].unit_dimension = {Unit_Dimension.M: 1}
electrons["displacement"]["x"].unit_SI = 1.0e-6
del electrons["displacement"]
electrons["weighting"].reset_dataset(
Dataset(np.dtype("float32"), extent=[1])
).make_constant(1.0e-5)
electrons["weighting"].reset_dataset(Dataset("float32", extent=[1])).make_constant(
1.0e-5
)

mesh = cur_it.meshes["lowRez_2D_field"]
mesh.axis_labels = ["x", "y"]
Expand Down Expand Up @@ -122,7 +122,7 @@
d = Dataset(partial_particleOff.dtype, mpiDims)
electrons["positionOffset"]["x"].reset_dataset(d)

dset = Dataset(np.dtype("uint64"), extent=[2])
dset = Dataset("uint64", extent=[2])
electrons.particle_patches["numParticles"].reset_dataset(dset)
electrons.particle_patches["numParticlesOffset"].reset_dataset(dset)

Expand Down
4 changes: 2 additions & 2 deletions examples/9_particle_write_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
# let's set a weird user-defined record this time
electrons["displacement"].unit_dimension = {Unit_Dimension.M: 1}
electrons["displacement"].unit_SI = 1.0e-6
dset = Dataset(np.dtype("float64"), extent=[n_particles])
dset = Dataset("float64", extent=[n_particles])
electrons["displacement"].reset_dataset(dset)
electrons["displacement"].make_constant(42.43)
# don't like it anymore? remove it with:
# del electrons["displacement"]

electrons["weighting"].reset_dataset(
Dataset(np.dtype("float32"), extent=[n_particles])
Dataset("float32", extent=[n_particles])
).make_constant(1.0e-5)

particlePos_x = np.random.rand(n_particles).astype(np.float32)
Expand Down
38 changes: 38 additions & 0 deletions include/openPMD/binding/python/Numpy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,44 @@ inline Datatype dtype_from_numpy(pybind11::dtype const dt)
}
}

inline Datatype dtype_from_numpy(pybind11::object dt)
{
if (py::isinstance<pybind11::dtype>(dt))
{
return dtype_from_numpy(py::cast<pybind11::dtype>(std::move(dt)));
}
else
{
pybind11::module_ numpy;
try
{
numpy = pybind11::module_::import("numpy");
}
catch (std::exception const &e)
{
throw std::runtime_error(
std::string(
"dtype_from_numpy: Cannot convert from object type to "
"datatype without numpy, failed importing: ") +
e.what());
}
pybind11::object create_dtype = numpy.attr("dtype");
pybind11::object dtype_obj;
try
{
dtype_obj = create_dtype(std::move(dt));
}
catch (std::exception const &e)
{
throw std::runtime_error(
std::string("dtype_from_numpy: Failed to create dtype from: ") +
pybind11::str(dt).cast<std::string>() +
std::string(", error: ") + e.what());
}
return dtype_from_numpy(py::cast<pybind11::dtype>(dtype_obj));
}
}

/** Return openPMD::Datatype from py::buffer_info::format
*/
inline Datatype dtype_from_bufferformat(std::string const &fmt)
Expand Down
8 changes: 5 additions & 3 deletions src/binding/python/Dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void init_Dataset(py::module &m)
py::arg("extent"),
py::arg("options") = "{}")
.def(
py::init([](py::dtype dt, Extent e, std::string options) {
py::init([](py::object dt, Extent e, std::string options) {
auto const d = dtype_from_numpy(std::move(dt));
return new Dataset{d, std::move(e), std::move(options)};
}),
Expand All @@ -54,8 +54,10 @@ void init_Dataset(py::module &m)
py::arg("extent"),
py::arg("options"))
.def(
py::init([](py::dtype dt, Extent e, py::object const &options) {
auto const d = dtype_from_numpy(std::move(dt));
py::init([](py::object const &dt,
Extent e,
py::object const &options) {
auto const d = dtype_from_numpy(dt);
auto resolved_options = ::auxiliary::json_dumps(options);
return new Dataset{
d, std::move(e), std::move(resolved_options)};
Expand Down
5 changes: 3 additions & 2 deletions src/binding/python/RecordComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1020,9 +1020,10 @@ void init_RecordComponent(py::module &m)
.def(
"make_empty",
[](RecordComponent &rc,
pybind11::dtype const &dt,
pybind11::object dt,
uint8_t dimensionality) {
return rc.makeEmpty(dtype_from_numpy(dt), dimensionality);
return rc.makeEmpty(
dtype_from_numpy(std::move(dt)), dimensionality);
})

// deprecated: pass-through C++ API
Expand Down
Loading
Loading