LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Public Types | Static Public Member Functions | List of all members
ndarray::PyConverter< Array< T, N, C > > Struct Template Reference

A traits class providing Python conversion functions for Array. More...

#include <numpy.h>

Inheritance diagram for ndarray::PyConverter< Array< T, N, C > >:
ndarray::detail::PyConverterBase< Array< T, N, C > >

Public Types

typedef Array< T, N, C >::Element Element
 
typedef boost::remove_const
< Element >::type 
NonConst
 

Static Public Member Functions

static bool fromPythonStage1 (PyPtr &p)
 Check if a Python object is convertible to T and optionally begin the conversion by replacing the input with an intermediate. More...
 
static bool fromPythonStage2 (PyPtr const &input, Array< T, N, C > &output)
 Complete a Python to C++ conversion begun with fromPythonStage1(). More...
 
static PyObject * toPython (Array< T, N, C > const &m, PyObject *owner=NULL)
 Create a numpy.ndarray from an ndarray::Array. More...
 
static PyTypeObject const * getPyType ()
 
- Static Public Member Functions inherited from ndarray::detail::PyConverterBase< Array< T, N, C > >
static bool matches (PyObject *arg)
 Check if a Python object might be convertible to T. More...
 
static int fromPython (PyObject *arg, Array< T, N, C > *output)
 Convert a Python object to a C++ object. More...
 

Detailed Description

template<typename T, int N, int C>
struct ndarray::PyConverter< Array< T, N, C > >

A traits class providing Python conversion functions for Array.

This specialization, for Array, adds addititional optional arguments to the toPython() conversion member function.

Definition at line 112 of file numpy.h.

Member Typedef Documentation

template<typename T , int N, int C>
typedef Array<T,N,C>::Element ndarray::PyConverter< Array< T, N, C > >::Element

Definition at line 113 of file numpy.h.

template<typename T , int N, int C>
typedef boost::remove_const<Element>::type ndarray::PyConverter< Array< T, N, C > >::NonConst

Definition at line 114 of file numpy.h.

Member Function Documentation

template<typename T , int N, int C>
static bool ndarray::PyConverter< Array< T, N, C > >::fromPythonStage1 ( PyPtr p)
inlinestatic

Check if a Python object is convertible to T and optionally begin the conversion by replacing the input with an intermediate.

Returns
true if a conversion may be possible, and false if it is not (with a Python exception set).
Parameters
pOn input, a Python object to be converted. On output, a Python object to be passed to fromPythonStage2().

Definition at line 124 of file numpy.h.

129  {
130  if (!PyArray_Check(p.get())) {
131  PyErr_SetString(PyExc_TypeError, "numpy.ndarray argument required");
132  return false;
133  }
134  int actualType = PyArray_TYPE(p.get());
135  int requiredType = detail::NumpyTraits<NonConst>::getCode();
136  if (actualType != requiredType) {
137  PyErr_SetString(PyExc_ValueError, "numpy.ndarray argument has incorrect data type");
138  return false;
139  }
140  if (PyArray_NDIM(p.get()) != N) {
141  PyErr_SetString(PyExc_ValueError, "numpy.ndarray argument has incorrect number of dimensions");
142  return false;
143  }
144  bool writeable = !boost::is_const<Element>::value;
145  if (writeable && !(PyArray_FLAGS(p.get()) & NPY_WRITEABLE)) {
146  PyErr_SetString(PyExc_TypeError, "numpy.ndarray argument must be writeable");
147  return false;
148  }
149  if (C > 0) {
150  int requiredStride = sizeof(Element);
151  for (int i = 0; i < C; ++i) {
152  int actualStride = PyArray_STRIDE(p.get(), N-i-1);
153  if (actualStride != requiredStride) {
154  PyErr_SetString(
155  PyExc_ValueError,
156  "numpy.ndarray does not have enough row-major contiguous dimensions"
157  );
158  return false;
159  }
160  requiredStride *= PyArray_DIM(p.get(), N-i-1);
161  }
162  } else if (C < 0) {
163  int requiredStride = sizeof(Element);
164  for (int i = 0; i < -C; ++i) {
165  int actualStride = PyArray_STRIDE(p.get(), i);
166  if (actualStride != requiredStride) {
167  PyErr_SetString(
168  PyExc_ValueError,
169  "numpy.ndarray does not have enough column-major contiguous dimensions"
170  );
171  return false;
172  }
173  requiredStride *= PyArray_DIM(p.get(), i);
174  }
175  }
176  return true;
177  }
Array< T, N, C >::Element Element
Definition: numpy.h:113
template<typename T , int N, int C>
static bool ndarray::PyConverter< Array< T, N, C > >::fromPythonStage2 ( PyPtr const &  input,
Array< T, N, C > &  output 
)
inlinestatic

Complete a Python to C++ conversion begun with fromPythonStage1().

