Skip to content

Commit

Permalink
Refs #4333. Export Axis, WorkspaceGroup and Unit to Python + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Dec 16, 2011
1 parent 7d8b417 commit 650af66
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 0 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 @@ -89,6 +89,7 @@ set ( TEST_PY_FILES
test/python/V3DTest.py
test/python/UnitCellTest.py
test/python/WorkspaceTest.py
test/python/WorkspaceGroupTest.py
)

set ( PYTEST_HELPERS test/python/testhelpers.py )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ set ( EXPORT_FILES
src/MatrixWorkspace.cpp
src/IEventWorkspace.cpp
src/BinaryOperations.cpp
src/WorkspaceGroup.cpp
src/Axis.cpp
)

# Files containing additional helper code that are not related to exporting class/functions
Expand Down
60 changes: 60 additions & 0 deletions Code/Mantid/Framework/PythonInterface/mantid/api/src/Axis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "MantidAPI/Axis.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/SpectraAxis.h"
#include "MantidAPI/TextAxis.h"
#include <boost/python/class.hpp>
#include <boost/python/register_ptr_to_python.hpp>
#include <boost/python/copy_const_reference.hpp>

using Mantid::API::Axis;
using Mantid::API::NumericAxis;
using Mantid::API::TextAxis;
using Mantid::API::SpectraAxis;
using Mantid::Kernel::Unit_sptr;
using Mantid::specid_t;
using namespace boost::python;

void export_Axis()
{
register_ptr_to_python<Axis*>();

// Class
class_< Axis, boost::noncopyable >("MantidAxis", no_init)
.def("length", &Axis::length, "Returns the length of the axis")
.def("title", (std::string & (Axis::*)() ) &Axis::title, return_internal_reference<>(),
"Get/set the axis title. Can be used as ax.title = ")
.def("isSpectra", &Axis::isSpectra, "Returns true if this is a SpectraAxis")
.def("isNumeric", &Axis::isNumeric, "Returns true if this is a NumericAxis")
.def("isText", &Axis::isText, "Returns true if this is a TextAxis")
.def("label", &Axis::label, "Return the axis label")
.def("getUnit", (const Unit_sptr & (Axis::*)() const) &Axis::unit, return_value_policy<copy_const_reference>(),
"Returns the unit object for the axis")
.def("setUnit", & Axis::setUnit, "Set the unit for this axis")
;
}

void export_NumericAxis()
{
class_< NumericAxis, bases<Axis>, boost::noncopyable >("NumericAxis", no_init)
.def("setValue", &NumericAxis::setValue, "Set a value at the given index")
;
}

void export_SpectraAxis()
{
class_< SpectraAxis, bases<Axis>, boost::noncopyable >("SpectraAxis", no_init)
.def("spectraNo", (const specid_t &(SpectraAxis::*)(const size_t &) const)&SpectraAxis::spectraNo,
return_value_policy<copy_const_reference>(), "Returns the spectrum no at the given index")
.def("setValue", & SpectraAxis::setValue, "Set a value at the given index")
.def("populateOneToOne", & SpectraAxis::populateOneToOne, "Populate the list with 1:1 values")
;
}

