-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #4333. Export Axis, WorkspaceGroup and Unit to Python + tests
- Loading branch information
1 parent
7d8b417
commit 650af66
Showing
8 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
Code/Mantid/Framework/PythonInterface/mantid/api/src/Axis.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
; | ||
} | ||
|
25 changes: 25 additions & 0 deletions
25
Code/Mantid/Framework/PythonInterface/mantid/api/src/WorkspaceGroup.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ set ( EXPORT_FILES | |
src/V3D.cpp | ||
src/StlContainers.cpp | ||
src/Logger.cpp | ||
src/Unit.cpp | ||
) | ||
|
||
set ( SRC_FILES | ||
|
20 changes: 20 additions & 0 deletions
20
Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Unit.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
; | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
Code/Mantid/Framework/PythonInterface/test/python/WorkspaceGroupTest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.') |