13#include "MantidPythonInterface/core/DllConfig.h"
15#include <boost/python/extract.hpp>
16#include <boost/python/stl_iterator.hpp>
17#include <boost/python/str.hpp>
19#define PY_ARRAY_UNIQUE_SYMBOL CORE_ARRAY_API
20#define NO_IMPORT_ARRAY
21#include <numpy/arrayobject.h>
23using boost::python::extract;
24using boost::python::handle;
25using boost::python::object;
26using boost::python::str;
33#define DECLARE_EXTERN(HeldType) \
34 extern template int NDArrayTypeIndex<HeldType>::typenum; \
35 extern template char NDArrayTypeIndex<HeldType>::typecode;
60template <
typename DestElementType>
struct CopyToImpl {
67 void *data = PyArray_ITER_DATA(iter.ptr());
68 *first++ = *
static_cast<DestElementType *
>(data);
69 PyArray_ITER_NEXT(iter.ptr());
70 }
while (PyArray_ITER_NOTDONE(iter.ptr()));
78template <>
struct CopyToImpl<
std::string> {
79 void operator()(
typename std::vector<std::string>::iterator first,
PyArrayObject *arr) {
80 object flattened(handle<>(PyArray_Ravel(arr, NPY_CORDER)));
81 const Py_ssize_t nelements = PyArray_Size(flattened.ptr());
82 for (Py_ssize_t i = 0; i < nelements; ++i) {
83 *first++ = extract<std::string>(str(flattened[i]))();
93template <
typename DestElementType>
struct CoerceType {
94 NDArray operator()(
const NDArray &
x) {
103template <>
struct CoerceType<
std::string> {
104 object operator()(
const NDArray &
x) {
return x; }
117template <
typename DestElementType>
125template <
typename DestElementType>
127 std::vector<DestElementType> cvector(PyArray_SIZE(
reinterpret_cast<PyArrayObject *
>(m_arr.ptr())));
138 if (PyArray_SIZE(
reinterpret_cast<PyArrayObject *
>(m_arr.ptr())) > 0) {
139 throwIfSizeMismatched(dest);
140 CopyToImpl<DestElementType>()(std::begin(dest),
reinterpret_cast<PyArrayObject *
>(m_arr.ptr()));
150template <
typename DestElementType>
152 if (PyArray_SIZE(
reinterpret_cast<PyArrayObject *
>(m_arr.ptr())) ==
153 static_cast<boost::python::ssize_t
>(dest.size())) {
156 throw std::invalid_argument(
"Invalid number of elements while copying from ndarray. "
166#define INSTANTIATE_TOVECTOR(ElementType) template struct MANTID_PYTHONINTERFACE_CORE_DLL NDArrayToVector<ElementType>;
double value
The value of the point.
#define INSTANTIATE_TOVECTOR(ElementType)
#define DECLARE_EXTERN(HeldType)
Macro to declare template instantiations as extern; those are defined in NDArrayTypeIndex....
tagPyArrayObject PyArrayObject
Thin object wrapper around a numpy array.
MANTID_PYTHONINTERFACE_CORE_DLL PyObject * func_PyArray_IterNew(PyArrayObject *arr)
equivalent to macro PyArray_IterNew
std::string to_string(const wide_integer< Bits, Signed > &n)
Converter taking an input numpy array and converting it to a std::vector.
NDArrayToVector(const NDArray &value)
Constructor.
void throwIfSizeMismatched(const TypedVector &dest) const
void copyTo(TypedVector &dest) const
Fill the container with data from the array.
std::vector< DestElementType > TypedVector
TypedVector operator()()
Create a new vector from the contents of the array.
Defines a mapping between C++ type given by the template parameter and numpy type enum NPY_TYPES.