Mantid
Loading...
Searching...
No Matches
IFunction.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
17
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>
24
29using namespace boost::python;
30
32
33namespace {
35
36//------------------------------------------------------------------------------------------------------
42PyObject *getCategories(const IFunction &self) {
43 const auto categories = self.categories();
44
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");
50 }
51
52 return registered;
53}
54
55// -- Set property overloads --
56// setProperty(index,value,explicit)
57using setParameterType1 = void (IFunction::*)(size_t, const double &, bool);
58GNU_DIAG_OFF("unused-local-typedef")
59// Ignore -Wconversion warnings coming from boost::python
60// Seen with GCC 7.1.1 and Boost 1.63.0
61GNU_DIAG_OFF("conversion")
62
63BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(setParameterType1_Overloads, setParameter, 2, 3)
64// setProperty(name,value,explicit)
65using setParameterType2 = void (IFunction::*)(const std::string &, const double &, bool);
66BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(setParameterType2_Overloads, setParameter, 2, 3)
67
68// setError(index,value)
69using setErrorType1 = void (IFunction::*)(size_t, double);
70BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(setErrorType1_Overloads, setError, 2, 2)
71// setError(name,value)
72using setErrorType2 = void (IFunction::*)(const std::string &, double);
73BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(setErrorType2_Overloads, setError, 2, 2)
74
75// getError(index)
76using getErrorType1 = double (IFunction::*)(size_t) const;
77BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getErrorType1_Overloads, getError, 1, 1)
78// getError(name)
79using getErrorType2 = double (IFunction::*)(const std::string &) const;
80BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getErrorType2_Overloads, getError, 1, 1)
81
82// declareAttribute(name, defaultValue)
83using declareAttributeType1 = void (IFunctionAdapter::*)(const std::string &name,
84 const boost::python::object &defaultValue);
85BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(declareAttributeType1_Overloads, declareAttribute, 2, 2)
86// declareAttribute(name, defaultValue, validator)
87using declareAttributeType2 = void (IFunctionAdapter::*)(const std::string &name,
88 const boost::python::object &defaultValue,
89 const boost::python::object &validator);
90BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(declareAttributeType2_Overloads, declareAttribute, 3, 3)
91
92BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(tie_Overloads, tie, 2, 3)
93BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(addTies_Overloads, addTies, 1, 2)
94BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(addConstraints_Overloads, addConstraints, 1, 2)
95BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(fixParameter_Overloads, fixParameter, 1, 2)
96BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(fix_Overloads, fix, 1, 2)
97BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(fixAll_Overloads, fixAll, 0, 1)
98using removeTieByName = void (IFunction::*)(const std::string &);
99
100GNU_DIAG_ON("conversion")
101GNU_DIAG_ON("unused-local-typedef")
103
104Mantid::API::Jacobian *getFunctionDeriv(IFunction &self, const Mantid::API::FunctionDomain &domain) {
105 auto *out = new Mantid::CurveFitting::Jacobian(domain.size(), self.nParams());
106 self.functionDeriv(domain, *out);
107 return out;
108}
109
110void setMatrixWorkspace(IFunction &self, const boost::python::object &workspace, int wi, float startX, float endX) {
111 Mantid::API::MatrixWorkspace_sptr matWS = std::dynamic_pointer_cast<Mantid::API::MatrixWorkspace>(
113 self.setMatrixWorkspace(matWS, wi, startX, endX);
114}
115
116} // namespace
117
119
120 register_ptr_to_python<std::shared_ptr<IFunction>>();
121
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")
124
125 .def("category", &IFunctionAdapter::category, arg("self"),
126 "Return a semi-colon(;) separated string for the categories this "
127 "class should belong to. For sub-categories use a \\ separator")
128
129 .def("clone", &IFunction::clone, arg("self"), "Clones the function")
130
131 .def("initialize", &IFunction::initialize, arg("self"), "Declares any parameters and attributes on the function")
132
133 .def("getCategories", &getCategories, arg("self"), "Returns a list of the categories for an algorithm")
134
135 .def("nAttributes", &IFunction::nAttributes, arg("self"),
136 "Return the number of attributes (non-fitting arguments)")
137
138 .def("attributeNames", &IFunction::getAttributeNames, arg("self"), "The names of all the attributes")
139
140 .def("hasAttribute", &IFunction::hasAttribute, (arg("self"), arg("name")),
141 "Return whether there is an attribute of the given name")
142
143 .def("hasParameter", &IFunction::hasParameter, (arg("self"), arg("name")),
144 "Return whether there is an parameter of the given name")
145
146 .def("nParams", &IFunction::nParams, arg("self"), "Return the number of parameters")
147
148 .def("parameterName", &IFunction::parameterName, (arg("self"), arg("i")), "Return the name of the ith parameter")
149
150 .def("paramDescription", &IFunction::parameterDescription, (arg("self"), arg("i")),
151 "Return a description of the ith parameter")
152
153 .def("isExplicitlySet", &IFunction::isExplicitlySet, (arg("self"), arg("i")),
154 "Return whether the ith parameter needs to be explicitely set")
155
156 .def("getParameterValue", (double (IFunction::*)(size_t) const) & IFunction::getParameter,
157 (arg("self"), arg("i")), "Get the value of the ith parameter")
158
159 .def("getParameterValue", (double (IFunction::*)(const std::string &) const) & IFunction::getParameter,
160 (arg("self"), arg("name")), "Get the value of the named parameter")
161
162 .def("__getitem__", (double (IFunction::*)(const std::string &) const) & IFunction::getParameter,
163 (arg("self"), arg("name")), "Get the value of the named parameter")
164
165 .def("setParameter", (setParameterType1)&IFunction::setParameter,
166 setParameterType1_Overloads((arg("self"), arg("i"), arg("value"), arg("explicitlySet")),
167 "Sets the value of the ith parameter"))
168
169 .def("setParameter", (setParameterType2)&IFunction::setParameter,
170 setParameterType2_Overloads((arg("self"), arg("name"), arg("value"), arg("explicitlySet")),
171 "Sets the value of the named parameter"))
172
173 .def(
174 "setError", (setErrorType1)&IFunction::setError,
175 setErrorType1_Overloads((args("self"), arg("index"), args("err")), "Sets the error on the indexed parameter"))
176
177 .def("setError", (setErrorType2)&IFunction::setError,
178 setErrorType2_Overloads((args("self"), arg("name"), args("err")), "Sets the error on the named parameter"))
179
180 .def("getError", (getErrorType1)&IFunction::getError,
181 getErrorType1_Overloads((arg("self"), arg("index")), "Return fitting error of the index parameter"))
182
183 .def("getError", (getErrorType2)&IFunction::getError,
184 getErrorType2_Overloads((arg("self"), arg("name")), "Return fitting error of the named parameter"))
185
186 .def("__setitem__", (setParameterType2)&IFunction::setParameter,
187 setParameterType2_Overloads((arg("self"), arg("name"), arg("value"), arg("explicitlySet")),
188 "Sets the value of the named parameter"))
189
190 .def("declareAttribute", (declareAttributeType1)&IFunctionAdapter::declareAttribute,
191 declareAttributeType1_Overloads((arg("self"), arg("name"), arg("default_value")),
192 "Declare an attribute with an initial value"))
193
194 .def("declareAttribute", (declareAttributeType2)&IFunctionAdapter::declareAttribute,
195 declareAttributeType2_Overloads((arg("self"), arg("name"), arg("default_value"), arg("validator")),
196 "Declare an attribute with an initial value, with a validator"))
197
198 .def("getAttributeValue",
199 (PyObject * (*)(const IFunction &, const std::string &)) IFunctionAdapter::getAttributeValue,
200 (arg("self"), arg("name")), "Return the value of the named attribute")
201
202 .def("setAttributeValue", &IFunctionAdapter::setAttributePythonValue, (arg("self"), arg("name"), arg("value")),
203 "Set a value of a named attribute")
204
205 .def("declareParameter", &IFunctionAdapter::declareFitParameter,
206 (arg("self"), arg("name"), arg("init_value"), arg("description")),
207 "Declare a fitting parameter settings its default value & "
208 "description")
209
210 .def("declareParameter", &IFunctionAdapter::declareFitParameterNoDescr,
211 (arg("self"), arg("name"), arg("init_value")), "Declare a fitting parameter settings its default value")
212
213 .def("declareParameter", &IFunctionAdapter::declareFitParameterZeroInit, (arg("self"), arg("name")),
214 "Declare a fitting parameter settings its default value to 0.0")
215
216 .def("fixParameter", &IFunction::fix,
217 fix_Overloads((arg("self"), arg("i"), arg("isDefault")), "Fix the ith parameter"))
218
219 .def("fixParameter", &IFunction::fixParameter,
220 fixParameter_Overloads((arg("self"), arg("name"), arg("isDefault")), "Fix the named parameter"))
221
222 .def("freeParameter", &IFunction::unfix, (arg("self"), arg("i")), "Free the ith parameter")
223
224 .def("freeParameter", &IFunction::unfixParameter, (arg("self"), arg("name")), "Free the named parameter")
225
226 .def("isFixed", &IFunction::isFixed, (arg("self"), arg("i")), "Return whether the ith parameter is fixed or tied")
227
228 .def("fixAll", &IFunction::fixAll, fixAll_Overloads((arg("self"), arg("isDefault")), "Fix all parameters"))
229
230 .def("freeAll", &IFunction::unfixAll, (arg("self")), "Free all parameters")
231
232 .def("tie", &IFunction::tie,
233 tie_Overloads((arg("self"), arg("name"), arg("expr"), arg("isDefault")),
234 "Tie a named parameter to an expression"))
235
236 .def("addTies", &IFunction::addTies,
237 addTies_Overloads((arg("self"), arg("ties"), arg("isDefault")), "Add several ties to an IFunction."))
238
239 .def("removeTie", (bool (IFunction::*)(size_t)) & IFunction::removeTie, (arg("self"), arg("i")),
240 "Remove the tie of the ith parameter")
241
242 .def("removeTie", (void (IFunction::*)(const std::string &)) & IFunction::removeTie, (arg("self"), arg("name")),
243 "Remove the tie of the named parameter")
244
245 .def("getTies", &IFunction::writeTies, arg("self"), "Returns the list of current ties as a string")
246
247 .def("addConstraints", &IFunction::addConstraints,
248 addConstraints_Overloads((arg("self"), arg("constraints"), arg("isDefault")), "Constrain named parameters"))
249
250 .def("removeConstraint", &IFunction::removeConstraint, (arg("self"), arg("name")),
251 "Remove the constraint on the named parameter")
252
253 .def("getConstraints", &IFunction::writeConstraints, arg("self"),
254 "Returns the list of current constraints as a string")
255
256 .def("setConstraintPenaltyFactor", &IFunction::setConstraintPenaltyFactor,
257 (arg("self"), arg("name"), arg("value")), "Set the constraint penalty factor for named parameter")
258
259 .def("getNumberDomains", &IFunction::getNumberDomains, (arg("self")),
260 "Get number of domains of a multi-domain function")
261
262 .def("createEquivalentFunctions", &IFunctionAdapter::createPythonEquivalentFunctions, (arg("self")),
263 "Split this function (if needed) into a list of "
264 "independent functions")
265
266 .def("getFunction", &IFunction::getFunction, (arg("self"), arg("index")),
267 "Returns the pointer to i-th child function")
268
269 .def("nDomains", &IFunction::getNumberDomains, arg("self"), "Get the number of domains.")
270
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")
273
274 .def("setMatrixWorkspace", &setMatrixWorkspace,
275 (arg("self"), arg("workspace"), arg("wi"), arg("startX"), arg("endX")),
276 "Set matrix workspace to parse Parameters.xml")
277
278 //-- Deprecated functions that have the wrong names --
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")
281 .def("getParamName", &IFunction::parameterName, (arg("self"), arg("i")), "Return the name of the ith parameter")
282 .def("getParamDescr", &IFunction::parameterDescription, (arg("self"), arg("i")),
283 "Return a description of the ith parameter")
284 .def("getParamExplicit", &IFunction::isExplicitlySet, (arg("self"), arg("i")),
285 "Return whether the ith parameter needs to be explicitely set")
286 .def("getParamValue", (double (IFunction::*)(std::size_t) const) & IFunction::getParameter,
287 (arg("self"), arg("i")), "Get the value of the ith parameter")
288 .def("getParameterIndex", &IFunction::parameterIndex, (arg("self"), arg("name")),
289 "Returns the index of the provided parameter.")
290
291 //-- Python special methods --
292 .def("__repr__", &IFunction::asString, arg("self"), "Return a string representation of the function");
293
294 TypeRegistry::subscribe<TypedPropertyValueHandler<IFunction_sptr>>();
295}
double value
The value of the point.
Definition: FitMW.cpp:51
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition: GetPointer.h:17
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
void export_IFunction()
Definition: IFunction.cpp:118
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(valueAsPrettyStrOverloader, valueAsPrettyStr, 0, 2) void export_Property()
Definition: Property.cpp:102
#define GNU_DIAG_ON(x)
#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.
Definition: IFunction.h:163
virtual size_t nParams() const =0
Total number of parameters.
virtual void removeConstraint(const std::string &parName)
Remove a constraint.
Definition: IFunction.cpp:403
std::string writeTies() const
Write a parameter tie to a string.
Definition: IFunction.cpp:261
void unfixParameter(const std::string &name)
Free a parameter.
Definition: IFunction.cpp:1522
virtual double getParameter(size_t i) const =0
Get i-th parameter.
virtual void initialize()
Iinialize the function.
Definition: IFunction.h:422
virtual std::shared_ptr< IFunction > clone() const
Virtual copy constructor.
Definition: IFunction.cpp:109
virtual void tie(const std::string &parName, const std::string &expr, bool isDefault=false)
Tie a parameter to other parameters (or a constant)
Definition: IFunction.cpp:214
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.
Definition: IFunction.cpp:1100
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.
Definition: IFunction.cpp:572
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.
Definition: IFunction.cpp:1515
virtual std::vector< std::string > getAttributeNames() const
Returns a list of attribute names.
Definition: IFunction.cpp:1368
virtual void setConstraintPenaltyFactor(const std::string &parName, const double &c)
Set a constraint penalty.
Definition: IFunction.cpp:417
virtual void addTies(const std::string &ties, bool isDefault=false)
Add several ties.
Definition: IFunction.cpp:233
std::string asString() const
Writes itself into a string.
Definition: IFunction.cpp:462
void unfix(size_t i)
Restores a declared parameter i to the active status.
Definition: IFunction.cpp:197
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.
Definition: IFunction.cpp:1339
void unfixAll()
Free all parameters.
Definition: IFunction.cpp:1538
virtual std::shared_ptr< IFunction > getFunction(size_t i) const
Returns the pointer to i-th child function.
Definition: IFunction.cpp:1363
virtual void addConstraints(const std::string &str, bool isDefault=false)
Add a list of conatraints from a string.
Definition: IFunction.cpp:521
void fixAll(bool isDefault=false)
Fix all parameters.
Definition: IFunction.cpp:1529
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.
Definition: IFunction.cpp:441
virtual void removeTie(const std::string &parName)
Removes the tie off a parameter.
Definition: IFunction.cpp:254
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.
Definition: IFunction.cpp:181
bool isFixed(size_t i) const
Check if a parameter i is fixed.
Definition: IFunction.cpp:167
virtual size_t nAttributes() const
Returns the number of attributes associated with the function.
Definition: IFunction.cpp:1336
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.
Definition: IFunction.cpp:1572
Represents the Jacobian in IFitFunction::functionDeriv.
Definition: Jacobian.h:22
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
Definition: IFunction.h:732
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
Helper class which provides the Collimation Length for SANS instruments.
Definition: NDArray.h:49
STL namespace.