Skip to content

Commit

Permalink
Finish MDGeometry exports to Python. Refs #6027
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Aug 5, 2013
1 parent e26c886 commit f5f112d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#include "MantidAPI/MDGeometry.h"
#include "MantidPythonInterface/kernel/Policies/VectorToNumpy.h"
#include "MantidPythonInterface/kernel/Policies/downcast_returned_value.h"

#include <boost/python/class.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <boost/python/list.hpp>
#include <boost/python/return_value_policy.hpp>

using Mantid::API::MDGeometry;
using Mantid::Geometry::IMDDimension_const_sptr;
using Mantid::PythonInterface::Policies::VectorToNumpy;
using Mantid::PythonInterface::Policies::downcast_returned_value;
using namespace boost::python;

namespace
Expand Down Expand Up @@ -68,6 +71,26 @@ void export_MDGeometry()

.def("getTDimension", &MDGeometry::getTDimension,
"Returns the dimension description mapped to time")

.def("getGeometryXML", &MDGeometry::getGeometryXML,
"Returns an XML representation, as a string, of the geometry of the workspace")

.def("getBasisVector", (const Mantid::Kernel::VMD & (MDGeometry::*)(size_t) const)&MDGeometry::getBasisVector,
(args("index")), return_value_policy<copy_const_reference>(),
"Returns a VMD object defining the basis vector for the specified dimension")

.def("hasOriginalWorkspace", &MDGeometry::hasOriginalWorkspace, (args("index")),
"Returns True if there is a source workspace at the given index")

.def("numOriginalWorkspaces", &MDGeometry::numOriginalWorkspaces,
"Returns the number of source workspaces attached" )

.def("getOriginalWorkspace", &MDGeometry::getOriginalWorkspace, (args("index")), return_value_policy<downcast_returned_value>(),
"Returns the source workspace attached at the given index")

.def("getOrigin", (const Mantid::Kernel::VMD & (MDGeometry::*)() const)&MDGeometry::getOrigin,
return_value_policy<copy_const_reference>(),
"Returns the vector of the origin (in the original workspace) that corresponds to 0,0,0... in this workspace")
;
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mantid
from mantid.kernel import VMD
from mantid.geometry import IMDDimension
from mantid.api import MDGeometry
import numpy
Expand Down Expand Up @@ -63,6 +64,23 @@ def test_getTDimension_returns_correct_dimension_if_workspace_has_enough_dimensi
dimension = self._test_mdws.getTDimension()
self._check_is_dimension_with_id(dimension, "t")

def test_getGeometryXML_returns_non_empty_string(self):
xml = self._test_mdws.getGeometryXML()
self.assertTrue(len(xml) > 0)

def test_getBasisVector_returns_VMD(self):
basis = self._test_mdws.getBasisVector(0)
self.assertTrue(isinstance(basis, VMD))

def test_getOrigin_returns_VMD(self):
origin = self._test_mdws.getOrigin()
self.assertTrue(isinstance(origin, VMD))

def test_original_workspace_access(self):
self.assertFalse(self._test_mdws.hasOriginalWorkspace(0))
self.assertEqual(0,self._test_mdws.numOriginalWorkspaces())
self.assertRaises(RuntimeError,self._test_mdws.getOriginalWorkspace, 0)

#====================== Failure cases ==================================================

def test_getDimension_by_index_raises_RuntimeError_for_invalid_index(self):
Expand All @@ -77,6 +95,9 @@ def test_getDimensionIndexByName_raises_RuntimeError_for_invalid_name(self):
def test_getDimensionIndexById_raises_ValueError_for_invalid_name(self):
self.assertRaises(RuntimeError, self._test_mdws.getDimensionIndexById, id="NOTANID")

def test_getBasisVector_raises_ValueError_for_invalid_index(self):
self.assertRaises(ValueError, self._test_mdws.getBasisVector, index=self._test_ndims+1)

#========================================================================================

def _check_is_dimension_with_id(self, dimension, expected_id):
Expand Down

0 comments on commit f5f112d

Please sign in to comment.