Skip to content

Commit

Permalink
Add default arguments and configurable array type to getNumpy
Browse files Browse the repository at this point in the history
  • Loading branch information
bengineerd committed Oct 15, 2024
1 parent b47e9cd commit 1595980
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
4 changes: 3 additions & 1 deletion include/rogue/interfaces/stream/Frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "rogue/EnableSharedFromThis.h"

#ifndef NO_PYTHON
#include <numpy/ndarrayobject.h>

#include <boost/python.hpp>
#endif

Expand Down Expand Up @@ -354,7 +356,7 @@ class Frame : public rogue::EnableSharedFromThis<rogue::interfaces::stream::Fram
* @param[in] size The number of bytes to write
*
*/
boost::python::object getNumpy(uint32_t offset, uint32_t size);
boost::python::object getNumpy(uint32_t offset = 0, uint32_t size = 0, int dtype = NPY_UINT8);

//! Python Frame data write using a numpy array as the source
/*
Expand Down
16 changes: 11 additions & 5 deletions src/rogue/interfaces/stream/Frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
namespace ris = rogue::interfaces::stream;

#ifndef NO_PYTHON
#include <numpy/arrayobject.h>
#include <numpy/ndarraytypes.h>
#include <numpy/ndarrayobject.h>

#include <boost/python.hpp>
namespace bp = boost::python;
Expand Down Expand Up @@ -397,10 +396,14 @@ void ris::Frame::writePy(boost::python::object p, uint32_t offset) {
}

//! Read the specified number of bytes at the specified offset of frame data into a numpy array
boost::python::object ris::Frame::getNumpy(uint32_t offset, uint32_t count) {
boost::python::object ris::Frame::getNumpy(uint32_t offset, uint32_t count, int dtype) {
// Retrieve the size, in bytes of the data
npy_intp size = getPayload();

if (count == 0) {
count = size - offset;
}

// Check this does not request data past the EOF
if ((offset + count) > size) {
throw(rogue::GeneralError::create("Frame::getNumpy",
Expand All @@ -413,7 +416,7 @@ boost::python::object ris::Frame::getNumpy(uint32_t offset, uint32_t count) {

// Create a numpy array to receive it and locate the destination data buffer
npy_intp dims[1] = {count};
PyObject* obj = PyArray_SimpleNew(1, dims, NPY_UINT8);
PyObject* obj = PyArray_SimpleNew(1, dims, dtype);
PyArrayObject* arr = reinterpret_cast<PyArrayObject*>(obj);
uint8_t* dst = reinterpret_cast<uint8_t*>(PyArray_DATA(arr));

Expand Down Expand Up @@ -502,7 +505,10 @@ void ris::Frame::setup_python() {
.def("getLastUser", &ris::Frame::getLastUser)
.def("setChannel", &ris::Frame::setChannel)
.def("getChannel", &ris::Frame::getChannel)
.def("getNumpy", &ris::Frame::getNumpy)
.def("getNumpy", &ris::Frame::getNumpy, (
bp::arg("offset")=0,
bp::arg("count")=0,
bp::arg("dtype")=NPY_UINT8))
.def("putNumpy", &ris::Frame::putNumpy)
.def("_debug", &ris::Frame::debug);
#endif
Expand Down

0 comments on commit 1595980

Please sign in to comment.