Skip to content

Commit

Permalink
Allow python functions to override catgeories.
Browse files Browse the repository at this point in the history
Also adds simple Python Jacobian test.
Refs #970
  • Loading branch information
martyngigg committed Apr 18, 2013
1 parent f4ca9d7 commit 9a5e869
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ namespace Mantid

/// Returns the name of the function
std::string name() const;
/// Specify a category for the function
const std::string category() const;
/// Declare all attributes & parameters
void init();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ void export_IFunction()
class_<IFunction, IFunctionAdapter, boost::noncopyable>("IFunction", "Base class for all functions", no_init)
.def("name", &IFunction::name, "Return the name of the function")

.def("category", &IFunctionAdapter::category, "Return a semi-colon(;) separated string for the categories this class should belong to. For sub-categories use a \\ separator")

.def("initialize", &IFunction::initialize, "Declares any parameters and attributes on the function")

.def("getCategories", &getCategories, "Returns a list of the categories for an algorithm")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ namespace Mantid
return m_name;
}

/**
* Specify a category for the function
*/
const std::string IFunctionAdapter::category() const
{
return CallMethod0<std::string>::dispatchWithDefaultReturn(getSelf(),"category", IFunction::category());
}

/**
*/
void IFunctionAdapter::init()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set ( TEST_PY_FILES
IFunction1DTest.py
IPeaksWorkspaceTest.py
ITableWorkspaceTest.py
JacobianTest.py
MatrixWorkspaceTest.py
MDHistoWorkspaceTest.py
MultipleExperimentInfos.py
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
from mantid.api import IFunction1D, IFunction, FunctionFactory
import numpy as np

class NoCatgeoryFunction(IFunction1D):

def init(self):
pass

class Times2(IFunction1D):

def category(self):
return "SimpleFunction"

def init(self):
self.declareAttribute("IntAtt", 1)
self.declareAttribute("DoubleAtt", 3.4)
Expand All @@ -30,6 +38,18 @@ def test_instance_can_be_created_from_factory(self):
self.assertTrue(isinstance(func, IFunction1D))
FunctionFactory.unsubscribe(func_name)

def test_category_with_no_override_returns_default_category(self):
FunctionFactory.subscribe(NoCatgeoryFunction)
func = FunctionFactory.createFunction("NoCatgeoryFunction")
self.assertEquals("General", func.category())
FunctionFactory.unsubscribe("NoCatgeoryFunction")

def test_category_override_returns_overridden_result(self):
FunctionFactory.subscribe(Times2)
func = FunctionFactory.createFunction("Times2")
self.assertEquals("SimpleFunction", func.category())
FunctionFactory.unsubscribe("Times2")

def test_declareAttribute_only_accepts_known_types(self):
func = Times2()
func.initialize() # Contains known types
Expand Down

0 comments on commit 9a5e869

Please sign in to comment.