Skip to content

Commit

Permalink
Export UnitConversion class to new python API. Refs #5624
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Jul 17, 2012
1 parent 078cb1f commit e7dabb9
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Code/Mantid/Framework/PythonInterface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ set ( TEST_PY_FILES
test/python/CompositeValidatorTest.py
test/python/ConfigServiceTest.py
test/python/CreateWorkspaceTest.py
test/python/DeltaEModeTest.py
test/python/ExperimentInfoTest.py
test/python/FacilityInfoTest.py
test/python/FilePropertyTest.py
Expand Down Expand Up @@ -123,6 +124,7 @@ set ( TEST_PY_FILES
test/python/QuatTest.py
test/python/V3DTest.py
test/python/UnitCellTest.py
test/python/UnitConversionTest.py
test/python/UnitFactoryTest.py
test/python/WorkspaceFactoryTest.py
test/python/WorkspaceTest.py
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ set ( EXPORT_FILES
src/Exports/MandatoryValidator.cpp
src/Exports/CompositeValidator.cpp
src/Exports/LogFilter.cpp
src/Exports/UnitConversion.cpp
src/Exports/UnitFactory.cpp
src/Exports/DeltaEMode.cpp
)

set ( SRC_FILES
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "MantidKernel/DeltaEMode.h"
#include "MantidPythonInterface/kernel/Policies/VectorToNumpy.h"

#include <boost/python/class.hpp>
#include <boost/python/enum.hpp>
#include <boost/python/return_value_policy.hpp>

using Mantid::Kernel::DeltaEMode;
namespace Policies = Mantid::PythonInterface::Policies;
using namespace boost::python;

void export_DeltaEMode()
{
enum_<Mantid::Kernel::DeltaEMode::Type>("DeltaEModeType")
.value("Elastic", DeltaEMode::Elastic)
.value("Direct", DeltaEMode::Direct)
.value("Indirect", DeltaEMode::Indirect)
.export_values()
;

class_<DeltaEMode, boost::noncopyable>("DeltaEMode", no_init)
.def("asString", &DeltaEMode::asString, "Returns the given type translated to a string")
.def("fromString", &DeltaEMode::fromString, "Returns the enumerated type translated from a string")
.def("availableTypes", &DeltaEMode::availableTypes, return_value_policy<Policies::VectorToNumpy>(),
"Returns a list of known delta E Modes as strings")
.staticmethod("availableTypes")
;

}

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

using Mantid::Kernel::UnitConversion;
using Mantid::Kernel::DeltaEMode;
using namespace boost::python;

void export_UnitConversion()
{
// Function pointer typedef
typedef double (*StringVersion)(const std::string & src, const std::string & dest,
const double srcValue,
const double l1, const double l2,
const double twoTheta, const DeltaEMode::Type emode, const double efixed);

class_<UnitConversion, boost::noncopyable>("UnitConversion", no_init)
.def("run", (StringVersion)&UnitConversion::run,
(arg("src"), arg("dest"), arg("srcValue"), arg("l1"), arg("l2"), arg("twoTheta"),
arg("emode"), arg("efixed")),
"Performs a unit conversion on a single value.")
.staticmethod("run")
;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import unittest
from mantid.kernel import DeltaEMode, DeltaEModeType

class DeltaEModeTest(unittest.TestCase):

def test_availableTypes_contains_three_modes(self):

modes = DeltaEMode.availableTypes()

self.assertEquals(3, len(modes))
self.assertTrue("Elastic" in modes)
self.assertTrue("Direct" in modes)
self.assertTrue("Indirect" in modes)

def test_DeltaEModeType_has_three_attrs_corresponding_to_three_modes(self):

self.assertTrue(hasattr(DeltaEModeType, "Elastic"))
self.assertTrue(hasattr(DeltaEModeType, "Direct"))
self.assertTrue(hasattr(DeltaEModeType, "Indirect"))

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import unittest
from mantid.kernel import UnitConversion, DeltaEModeType
import math

class UnitConversionTest(unittest.TestCase):

def test_run_accepts_string_units(self):
src_unit = "Wavelength"
src_value = 1.5
dest_unit = "Momentum"

l1 = l2 = twoTheta = efixed = 0.0
emode = DeltaEModeType.Indirect;
expected = 2.0*math.pi/src_value

result = UnitConversion.run(src_unit, dest_unit, src_value, l1, l2, twoTheta, emode, efixed)
self.assertAlmostEqual(result, expected, 12)

if __name__ == '__main__':
unittest.main()

0 comments on commit e7dabb9

Please sign in to comment.