-
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.
Export UnitConversion class to new python API. Refs #5624
- Loading branch information
1 parent
078cb1f
commit e7dabb9
Showing
6 changed files
with
101 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
30 changes: 30 additions & 0 deletions
30
Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Exports/DeltaEMode.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,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") | ||
; | ||
|
||
} | ||
|
25 changes: 25 additions & 0 deletions
25
Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Exports/UnitConversion.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 "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") | ||
; | ||
} | ||
|
22 changes: 22 additions & 0 deletions
22
Code/Mantid/Framework/PythonInterface/test/python/DeltaEModeTest.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,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() |
20 changes: 20 additions & 0 deletions
20
Code/Mantid/Framework/PythonInterface/test/python/UnitConversionTest.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,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() |