Ticket #948 (closed enhancement)

Opened 11 months ago

Last modified 2 weeks ago

Improve NumPy integration

Reported by: Nick Draper Owned by: Martyn Gigg
Priority: critical Milestone: Iteration 24
Keywords: Cc:
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

Changed 5 months ago by Nick Draper

  • owner changed from Martyn Gigg to Steve Williams
  • priority changed from minor to critical
  • status changed from new to assigned
  • description modified (diff)
  • 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.])

Changed 3 months ago by Steve Williams

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

Changed 7 weeks ago by Martyn Gigg

  • owner changed from Steve Williams to Martyn Gigg

Changed 3 weeks ago by Martyn Gigg

  • status changed from assigned to accepted

Changed 3 weeks 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

Changed 3 weeks ago by Martyn Gigg

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

Changed 3 weeks ago by Martyn Gigg

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

Changed 3 weeks ago by Martyn Gigg

  • status changed from accepted to verify

Changed 2 weeks ago by Martyn Gigg

  • status changed from verify to reopened

Changed 2 weeks ago by Martyn Gigg

  • status changed from reopened to accepted

Changed 2 weeks ago by Martyn Gigg

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

Changed 2 weeks ago by Martyn Gigg

  • status changed from accepted to verify

Changed 2 weeks ago by Mathieu Doucet

  • status changed from verify to verifying
  • tester set to Mathieu Doucet

Changed 2 weeks ago by Mathieu Doucet

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

Changed 2 weeks ago by Mathieu Doucet

  • status changed from verifying to closed
Note: See TracTickets for help on using tickets.