Skip to content

Commit

Permalink
Export FunctionProperty to Python. Refs #6741
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Mar 20, 2013
1 parent 1d6bbf6 commit f57b88b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ set ( EXPORT_FILES
src/Exports/PropertyManagerDataService.cpp
src/Exports/FunctionFactory.cpp
src/Exports/Progress.cpp
src/Exports/FunctionProperty.cpp
)

# Files containing additional helper code that are not related to exporting class/functions
Expand Down
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"))
;
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set ( TEST_PY_FILES
FileFinderTest.py
FrameworkManagerTest.py
FunctionFactoryTest.py
FunctionPropertyTest.py
IEventWorkspaceTest.py
IPeaksWorkspaceTest.py
ITableWorkspaceTest.py
Expand Down
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()

0 comments on commit f57b88b

Please sign in to comment.