Skip to content

Commit

Permalink
Refs #4315. Add first UnitCell function for numpy pass throughs
Browse files Browse the repository at this point in the history
Allows the test to do something moderately useful and this allows me to
check it works on all platforms
  • Loading branch information
martyngigg committed Dec 14, 2011
1 parent 12ec10b commit 4e02a89
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 4 deletions.
1 change: 1 addition & 0 deletions Code/Mantid/Framework/PythonInterface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ set ( TEST_PY_FILES
test/python/SimpleAPITest.py
test/python/QuatTest.py
test/python/V3DTest.py
test/python/UnitCellTest.py
test/python/WorkspaceTest.py
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,23 @@ namespace Mantid
{
namespace Numpy
{
/** @name Create Numpy arrays */
//@{
/// Create a numpy array wrapper around existing vector. This is only possible for contiguous data
DLLExport PyObject *wrapWithNumpy(const std::vector<double> & data);
/// Create a read-only numpy array wrapper around existing data.
DLLExport PyObject *wrapWithReadOnlyNumpy(const std::vector<double> & data);

/// Create a read-only array wrapper around a double Matrix
DLLExport PyObject *wrapWithNumpy(const Kernel::DblMatrix & data);
/// Create a read-only array wrapper around a double Matrix
DLLExport PyObject *wrapWithReadOnlyNumpy(const Kernel::DblMatrix & data);
//@}

/** @name Create Mantid objects from python sequences */
//@{
DLLExport Kernel::DblMatrix createMatrixFromNumpyArray(PyObject* data);
//@}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// See http://docs.scipy.org/doc/numpy/reference/c-api.array.html#PY_ARRAY_UNIQUE_SYMBOL
#define PY_ARRAY_UNIQUE_SYMBOL API_ARRAY_API
#include <numpy/arrayobject.h>
#include <boost/python/numeric.hpp>

// Forward declare
@EXPORT_DECLARE@
Expand All @@ -14,6 +15,7 @@ BOOST_PYTHON_MODULE(_api)
boost::python::docstring_options docstrings(true, true, false);
// Import numpy
_import_array();
boost::python::numeric::array::set_module_and_type("numpy", "ndarray");

@EXPORT_FUNCTIONS@
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "MantidGeometry/Crystal/UnitCell.h"
#include "MantidPythonInterface/kernel/NumpyConverters.h"
#include <boost/python/class.hpp>
#include <boost/python/enum.hpp>
#include <boost/python/scope.hpp>
Expand All @@ -7,8 +8,19 @@ using Mantid::Geometry::UnitCell;
using Mantid::Geometry::AngleUnits;
using Mantid::Geometry::angRadians;
using Mantid::Geometry::angDegrees;
using Mantid::Kernel::DblMatrix;
using namespace boost::python;

// Functions purely to aid with wrapping
namespace //<unnamed>
{
void recalculateFromGstar(UnitCell & self, PyObject* values)
{
// Create a double matrix and put this in to the unit cell
self.recalculateFromGstar(Mantid::PythonInterface::Numpy::createMatrixFromNumpyArray(values));
}
}

void export_UnitCell()
{
enum_<AngleUnits>("AngleUnits")
Expand Down Expand Up @@ -58,8 +70,8 @@ void export_UnitCell()
.def( "volume", (double ( UnitCell::* )() const) &UnitCell::volume)
// .def( "getG", &UnitCellWrapper::getG)
// .def( "getGstar", &UnitCellWrapper::getGstar)
// .def( "getB", &UnitCellWrapper::getB )
// .def( "recalculateFromGstar", &UnitCellWrapper::recalculateFromGStar) ;
//.def( "getB", &getB )
.def( "recalculateFromGstar", &recalculateFromGstar)
;

scope().attr("deg2rad") = Mantid::Geometry::deg2rad;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// See http://docs.scipy.org/doc/numpy/reference/c-api.array.html#PY_ARRAY_UNIQUE_SYMBOL
#define PY_ARRAY_UNIQUE_SYMBOL GEOMETRY_ARRAY_API
#include <numpy/arrayobject.h>
#include <boost/python/numeric.hpp>

using boost::python::def;

Expand All @@ -17,6 +18,7 @@ BOOST_PYTHON_MODULE(_geometry)
boost::python::docstring_options docstrings(true, true, false);
// Import numpy
_import_array();
boost::python::numeric::array::set_module_and_type("numpy", "ndarray");

@EXPORT_FUNCTIONS@
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#define NO_IMPORT_ARRAY
#include <numpy/ndarrayobject.h>

#include <boost/python/tuple.hpp>
#include <boost/python/numeric.hpp>
#include <boost/python/extract.hpp>

namespace Mantid
{
namespace PythonInterface
Expand Down Expand Up @@ -61,6 +65,43 @@ namespace Mantid
return (PyObject*)nparray;
}

}
/**
* Create a Matrix of doubles from a 2D numpy array
* @param data A python object that points to a 2D numpy array
* @return A Matrix<double> created from the input array
*/
Kernel::DblMatrix createMatrixFromNumpyArray(PyObject *data)
{
using namespace boost::python;
if( PyArray_Check(data) )
{
numeric::array numarray = extract<numeric::array>(data);
numarray = (boost::python::numeric::array) numarray.astype('d'); // Force the array to be of double type (in case it was int)
boost::python::tuple shape( numarray.attr("shape") );
if( boost::python::len(shape) != 2 )
{
std::ostringstream msg;
msg << "createMatrixFromNumpyArray - Expected an array with 2 dimensions but was given array with " << boost::python::len(shape) << " dimensions.";
throw std::invalid_argument(msg.str());
}
size_t nx = boost::python::extract<size_t>(shape[0]);
size_t ny = boost::python::extract<size_t>(shape[1]);
Kernel::Matrix<double> matrix(nx,ny);
for( size_t i = 0; i < nx; i++ )
{
for( size_t j = 0; j < ny; j++ )
{
matrix[i][j] = extract<double>( numarray[boost::python::make_tuple( i, j )]);
}
}
return matrix;
}
else
{
throw std::invalid_argument(std::string("createMatrixFromNumpyArray - Expected numpy array as input, found ") + data->ob_type->tp_name);
}
}

} // Numpy
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// See http://docs.scipy.org/doc/numpy/reference/c-api.array.html#PY_ARRAY_UNIQUE_SYMBOL
#define PY_ARRAY_UNIQUE_SYMBOL KERNEL_ARRAY_API
#include <numpy/arrayobject.h>
#include <boost/python/numeric.hpp>

using namespace Mantid::PythonInterface;
using PropertyMarshal::SingleValueTypeHandler;
Expand All @@ -25,6 +26,7 @@ BOOST_PYTHON_MODULE(_kernel)
boost::python::docstring_options docstrings(true, true, false);
// Import numpy
_import_array();
boost::python::numeric::array::set_module_and_type("numpy", "ndarray");

def("version_str", &Mantid::Kernel::MantidVersion::version,
"Returns the Mantid version string in the form \"major.minor.patch\"");
Expand Down

0 comments on commit 4e02a89

Please sign in to comment.