Skip to content

Commit

Permalink
Refs #9358. Added and fixed unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
wdzhou committed Jun 9, 2014
1 parent 84bf243 commit eed5c15
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 37 deletions.
83 changes: 57 additions & 26 deletions Code/Mantid/Framework/Algorithms/src/GeneratePeaks.cpp
Expand Up @@ -137,8 +137,7 @@ namespace Algorithms
declareProperty(paramwsprop,
"Input TableWorkspace for peak's parameters.");

#if 1
// Peak properties
// Peak function properties
std::vector<std::string> peakNames = FunctionFactory::Instance().getFunctionNames<IPeakFunction>();
std::vector<std::string> peakFullNames = addFunctionParameterNames(peakNames);
declareProperty("PeakType", "", boost::make_shared<StringListValidator>(peakFullNames),
Expand All @@ -165,18 +164,6 @@ namespace Algorithms
declareProperty(new ArrayProperty<double>("BackgroundParameterValues"),
"List of background parameter values. They must have a 1-to-1 mapping to PeakParameterNames list. ");

#else
declareProperty("PeakFunction", "Gaussian", boost::make_shared<StringListValidator>(
FunctionFactory::Instance().getFunctionNames<API::IPeakFunction>()),
"Peak function to calculate.");
std::vector<std::string> bkgdnames = FunctionFactory::Instance().getFunctionNames<API::IBackgroundFunction>();
bkgdnames.push_back("None");
bkgdnames.push_back("Auto");
declareProperty("BackgroundFunction", "FlatBackground", boost::make_shared<StringListValidator>(bkgdnames),
"Background function to calculate. ");
#endif

declareProperty(new API::WorkspaceProperty<API::MatrixWorkspace>("InputWorkspace", "", Direction::Input, PropertyMode::Optional),
"InputWorkspace (optional) to take information for the instrument, and where to evaluate the x-axis.");

Expand All @@ -201,7 +188,11 @@ namespace Algorithms
declareProperty("IgnoreWidePeaks", false, "If selected, the peaks that are wider than fit window "
"(denoted by negative chi^2) are ignored.");

declareProperty("IsRawParameterTable", true, "Flag to show whether the parameter table contains raw parameters. ");
declareProperty("IsRawParameter", true,
"Flag to show whether the parameter table contains raw parameters. "
"In the case that parameter values are input via vector, and this flag is set to false, "
"the default order of effective peak parameters is centre, height and width; "
"the default order of effective background parameters is A0, A1 and A2. ");

return;
}
Expand Down Expand Up @@ -283,7 +274,7 @@ namespace Algorithms

m_genBackground = getProperty("GenerateBackground");

m_useRawParameter = getProperty("IsRawParameterTable");
m_useRawParameter = getProperty("IsRawParameter");

// Special properties related
m_maxChi2 = this->getProperty("MaxAllowedChi2");
Expand Down Expand Up @@ -419,39 +410,75 @@ namespace Algorithms
return;
}

