30 #ifdef NUMCPP_INCLUDE_PYBIND_PYTHON_INTERFACE
32 #include "pybind11/numpy.h"
33 #include "pybind11/pybind11.h"
42 namespace nc::pybindInterface
45 enum class ReturnPolicy
52 static const std::map<ReturnPolicy, std::string> returnPolicyStringMap = { { ReturnPolicy::COPY,
"COPY" },
53 { ReturnPolicy::REFERENCE,
"REFERENCE" },
54 { ReturnPolicy::TAKE_OWNERSHIP,
57 template<
typename dtype>
58 using pbArray = pybind11::array_t<dtype, pybind11::array::c_style>;
59 using pbArrayGeneric = pybind11::array;
69 template<
typename dtype>
70 NdArray<dtype> pybind2nc(pbArray<dtype>& numpyArray)
72 const auto dataPtr = numpyArray.mutable_data();
73 switch (numpyArray.ndim())
77 return NdArray<dtype>(dataPtr, 0, 0,
false);
81 const auto size =
static_cast<uint32>(numpyArray.size());
82 return NdArray<dtype>(dataPtr, 1,
size,
false);
86 const auto numRows =
static_cast<uint32>(numpyArray.shape(0));
87 const auto numCols =
static_cast<uint32>(numpyArray.shape(1));
88 return NdArray<dtype>(dataPtr, numRows, numCols,
false);
106 template<
typename dtype>
107 NdArray<dtype> pybind2nc_copy(
const pbArray<dtype>& numpyArray)
109 const auto dataPtr = numpyArray.data();
110 switch (numpyArray.ndim())
114 return NdArray<dtype>(dataPtr, 0, 0);
118 const auto size =
static_cast<uint32>(numpyArray.size());
119 return NdArray<dtype>(dataPtr, 1,
size);
123 const auto numRows =
static_cast<uint32>(numpyArray.shape(0));
124 const auto numCols =
static_cast<uint32>(numpyArray.shape(1));
125 return NdArray<dtype>(dataPtr, numRows, numCols);
142 template<
typename dtype>
143 pbArrayGeneric nc2pybind(
const NdArray<dtype>& inArray)
145 const Shape inShape = inArray.shape();
146 const std::vector<pybind11::ssize_t>
shape{
static_cast<pybind11::ssize_t
>(inShape.rows),
147 static_cast<pybind11::ssize_t
>(inShape.cols) };
148 const std::vector<pybind11::ssize_t> strides{
static_cast<pybind11::ssize_t
>(inShape.cols *
sizeof(dtype)),
149 static_cast<pybind11::ssize_t
>(
sizeof(dtype)) };
150 return pbArrayGeneric(
shape, strides, inArray.data());
161 template<
typename dtype>
162 pbArrayGeneric nc2pybind(NdArray<dtype>& inArray, ReturnPolicy returnPolicy)
164 const Shape inShape = inArray.shape();
165 const std::vector<pybind11::ssize_t>
shape{
static_cast<pybind11::ssize_t
>(inShape.rows),
166 static_cast<pybind11::ssize_t
>(inShape.cols) };
167 const std::vector<pybind11::ssize_t> strides{
static_cast<pybind11::ssize_t
>(inShape.cols *
sizeof(dtype)),
168 static_cast<pybind11::ssize_t
>(
sizeof(dtype)) };
170 switch (returnPolicy)
172 case ReturnPolicy::COPY:
174 return nc2pybind(inArray);
176 case ReturnPolicy::REFERENCE:
178 typename pybind11::capsule reference(inArray.data(), [](
void* ) {});
179 return pbArrayGeneric(
shape, strides, inArray.data(), reference);
181 case ReturnPolicy::TAKE_OWNERSHIP:
183 typename pybind11::capsule garbageCollect(inArray.dataRelease(),
186 auto* dataPtr = reinterpret_cast<dtype*>(ptr);
189 return pbArrayGeneric(
shape, strides, inArray.data(), garbageCollect);
193 std::stringstream sstream;
194 sstream <<
"ReturnPolicy " << returnPolicyStringMap.at(returnPolicy) <<
" has not been implemented yet"
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
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