Commit e9a143f4 authored by Roman Tolchenov's avatar Roman Tolchenov
Browse files

Re #18518. Refactored ties and constraints output.

parent c509632f
......@@ -471,7 +471,7 @@ public:
/// Add a new tie. Derived classes must provide storage for ties
virtual void addTie(std::unique_ptr<ParameterTie> tie);
/// Write a parameter tie to a string
virtual std::string writeTie(size_t iParam) const;
std::string writeTies() const;
//@}
/** @name Constraints */
......@@ -485,7 +485,7 @@ public:
/// Remove a constraint
virtual void removeConstraint(const std::string &parName);
/// Write a parameter constraint to a string
virtual std::string writeConstraint(size_t iParam) const;
std::string writeConstraints() const;
/// Remove all constraints.
virtual void clearConstraints();
//@}
......
......@@ -7,6 +7,7 @@
#include "MantidAPI/FunctionFactory.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/Logger.h"
#include "MantidKernel/Strings.h"
#include <boost/lexical_cast.hpp>
#include <boost/shared_array.hpp>
......@@ -89,26 +90,21 @@ std::string CompositeFunction::asString() const {
ostr << ';';
}
}
std::string ties;
for (size_t i = 0; i < nParams(); i++) {
const ParameterTie *tie = getTie(i);
if (tie) {
IFunction_sptr fun = getFunction(functionIndex(i));
std::string tmp = tie->asString(fun.get());
if (tmp.empty()) {
tmp = tie->asString(this);
if (!tmp.empty()) {
if (!ties.empty()) {
ties += ",";
}
ties += tmp;
}
}
}
// collect non-default constraints
std::string constraints = writeConstraints();
// print constraints
if (!constraints.empty()) {
ostr << ";constraints=(" << constraints << ")";
}
// collect the non-default ties
std::string ties = writeTies();
// print the ties
if (!ties.empty()) {
ostr << ";ties=(" << ties << ")";
}
return ostr.str();
}
......
......@@ -164,18 +164,18 @@ void IFunction::removeTie(const std::string &parName) {
this->removeTie(i);
}
/// Write a parameter tie to a string
/// @param iParam :: An index of a parameter.
/// Write all parameter ties owned by this function to a string
/// @return A tie string for the parameter.
std::string IFunction::writeTie(size_t iParam) const {
std::string IFunction::writeTies() const {
std::ostringstream tieStream;
const ParameterTie *tie = getTie(iParam);
if (tie) {
if (!tie->isDefault()) {
tieStream << tie->asString(this);
bool first = true;
for(auto &tie: m_ties) {
if (!first) {
tieStream << ',';
} else {
first = false;
}
} else if (isFixed(iParam)) {
tieStream << parameterName(iParam) << "=" << getParameter(iParam);
tieStream << tie->asString(this);
}
return tieStream.str();
}
......@@ -331,18 +331,20 @@ void IFunction::setUpForFit() {
}
}
/// Write a parameter constraint to a string
/// @param iParam :: An index of a parameter.
/// Write all parameter constraints owned by this function to a string
/// @return A constraint string for the parameter.
std::string IFunction::writeConstraint(size_t iParam) const {
const IConstraint *c = getConstraint(iParam);
if (c && !c->isDefault()) {
std::string constraint = c->asString();
if (!constraint.empty()) {
return constraint;
std::string IFunction::writeConstraints() const {
std::ostringstream stream;
bool first = true;
for(auto &constrint: m_constraints) {
if (!first) {
stream << ',';
} else {
first = false;
}
stream << constrint->asString();
}
return "";
return stream.str();
}
/**
......@@ -361,35 +363,29 @@ std::string IFunction::asString() const {
ostr << ',' << attName << '=' << attValue;
}
}
std::vector<std::string> ties;
// print the parameters
for (size_t i = 0; i < nParams(); i++) {
if (!isFixed(i)) {
ostr << ',' << parameterName(i) << '=' << getParameter(i);
std::ostringstream paramOut;
paramOut << parameterName(i) << '=' << getParameter(i);
if (isActive(i)) {
ostr << ',' << paramOut.str();
} else if (isFixed(i)) {
ties.push_back(paramOut.str());
}
}
// collect non-default constraints
std::vector<std::string> constraints;
for (size_t i = 0; i < nParams(); i++) {
auto constraint = writeConstraint(i);
if (!constraint.empty()) {
constraints.push_back(constraint);
}
}
std::string constraints = writeConstraints();
// print constraints
if (!constraints.empty()) {
ostr << ",constraints=("
<< Kernel::Strings::join(constraints.begin(), constraints.end(), ",")
<< ")";
ostr << ",constraints=(" << constraints << ")";
}
// collect the non-default ties
std::vector<std::string> ties;
for (size_t i = 0; i < nParams(); i++) {
auto tie = writeTie(i);
if (!tie.empty()) {
ties.push_back(tie);
}
auto tiesString = writeTies();
if (!tiesString.empty()) {
ties.push_back(tiesString);
}
// print the ties
if (!ties.empty()) {
......
......@@ -38,19 +38,22 @@ std::size_t ParameterReference::getIndex() const { return m_index; }
*/
void ParameterReference::reset(IFunction *fun, std::size_t index,
bool isDefault) {
IFunction *fLocal = fun;
size_t iLocal = index;
CompositeFunction *cf = dynamic_cast<CompositeFunction *>(fun);
while (cf) {
size_t iFun =
cf->functionIndex(iLocal); // TODO squashing the warning breaks the code
fLocal = cf->getFunction(iFun).get();
iLocal = fLocal->parameterIndex(cf->parameterLocalName(iLocal));
cf = dynamic_cast<CompositeFunction *>(fLocal);
}
//IFunction *fLocal = fun;
//size_t iLocal = index;
//CompositeFunction *cf = dynamic_cast<CompositeFunction *>(fun);
//while (cf) {
// size_t iFun =
// cf->functionIndex(iLocal); // TODO squashing the warning breaks the code
// fLocal = cf->getFunction(iFun).get();
// iLocal = fLocal->parameterIndex(cf->parameterLocalName(iLocal));
// cf = dynamic_cast<CompositeFunction *>(fLocal);
//}
m_function = fLocal;
m_index = iLocal;
//m_function = fLocal;
//m_index = iLocal;
//m_isDefault = isDefault;
m_function = fun;
m_index = index;
m_isDefault = isDefault;
}
......
......@@ -51,7 +51,7 @@ BoundaryConstraint::BoundaryConstraint(API::IFunction *fun,
: m_penaltyFactor(1000.0), m_parameterName(paramName),
m_hasLowerBound(true), m_hasUpperBound(true), m_lowerBound(lowerBound),
m_upperBound(upperBound) {
reset(fun, fun->parameterIndex(paramName), isDefault);
//reset(fun, fun->parameterIndex(paramName), isDefault);
}
BoundaryConstraint::BoundaryConstraint(API::IFunction *fun,
......@@ -60,7 +60,7 @@ BoundaryConstraint::BoundaryConstraint(API::IFunction *fun,
: m_penaltyFactor(1000.0), m_parameterName(paramName),
m_hasLowerBound(true), m_hasUpperBound(false), m_lowerBound(lowerBound),
m_upperBound(-DBL_MAX) {
reset(fun, fun->parameterIndex(paramName), isDefault);
//reset(fun, fun->parameterIndex(paramName), isDefault);
}
/** Initialize the constraint from an expression.
......
......@@ -109,29 +109,15 @@ std::string CrystalFieldSpectrum::asString() const {
ostr << ',' << attName << '=' << attValue;
}
}
// Print own parameters
for (size_t i = 0; i < m_nOwnParams; i++) {
const ParameterTie *tie = getTie(i);
if (!tie || !tie->isDefault()) {
ostr << ',' << parameterName(i) << '=' << getParameter(i);
}
}
// collect non-default constraints
std::vector<std::string> constraints;
for (size_t i = 0; i < m_nOwnParams; i++) {
auto constraint = writeConstraint(i);
if (!constraint.empty()) {
constraints.push_back(constraint);
}
}
// collect the non-default ties
std::vector<std::string> ties;
// Print own parameters
for (size_t i = 0; i < m_nOwnParams; i++) {
auto tie = writeTie(i);
if (!tie.empty()) {
ties.push_back(tie);
std::ostringstream paramOut;
paramOut << parameterName(i) << '=' << getParameter(i);
if (isActive(i)) {
ostr << ',' << paramOut.str();
} else if (isFixed(i)) {
ties.push_back(paramOut.str());
}
}
......@@ -160,24 +146,18 @@ std::string CrystalFieldSpectrum::asString() const {
}
} // for peaks
for (size_t i = 0; i < nPeakParams; ++i) {
auto constraint = writeConstraint(i + m_nOwnParams);
if (!constraint.empty()) {
constraints.push_back(constraint);
}
auto tieStr = m_target->writeTie(i);
if (!tieStr.empty()) {
ties.push_back(tieStr);
}
}
// collect non-default constraints
std::string constraints = writeConstraints();
// print constraints
if (!constraints.empty()) {
ostr << ",constraints=("
<< Kernel::Strings::join(constraints.begin(), constraints.end(), ",")
<< ")";
ostr << ";constraints=(" << constraints << ")";
}
// collect the non-default ties
auto tiesString = writeTies();
if (!tiesString.empty()) {
ties.push_back(tiesString);
}
// print the ties
if (!ties.empty()) {
ostr << ",ties=(" << Kernel::Strings::join(ties.begin(), ties.end(), ",")
......
......@@ -404,6 +404,88 @@ public:
TS_ASSERT_DELTA(mfun->getParameter("f1.b"), 4.4, 0.01);
TS_ASSERT_EQUALS(s.getError(), "success");
}
void test_constraints_str() {
auto fun = FunctionFactory::Instance().createInitialized(
"name=Gaussian,constraints=(Height>0)");
TS_ASSERT_EQUALS(
fun->asString(),
"name=Gaussian,Height=0,PeakCentre=0,Sigma=0,constraints=(0<Height)");
fun = FunctionFactory::Instance().createInitialized(
"name=Gaussian,constraints=(Height>0);name=LinearBackground,"
"constraints=(A0<0)");
TS_ASSERT_EQUALS(fun->asString(), "name=Gaussian,Height=0,PeakCentre=0,"
"Sigma=0,constraints=(0<Height);name="
"LinearBackground,A0=0,A1=0,constraints=("
"A0<0)");
fun = FunctionFactory::Instance().createInitialized(
"name=Gaussian;name=LinearBackground;"
"constraints=(f0.Height>0, f1.A0<0)");
TS_ASSERT_EQUALS(fun->asString(), "name=Gaussian,Height=0,PeakCentre=0,"
"Sigma=0;name=LinearBackground,A0=0,A1=0;"
"constraints=(0<f0.Height,f1.A0<0)");
fun = FunctionFactory::Instance().createInitialized(
"name=Gaussian,constraints=(Height>0);name=LinearBackground,"
"constraints=(A0<0);constraints=(f0.Sigma<0, f1.A1>10)");
TS_ASSERT_EQUALS(fun->asString(),
"name=Gaussian,Height=0,PeakCentre=0,Sigma=0,constraints=("
"0<Height);name=LinearBackground,A0=0,A1=0,constraints=("
"A0<0);constraints=(f0.Sigma<0,10<f1.A1)");
}
void test_ties_str() {
auto fun = FunctionFactory::Instance().createInitialized(
"name=Gaussian,ties=(Height=10)");
TS_ASSERT_EQUALS(
fun->asString(),
"name=Gaussian,PeakCentre=0,Sigma=0,ties=(Height=10)");
fun = FunctionFactory::Instance().createInitialized(
"name=Gaussian,ties=(Height=10*Sigma)");
TS_ASSERT_EQUALS(
fun->asString(),
"name=Gaussian,PeakCentre=0,Sigma=0,ties=(Height=10*Sigma)");
fun = FunctionFactory::Instance().createInitialized(
"name=Gaussian,ties=(Height=10);name=LinearBackground,"
"ties=(A0=0)");
TS_ASSERT_EQUALS(fun->asString(), "name=Gaussian,PeakCentre=0,Sigma=0,ties="
"(Height=10);name=LinearBackground,A1=0,"
"ties=(A0=0)");
fun = FunctionFactory::Instance().createInitialized(
"name=Gaussian,ties=(Height=10*Sigma);name=LinearBackground,"
"ties=(A0=A1)");
TS_ASSERT_EQUALS(fun->asString(), "name=Gaussian,PeakCentre=0,Sigma=0,ties="
"(Height=10*Sigma);name=LinearBackground,"
"A1=0,ties=(A0=A1)");
fun = FunctionFactory::Instance().createInitialized(
"name=Gaussian;name=LinearBackground;"
"ties=(f0.Height=2, f1.A0=f1.A1)");
TS_ASSERT_EQUALS(fun->asString(), "name=Gaussian,PeakCentre=0,Sigma=0,ties="
"(Height=2);name=LinearBackground,A0=0,"
"A1=0;ties=(f1.A0=f1.A1)");
fun = FunctionFactory::Instance().createInitialized(
"name=Gaussian;name=LinearBackground;"
"ties=(f0.Height=f1.A0=f1.A1)");
TS_ASSERT_EQUALS(fun->asString(), "name=Gaussian,Height=0,PeakCentre=0,"
"Sigma=0;name=LinearBackground,A0=0,A1=0;"
"ties=(f1.A0=f1.A1,f0.Height=f1.A1)");
fun = FunctionFactory::Instance().createInitialized(
"name=Gaussian,ties=(Height=0);name=LinearBackground,"
"ties=(A0=A1);ties=(f0.Sigma=f1.A1)");
TS_ASSERT_EQUALS(fun->asString(), "name=Gaussian,PeakCentre=0,Sigma=0,ties="
"(Height=0);name=LinearBackground,A1=0,"
"ties=(A0=A1);ties=(f0.Sigma=f1.A1)");
}
};
#endif /*CURVEFITTING_COMPOSITEFUNCTIONTEST_H_*/
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment