Skip to content

NDArray

class NDArray : protected Hash

@brief A class representing multi-dimensional data in Karabo that seaminglessy converts to numpy.ndarray

The NDArray class is intended to store any multidimensional data occurring in Karabo. Internally it holds the data in a ByteArray. It is a Hash-derived structure, which means it serializes into a karabo::data::Hash. Its meta-data is chosen such that it can be seamlessly converted into a numpy.ndarray

Internally (i.e. relevant only for serialisation), it holds the following keys: - "data": a ByteArray - "shape": a vector - "type": an int matching a Types::ReferenceType value - "isBigEndian": a bool (If this changes, it needs to be followed up in the NDArrayDescription helper class of the NDArrayElement.)

Functions

Name Description
NDArray This constructor creates an empty NDArray shape : type : isBigEndian :
NDArray This constructor creates an NDArray where all values are initialized with a fill value shape : fill : isBigEndian :
NDArray This constructor copies data from the provided memory location.
NDArray This constructor copies data from the provided iterator range.
NDArray This constructor does NOT copy data.
NDArray Non-templated copy/no-copy construction depending on 'copy' flag (default is false).
setShape Set the schape of the array shape :
size The number of items in the array.
byteSize The total size of the array, in bytes.
itemSize The size of each item, in bytes.
getData Get the data contained in the array as a pointer Return :
getDataPtr Get a shared pointer to the underlying ByteArray data Return :
getByteArray Return the underlying ByteArray Return :
getShape Return the shape of the array as a karabo::data::Dims object Return :
isBigEndian Evaluate if the data contained in the array is big endian Return : true if big endian, false if little ednian
toLittleEndian Convert data to little endian
toBigEndian Convert data to big endian

Function Details

NDArray

explicit NDArray(const Dims& shape = Dims(), const karabo::data::Types::ReferenceType& type = karabo::data::Types::DOUBLE, const bool isBigEndian = karabo::data::isBigEndian())

This constructor creates an empty NDArray

shape :

type :

isBigEndian :

template <typename T> NDArray(const Dims& shape, const T& fill, const bool isBigEndian = karabo::data::isBigEndian())

This constructor creates an NDArray where all values are initialized with a fill value

shape :

fill :

isBigEndian :

template <typename T> NDArray(const T* dataPtr, const size_t numElems, const Dims& shape = Dims(), const bool isBigEndian = karabo::data::isBigEndian())

This constructor copies data from the provided memory location. Internally the data is kept as shared pointer.

dataPtr : Typed address to a stretch of contiguous memory

numElems : Number of elements (of type T)

shape : Shape information

isBigEndian : Endianess flag

template <typename InputIterator> NDArray(InputIterator first, InputIterator last, const Dims& shape = Dims(), const bool isBigEndian = karabo::data::isBigEndian())

This constructor copies data from the provided iterator range. Data type is deduced from the value_type of the InputIterator. The range from 'first' (included) to 'last' (excluded) has to be valid range.

first : Begin of range

last : End of range (i.e. points one behind as vector.end())

shape : Shape information

isBigEndian : Endianess flag

template <typename T, typename D> NDArray(const T* dataPtr, const size_t numElems, const D& deleter, const Dims& shape = Dims(), const bool isBigEndian = karabo::data::isBigEndian())

This constructor does NOT copy data. Only a view on the external memory is established. A functor for dealing with the viewed on memory must be provided in case this object gets destructed.

dataPtr : Typed address to a stretch of contiguous memory

numElems : Number of elements (of type T)

deleter : A functor defining the deletion behavior

shape : Shape information

isBigEndian : Endianess flag

NDArray(const DataPointer& ptr, const karabo::data::Types::ReferenceType& type, const size_t& numElems, const Dims& shape = Dims(), const bool isBigEndian = karabo::data::isBigEndian(), bool copy = false)

Non-templated copy/no-copy construction depending on 'copy' flag (default is false).

ptr : Shared_ptr to external memory location

type : Type of provided data

numElems : Number of elements of provided type

shape : Shape information

isBigEndian : Endianess flag

byteSize

inline size_t byteSize() const

The total size of the array, in bytes.

getByteArray

ByteArray getByteArray()

Return the underlying ByteArray

Return :

getData

template <typename T> const T* getData() const

Get the data contained in the array as a pointer

Return :

getDataPtr

const DataPointer& getDataPtr() const

Get a shared pointer to the underlying ByteArray data

Return :

getShape

Dims getShape() const

Return the shape of the array as a karabo::data::Dims object

Return :

isBigEndian

bool isBigEndian() const

Evaluate if the data contained in the array is big endian

Return : true if big endian, false if little ednian

itemSize

inline size_t itemSize() const

The size of each item, in bytes.

setShape

void setShape(const Dims& shape)

Set the schape of the array

shape :

size

inline size_t size() const

The number of items in the array.

toBigEndian

void toBigEndian()

Convert data to big endian

toLittleEndian

void toLittleEndian()

Convert data to little endian