14#include "MantidPythonInterface/core/DllConfig.h"
15#include "MantidTypes/Core/DateAndTime.h"
16#include <boost/python/list.hpp>
19#define PY_ARRAY_UNIQUE_SYMBOL CORE_ARRAY_API
20#define NO_IMPORT_ARRAY
21#include <numpy/arrayobject.h>
43template <
typename ElementType> PyObject *
clone1D(
const std::vector<ElementType> &cvector) {
44 Py_intptr_t dims[1] = {
static_cast<int>(cvector.size())};
45 return cloneND(cvector.data(), 1, dims);
53template <> MANTID_PYTHONINTERFACE_CORE_DLL PyObject *
clone1D(
const std::vector<Types::Core::DateAndTime> &cvector) {
56 Py_intptr_t dims[1] = {
static_cast<int>(cvector.size())};
58 PyArray_NewFromDescr(&PyArray_Type, descr, 1, dims,
nullptr,
nullptr, 0,
nullptr));
60 for (Py_intptr_t i = 0; i < dims[0]; ++i) {
61 void *itemPtr = PyArray_GETPTR1(nparray, i);
63 auto scalar = PyArray_Scalar(
reinterpret_cast<char *
>(&abstime), descr,
nullptr);
64 PyArray_SETITEM(nparray,
reinterpret_cast<char *
>(itemPtr), scalar);
67 return reinterpret_cast<PyObject *
>(nparray);
76template <> MANTID_PYTHONINTERFACE_CORE_DLL PyObject *
clone1D(
const std::vector<bool> &cvector) {
77 Py_intptr_t dims[1] = {
static_cast<int>(cvector.size())};
81 for (Py_intptr_t i = 0; i < dims[0]; ++i) {
82 void *itemPtr = PyArray_GETPTR1(nparray, i);
83 auto py_bool = PyBool_FromLong(
static_cast<long int>(cvector[i]));
84 PyArray_SETITEM(nparray,
reinterpret_cast<char *
>(itemPtr), py_bool);
87 return reinterpret_cast<PyObject *
>(nparray);
99template <
typename ElementType> PyObject *
cloneND(
const ElementType *carray,
const int ndims, Py_intptr_t *dims) {
103 size_t length(dims[0]);
105 for (
int i = 1; i < ndims; ++i) {
109 void *arrayData = PyArray_DATA(nparray);
110 const void *data =
static_cast<void *
>(
const_cast<ElementType *
>(carray));
112 std::memcpy(arrayData, data, PyArray_ITEMSIZE(nparray) * length);
114 return reinterpret_cast<PyObject *
>(nparray);
128template <> PyObject *
cloneND(
const std::string *carray,
const int ndims, Py_intptr_t *dims) {
129 boost::python::list pystrs;
130 const std::string *iter = carray;
131 for (
int i = 0; i < ndims; ++i) {
132 const Py_intptr_t length = dims[i];
133 for (Py_intptr_t j = 0; j < length; ++j) {
134 pystrs.append(iter->c_str());
138 PyObject *rawptr = pystrs.ptr();
146#define INSTANTIATE_CLONE1D(ElementType) \
147 template MANTID_PYTHONINTERFACE_CORE_DLL PyObject *clone1D<ElementType>(const std::vector<ElementType> &cvector);
149#define INSTANTIATE_CLONEND(ElementType) \
150 template MANTID_PYTHONINTERFACE_CORE_DLL PyObject *cloneND<ElementType>(const ElementType *, const int ndims, \
153#define INSTANTIATE_CLONE(ElementType) \
154 INSTANTIATE_CLONE1D(ElementType) \
155 INSTANTIATE_CLONEND(ElementType)
#define INSTANTIATE_CLONE(ElementType)
#define INSTANTIATE_CLONEND(ElementType)
#define INSTANTIATE_CLONE1D(ElementType)
tagPyArrayObject PyArrayObject
_PyArray_Descr PyArray_Descr
PyObject * cloneND(const ElementType *carray, const int ndims, Py_intptr_t *dims)
Returns a new numpy array with the a copy of the data from array.
PyObject * clone1D(const std::vector< ElementType > &cvector)
Returns a new numpy array with the a copy of the data from 1D vector with the exception of string ele...
MANTID_PYTHONINTERFACE_CORE_DLL PyArrayObject * func_PyArray_NewFromDescr(int datatype, int ndims, Py_intptr_t *dims)
equivalent to macro PyArray_NewFromDescr
MANTID_PYTHONINTERFACE_CORE_DLL PyArray_Descr * descr_ns()
MANTID_PYTHONINTERFACE_CORE_DLL npy_datetime to_npy_datetime(const Types::Core::DateAndTime &dateandtime)
Total nanoseconds since the unix epoch.
Defines a mapping between C++ type given by the template parameter and numpy type enum NPY_TYPES.