The copy will be shallow if possible and deep if necessary to meet the data type and contiguousness requirements. If a non-const array is required, the copy will always be shallow; if this is not possible, ValueError will be raised.

The output Array's shared_ptr owner attribute will own a reference to the numpy array that ultimately owns the data (either the original or the copy).

Returns
true on success, false on failure (with a Python exception set).
Parameters
inputResult of fromPythonStage1().
outputReference to existing output C++ object.

Definition at line 191 of file numpy.h.

194  {
195  if (!(PyArray_FLAGS(input.get()) & NPY_ALIGNED)) {
196  PyErr_SetString(PyExc_ValueError, "unaligned arrays cannot be converted to C++");
197  return false;
198  }
199  Vector<int,N> shape;
200  Vector<int,N> strides;
201  std::copy(PyArray_DIMS(input.get()), PyArray_DIMS(input.get()) + N, shape.begin());
202  std::copy(PyArray_STRIDES(input.get()), PyArray_STRIDES(input.get()) + N , strides.begin());
203  for (int i = 0; i < N; ++i) strides[i] /= sizeof(Element);
204  output = external(
205  reinterpret_cast<Element*>(PyArray_DATA(input.get())),
206  shape, strides, input
207  );
208  return true;
209  }
Array< T, N, C >::Element Element
Definition: numpy.h:113
detail::ExternalInitializer< T, N, Owner > external(T *data, Vector< int, N > const &shape, Vector< int, N > const &strides, Owner const &owner)
Create an expression that initializes an Array with externally allocated memory.
SelectEigenView< T >::Type copy(Eigen::EigenBase< T > const &other)
Copy an arbitrary Eigen expression into a new EigenView.
Definition: eigen.h:390
template<typename T , int N, int C>
static PyTypeObject const* ndarray::PyConverter< Array< T, N, C > >::getPyType ( )
inlinestatic

Definition at line 262 of file numpy.h.

262 { return &PyArray_Type; }
template<typename T , int N, int C>
static PyObject* ndarray::PyConverter< Array< T, N, C > >::toPython ( Array< T, N, C > const &  m,
PyObject *  owner = NULL 
)
inlinestatic

Create a numpy.ndarray from an ndarray::Array.

The Array will be shallow-copied with reference counting if either m.getManager() is not empty or the optional owner argument is supplied; otherwise a deep copy will be made.

Returns
a new Python object, or NULL on failure (with a Python exception set).
Parameters
mThe input Array object.
ownerA Python object that owns the memory in the Array. If NULL, one will be constructed from m.getManager().

Definition at line 221 of file numpy.h.

225  {
226  int flags = NPY_ALIGNED;
227  if (C==N) flags |= NPY_C_CONTIGUOUS;
228  bool writeable = !boost::is_const<Element>::value;
229  if (writeable) flags |= NPY_WRITEABLE;
230  npy_intp outShape[N];
231  npy_intp outStrides[N];
232  Vector<int,N> inShape = m.getShape();
233  Vector<int,N> inStrides = m.getStrides();
234  std::copy(inShape.begin(), inShape.end(), outShape);
235  for (int i = 0; i < N; ++i) outStrides[i] = inStrides[i] * sizeof(Element);
236  PyPtr array(
237  PyArray_New(
238  &PyArray_Type, N, outShape, detail::NumpyTraits<NonConst>::getCode(),
239  outStrides, const_cast<NonConst*>(m.getData()), sizeof(Element), flags, NULL
240  ),
241  false
242  );
243  if (!array) return NULL;
244  if (!m.getManager() && owner == NULL) {
245  flags = NPY_CARRAY_RO | NPY_ENSURECOPY | NPY_C_CONTIGUOUS;
246  if (writeable) flags |= NPY_WRITEABLE;
247  PyPtr r = PyArray_FROM_OF(array.get(),flags);
248  if (!r) return NULL;
249  array.swap(r);
250  } else {
251  if (owner != NULL) {
252  Py_INCREF(owner);
253  } else {
254  owner = PyCObject_FromVoidPtr(new Manager::Ptr(m.getManager()), detail::destroyCObject);
255  }
256  reinterpret_cast<PyArrayObject*>(array.get())->base = owner;
257  }
258  Py_INCREF(array.get());
259  return PyArray_Return(reinterpret_cast<PyArrayObject*>(array.get()));
260  }
void destroyCObject(void *p)
Definition: numpy.h:97
Array< T, N, C >::Element Element
Definition: numpy.h:113
boost::intrusive_ptr< PyObject > PyPtr
A reference-counting smart pointer for PyObject.
Definition: PyConverter.h:48
SelectEigenView< T >::Type copy(Eigen::EigenBase< T > const &other)
Copy an arbitrary Eigen expression into a new EigenView.
Definition: eigen.h:390
boost::intrusive_ptr< Manager > Ptr
Definition: Manager.h:42
tuple m
Definition: lsstimport.py:48

The documentation for this struct was generated from the following file: