MCPcopy Create free account
hub / github.com/MITK/MITK / BuildNumpyArray

Function BuildNumpyArray

Wrapping/Python/mitk/Image.cpp:126–158  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

124}
125
126py::array BuildNumpyArray(const Image& img, const void* data, bool writeable, py::capsule capsule)
127{
128 auto dtype = PixelTypeToDType(img.GetPixelType());
129 auto format = PixelTypeFormat(img.GetPixelType());
130 auto shape = ComputeNumpyShape(img);
131
132 std::vector<py::ssize_t> strides(shape.size());
133 strides.back() = dtype.itemsize();
134 for (py::ssize_t i = static_cast<py::ssize_t>(shape.size()) - 2; i >= 0; --i)
135 strides[i] = strides[i + 1] * shape[i + 1];
136
137 // pybind11's buffer_info stores a non-const void*. Mirror what the
138 // library itself does in its typed buffer_info(const T*, ...) overload:
139 // const_cast the pointer here and let the readonly flag enforce
140 // const-ness at the Python boundary.
141 py::array arr(
142 py::buffer_info(
143 const_cast<void*>(data),
144 dtype.itemsize(),
145 format,
146 shape.size(),
147 shape,
148 strides,
149 !writeable
150 ),
151 capsule
152 );
153
154 if (!writeable)
155 arr.attr("flags").attr("writeable") = py::bool_(false);
156
157 return arr;
158}
159
160py::array AsNumpyDirect(Image& img, bool writeable, mitk::TimeStepType time_step)
161{

Callers 2

AsNumpyDirectFunction · 0.85
AsNumpyAccessorFunction · 0.85

Calls 6

PixelTypeToDTypeFunction · 0.85
PixelTypeFormatFunction · 0.85
ComputeNumpyShapeFunction · 0.85
backMethod · 0.80
GetPixelTypeMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected