-
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 FunctionProperty to Python. Refs #6741
- Loading branch information
1 parent
1d6bbf6
commit f57b88b
Showing
4 changed files
with
66 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
21 changes: 21 additions & 0 deletions
21
Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/FunctionProperty.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,21 @@ | ||
#include "MantidAPI/FunctionProperty.h" | ||
#include "MantidPythonInterface/kernel/PropertyWithValue.h" | ||
#include <boost/python/class.hpp> | ||
|
||
using Mantid::API::FunctionProperty; | ||
using Mantid::API::IFunction; | ||
using Mantid::Kernel::PropertyWithValue; | ||
using namespace boost::python; | ||
|
||
void export_FunctionProperty() | ||
{ | ||
// FuncitonProperty has base PropertyWithValue<boost::shared_ptr<IFunction>> | ||
// which must be exported | ||
typedef boost::shared_ptr<IFunction> HeldType; | ||
EXPORT_PROP_W_VALUE(HeldType, IFunction); | ||
|
||
class_<FunctionProperty, bases<PropertyWithValue<HeldType>>, boost::noncopyable>("FunctionProperty", no_init) | ||
.def(init<const std::string &>(arg("name"), "Constructs a FunctionProperty with the given 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
43 changes: 43 additions & 0 deletions
43
Code/Mantid/Framework/PythonInterface/test/python/mantid/api/FunctionPropertyTest.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,43 @@ | ||
from mantid.api import FunctionProperty,PythonAlgorithm, IFunction | ||
from testhelpers import assertRaisesNothing | ||
import unittest | ||
import math | ||
|
||
class FunctionPropertyTest(unittest.TestCase): | ||
|
||
#------------------------------------------------------------ | ||
class TestFunctionPropAlg(PythonAlgorithm): | ||
def PyInit(self): | ||
self.declareProperty(FunctionProperty("fun")) | ||
def PyExec(self): | ||
fp = self.getProperty("fun") | ||
if not isinstance(fp, FunctionProperty): | ||
raise RuntimeError("Expected a FunctionProperty but found %s " % str(type(fp))) | ||
func = fp.value | ||
if not isinstance(func, IFunction): | ||
raise RuntimeError("Expected an IFunction but found %s " % str(type(func))) | ||
|
||
height=func.getParamValue(0) | ||
if math.fabs(height - 1.0) > 1e-12: | ||
raise RuntimeError("Height does not have the expected value") | ||
#------------------------------------------------------------ | ||
|
||
#---- Success cases ---- | ||
def test_constructor_succeeds_with_non_empty_string_name(self): | ||
assertRaisesNothing(self, FunctionProperty, "Function") | ||
|
||
def test_valid_string_value_gives_function_object_as_value(self): | ||
alg=self.TestFunctionPropAlg() | ||
alg.initialize() | ||
alg.setProperty("fun", "name=Gaussian,PeakCentre=5.0,Height=1.0") | ||
alg.setRethrows(True) | ||
assertRaisesNothing(self, alg.execute) | ||
|
||
#---- Error cases ---- | ||
def test_invalid_string_value_gives_function_object_as_value(self): | ||
alg=self.TestFunctionPropAlg() | ||
alg.initialize() | ||
self.assertRaises(ValueError, alg.setProperty, "fun", "blah") | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |