38void Fit::initConcrete() {
42 "the fitting function.");
45 auto mustBePositive = std::make_shared<Kernel::BoundedValidator<int>>();
46 mustBePositive->setLower(0);
48 "Stop after this number of iterations if a good fit is not found");
56 declareProperty(
"Minimizer",
"Levenberg-Marquardt", minimizerValidator,
"Minimizer to use for fitting.");
60 for (
auto &costFuncOption : costFuncOptions) {
61 auto costFunc = std::dynamic_pointer_cast<CostFunctions::CostFuncFitting>(
67 Kernel::IValidator_sptr costFuncValidator = std::make_shared<Kernel::ListValidator<std::string>>(costFuncOptions);
71 "Set to true to create output workspaces with the results of the fit"
72 "(default is false).");
74 "A base name for the output workspaces (if not "
75 "given default names will be created). The "
76 "default is to use the name of the original data workspace as prefix "
77 "followed by suffixes _Workspace, _Parameters, etc.");
79 "Set to true to calcuate errors when output isn't created "
80 "(default is false).");
82 "If true and CreateOutput is true then the value of each "
83 "member of a Composite Function is also output.");
85 "If true and OutputCompositeMembers is true members of any "
86 "Convolution are output convolved\n"
87 "with corresponding resolution");
89 "Set to true to output only the parameters and not "
90 "workspace(s) with the calculated values\n"
91 "(default is false, ignored if CreateOutput is false and "
92 "Output is an empty string).");
95std::map<std::string, std::string> Fit::validateInputs() {
96 std::map<std::string, std::string> issues;
100 if (constraints.size() > 0) {
101 auto operatorPresent =
false;
102 for (
const auto &op : possibleOperators) {
103 const auto it = constraints.find_first_of(op);
104 if (it <= constraints.size()) {
105 operatorPresent =
true;
109 if (!operatorPresent) {
110 issues[
"Constraints"] =
"No operator is present in the constraint.";
118void Fit::readProperties() {
124 if (!constraints.empty()) {
130 int intMaxIterations =
getProperty(
"MaxIterations");
136void Fit::initializeMinimizer(
size_t maxIterations) {
137 const bool unrollComposites =
getProperty(
"OutputCompositeMembers");
139 if (convolveMembers) {
142 m_domainCreator->separateCompositeMembersInOutput(unrollComposites, convolveMembers);
155 for (
auto property : properties) {
157 auto clonedProperty = std::unique_ptr<Kernel::Property>((*property).clone());
165size_t Fit::runMinimizer() {
167 auto prog = std::make_shared<API::Progress>(
this, 0.0, 1.0, nsteps);
172 bool isFinished =
false;
173 g_log.
debug(
"Starting minimizer iteration\n");
175 g_log.
debug() <<
"Starting iteration " << iter <<
"\n";
201 g_log.
debug() <<
"Number of minimizer iterations=" << iter <<
"\n";
207void Fit::finalizeMinimizer(
size_t nIterations) {
211 g_log.
debug() <<
"Iteration stopped. Minimizer status string=" << errorString <<
"\n";
214 if (!errorString.empty()) {
220 if (errorString.empty()) {
221 errorString =
"success";
228 logStream <<
"Fit status: " << errorString <<
'\n';
229 logStream <<
"Stopped after " << nIterations <<
" iterations" <<
'\n';
234void Fit::createOutput() {
240 double rawcostfuncval =
m_minimizer->costFunctionVal();
241 double finalCostFuncVal = rawcostfuncval / double(dof);
243 setProperty(
"OutputChi2overDoF", finalCostFuncVal);
247 if (!baseName.empty()) {
248 doCreateOutput =
true;
251 if (doCreateOutput) {
255 doCalcErrors =
false;
265 if (doCreateOutput) {
271 if (baseName.empty()) {
272 baseName = ws->getName();
273 if (baseName.empty()) {
281 "The name of the TableWorkspace in which to store the final covariance "
283 setPropertyValue(
"OutputNormalisedCovarianceMatrix", baseName +
"NormalisedCovarianceMatrix");
287 covariance->addColumn(
"str",
"Name");
289 covariance->getColumn(covariance->columnCount() - 1)->setPlotType(6);
290 for (
size_t i = 0; i <
m_function->nParams(); i++) {
292 covariance->addColumn(
"double",
m_function->parameterName(i));
298 for (
size_t i = 0; i < np; i++) {
304 for (
size_t j = 0; j < np; j++) {
311 throw std::runtime_error(
"There was an error while allocating the covariance "
313 "which is needed to produce fitting error results.");
315 row << 100.0 * covar.
get(ia, ja) / sqrt(covar.
get(ia, ia) * covar.
get(ja, ja));
319 if (ja >= covar.
size2())
324 if (ia >= covar.
size1())
328 setProperty(
"OutputNormalisedCovarianceMatrix", covariance);
335 "The name of the TableWorkspace in which to store the "
336 "final fit parameters");
341 result->addColumn(
"str",
"Name");
343 result->getColumn(result->columnCount() - 1)->setPlotType(6);
344 result->addColumn(
"double",
"Value");
345 result->addColumn(
"double",
"Error");
347 result->getColumn(result->columnCount() - 1)->setPlotType(5);
349 for (
size_t i = 0; i <
m_function->nParams(); i++) {
357 if (costfuncname ==
"Rwp")
358 row <<
"Cost function value" << rawcostfuncval;
360 row <<
"Cost function value" << finalCostFuncVal;
363 bool outputParametersOnly =
getProperty(
"OutputParametersOnly");
365 if (!outputParametersOnly) {
376void Fit::execConcrete() {
#define DECLARE_ALGORITHM(classname)
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Kernel::Property * getPointerToProperty(const std::string &name) const override
Get a property by name.
bool existsProperty(const std::string &name) const override
Checks whether the named property is already in the list of managed property.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
bool isChild() const override
To query whether algorithm is a child.
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
void setPropertyValue(const std::string &name, const std::string &value) override
Set the value of a property by string N.B.
A composite function is a function containing other functions.
static const std::vector< std::string > DEFAULT_OPS_STR
An interface for function minimizers.
TableRow represents a row in a TableWorkspace.
A property class for workspaces.
A generic fitting algorithm.
void finalizeMinimizer(size_t nIterations)
Finalize the minimizer.
std::shared_ptr< CostFunctions::CostFuncFitting > m_costFunction
The cost function.
void copyMinimizerOutput(const API::IFuncMinimizer &minimizer)
Copy all output workspace properties from the minimizer to Fit algorithm.
size_t m_maxIterations
Max number of iterations.
size_t runMinimizer()
Run the minimizer's iteration loop.
void createOutput()
Create algorithm output workspaces.
std::shared_ptr< API::IFuncMinimizer > m_minimizer
The minimizer.
void readProperties()
Read in the properties specific to Fit.
void initializeMinimizer(size_t maxIterations)
Initialize the minimizer for this fit.
A wrapper around Eigen::Matrix.
double get(size_t i, size_t j) const
Get an element.
size_t size1() const
First size of the matrix.
size_t size2() const
Second size of the matrix.
const map_type inspector() const
Get a const copy of the Eigen matrix.
A base class for fitting algorithms.
std::shared_ptr< API::IDomainCreator > m_domainCreator
Pointer to a domain creator.
std::shared_ptr< CostFunctions::CostFuncFitting > getCostFunctionInitialized() const
Create a cost function from the "CostFunction" property and make it ready for evaluation.
std::shared_ptr< API::IFunction > m_function
Pointer to the fitting function.
Exception thrown when a fitting function changes number of parameters during fit.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
void notice(const std::string &msg)
Logs at notice level.
void warning(const std::string &msg)
Logs at warning level.
const std::vector< Property * > & getProperties() const override
Get the list of managed properties.
The concrete, templated class for properties.
void setDocumentation(const std::string &documentation)
Sets the user level description of the property.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< const Workspace > Workspace_const_sptr
shared pointer to Mantid::API::Workspace (const version)
std::unique_ptr< T > create(const P &parent, const IndexArg &indexArg, const HistArg &histArg)
This is the create() method that all the other create() methods call.
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.
std::string to_string(const wide_integer< Bits, Signed > &n)
@ InOut
Both an input & output workspace.
@ Input
An input workspace.
@ Output
An output workspace.