18#include <boost/python/class.hpp>
19#include <boost/python/def.hpp>
20#include <boost/python/manage_new_object.hpp>
21#include <boost/python/overloads.hpp>
22#include <boost/python/register_ptr_to_python.hpp>
23#include <boost/python/to_python_value.hpp>
42PyObject *getCategories(
const IFunction &self) {
45 PyObject *registered = PyList_New(0);
46 for (
const auto &category : categories) {
47 PyObject *
value = to_python_value<const std::string &>()(category);
48 if (PyList_Append(registered,
value))
49 throw std::runtime_error(
"Failed to insert value into PyList");
57using setParameterType1 = void (
IFunction::*)(size_t,
const double &, bool);
65using setParameterType2 =
void (
IFunction::*)(const
std::
string &, const
double &,
bool);
69using setErrorType1 =
void (
IFunction::*)(
size_t,
double);
72using setErrorType2 =
void (
IFunction::*)(const
std::
string &,
double);
76using getErrorType1 =
double (
IFunction::*)(
size_t) const;
79using getErrorType2 =
double (
IFunction::*)(const
std::
string &) const;
84 const
boost::python::
object &defaultValue);
88 const
boost::python::
object &defaultValue,
89 const
boost::python::
object &validator);
98using removeTieByName =
void (
IFunction::*)(const
std::
string &);
106 self.functionDeriv(domain, *out);
110void setMatrixWorkspace(
IFunction &self,
const boost::python::object &
workspace,
int wi,
float startX,
float endX) {
120 register_ptr_to_python<std::shared_ptr<IFunction>>();
122 class_<IFunction, IFunctionAdapter, boost::noncopyable>(
"IFunction",
"Base class for all functions", no_init)
123 .def(
"name", &
IFunction::name, arg(
"self"),
"Return the name of the function")
126 "Return a semi-colon(;) separated string for the categories this "
127 "class should belong to. For sub-categories use a \\ separator")
131 .def(
"initialize", &
IFunction::initialize, arg(
"self"),
"Declares any parameters and attributes on the function")
133 .def(
"getCategories", &getCategories, arg(
"self"),
"Returns a list of the categories for an algorithm")
136 "Return the number of attributes (non-fitting arguments)")
141 "Return whether there is an attribute of the given name")
144 "Return whether there is an parameter of the given name")
151 "Return a description of the ith parameter")
154 "Return whether the ith parameter needs to be explicitely set")
157 (arg(
"self"), arg(
"i")),
"Get the value of the ith parameter")
160 (arg(
"self"), arg(
"name")),
"Get the value of the named parameter")
163 (arg(
"self"), arg(
"name")),
"Get the value of the named parameter")
166 setParameterType1_Overloads((arg(
"self"), arg(
"i"), arg(
"value"), arg(
"explicitlySet")),
167 "Sets the value of the ith parameter"))
170 setParameterType2_Overloads((arg(
"self"), arg(
"name"), arg(
"value"), arg(
"explicitlySet")),
171 "Sets the value of the named parameter"))
175 setErrorType1_Overloads((args(
"self"), arg(
"index"), args(
"err")),
"Sets the error on the indexed parameter"))
178 setErrorType2_Overloads((args(
"self"), arg(
"name"), args(
"err")),
"Sets the error on the named parameter"))
181 getErrorType1_Overloads((arg(
"self"), arg(
"index")),
"Return fitting error of the index parameter"))
184 getErrorType2_Overloads((arg(
"self"), arg(
"name")),
"Return fitting error of the named parameter"))
187 setParameterType2_Overloads((arg(
"self"), arg(
"name"), arg(
"value"), arg(
"explicitlySet")),
188 "Sets the value of the named parameter"))
191 declareAttributeType1_Overloads((arg(
"self"), arg(
"name"), arg(
"default_value")),
192 "Declare an attribute with an initial value"))
195 declareAttributeType2_Overloads((arg(
"self"), arg(
"name"), arg(
"default_value"), arg(
"validator")),
196 "Declare an attribute with an initial value, with a validator"))
198 .def(
"getAttributeValue",
200 (arg(
"self"), arg(
"name")),
"Return the value of the named attribute")
203 "Set a value of a named attribute")
206 (arg(
"self"), arg(
"name"), arg(
"init_value"), arg(
"description")),
207 "Declare a fitting parameter settings its default value & "
211 (arg(
"self"), arg(
"name"), arg(
"init_value")),
"Declare a fitting parameter settings its default value")
214 "Declare a fitting parameter settings its default value to 0.0")
217 fix_Overloads((arg(
"self"), arg(
"i"), arg(
"isDefault")),
"Fix the ith parameter"))
220 fixParameter_Overloads((arg(
"self"), arg(
"name"), arg(
"isDefault")),
"Fix the named parameter"))
222 .def(
"freeParameter", &
IFunction::unfix, (arg(
"self"), arg(
"i")),
"Free the ith parameter")
226 .def(
"isFixed", &
IFunction::isFixed, (arg(
"self"), arg(
"i")),
"Return whether the ith parameter is fixed or tied")
228 .def(
"fixAll", &
IFunction::fixAll, fixAll_Overloads((arg(
"self"), arg(
"isDefault")),
"Fix all parameters"))
233 tie_Overloads((arg(
"self"), arg(
"name"), arg(
"expr"), arg(
"isDefault")),
234 "Tie a named parameter to an expression"))
237 addTies_Overloads((arg(
"self"), arg(
"ties"), arg(
"isDefault")),
"Add several ties to an IFunction."))
240 "Remove the tie of the ith parameter")
243 "Remove the tie of the named parameter")
245 .def(
"getTies", &
IFunction::writeTies, arg(
"self"),
"Returns the list of current ties as a string")
248 addConstraints_Overloads((arg(
"self"), arg(
"constraints"), arg(
"isDefault")),
"Constrain named parameters"))
251 "Remove the constraint on the named parameter")
254 "Returns the list of current constraints as a string")
257 (arg(
"self"), arg(
"name"), arg(
"value")),
"Set the constraint penalty factor for named parameter")
260 "Get number of domains of a multi-domain function")
263 "Split this function (if needed) into a list of "
264 "independent functions")
267 "Returns the pointer to i-th child function")
271 .def(
"functionDeriv", &getFunctionDeriv, (arg(
"self"), arg(
"domain")), return_value_policy<manage_new_object>(),
272 "Calculate the values of the function for the given domain and returns them")
274 .def(
"setMatrixWorkspace", &setMatrixWorkspace,
275 (arg(
"self"), arg(
"workspace"), arg(
"wi"), arg(
"startX"), arg(
"endX")),
276 "Set matrix workspace to parse Parameters.xml")
279 .def(
"categories", &getCategories, arg(
"self"),
"Returns a list of the categories for an algorithm")
280 .def(
"numParams", &
IFunction::nParams, arg(
"self"),
"Return the number of parameters")
283 "Return a description of the ith parameter")
285 "Return whether the ith parameter needs to be explicitely set")
287 (arg(
"self"), arg(
"i")),
"Get the value of the ith parameter")
289 "Returns the index of the provided parameter.")
292 .def(
"__repr__", &
IFunction::asString, arg(
"self"),
"Return a string representation of the function");
294 TypeRegistry::subscribe<TypedPropertyValueHandler<IFunction_sptr>>();
double value
The value of the point.
#define GET_POINTER_SPECIALIZATION(TYPE)
IPeaksWorkspace_sptr workspace
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(valueAsPrettyStrOverloader, valueAsPrettyStr, 0, 2) void export_Property()
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
Base class that represents the domain of a function.
This is an interface to a fitting function - a semi-abstarct class.
virtual size_t nParams() const =0
Total number of parameters.
virtual void removeConstraint(const std::string &parName)
Remove a constraint.
std::string writeTies() const
Write a parameter tie to a string.
void unfixParameter(const std::string &name)
Free a parameter.
virtual double getParameter(size_t i) const =0
Get i-th parameter.
virtual void initialize()
Iinialize the function.
virtual std::shared_ptr< IFunction > clone() const
Virtual copy constructor.
virtual void tie(const std::string &parName, const std::string &expr, bool isDefault=false)
Tie a parameter to other parameters (or a constant)
virtual bool hasParameter(const std::string &name) const =0
Check if function has a parameter with this name.
virtual void setMatrixWorkspace(std::shared_ptr< const API::MatrixWorkspace > workspace, size_t wi, double startX, double endX)
Set matrix workspace.
virtual void setParameter(size_t, const double &value, bool explicitlySet=true)=0
Set i-th parameter.
virtual void setError(size_t i, double err)=0
Set the fitting error for a parameter.
virtual const std::vector< std::string > categories() const
Function to return all of the categories that contain this algorithm.
virtual std::string parameterDescription(size_t i) const =0
Returns the description of parameter i.
void fixParameter(const std::string &name, bool isDefault=false)
Fix a parameter.
virtual std::vector< std::string > getAttributeNames() const
Returns a list of attribute names.
virtual void setConstraintPenaltyFactor(const std::string &parName, const double &c)
Set a constraint penalty.
virtual void addTies(const std::string &ties, bool isDefault=false)
Add several ties.
std::string asString() const
Writes itself into a string.
void unfix(size_t i)
Restores a declared parameter i to the active status.
virtual std::string parameterName(size_t i) const =0
Returns the name of parameter i.
virtual std::string name() const =0
Returns the function's name.
virtual bool hasAttribute(const std::string &name) const
Check if attribute attName exists.
void unfixAll()
Free all parameters.
virtual std::shared_ptr< IFunction > getFunction(size_t i) const
Returns the pointer to i-th child function.
virtual void addConstraints(const std::string &str, bool isDefault=false)
Add a list of conatraints from a string.
void fixAll(bool isDefault=false)
Fix all parameters.
virtual size_t parameterIndex(const std::string &name) const =0
Returns the index of parameter name.
std::string writeConstraints() const
Write a parameter constraint to a string.
virtual void removeTie(const std::string &parName)
Removes the tie off a parameter.
virtual double getError(size_t i) const =0
Get the fitting error for a parameter.
void fix(size_t i, bool isDefault=false)
Removes a parameter i from the list of active.
bool isFixed(size_t i) const
Check if a parameter i is fixed.
virtual size_t nAttributes() const
Returns the number of attributes associated with the function.
virtual bool isExplicitlySet(size_t i) const =0
Checks if a parameter has been set explicitly.
virtual size_t getNumberDomains() const
Get number of domains required by this function.
Represents the Jacobian in IFitFunction::functionDeriv.
Provides a layer to hook into the protected functions of IFunction.
void declareFitParameter(const std::string &name, double initValue, const std::string &description)
Declare a named parameter with initial value & description.
static void setAttributePythonValue(IFunction &self, const std::string &name, const boost::python::object &value)
Set the attribute's value.
static boost::python::list createPythonEquivalentFunctions(const IFunction &self)
Split this function (if needed) into a list of independent functions.
void declareFitParameterZeroInit(const std::string &name)
Declare a named parameter with initial value = 0.0.
const std::string category() const override
Specify a category for the function.
void declareFitParameterNoDescr(const std::string &name, double initValue)
Declare a named parameter with initial value.
void declareAttribute(const std::string &name, const boost::python::object &defaultValue)
Declare an attribute with an initial value.
static PyObject * getAttributeValue(const IFunction &self, const std::string &name)
Get a named attribute value.
std::shared_ptr< IFunction > IFunction_sptr
shared pointer to the function base class
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
Helper class which provides the Collimation Length for SANS instruments.