LvArray
Namespaces | Functions | Variables
numpyHelpers.hpp File Reference

Contains methods to help with conversion to python objects. More...

#include "pythonForwardDeclarations.hpp"
#include "pythonHelpers.hpp"
#include "../limits.hpp"
#include <vector>
#include <typeindex>
#include <type_traits>
Include dependency graph for numpyHelpers.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

 LvArray
 The top level namespace.
 
 LvArray::python
 Contains all the Python code.
 

Functions

PyObject * LvArray::python::internal::createNumpyArrayImpl (void *const data, std::type_index const type, bool const dataIsConst, int const ndim, long long const *const dims, long long const *const strides)
 Return a NumPy ndarray that is a view into data. More...
 
PyObject * LvArray::python::internal::createCupyArrayImpl (void *const data, std::type_index const type, bool const dataIsConst, int const ndim, long long const *const dims, long long const *const strides)
 Return a CuPy ndarray that is a view into data. More...
 
bool LvArray::python::import_array_wrapper ()
 Attempt to import the NumPy API if it was not already imported, return true iff successful. More...
 
template<typename T , typename INDEX_TYPE >
std::enable_if_t< internal::canExportToNumpy< T >, PyObject *> LvArray::python::createNumPyArray (T *const data, bool const modify, int const ndim, INDEX_TYPE const *const dimsPtr, INDEX_TYPE const *const stridesPtr)
 Return a NumPy ndarray view of data. More...
 
template<typename T >
std::enable_if_t< internal::canExportToNumpy< T >, PyObject *> LvArray::python::create (T &value)
 Create a NumPy 1D array of length 1 containing the scalar value. More...
 
std::tuple< PyObjectRef< PyObject >, void const *, long long > LvArray::python::parseNumPyArray (PyObject *const obj, std::type_index const expectedType)
 Attempt to parse obj into a NumPy ndarray of type expectedType. More...
 
std::type_index LvArray::python::getTypeIndexFromNumPy (int const numpyType)
 Return the std::type_index corresponding to the NumPy type numpyType. More...
 
std::string LvArray::python::getNumPyTypeName (int const numpyType)
 Return the name corresponding to the NumPy type numpyType. More...
 
PyObject * LvArray::python::getNumPyTypeObject (std::type_index const typeIndex)
 Return the NumPy type object corresponding to typeIndex. More...
 

Variables

template<typename T >
constexpr bool LvArray::python::internal::canExportToNumpy = std::is_arithmetic< T >::value
 True if T can be represented by a NumPy native data type. More...
 

Detailed Description

Contains methods to help with conversion to python objects.

Function Documentation

◆ createCupyArrayImpl()

PyObject * LvArray::python::internal::createCupyArrayImpl ( void *const  data,
std::type_index const  type,
bool const  dataIsConst,
int const  ndim,
long long const *const  dims,
long long const *const  strides 
)

Return a CuPy ndarray that is a view into data.

Parameters
dataA pointer to the data to export.
typeThe type of the data to export.
dataIsConstTrue iff the data should be immutable. Note that unlike NumPy this is not honored by CuPy, although this may change in the future.
ndimThe number of dimensions of data.
dimsThe size of each dimension.
stridesThe strides of each dimension.
Returns
A CuPy ndarray that is a view into data, or nullptr if there was an error.

◆ createNumpyArrayImpl()

PyObject * LvArray::python::internal::createNumpyArrayImpl ( void *const  data,
std::type_index const  type,
bool const  dataIsConst,
int const  ndim,
long long const *const  dims,
long long const *const  strides 
)

Return a NumPy ndarray that is a view into data.

Parameters
dataA pointer to the data to export.
typeThe type of the data to export.
dataIsConstTrue iff the data should be immutable. When this is true modifying the values in the returned ndarray raises an exception.
ndimThe number of dimensions of data.
dimsThe size of each dimension.
stridesThe strides of each dimension.
Returns
A NumPy ndarray that is a view into data, or nullptr if there was an error.

Variable Documentation

◆ canExportToNumpy

template<typename T >
constexpr bool LvArray::python::internal::canExportToNumpy = std::is_arithmetic< T >::value

True if T can be represented by a NumPy native data type.

Template Parameters
TThe type to query.