//----------------------------------------------------------------------------------------------
/** Import one and only one function vector input
*/
void GeneratePeaks::importPeakFromVector(std::vector<std::pair<double, API::IFunction_sptr> >& functionmap)
{
API::CompositeFunction_sptr compfunc = boost::make_shared<API::CompositeFunction>();

// Set up and clone peak function
size_t numpeakparams = m_peakFunction->nParams();
if (m_vecPeakParamValues.size() != numpeakparams)
if (m_useRawParameter)
{
std::stringstream errss;
errss << "Number of input peak parameters' value (" << m_vecPeakParamValues.size() << ") is not correct (should be "
<< numpeakparams << " for peak of type " << m_peakFunction->name() << "). ";
throw std::runtime_error(errss.str());
// Input vector of values are for raw parameter name
size_t numpeakparams = m_peakFunction->nParams();
if (m_vecPeakParamValues.size() == numpeakparams)
{
for (size_t i = 0; i < numpeakparams; ++i)
m_peakFunction->setParameter(i, m_vecPeakParamValues[i]);
}
else
{
// Number of input parameter values is not correct. Throw!
std::stringstream errss;
errss << "Number of input peak parameters' value (" << m_vecPeakParamValues.size() << ") is not correct (should be "
<< numpeakparams << " for peak of type " << m_peakFunction->name() << "). ";
throw std::runtime_error(errss.str());
}
}
else
{
for (size_t i = 0; i < numpeakparams; ++i)
m_peakFunction->setParameter(i, m_vecPeakParamValues[i]);
// Input vector of values are for effective parameter names
if (m_vecPeakParamValues.size() != 3)
throw std::runtime_error("Input peak parameters must have 3 numbers for effective parameter names.");

m_peakFunction->setCentre(m_vecPeakParamValues[0]);
m_peakFunction->setHeight(m_vecPeakParamValues[1]);
m_peakFunction->setFwhm(m_vecPeakParamValues[2]);
}

compfunc->addFunction(m_peakFunction->clone());
g_log.notice() << "[***] Peak function: " << m_peakFunction->asString() << "\n";

// Set up and clone background function
if (m_genBackground)
{
size_t numbkgdparams = m_bkgdFunction->nParams();
if (m_vecBkgdParamValues.size() != numbkgdparams)
throw std::runtime_error("Number of background parameters' value is not correct. ");
if (m_useRawParameter)
{
// Raw background parameters
if (m_vecBkgdParamValues.size() != numbkgdparams)
throw std::runtime_error("Number of background parameters' value is not correct. ");
else
{
for (size_t i = 0; i < numbkgdparams; ++i)
m_bkgdFunction->setParameter(i, m_vecBkgdParamValues[i]);
}
}
else
{
// Effective background parameters
if (m_vecBkgdParamValues.size() < 3 && m_vecBkgdParamValues.size() < numbkgdparams)
{
throw std::runtime_error("There is no enough effective background parameter values.");
}

// FIXME - Assume that all background functions define parameter i for A_i
for (size_t i = 0; i < numbkgdparams; ++i)
m_bkgdFunction->setParameter(i, m_vecBkgdParamValues[i]);

}

compfunc->addFunction(m_bkgdFunction->clone());
}

Expand Down Expand Up @@ -483,6 +510,8 @@ namespace Algorithms
const std::vector<std::pair<double, API::IFunction_sptr> >& vec_centrefunc = mapiter->second;
size_t numpeaksinspec = mapiter->second.size();

g_log.notice() << "[***] Number of peaks to plot = " << numpeaksinspec << "\n";

for (size_t ipeak = 0; ipeak < numpeaksinspec; ++ipeak)
{
const std::pair<double, API::IFunction_sptr>& centrefunc = vec_centrefunc[ipeak];
Expand Down Expand Up @@ -529,9 +558,11 @@ namespace Algorithms
// Put to output
std::size_t offset = (left-X.begin());
std::size_t numY = values.size();
g_log.notice() << "[***] Number of Y = " << numY << "\n";
for (std::size_t i = 0; i < numY; i ++)
{
dataWS->dataY(wsindex)[i + offset] += values[i];
g_log.notice() << "[***] Y[" << i+offset << "] = " << values[i] << "\n";
}

} // ENDFOR(ipeak)
Expand Down
24 changes: 13 additions & 11 deletions Code/Mantid/Framework/Algorithms/test/GeneratePeaksTest.h
Expand Up @@ -37,7 +37,7 @@ class GeneratePeaksTest : public CxxTest::TestSuite
FrameworkManager::Instance();
}

void test_Init()
void Ptest_Init()
{
GeneratePeaks alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize());
Expand All @@ -59,7 +59,7 @@ class GeneratePeaksTest : public CxxTest::TestSuite
//----------------------------------------------------------------------------------------------
/** Test to use user-provided binning parameters and effective function parameters
*/
void test_UserBinningParameters()
void Ptest_UserBinningParameters()
{
// Create input parameter table workspace
DataObjects::TableWorkspace_sptr peakparmsws = createTestEffectiveFuncParameters();
Expand All @@ -76,7 +76,7 @@ class GeneratePeaksTest : public CxxTest::TestSuite
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("BinningParameters", "0.0, 0.01, 10.0"));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("OutputWorkspace", "Test01WS"));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("GenerateBackground", false));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("IsRawParameterTable", false));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("IsRawParameter", false));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("MaxAllowedChi2", 100.0));

