14#include <boost/python/extract.hpp>
15#include <boost/python/stl_iterator.hpp>
16#include <boost/python/str.hpp>
18#define PY_ARRAY_UNIQUE_SYMBOL CORE_ARRAY_API
19#define NO_IMPORT_ARRAY
20#include <numpy/arrayobject.h>
22using boost::python::extract;
23using boost::python::handle;
24using boost::python::object;
25using boost::python::str;
32#define DECLARE_EXTERN(HeldType) \
33 extern template int NDArrayTypeIndex<HeldType>::typenum; \
34 extern template char NDArrayTypeIndex<HeldType>::typecode;
59template <
typename DestElementType>
struct CopyToImpl {
66 void *data = PyArray_ITER_DATA(iter.ptr());
67 *first++ = *
static_cast<DestElementType *
>(data);
68 PyArray_ITER_NEXT(iter.ptr());
69 }
while (PyArray_ITER_NOTDONE(iter.ptr()));
77template <>
struct CopyToImpl<
std::string> {
78 void operator()(
typename std::vector<std::string>::iterator first,
PyArrayObject *arr) {
79 object flattened(handle<>(PyArray_Ravel(arr, NPY_CORDER)));
80 const Py_ssize_t nelements = PyArray_Size(flattened.ptr());
81 for (Py_ssize_t i = 0; i < nelements; ++i) {
82 *first++ = extract<std::string>(str(flattened[i]))();
92template <
typename DestElementType>
struct CoerceType {
93 NDArray operator()(
const NDArray &
x) {
102template <>
struct CoerceType<
std::string> {
103 object operator()(
const NDArray &
x) {
return x; }
116template <
typename DestElementType>
124template <
typename DestElementType>
126 std::vector<DestElementType> cvector(PyArray_SIZE((
PyArrayObject *)m_arr.ptr()));
138 throwIfSizeMismatched(dest);
139 CopyToImpl<DestElementType>()(std::begin(dest), (
PyArrayObject *)m_arr.ptr());
149template <
typename DestElementType>
151 if (PyArray_SIZE((
PyArrayObject *)m_arr.ptr()) ==
static_cast<ssize_t
>(dest.size())) {
154 throw std::invalid_argument(
"Invalid number of elements while copying from ndarray. "
164#define INSTANTIATE_TOVECTOR(ElementType) template struct DLLExport 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.