Skip to content

Commit

Permalink
Refs #4315. More of the geometry exports.
Browse files Browse the repository at this point in the history
This also requires Numpy converts for Matrix objects
  • Loading branch information
martyngigg committed Dec 13, 2011
1 parent 81a3e36 commit 29a1af0
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 13 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 @@ -77,6 +77,7 @@ set ( TEST_PY_FILES
test/python/IComponentTest.py
test/python/IEventWorkspaceTest.py
test/python/ImportModuleTest.py
test/python/InstrumentTest.py
test/python/LoggerTest.py
test/python/MatrixWorkspaceTest.py
test/python/PythonPluginsTest.py
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,23 @@
#include "MantidKernel/System.h"
#include <boost/python/object.hpp> //Safer way to include Python.h
#include <vector>
#include "MantidKernel/Matrix.h"

namespace Mantid
{
namespace PythonInterface
{
namespace Numpy
{
/// Create a numpy array wrapper around existing data. This is only possible for contiguous data
/// 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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace Mantid
*
*/
PyArrayObject *cloneArray(MatrixWorkspace_sptr workspace,
DataField field, const size_t start, const size_t endp1)
DataField field, const size_t start, const size_t endp1)
{
const size_t numHist = endp1 - start;
npy_intp stride(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set ( EXPORT_FILES
src/Detector.cpp
src/DetectorGroup.cpp
src/Instrument.cpp
src/UnitCell.cpp
)

set ( SRC_FILES
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "MantidGeometry/Crystal/UnitCell.h"
#include <boost/python/class.hpp>
#include <boost/python/enum.hpp>
#include <boost/python/scope.hpp>

using Mantid::Geometry::UnitCell;
using Mantid::Geometry::AngleUnits;
using Mantid::Geometry::angRadians;
using Mantid::Geometry::angDegrees;
using namespace boost::python;

void export_UnitCell()
{
enum_<AngleUnits>("AngleUnits")
.value("Degrees", angDegrees)
.value("Radians", angRadians)
.export_values();

class_< UnitCell >( "UnitCell", init< >() )
.def( init<UnitCell const &>(arg("other") ) )
.def( init< double, double, double >(( arg("_a"), arg("_b"), arg("_c") )) )
.def( init< double, double, double, double, double, double, optional< int > >(( arg("_a"), arg("_b"), arg("_c"), arg("_alpha"), arg("_beta"), arg("_gamma"), arg("Unit")=(int)(angDegrees) )) )
.def( "a", (double ( UnitCell::* )() const) &UnitCell::a )
.def( "a1", (double ( UnitCell::* )() const) &UnitCell::a1 )
.def( "a2", (double ( UnitCell::* )() const) &UnitCell::a2 )
.def( "a3", (double ( UnitCell::* )() const) &UnitCell::a3 )
.def( "alpha", (double ( UnitCell::* )() const) &UnitCell::alpha )
.def( "alpha1", (double ( UnitCell::* )() const) &UnitCell::alpha1 )
.def( "alpha2", (double ( UnitCell::* )() const) &UnitCell::alpha2 )
.def( "alpha3", (double ( UnitCell::* )() const) &UnitCell::alpha3 )
.def( "alphastar", (double ( UnitCell::* )() const) &UnitCell::alphastar )
.def( "astar", (double ( UnitCell::* )() const) &UnitCell::astar )
.def( "b", (double ( UnitCell::* )() const) &UnitCell::b )
.def( "b1", (double ( UnitCell::* )() const) &UnitCell::b1 )
.def( "b2", (double ( UnitCell::* )() const) &UnitCell::b2 )
.def( "b3", (double ( UnitCell::* )() const) &UnitCell::b3 )
.def( "beta", (double ( UnitCell::* )() const) &UnitCell::beta )
.def( "beta1", (double ( UnitCell::* )() const) &UnitCell::beta1 )
.def( "beta2", (double ( UnitCell::* )() const) &UnitCell::beta2 )
.def( "beta3", (double ( UnitCell::* )() const) &UnitCell::beta3 )
.def( "betastar", (double ( UnitCell::* )() const) &UnitCell::betastar )
.def( "bstar", (double ( UnitCell::* )() const) &UnitCell::bstar )
.def( "c", (double ( UnitCell::* )() const) &UnitCell::c )
.def( "cstar", (double ( UnitCell::* )() const) &UnitCell::cstar )
.def( "d", (double ( UnitCell::* )( double,double,double ) const) &UnitCell::d, (arg("h"), arg("k"), arg("l") ))
.def( "dstar", (double ( UnitCell::* )( double,double,double ) const) &UnitCell::dstar , (arg("h"), arg("k"), arg("l") ))
.def( "gamma", (double ( UnitCell::* )() const) &UnitCell::gamma )
.def( "gammastar", (double ( UnitCell::* )() const) &UnitCell::gammastar )
.def( "recAngle", (double ( UnitCell::* )( double,double,double,double,double,double,int const ) const)&UnitCell::recAngle, ( arg("h1"), arg("k1"), arg("l1"), arg("h2"), arg("k2"), arg("l2"), arg("Unit")=(int)(angDegrees) ))
.def( "recVolume", (double ( UnitCell::* )() const) &UnitCell::recVolume )
.def( "set", (void ( UnitCell::* )( double,double,double,double,double,double,int const)) &UnitCell::set, ( arg("_a"), arg("_b"), arg("_c"), arg("_alpha"), arg("_beta"), arg("_gamma"), arg("Unit")=(int)(angDegrees)))
.def( "seta", (void ( UnitCell::* )( double ) )( &UnitCell::seta ), ( arg("_a") ) )
.def( "setalpha", (void ( UnitCell::* )( double,int const ) )( &UnitCell::setalpha ), ( arg("_alpha"), arg("Unit")=(int)(angDegrees) ) )
.def( "setb", (void ( UnitCell::* )( double ) )( &UnitCell::setb ), ( arg("_b") ) )
.def( "setbeta", (void ( UnitCell::* )( double,int const ) )( &UnitCell::setbeta ), ( arg("_beta"), arg("Unit")=(int)(angDegrees) ) )
.def( "setc", (void ( UnitCell::* )( double ) )( &UnitCell::setc ), ( arg("_c") ) )
.def( "setgamma", (void ( UnitCell::* )( double,int const ) )( &UnitCell::setgamma ), ( arg("_gamma"), arg("Unit")=(int)(angDegrees) ) )
.def( "volume", (double ( UnitCell::* )() const) &UnitCell::volume)
// .def( "getG", &UnitCellWrapper::getG)
// .def( "getGstar", &UnitCellWrapper::getGstar)
// .def( "getB", &UnitCellWrapper::getB )
// .def( "recalculateFromGstar", &UnitCellWrapper::recalculateFromGStar) ;
;

scope().attr("deg2rad") = Mantid::Geometry::deg2rad;
scope().attr("rad2deg") = Mantid::Geometry::rad2deg;
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace Mantid
{
namespace Numpy
{
/**

/**
* Create a numpy array wrapper around existing data. This is only possible for contiguous data
* @param data :: A const reference to an existing contiguous array
* @return A numpy wrapper around the existing data, no copy is performed. It is left in a read-write state.
Expand All @@ -21,17 +22,44 @@ namespace Mantid
npy_intp dims[1] = { data.size() };
return PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE,(void*)&(data[0]));
}

/**
* Create a read-only numpy array wrapper around existing data. This is only possible for contiguous data
* @param data :: A const reference to an existing contiguous array
* @return A numpy wrapper around the existing data, no copy is performed. It is marked read-only.
*/
PyObject *wrapWithReadOnlyNumpy(const std::vector<double> & data)
{
PyArrayObject *nparray = (PyArrayObject*)wrapWithNumpy(data);
nparray->flags &= ~NPY_WRITEABLE;
return (PyObject*)nparray;
}
* Create a read-only numpy array wrapper around existing data. This is only possible for contiguous data
* @param data :: A const reference to an existing contiguous array
* @return A numpy wrapper around the existing data, no copy is performed. It is marked read-only.
*/
PyObject *wrapWithReadOnlyNumpy(const std::vector<double> & data)
{
PyArrayObject *nparray = (PyArrayObject*)wrapWithNumpy(data);
nparray->flags &= ~NPY_WRITEABLE;
return (PyObject*)nparray;
}

/**
* Create a numpy array wrapper around a Matrix.
* @param data :: A const reference to an existing Matrix
* @return A numpy wrapper around the existing data, no copy is performed. It is left in a read-write state.
*/
PyObject *wrapWithNumpy(const Kernel::DblMatrix & data)
{
std::pair<size_t,size_t> matrixSize = data.size();
npy_intp dims[2] = {matrixSize.first, matrixSize.second};
PyArrayObject * ndarray =
(PyArrayObject*)PyArray_SimpleNewFromData(2, dims, NPY_DOUBLE,(void*)&(data[0][0]));
return (PyObject*)ndarray;
}

/**
* Create a read-only numpy array wrapper around a Matrix
* @param data :: A const reference to an existing Matrix
* @return A numpy wrapper around the existing data, no copy is performed. It is marked read-only.
*/
PyObject *wrapWithReadOnlyNumpy(const Kernel::DblMatrix& data)
{
PyArrayObject *nparray = (PyArrayObject*)wrapWithNumpy(data);
nparray->flags &= ~NPY_WRITEABLE;
return (PyObject*)nparray;
}

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ class IComponentTest(unittest.TestCase):

def test_IComponent_cannot_be_instantiated(self):
self.assertFalse(can_be_instantiated(IComponent))

def test_IComponent_has_expected_attributes(self):
attrs = dir(IComponent)
expected_attrs = ["get_pos", "get_distance", "get_name", "type"]
for att in expected_attrs:
self.assertTrue(att in attrs)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import unittest
from mantid import Instrument
from testhelpers import can_be_instantiated

class InstrumentTest(object):

def test_Instrument_cannot_be_instantiated(self):
self.assertFalse(can_be_instantiated(Instrument))

def test_Instrument_has_expected_attrs(self):
expected_attrs = ["getSample", "getSource",
"getComponentByName", "getDetector",
"nelements", "__getitem__",
"get_parameter_names", "has_parameter",
"get_number_parameter", "get_position_parameter",
"get_rotation_parameter", "get_string_parameter",
"get_pos", "get_distance", "get_name", "type"]
for att in expected_attrs:
self.assertTrue(att in attrs)

0 comments on commit 29a1af0

Please sign in to comment.