Skip to content

Commit 2388fb8

Browse files
committed
Add extraction of NumPy arrays from python::object.
Add functions extractNumPyDoubleArray and createNumPyDoubleArrayLike.
1 parent d8b7c00 commit 2388fb8

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

srrealmodule/srreal_converters.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ NumPyArray_DoublePtr createNumPyDoubleArray(int dim, const int* sz)
106106
}
107107

108108

109+
/// helper for creating numpy array of the same shape as the argument
110+
NumPyArray_DoublePtr createNumPyDoubleArrayLike(boost::python::object& obj)
111+
{
112+
PyObject* arr = obj.ptr();
113+
int dim = PyArray_NDIM(arr);
114+
npy_intp* shape = PyArray_DIMS(arr);
115+
// create numpy array
116+
boost::python::object rvobj(
117+
boost::python::handle<>(
118+
PyArray_SimpleNew(dim, shape, PyArray_DOUBLE)));
119+
double* rvdata = static_cast<double*>(PyArray_DATA(rvobj.ptr()));
120+
NumPyArray_DoublePtr rv(rvobj, rvdata);
121+
return rv;
122+
}
123+
124+
109125
/// helper for creating a numpy array view on a double array
110126
boost::python::object
111127
createNumPyDoubleView(double* data, int dim, const int* sz)
@@ -248,6 +264,24 @@ extractQuantityType(
248264
}
249265

250266

267+
/// efficient conversion of Python object to a numpy array of doubles
268+
NumPyArray_DoublePtr extractNumPyDoubleArray(::boost::python::object& obj)
269+
{
270+
PyObject* arr = PyArray_ContiguousFromAny(obj.ptr(), PyArray_DOUBLE, 0, 0);
271+
if (!arr)
272+
{
273+
const char* emsg = "Cannot convert this object to numpy array.";
274+
PyErr_SetString(PyExc_TypeError, emsg);
275+
boost::python::throw_error_already_set();
276+
assert(false);
277+
}
278+
boost::python::object rvobj((boost::python::handle<>(arr)));
279+
double* rvdata = static_cast<double*>(PyArray_DATA(arr));
280+
NumPyArray_DoublePtr rv(rvobj, rvdata);
281+
return rv;
282+
}
283+
284+
251285
/// extract integer with a support for numpy.int types
252286
int extractint(boost::python::object obj)
253287
{

srrealmodule/srreal_converters.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,19 @@ void fillPyListWithSets(::boost::python::list lst, const T& value)
227227
/// Type for numpy array object and a raw pointer to its double data
228228
typedef std::pair<boost::python::object, double*> NumPyArray_DoublePtr;
229229

230+
230231
/// helper for creating numpy array of doubles
231232
NumPyArray_DoublePtr createNumPyDoubleArray(int dim, const int* sz);
232233

234+
235+
/// helper for creating numpy array of the same shape as the argument
236+
NumPyArray_DoublePtr createNumPyDoubleArrayLike(boost::python::object& obj);
237+
238+
233239
/// helper for creating numpy views on existing double array
234240
boost::python::object createNumPyDoubleView(double*, int dim, const int* sz);
235241

242+
236243
/// template function for converting iterables to numpy array of doubles
237244
template <class Iter>
238245
::boost::python::object
@@ -296,25 +303,31 @@ convertToNumPyArray(const ::diffpy::srreal::QuantityType& value)
296303
return convertToNumPyArray(value.begin(), value.end());
297304
}
298305

306+
299307
/// NumPy array view specializations for R3::Vector
300308
boost::python::object
301309
viewAsNumPyArray(::diffpy::srreal::R3::Vector&);
302310

311+
303312
/// NumPy array view specializations for R3::Matrix
304313
boost::python::object
305314
viewAsNumPyArray(::diffpy::srreal::R3::Matrix&);
306315

316+
307317
/// Copy possible NumPy array to R3::Vector
308318
void assignR3Vector(
309319
::diffpy::srreal::R3::Vector& dst, boost::python::object& value);
310320

321+
311322
/// Copy possible NumPy array to R3::Matrix
312323
void assignR3Matrix(
313324
::diffpy::srreal::R3::Matrix& dst, boost::python::object& value);
314325

326+
315327
/// Type for numpy array object and a raw pointer to its double data
316328
typedef std::pair<boost::python::object, int*> NumPyArray_IntPtr;
317329

330+
318331
/// helper for creating numpy array of integers
319332
NumPyArray_IntPtr createNumPyIntArray(int dim, const int* sz);
320333

@@ -361,6 +374,10 @@ extractQuantityType(::boost::python::object obj,
361374
::diffpy::srreal::QuantityType& rv);
362375

363376

377+
/// efficient conversion of Python object to a numpy array of doubles
378+
NumPyArray_DoublePtr extractNumPyDoubleArray(::boost::python::object& obj);
379+
380+
364381
/// extract integer with a support for numpy.int types
365382
int extractint(::boost::python::object obj);
366383

0 commit comments

Comments
 (0)