// Execute
Expand Down Expand Up @@ -127,7 +127,7 @@ class GeneratePeaksTest : public CxxTest::TestSuite
//----------------------------------------------------------------------------------------------
/** Test algorithm by using an existing input workspace as X-values
*/
void test_FromInputWorkspace()
void Ptest_FromInputWorkspace()
{
// Create input
DataObjects::TableWorkspace_sptr peakparmsws = createTestPeakParameters2();
Expand Down Expand Up @@ -199,7 +199,7 @@ class GeneratePeaksTest : public CxxTest::TestSuite
//----------------------------------------------------------------------------------------------
/** Test to use user-provided binning parameters
*/
void test_Background()
void Ptest_Background()
{
// Create input
DataObjects::TableWorkspace_sptr peakparmsws = createTestPeakParameters3();
Expand All @@ -217,7 +217,7 @@ class GeneratePeaksTest : public CxxTest::TestSuite
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("OutputWorkspace", "Test03WS"));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("GenerateBackground", true));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("MaxAllowedChi2", 100.0));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("IsRawParameterTable", false));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("IsRawParameter", false));

// Execute
TS_ASSERT_THROWS_NOTHING(alg.execute());
Expand Down Expand Up @@ -264,7 +264,7 @@ class GeneratePeaksTest : public CxxTest::TestSuite
//----------------------------------------------------------------------------------------------
/** Test to input parameter values by vectors user-provided binning parameters
*/
void test_InputValueViaVector()
void Ptest_InputValueViaVector()
{
// Create vectors for peak and background parameters
std::string vecpeakvalue("5.0, 2.0, 0.0849322");
Expand All @@ -282,7 +282,7 @@ class GeneratePeaksTest : public CxxTest::TestSuite
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("BinningParameters", "0.0, 0.01, 10.0"));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("OutputWorkspace", "Test04WS"));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("GenerateBackground", false));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("IsRawParameterTable", true));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("IsRawParameter", true));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("MaxAllowedChi2", 100.0));

// Execute
Expand Down Expand Up @@ -319,10 +319,10 @@ class GeneratePeaksTest : public CxxTest::TestSuite
//----------------------------------------------------------------------------------------------
/** Test to input parameter values by vectors user-provided binning parameters
*/
void Xtest_InputValueViaVectorEffective()
void test_InputValueViaVectorEffective()
{
// Create vectors for peak and background parameters
std::string vecpeakvalue("5.0, 2.0, 0.02");
std::string vecpeakvalue("2.0, 5.0, 0.2");
std::string vecbkgdvalue("1.0, 2.0, 0.0");

// Initialize algorithm GenertePeaks
Expand All @@ -337,7 +337,7 @@ class GeneratePeaksTest : public CxxTest::TestSuite
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("BinningParameters", "0.0, 0.01, 10.0"));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("OutputWorkspace", "Test01WS"));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("GenerateBackground", false));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("IsRawParameterTable", false));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("IsRawParameter", false));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("MaxAllowedChi2", 100.0));

// Execute
Expand All @@ -358,6 +358,8 @@ class GeneratePeaksTest : public CxxTest::TestSuite
TS_ASSERT_DELTA(p0_x[200], 2.0, 1.0E-8);
TS_ASSERT_DELTA(p0_y[200], 5.0, 1.0E-4);

std::cout << "value 200: " << "x = " << p0_x[200] << ", y = " << p0_y[200] << "\n";

TS_ASSERT_DELTA(p0_x[201], 2.01, 1.0E-8);
TS_ASSERT_DELTA(p0_y[201], 4.96546, 1.0E-4);

Expand Down

0 comments on commit eed5c15

Please sign in to comment.