30 #ifdef NUMCPP_INCLUDE_PYBIND_PYTHON_INTERFACE
32 #include "pybind11/numpy.h"
33 #include "pybind11/pybind11.h"
44 namespace pybindInterface
47 enum class ReturnPolicy
54 static const std::map<ReturnPolicy, std::string> returnPolicyStringMap = {
55 { ReturnPolicy::COPY,
"COPY" },
56 { ReturnPolicy::REFERENCE,
"REFERENCE" },
57 { ReturnPolicy::TAKE_OWNERSHIP,
"TAKE_OWNERSHIP" }
60 template<
typename dtype>
61 using pbArray = pybind11::array_t<dtype, pybind11::array::c_style>;
62 using pbArrayGeneric = pybind11::array;
72 template<
typename dtype>
73 NdArray<dtype> pybind2nc(pbArray<dtype>& numpyArray)
75 const auto dataPtr = numpyArray.mutable_data();
76 switch (numpyArray.ndim())
80 return NdArray<dtype>(dataPtr, 0, 0,
false);
85 return NdArray<dtype>(dataPtr, 1,
size,
false);
89 const uint32 numRows =
static_cast<uint32>(numpyArray.shape(0));
90 const uint32 numCols =
static_cast<uint32>(numpyArray.shape(1));
91 return NdArray<dtype>(dataPtr, numRows, numCols,
false);
109 template<
typename dtype>
110 NdArray<dtype> pybind2nc_copy(
const pbArray<dtype>& numpyArray)
112 const auto dataPtr = numpyArray.data();
113 switch (numpyArray.ndim())
117 return NdArray<dtype>(dataPtr, 0, 0);
122 return NdArray<dtype>(dataPtr, 1,
size);
126 const uint32 numRows =
static_cast<uint32>(numpyArray.shape(0));
127 const uint32 numCols =
static_cast<uint32>(numpyArray.shape(1));
128 return NdArray<dtype>(dataPtr, numRows, numCols);
145 template<
typename dtype>
146 pbArrayGeneric nc2pybind(
const NdArray<dtype>& inArray)
148 const Shape inShape = inArray.shape();
149 const std::vector<pybind11::ssize_t>
shape{
static_cast<pybind11::ssize_t
>(inShape.rows),
150 static_cast<pybind11::ssize_t
>(inShape.cols) };
151 const std::vector<pybind11::ssize_t> strides{
static_cast<pybind11::ssize_t
>(inShape.cols *
sizeof(dtype)),
152 static_cast<pybind11::ssize_t
>(
sizeof(dtype)) };
153 return pbArrayGeneric(
shape, strides, inArray.data());
164 template<
typename dtype>
165 pbArrayGeneric nc2pybind(NdArray<dtype>& inArray, ReturnPolicy returnPolicy)
167 const Shape inShape = inArray.shape();
168 const std::vector<pybind11::ssize_t>
shape{
static_cast<pybind11::ssize_t
>(inShape.rows),
169 static_cast<pybind11::ssize_t
>(inShape.cols) };
170 const std::vector<pybind11::ssize_t> strides{
static_cast<pybind11::ssize_t
>(inShape.cols *
sizeof(dtype)),
171 static_cast<pybind11::ssize_t
>(
sizeof(dtype)) };
173 switch (returnPolicy)
175 case ReturnPolicy::COPY:
177 return nc2pybind(inArray);
179 case ReturnPolicy::REFERENCE:
181 typename pybind11::capsule reference(inArray.data(), [](
void* ) {});
182 return pbArrayGeneric(
shape, strides, inArray.data(), reference);
184 case ReturnPolicy::TAKE_OWNERSHIP:
186 typename pybind11::capsule garbageCollect(inArray.dataRelease(),
189 dtype* dataPtr = reinterpret_cast<dtype*>(ptr);
192 return pbArrayGeneric(
shape, strides, inArray.data(), garbageCollect);
196 std::stringstream sstream;
197 sstream <<
"ReturnPolicy " << returnPolicyStringMap.at(returnPolicy)
198 <<
" has not been implemented yet" << std::endl;
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
Definition: Coordinate.hpp:45
uint32 size(const NdArray< dtype > &inArray) noexcept
Definition: size.hpp:43
Shape shape(const NdArray< dtype > &inArray) noexcept
Definition: Functions/Shape.hpp:42
std::uint32_t uint32
Definition: Types.hpp:40