Ticket #948 (closed)

Opened 11 years ago

Last modified 5 years ago

Improve NumPy integration

Reported by: Nick Draper Owned by: Martyn Gigg
Priority: critical Milestone: Iteration 24
Component: Keywords:
Cc: Blocked By:
Blocking: Tester: Mathieu Doucet

Description (last modified by Nick Draper) (diff)

Martyn can help you with this one, but the comment here should help.

Change History

comment:1 Changed 11 years ago by Nick Draper

  • Priority changed from minor to critical
  • Status changed from new to assigned
  • Description modified (diff)
  • Owner changed from Martyn Gigg to Steve Williams
  • Milestone changed from Iteration 22 to Iteration 23

Martyn,

thanks for pointing to me the book with the references to the Numpy C_API. It works very well and very easy to use.

Here is a quick test using std::vector instead of the usual C-array, as well as the command to compile it. I have checked and the data is definitely not copied. Since std::vector is guaranteed to be contiguous in memory on all compilers, we should be in business, and it should be super easy to expose the mantid spectrums.

*code*

#include <boost/python.hpp>
#include <numpy/arrayobject.h>
#include <vector> 

using namespace boost::python;

std::vector<double> x(1e7); 

PyObject* testexpose()
{
	npy_intp N=1e7;
	PyArrayObject* retval=(PyArrayObject*)PyArray_SimpleNewFromData(1,&N,PyArray_DOUBLE,&x[0]);
	return (PyObject*)retval;
}
BOOST_PYTHON_MODULE(libexpose)
{
	import_array();
	def("testexpose",testexpose);
}

*compile*

g++ exposeCArray.cpp -shared -o libexpose.so -I/usr/include/python2.6 -lpython2.6 -lboost_python-py26 -fPIC


*python test

>>> from libexpose import *
>>> a=testexpose()
>>> a
array([ 0.,  0.,  0., ...,  0.,  0.,  0.])
>>> a[2]=3
>>> a
array([ 0.,  0.,  3., ...,  0.,  0.,  0.])

comment:2 Changed 10 years ago by Steve Williams

Will Numpy de compulary or not for installation. Get this done in 24.

comment:3 Changed 10 years ago by Martyn Gigg

  • Owner changed from Steve Williams to Martyn Gigg

comment:4 Changed 10 years ago by Martyn Gigg

  • Status changed from assigned to accepted

comment:5 Changed 10 years ago by Martyn Gigg

(In [5787]) Incorporate numpy bindings into Mantid's PythonAPI. If numpy is installed the MatrixWorkspace read functions return a numpy.ndarray wrapped around the existing data structure such that the data is not copied any more. Re #948

comment:6 Changed 10 years ago by Martyn Gigg

(In [5788]) Missed the Sconscript changes to tell the build where numpy is. Re #948

comment:7 Changed 10 years ago by Martyn Gigg

(In [5799]) A little tidying. Re #948

comment:8 Changed 10 years ago by Martyn Gigg

  • Status changed from accepted to verify

comment:9 Changed 10 years ago by Martyn Gigg

  • Status changed from verify to reopened

comment:10 Changed 10 years ago by Martyn Gigg

  • Status changed from reopened to accepted

comment:11 Changed 10 years ago by Martyn Gigg

(In [5823]) Update the RPM spec template to include the numpy dependency. Re #948

comment:12 Changed 10 years ago by Martyn Gigg

  • Status changed from accepted to verify

comment:13 Changed 10 years ago by Mathieu Doucet

  • Status changed from verify to verifying
  • Tester set to Mathieu Doucet

comment:14 Changed 10 years ago by Mathieu Doucet

(In [5891]) Simple unit test for numpy integration. Re #948

comment:15 Changed 10 years ago by Mathieu Doucet

  • Status changed from verifying to closed

comment:16 Changed 5 years ago by Stuart Campbell

This ticket has been transferred to github issue 1796

Note: See TracTickets for help on using tickets.