Mantid
Loading...
Searching...
No Matches
FitParameter.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 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
15
16#include <boost/lexical_cast.hpp>
17
18namespace Mantid::Geometry {
19namespace {
21Kernel::Logger g_log("FitParameter");
22} // namespace
23
28std::string FitParameter::getConstraint() const {
29
30 if (m_constraintMin.empty() && m_constraintMax.empty())
31 return std::string();
32
33 std::stringstream constraint;
34 size_t foundMinPercentage, foundMaxPercentage;
35 foundMinPercentage = m_constraintMin.find('%');
36 foundMaxPercentage = m_constraintMax.find('%');
37 double min = 0;
38 double max = 0;
39 if (!m_constraintMin.empty()) {
40 if (foundMinPercentage != std::string::npos)
41 min = std::stod(m_constraintMin.substr(0, m_constraintMin.size() - 1)) * m_value * 0.01;
42 else
43 min = std::stod(m_constraintMin);
44 }
45 if (!m_constraintMax.empty()) {
46 if (foundMaxPercentage != std::string::npos)
47 max = std::stod(m_constraintMax.substr(0, m_constraintMax.size() - 1)) * m_value * 0.01;
48 else
49 max = std::stod(m_constraintMax);
50 }
51
52 if (!(m_constraintMin.empty() && m_constraintMax.empty())) {
53 constraint << min << " < " << m_name << " < " << max;
54 } else if (!m_constraintMin.empty()) {
55 constraint << min << " < " << m_name;
56 } else {
57 constraint << m_name << " < " << max;
58 }
59
60 return constraint.str();
61}
62
69double FitParameter::getValue(const double &at) const {
72 return m_value;
73 }
74
75 if (!m_formula.empty()) {
76 size_t found;
77 std::string equationStr = m_formula;
78 std::string toReplace = "centre"; // replace this string in formula
79 size_t len = toReplace.size();
80 found = equationStr.find(toReplace);
81 std::stringstream readDouble;
82 readDouble << at;
83 std::string extractedValueStr = readDouble.str();
84 if (found != std::string::npos)
85 equationStr.replace(found, len, extractedValueStr);
86
87 // check if more than one string to replace in m_eq
88
89 while (equationStr.find(toReplace) != std::string::npos) {
90 found = equationStr.find(toReplace);
91 equationStr.replace(found, len, extractedValueStr);
92 }
93
94 try {
95 mu::Parser p;
96 p.SetExpr(equationStr);
97 m_value = p.Eval();
98 return m_value;
99 } catch (mu::Parser::exception_type &e) {
100 g_log.error() << "Cannot evaluate fitting parameter formula."
101 << " Formula which cannot be passed is " << m_formula
102 << ". Muparser error message is: " << e.GetMsg() << '\n';
103 }
104 }
105
106 return m_value;
107}
108
113double FitParameter::getValue() const { return m_value; }
114
119void FitParameter::printSelf(std::ostream &os) const {
120 os << m_value << " , " << m_function << " , " << m_name << " , " << m_constraintMin << " , " << m_constraintMax
121 << " , " << m_constraintPenaltyFactor << " , " << m_tie << " , " << m_formula << " , " << m_formulaUnit << " , "
122 << m_resultUnit << " , " << m_lookUpTable;
123}
124
131std::ostream &operator<<(std::ostream &os, const FitParameter &f) {
132 f.printSelf(os);
133 return os;
134}
135
162std::istream &operator>>(std::istream &in, FitParameter &f) {
163
165 std::string str;
166 getline(in, str);
167
168 // allow a comma in the final position.
170 auto values = tokens.asVector();
171
172 if (values.size() < 3) {
173 g_log.warning() << "Expecting a comma separated list of at each three entries"
174 << " (any of which may be empty strings) to set information about a "
175 "fitting parameter"
176 << " instead of: " << str << '\n';
177 return in;
178 }
179
180 try {
181 f.setValue(boost::lexical_cast<double>(values[0]));
182 } catch (boost::bad_lexical_cast &) {
183 f.setValue(0.0);
184
185 if (!values.at(0).empty()) {
186 g_log.warning() << "Could not read " << values[0] << " as double for "
187 << " fitting parameter: " << values[1] << ":" << values[2] << '\n';
188 }
189 }
190
191 // read remaining required entries
192
193 f.setFunction(values[1]);
194 f.setName(values[2]);
195
196 // read optional entries
197 values.reserve(10);
198 while (values.size() < 10)
199 values.emplace_back("");
200
201 f.setConstraintMin(values[3]);
202 f.setConstraintMax(values[4]);
203 f.setConstraintPenaltyFactor(values[5]);
204 f.setTie(values[6]);
205 f.setFormula(values[7]);
206 f.setFormulaUnit(values[8]);
207 f.setResultUnit(values[9]);
208
209 if (values.size() > 10) {
210 std::stringstream lookupTableStream(values[10]);
211 Kernel::Interpolation lookupTable;
212 lookupTableStream >> lookupTable;
213 f.setLookUpTable(lookupTable);
214 }
215
216 return in;
217}
218} // namespace Mantid::Geometry
Store information about a fitting parameter such as its value if it is constrained or tied.
Definition: FitParameter.h:26
void setName(const std::string &name)
set name
Definition: FitParameter.h:72
void setFormulaUnit(const std::string &formulaUnit)
set formula unit
Definition: FitParameter.h:60
std::string m_tie
tie of parameter
Definition: FitParameter.h:85
void setResultUnit(const std::string &resultUnit)
set result formula unit
Definition: FitParameter.h:64
void setLookUpTable(const Kernel::Interpolation &lookupTable)
set look up table
Definition: FitParameter.h:76
void setFormula(const std::string &formula)
set formula
Definition: FitParameter.h:56
void setTie(const std::string &tie)
set tie
Definition: FitParameter.h:40
Kernel::Interpolation m_lookUpTable
look up table
Definition: FitParameter.h:94
std::string m_constraintPenaltyFactor
the penalty factor
Definition: FitParameter.h:88
std::string m_constraintMin
constraint min boundary
Definition: FitParameter.h:86
void setConstraintMax(const std::string &constraintMax)
set constraint max
Definition: FitParameter.h:48
void setFunction(const std::string &function)
set function
Definition: FitParameter.h:68
void setConstraintMin(const std::string &constraintMin)
set constraint min
Definition: FitParameter.h:46
std::string m_formulaUnit
the unit that the formula expects
Definition: FitParameter.h:97
void setConstraintPenaltyFactor(const std::string &constraintPenaltyFactor)
set the constraint penalty
Definition: FitParameter.h:50
std::string m_name
name of parameter
Definition: FitParameter.h:92
void setValue(double value)
set parameter value
Definition: FitParameter.h:36
std::string m_formula
the formula
Definition: FitParameter.h:96
std::string getConstraint() const
get constraint
std::string m_function
name of fitting function
Definition: FitParameter.h:90
void printSelf(std::ostream &os) const
Prints object to stream.
std::string m_resultUnit
the result unit
Definition: FitParameter.h:98
double m_value
value of parameter
Definition: FitParameter.h:83
std::string m_constraintMax
constraint max boundary
Definition: FitParameter.h:87
double getValue() const
get paramter value
Provide interpolation over a series of points.
Definition: Interpolation.h:29
bool containData() const
return false if no data has been added
Definition: Interpolation.h:78
double value(const double &at) const
get interpolated value at location at
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
@ TOK_IGNORE_FINAL_EMPTY_TOKEN
ignore an empty token at the end of the string.
@ TOK_TRIM
remove leading and trailing whitespace from tokens
const TokenVec & asVector()
Returns a vector of tokenized strings.
MANTID_GEOMETRY_DLL std::istream & operator>>(std::istream &stream, SymmetryOperation &operation)
Reads identifier from stream and tries to parse as a symbol.
Mantid::Kernel::Logger g_log("Goniometer")
MANTID_GEOMETRY_DLL std::ostream & operator<<(std::ostream &stream, const PointGroup &self)
Returns a streamed representation of the PointGroup object.
Definition: PointGroup.cpp:312