Skip to content

Commit

Permalink
Added parameter handling. Refs #970
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Apr 15, 2013
1 parent d97227e commit 0f63629
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace Mantid
* Provides a layer to hook into the protected functions
* of IFunction
*/
class IFunctionAdapter : public virtual API::IFunction
class IFunctionAdapter : virtual public API::IFunction
{
public:
/// A constructor that looks like a Python __init__ method
Expand All @@ -53,6 +53,40 @@ namespace Mantid
/// Get a named attribute value
PyObject * getAttributeValue(const std::string & name);

// Each overload of declareParamter requires a different name as we
// can't use a function pointer with a virtual base class

/**
* Declare a named parameter with initial value & description
* @param name :: The name of the parameter
* @initValue name :: The initial value
* @description name :: A short description of the parameter
*/
inline void declareFitParameter(const std::string& name, double initValue,
const std::string& description)
{
this->declareParameter(name,initValue,description);
}

/**
* Declare a named parameter with initial value
* @param name :: The name of the parameter
* @initValue name :: The initial value
*/
inline void declareFitParameterNoDescr(const std::string& name, double initValue)
{
this->declareFitParameter(name,initValue,"");
}

/**
* Declare a named parameter with initial value = 0.0
* @param name :: The name of the parameter
*/
inline void declareFitParameterZeroInit(const std::string& name)
{
this->declareFitParameter(name,0.0,"");
}

protected:
/**
* Returns the PyObject that owns this wrapper, i.e. self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <boost/python/class.hpp>
#include <boost/python/def.hpp>
#include <boost/python/overloads.hpp>

using Mantid::API::IFunction;
using Mantid::PythonInterface::IFunctionAdapter;
Expand Down Expand Up @@ -31,6 +32,16 @@ namespace

return registered;
}
// -- Declare property overloads --
// The usual BOOST_MEMBER_FUNCTION_OVERLOADS doesn't work using the wrapper held type

// declareProperty(name)
typedef void(IFunctionAdapter::*declareParameterType1)(const std::string &);
// declareProperty(name,defaultValue)
typedef void(IFunctionAdapter::*declareParameterType2)(const std::string &,double);
// declareProperty(name,defaultValue, description)
typedef void(IFunctionAdapter::*declareParameterType3)(const std::string &,double,const std::string &);

///@endcond
}

Expand Down Expand Up @@ -67,6 +78,14 @@ void export_IFunction()

.def("getAttributeValue", &IFunctionAdapter::getAttributeValue, "Return the value of the named attribute")

.def("declareParameter", &IFunctionAdapter::declareFitParameter,
"Declare a fitting parameter settings its default value & description")

.def("declareParameter", &IFunctionAdapter::declareFitParameterNoDescr,
"Declare a fitting parameter settings its default value")

.def("declareParameter", &IFunctionAdapter::declareFitParameterZeroInit,
"Declare a fitting parameter settings its default value to 0.0")

//-- Deprecated functions that have the wrong names --
.def("categories", &getCategories, "Returns a list of the categories for an algorithm")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def init(self):
self.declareAttribute("StringAtt", "filename")
self.declareAttribute("BoolAtt", True)

self.declareParameter("ParamZeroInitNoDescr")
self.declareParameter("ParamNoDescr", 1.5)
self.declareParameter("OtherParam",4,"Some fitting parameter")

def function1D(self, xvals, out):
pass

Expand All @@ -35,11 +39,28 @@ def test_correct_attribute_values_are_returned_when_asked(self):
func = PyLinear()
func.initialize() # Contains known types

# By name
self.assertEquals(1, func.getAttributeValue("IntAtt"))
self.assertEquals(3.4, func.getAttributeValue("DoubleAtt"))
self.assertEquals("filename", func.getAttributeValue("StringAtt"))
self.assertEquals(True, func.getAttributeValue("BoolAtt"))

def test_correct_parameters_are_attached_during_init(self):
func = PyLinear()
func.initialize()

self.assertEquals(3, func.nParams())

self.assertEquals("ParamZeroInitNoDescr",func.parameterName(0))
self.assertEquals("",func.paramDescription(0))
self.assertEquals(0.0,func.getParameterValue(0))

self.assertEquals("ParamNoDescr",func.parameterName(1))
self.assertEquals("",func.paramDescription(1))
self.assertEquals(1.5,func.getParameterValue(1))

self.assertEquals("OtherParam",func.parameterName(2))
self.assertEquals("Some fitting parameter",func.paramDescription(2))
self.assertEquals(4.0,func.getParameterValue(2))

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

0 comments on commit 0f63629

Please sign in to comment.