void export_TextAxis()
{
class_< TextAxis, bases<Axis>, boost::noncopyable >("TextAxis", no_init)
.def("setLabel", & TextAxis::setLabel, "Set the label at the given entry")
.def("label", & TextAxis::label, "Return the label at the given position")
;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "MantidAPI/WorkspaceGroup.h"
#include <boost/python/class.hpp>
#include <boost/python/register_ptr_to_python.hpp>

using Mantid::API::WorkspaceGroup;
using Mantid::API::WorkspaceGroup_sptr;
using Mantid::API::Workspace;
using namespace boost::python;

void export_WorkspaceGroup()
{
register_ptr_to_python<WorkspaceGroup_sptr>();

class_< WorkspaceGroup, bases<Workspace>, boost::noncopyable >("WorkspaceGroup", no_init)
.def("getNumberOfEntries", &WorkspaceGroup::getNumberOfEntries, "Returns the number of entries in the group")
.def("getNames", &WorkspaceGroup::getNames, "Returns the names of the entries in the group")
.def("contains", &WorkspaceGroup::contains, "Returns true if the given name is in the group")
.def("add", &WorkspaceGroup::add, "Add a name to the group")
.def("remove", &WorkspaceGroup::remove, "Remove a name from the group")
// ------------ Operators --------------------------------
.def("__len__", &WorkspaceGroup::getNumberOfEntries)
.def("__contains__", &WorkspaceGroup::contains)
;
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ set ( EXPORT_FILES
src/V3D.cpp
src/StlContainers.cpp
src/Logger.cpp
src/Unit.cpp
)

set ( SRC_FILES
Expand Down
20 changes: 20 additions & 0 deletions Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Unit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "MantidKernel/Unit.h"
#include <boost/python/class.hpp>
#include <boost/python/register_ptr_to_python.hpp>

using Mantid::Kernel::Unit;
using Mantid::Kernel::Unit_sptr;
using namespace boost::python;

void export_Unit()
{
register_ptr_to_python<Unit_sptr>();

class_<Unit,boost::noncopyable>("Unit", no_init)
.def("caption", &Unit::caption, "Return the full name of the unit")
.def("label", &Unit::label, "Returns a label to be printed on the axis")
.def("unitID", &Unit::unitID, "Returns the string ID of the unit. This may/may not match its name")
;

}

Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,29 @@ def test_workspace_data_information(self):
self.assertEquals(self._test_ws.blocksize(), 102)
self.assertEquals(self._test_ws.YUnit(), "Counts")
self.assertEquals(self._test_ws.YUnitLabel(), "Counts")

def test_axes(self):
# Workspace axes
self.assertEquals(self._test_ws.axes(), 2)
xaxis = self._test_ws.getAxis(0)
yaxis = self._test_ws.getAxis(1)

self.assertTrue(xaxis.isNumeric())
self.assertTrue(yaxis.isSpectra())

self.assertEquals(xaxis.length(), 103)
self.assertEquals(yaxis.length(), 2)

xunit = xaxis.getUnit()
self.assertEquals(xunit.caption(), "Time-of-flight")
self.assertEquals(xunit.label(), "microsecond")
self.assertEquals(xunit.unitID(), "TOF")

yunit = yaxis.getUnit()
self.assertEquals(yunit.caption(), "")
self.assertEquals(yunit.label(), "")
self.assertEquals(yunit.unitID(), "Empty")


def test_detector_retrieval(self):
det = self._test_ws.getDetector(0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import unittest
from testhelpers import run_algorithm
from mantid import mtd

class WorkspaceGroupTest(unittest.TestCase):

def test_group_interface(self):
run_algorithm('CreateWorkspace', OutputWorkspace='First',DataX=[1.,2.,3.], DataY=[2.,3.], DataE=[2.,3.],UnitX='TOF')
run_algorithm('CreateWorkspace', OutputWorkspace='Second',DataX=[1.,2.,3.], DataY=[2.,3.], DataE=[2.,3.],UnitX='TOF')
run_algorithm('GroupWorkspaces',InputWorkspaces='First,Second',OutputWorkspace='grouped')
grouped = mtd['grouped']

self.assertEquals(2, grouped.getNumberOfEntries())
# Matches operator
self.assertEquals(len(grouped), grouped.getNumberOfEntries())
# Matches length of name list
names = grouped.getNames()
self.assertEquals(len(grouped), len(names))
expected = ['First', 'Second']
for i in range(len(names)):
self.assertEquals(expected[i], names[i])

# Clearing the data should leave the handle unusable
mtd.clear()
try:
grouped.getNames()
self.fail("WorkspaceGroup handle is still usable after ADS has been cleared, it should be a weak reference and raise an error.")
except RuntimeError, exc:
self.assertEquals(str(exc), 'Variable invalidated, data has been deleted.')

0 comments on commit 650af66

Please sign in to comment.