Unverified Commit 102df5cb authored by Peterson, Peter's avatar Peterson, Peter Committed by GitHub
Browse files

Merge pull request #30790 from stonecoldhughes/add_intensity

Added intensity as an output for IPeakFunctions in SequentialFit
parents 87fe5f47 694c51d8
......@@ -69,6 +69,8 @@ public:
void setParameterDescription(size_t, const std::string &description) override;
/// Get i-th parameter
[[nodiscard]] double getParameter(size_t i) const override;
/// Get parameter from i-th function, j-th local parameter index
[[nodiscard]] double getParameter(size_t i, size_t j) const;
/// Set parameter by name.
void setParameter(const std::string &name, const double &value,
bool explicitlySet = true) override;
......@@ -99,6 +101,7 @@ public:
[[nodiscard]] size_t parameterIndex(const std::string &name) const override;
/// Returns the name of parameter i
[[nodiscard]] std::string parameterName(size_t i) const override;
[[nodiscard]] std::string parameterName(size_t i, size_t j) const;
/// Returns the name of attribute i
[[nodiscard]] std::string attributeName(size_t i) const override;
/// Returns the description of parameter i
......@@ -107,6 +110,8 @@ public:
[[nodiscard]] bool isExplicitlySet(size_t i) const override;
/// Get the fitting error for a parameter
[[nodiscard]] double getError(size_t i) const override;
/// Get the fitting error for i-th function's j-th parameter
[[nodiscard]] double getError(size_t i, size_t j) const;
/// Get the fitting error for a parameter by name
[[nodiscard]] double getError(const std::string &name) const override;
/// Set the fitting error for a parameter
......
......@@ -250,6 +250,15 @@ double CompositeFunction::getParameter(size_t i) const {
return m_functions[iFun]->getParameter(i - m_paramOffsets[iFun]);
}
/** Get the j-th parameter from the i-th function.
* @param i :: The function index
* @param j :: The i-th function's j-th parameter
* @return value of the requested parameter
*/
double CompositeFunction::getParameter(size_t i, size_t j) const {
return m_functions[i]->getParameter(j);
}
/**
* Check if function has a parameter with a particular name.
* @param name :: A name of a parameter.
......@@ -388,6 +397,16 @@ std::string CompositeFunction::parameterName(size_t i) const {
return ostr.str();
}
/// Returns the name of i-th function's j-th parameter
/// @param i :: The function index
/// @param j :: The local index of the parameter relative to the function index
/// @return The name of the parameter
std::string CompositeFunction::parameterName(size_t i, size_t j) const {
std::ostringstream ostr;
ostr << 'f' << i << '.' << m_functions[i]->parameterName(j);
return ostr.str();
}
/// Returns the name of the ith attribute
/// @param index :: The index of the attribute
/// @return The name of the attribute
......@@ -423,6 +442,15 @@ double CompositeFunction::getError(size_t i) const {
size_t iFun = functionIndex(i);
return m_functions[iFun]->getError(i - m_paramOffsets[iFun]);
}
/**
* Get the fitting error for the i-th function's j-th parameter
* @param i :: The index of the i-th function
* @param j :: The index of the i-th function's j-th parameter
* @return :: the error
*/
double CompositeFunction::getError(size_t i, size_t j) const {
return m_functions[i]->getError(j);
}
/**
* Get the fitting error for a parameter by name.
......
......@@ -11,6 +11,7 @@
//----------------------------------------------------------------------
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/IFunction.h"
#include "MantidAPI/IPeakFunction.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidCurveFitting/Algorithms/PlotPeakByLogValueHelper.h"
......@@ -79,10 +80,10 @@ private:
API::ITableWorkspace_sptr
createResultsTable(const std::string &logName,
const API::IFunction_sptr &ifunSingle, bool &isDataName);
const API::IFunction_sptr ifunSingle, bool &isDataName);
void appendTableRow(bool isDataName, API::ITableWorkspace_sptr &result,
const API::IFunction *const ifun,
const API::IFunction_sptr ifun,
const InputSpectraToFit &data, double logValue,
double chi2) const;
......
......@@ -316,7 +316,7 @@ void PlotPeakByLogValue::exec() {
// Find the log value: it is either a log-file value or
// simply the workspace number
double logValue = calculateLogValue(logName, data);
appendTableRow(isDataName, result, ifun.get(), data, logValue, chi2);
appendTableRow(isDataName, result, ifun, data, logValue, chi2);
Prog += dProg;
std::string current = std::to_string(i);
......@@ -403,7 +403,7 @@ void PlotPeakByLogValue::finaliseOutputWorkspaces(
void PlotPeakByLogValue::appendTableRow(bool isDataName,
ITableWorkspace_sptr &result,
const IFunction *const ifun,
const IFunction_sptr ifun,
const InputSpectraToFit &data,
double logValue, double chi2)
const { // Extract the fitted parameters and put them into the result table
......@@ -414,15 +414,44 @@ void PlotPeakByLogValue::appendTableRow(bool isDataName,
row << logValue;
}
for (size_t iPar = 0; iPar < ifun->nParams(); ++iPar) {
row << ifun->getParameter(iPar) << ifun->getError(iPar);
auto p = std::dynamic_pointer_cast<API::CompositeFunction>(ifun);
if (p) {
for (size_t i = 0; i < p->nFunctions(); ++i) {
auto f = ifun->getFunction(i);
for (size_t j = 0; j < f->nParams(); ++j) {
row << p->getParameter(i, j) << p->getError(i, j);
}
if (f->hasParameter("Intensity") == false) {
auto intensity_handle =
std::dynamic_pointer_cast<API::IPeakFunction>(f);
if (intensity_handle) {
row << intensity_handle->intensity();
}
}
}
}
else {
for (size_t iPar = 0; iPar < ifun->nParams(); ++iPar) {
row << ifun->getParameter(iPar) << ifun->getError(iPar);
}
if (ifun->hasParameter("Intensity") == false) {
auto intensity_handle =
std::dynamic_pointer_cast<API::IPeakFunction>(ifun);
if (intensity_handle) {
row << intensity_handle->intensity();
}
}
}
row << chi2;
}
ITableWorkspace_sptr
PlotPeakByLogValue::createResultsTable(const std::string &logName,
const IFunction_sptr &ifunSingle,
const IFunction_sptr ifunSingle,
bool &isDataName) {
ITableWorkspace_sptr result =
WorkspaceFactory::Instance().createTable("TableWorkspace");
......@@ -437,10 +466,40 @@ PlotPeakByLogValue::createResultsTable(const std::string &logName,
col->setPlotType(1); // X-values inplots
}
for (size_t iPar = 0; iPar < ifunSingle->nParams(); ++iPar) {
result->addColumn("double", ifunSingle->parameterName(iPar));
result->addColumn("double", ifunSingle->parameterName(iPar) + "_Err");
auto p = std::dynamic_pointer_cast<API::CompositeFunction>(ifunSingle);
if (p) {
for (size_t i = 0; i < p->nFunctions(); ++i) {
auto f = ifunSingle->getFunction(i);
for (size_t j = 0; j < f->nParams(); ++j) {
result->addColumn("double", p->parameterName(i, j));
result->addColumn("double", p->parameterName(i, j) + "_Err");
}
if (f->hasParameter("Intensity") == false) {
auto intensity_handle =
std::dynamic_pointer_cast<API::IPeakFunction>(f);
if (intensity_handle) {
result->addColumn("double", "f" + std::to_string(i) + ".Intensity");
}
}
}
}
else {
for (size_t iPar = 0; iPar < ifunSingle->nParams(); ++iPar) {
result->addColumn("double", ifunSingle->parameterName(iPar));
result->addColumn("double", ifunSingle->parameterName(iPar) + "_Err");
}
if (ifunSingle->hasParameter("Intensity") == false) {
auto intensity_handle =
std::dynamic_pointer_cast<API::IPeakFunction>(ifunSingle);
if (intensity_handle) {
result->addColumn("double", "Intensity");
}
}
}
result->addColumn("double", "Chi_squared");
this->setProperty("OutputWorkspace", result);
......
......@@ -125,10 +125,10 @@ public:
TWS_type result =
WorkspaceCreationHelper::getWS<TableWorkspace>("PlotPeakResult");
TS_ASSERT_EQUALS(result->columnCount(), 12);
TS_ASSERT_EQUALS(result->columnCount(), 13);
std::vector<std::string> tnames = result->getColumnNames();
TS_ASSERT_EQUALS(tnames.size(), 12);
TS_ASSERT_EQUALS(tnames.size(), 13);
TS_ASSERT_EQUALS(tnames[0], "var");
TS_ASSERT_EQUALS(tnames[1], "f0.A0");
TS_ASSERT_EQUALS(tnames[2], "f0.A0_Err");
......@@ -140,7 +140,8 @@ public:
TS_ASSERT_EQUALS(tnames[8], "f1.PeakCentre_Err");
TS_ASSERT_EQUALS(tnames[9], "f1.Sigma");
TS_ASSERT_EQUALS(tnames[10], "f1.Sigma_Err");
TS_ASSERT_EQUALS(tnames[11], "Chi_squared");
TS_ASSERT_EQUALS(tnames[11], "f1.Intensity");
TS_ASSERT_EQUALS(tnames[12], "Chi_squared");
TS_ASSERT_DELTA(result->Double(0, 0), 1, 1e-10);
TS_ASSERT_DELTA(result->Double(0, 1), 1, 1e-10);
......@@ -163,6 +164,11 @@ public:
TS_ASSERT_DELTA(result->Double(2, 7), 5.06, 1e-10);
TS_ASSERT_DELTA(result->Double(2, 9), 0.12, 1e-10);
/* Check intensity column: */
TS_ASSERT_DELTA(result->Double(0, 11), 0.501326, 1e-6);
TS_ASSERT_DELTA(result->Double(1, 11), 0.496312, 1e-6);
TS_ASSERT_DELTA(result->Double(2, 11), 0.481273, 1e-6);
deleteData();
WorkspaceCreationHelper::removeWS("PlotPeakResult");
}
......@@ -184,10 +190,10 @@ public:
TWS_type result =
WorkspaceCreationHelper::getWS<TableWorkspace>("PlotPeakResult");
TS_ASSERT_EQUALS(result->columnCount(), 12);
TS_ASSERT_EQUALS(result->columnCount(), 13);
std::vector<std::string> tnames = result->getColumnNames();
TS_ASSERT_EQUALS(tnames.size(), 12);
TS_ASSERT_EQUALS(tnames.size(), 13);
TS_ASSERT_EQUALS(tnames[0], "var");
TS_ASSERT_EQUALS(tnames[1], "f0.A0");
TS_ASSERT_EQUALS(tnames[2], "f0.A0_Err");
......@@ -199,7 +205,8 @@ public:
TS_ASSERT_EQUALS(tnames[8], "f1.PeakCentre_Err");
TS_ASSERT_EQUALS(tnames[9], "f1.Sigma");
TS_ASSERT_EQUALS(tnames[10], "f1.Sigma_Err");
TS_ASSERT_EQUALS(tnames[11], "Chi_squared");
TS_ASSERT_EQUALS(tnames[11], "f1.Intensity");
TS_ASSERT_EQUALS(tnames[12], "Chi_squared");
TS_ASSERT_DELTA(result->Double(0, 0), 1, 1e-10);
TS_ASSERT_DELTA(result->Double(0, 1), 1, 1e-10);
......@@ -222,6 +229,11 @@ public:
TS_ASSERT_DELTA(result->Double(2, 7), 5.06, 1e-10);
TS_ASSERT_DELTA(result->Double(2, 9), 0.12, 1e-10);
/* Check intensity column: */
TS_ASSERT_DELTA(result->Double(0, 11), 0.501326, 1e-6);
TS_ASSERT_DELTA(result->Double(1, 11), 0.496312, 1e-6);
TS_ASSERT_DELTA(result->Double(2, 11), 0.481273, 1e-6);
deleteData();
WorkspaceCreationHelper::removeWS("PlotPeakResult");
}
......@@ -243,16 +255,21 @@ public:
TWS_type result =
WorkspaceCreationHelper::getWS<TableWorkspace>("PlotPeakResult");
TS_ASSERT_EQUALS(result->columnCount(), 12);
TS_ASSERT_EQUALS(result->columnCount(), 13);
std::vector<std::string> tnames = result->getColumnNames();
TS_ASSERT_EQUALS(tnames.size(), 12);
TS_ASSERT_EQUALS(tnames.size(), 13);
TS_ASSERT_EQUALS(tnames[0], "SourceName");
TS_ASSERT_EQUALS(result->String(0, 0), "PlotPeakGroup_0");
TS_ASSERT_EQUALS(result->String(1, 0), "PlotPeakGroup_1");
TS_ASSERT_EQUALS(result->String(2, 0), "PlotPeakGroup_2");
/* Check intensity column: */
TS_ASSERT_DELTA(result->Double(0, 11), 0.501326, 1e-6);
TS_ASSERT_DELTA(result->Double(1, 11), 0.496312, 1e-6);
TS_ASSERT_DELTA(result->Double(2, 11), 0.481273, 1e-6);
deleteData();
WorkspaceCreationHelper::removeWS("PlotPeakResult");
}
......@@ -273,10 +290,10 @@ public:
TWS_type result =
WorkspaceCreationHelper::getWS<TableWorkspace>("PlotPeakResult");
TS_ASSERT_EQUALS(result->columnCount(), 12);
TS_ASSERT_EQUALS(result->columnCount(), 13);
std::vector<std::string> tnames = result->getColumnNames();
TS_ASSERT_EQUALS(tnames.size(), 12);
TS_ASSERT_EQUALS(tnames.size(), 13);
TS_ASSERT_EQUALS(tnames[0], "axis-1");
TS_ASSERT_EQUALS(result->Double(0, 0), 0.5);
......@@ -437,7 +454,7 @@ public:
TWS_type result =
WorkspaceCreationHelper::getWS<TableWorkspace>("PlotPeakResult");
TS_ASSERT_EQUALS(result->columnCount(), 12);
TS_ASSERT_EQUALS(result->columnCount(), 13);
auto matrices =
AnalysisDataService::Instance().retrieveWS<const WorkspaceGroup>(
......
......@@ -66,8 +66,9 @@ The output workspace is a table in which rows correspond to the spectra
in the order they (spectra) appear in the Input property. The first
column of the table has the log values. It is followed by pairs of
columns with parameter values and fitting errors. If a parameter was
fixed or tied the error will be zero. Here is an example of the output
workspace:
fixed or tied the error will be zero. If the function being fitted is
a Peak function, a column for its integrated intensity is added after its
parameter columns. Here is an example of the output workspace:
Minimizer setup
###############
......
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