Mantid
Loading...
Searching...
No Matches
NDArray.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include <boost/python/detail/prefix.hpp> // Safe include of Python.h
10#include <boost/python/tuple.hpp>
11#define PY_ARRAY_UNIQUE_SYMBOL CORE_ARRAY_API
12#include <numpy/arrayobject.h>
13
14using namespace boost::python;
15
17
23 if (!Py_IsInitialized()) {
24 throw std::runtime_error("Library requires an active Python interpreter.\n"
25 "Call Py_Initialize at an appropriate point in the application.");
26 }
27
28 if (_import_array() < 0) {
29 PyErr_Print();
30 PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
31 }
32}
33
38PyTypeObject *ndarrayType() { return &PyArray_Type; }
39
40// -----------------------------------------------------------------------------
41// NDArray
42// -----------------------------------------------------------------------------
43
49bool NDArray::check(const object &obj) { return PyArray_Check(obj.ptr()); }
50
56NDArray::NDArray(const object &obj) : object(detail::borrowed_reference(obj.ptr())) {}
57
61Py_intptr_t const *NDArray::get_shape() const { return PyArray_DIMS(reinterpret_cast<PyArrayObject *>(this->ptr())); }
62
66int NDArray::get_nd() const { return PyArray_NDIM(reinterpret_cast<PyArrayObject *>(this->ptr())); }
67
73void *NDArray::get_data() const { return PyArray_DATA(reinterpret_cast<PyArrayObject *>(this->ptr())); }
74
79char NDArray::get_typecode() const { return PyArray_DESCR(reinterpret_cast<PyArrayObject *>(this->ptr()))->type; }
80
89NDArray NDArray::astype(char dtype, bool copy) const {
90 auto callable = object(handle<>(PyObject_GetAttrString(this->ptr(), const_cast<char *>("astype"))));
91 auto args = tuple();
92 auto kwargs = object(handle<>(Py_BuildValue(const_cast<char *>("{s:c,s:i}"), "dtype", dtype, "copy", copy ? 1 : 0)));
93 return NDArray(boost::python::detail::new_reference(PyObject_Call(callable.ptr(), args.ptr(), kwargs.ptr())));
94}
95
96} // namespace Mantid::PythonInterface
97
98// -----------------------------------------------------------------------------
99// object_manager_traits specialisation for NDArray
100// -----------------------------------------------------------------------------
101namespace boost::python::converter {
102
104
110bool object_manager_traits<Mantid::PythonInterface::NDArray>::check(PyObject *obj) {
111 return ::PyObject_IsInstance(obj, (PyObject *)ndarrayType());
112}
113
121python::detail::new_reference object_manager_traits<Mantid::PythonInterface::NDArray>::adopt(PyObject *obj) {
122 return python::detail::new_reference(python::pytype_check(ndarrayType(), obj));
123}
124
129PyTypeObject const *object_manager_traits<Mantid::PythonInterface::NDArray>::get_pytype() { return ndarrayType(); }
130} // namespace boost::python::converter
tagPyArrayObject PyArrayObject
std::string dtype(Mantid::Kernel::PropertyWithValue< HeldType > &self)
double obj
the value of the quadratic function
Thin object wrapper around a numpy array.
Definition: NDArray.h:31
static bool check(const boost::python::object &obj)
Check if a python object points to an array type object.
Definition: NDArray.cpp:49
Py_intptr_t const * get_shape() const
Definition: NDArray.cpp:61
char get_typecode() const
See https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html.
Definition: NDArray.cpp:79
void * get_data() const
This returns char so stride math works properly on it.
Definition: NDArray.cpp:73
NDArray(const boost::python::object &obj)
Construction from a plain object.
Definition: NDArray.cpp:56
NDArray astype(char dtype, bool copy=true) const
Casts (and copies if necessary) the array to the given data type.
Definition: NDArray.cpp:89
MANTID_PYTHONINTERFACE_CORE_DLL void importNumpy()
Initialize the numpy array api for this DLL.
Definition: NDArray.cpp:22
MANTID_PYTHONINTERFACE_CORE_DLL PyTypeObject * ndarrayType()
Return the type object for a numpy.NDArray.
Definition: NDArray.cpp:38