Skip to content

Commit

Permalink
Skeleton algorithm & test.
Browse files Browse the repository at this point in the history
Refs #8378
  • Loading branch information
martyngigg committed Nov 28, 2013
1 parent 7dfd416 commit 0b5f556
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Code/Mantid/Framework/CurveFitting/CMakeLists.txt
Expand Up @@ -11,6 +11,7 @@ set ( SRC_FILES
src/BivariateNormal.cpp
src/Bk2BkExpConvPV.cpp
src/BoundaryConstraint.cpp
src/CalculateGammaBackground.cpp
src/Chebyshev.cpp
src/ComptonProfile.cpp
src/ComptonScatteringCountRate.cpp
Expand Down Expand Up @@ -66,7 +67,7 @@ set ( SRC_FILES
src/Quadratic.cpp
src/RefinePowderInstrumentParameters.cpp
src/RefinePowderInstrumentParameters3.cpp
src/ReflectivityMulf.cpp
src/ReflectivityMulf.cpp
src/Resolution.cpp
src/SeqDomain.cpp
src/SimplexMinimizer.cpp
Expand Down Expand Up @@ -106,6 +107,7 @@ set ( INC_FILES
inc/MantidCurveFitting/BivariateNormal.h
inc/MantidCurveFitting/Bk2BkExpConvPV.h
inc/MantidCurveFitting/BoundaryConstraint.h
inc/MantidCurveFitting/CalculateGammaBackground.h
inc/MantidCurveFitting/Chebyshev.h
inc/MantidCurveFitting/CompositeValues.h
inc/MantidCurveFitting/ComptonProfile.h
Expand Down Expand Up @@ -167,7 +169,7 @@ set ( INC_FILES
inc/MantidCurveFitting/Quadratic.h
inc/MantidCurveFitting/RefinePowderInstrumentParameters.h
inc/MantidCurveFitting/RefinePowderInstrumentParameters3.h
inc/MantidCurveFitting/ReflectivityMulf.h
inc/MantidCurveFitting/ReflectivityMulf.h
inc/MantidCurveFitting/Resolution.h
inc/MantidCurveFitting/SeqDomain.h
inc/MantidCurveFitting/SimplexMinimizer.h
Expand Down Expand Up @@ -205,6 +207,7 @@ set ( TEST_FILES
BivariateNormalTest.h
Bk2BkExpConvPVTest.h
BoundaryConstraintTest.h
CalculateGammaBackgroundTest.h
ChebyshevTest.h
CompositeFunctionTest.h
ComptonProfileTest.h
Expand Down Expand Up @@ -252,7 +255,7 @@ set ( TEST_FILES
ProductQuadraticExpTest.h
QuadraticTest.h
RefinePowderInstrumentParameters3Test.h
ReflectivityMulfTest.h
ReflectivityMulfTest.h
ResolutionTest.h
SimplexTest.h
SpecialFunctionSupportTest.h
Expand Down
@@ -0,0 +1,70 @@
#ifndef MANTID_CURVEFITTING_CALCULATEGAMMABACKGROUND_H_
#define MANTID_CURVEFITTING_CALCULATEGAMMABACKGROUND_H_

#include "MantidAPI/Algorithm.h"

namespace Mantid
{
namespace CurveFitting
{

/**
Copyright © 2013 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport CalculateGammaBackground : public API::Algorithm
{
public:
CalculateGammaBackground();

private:

const std::string name() const;
int version() const;
const std::string category() const;

void initDocs();
void init();
void exec();

/// Check and store appropriate input data
void retrieveInputs();
/// Create the output workspaces
void createOutputWorkspaces();
/// Calculate & correct the given index of the input workspace
void applyCorrection(const size_t index);

API::MatrixWorkspace_const_sptr m_inputWS;
size_t m_npeaks;
std::vector<double> m_masses;
std::vector<double> m_amplitudes;
std::vector<double> m_widths;

API::MatrixWorkspace_sptr m_backgroundWS;
API::MatrixWorkspace_sptr m_correctedWS;

};


} // namespace CurveFitting
} // namespace Mantid

#endif /* MANTID_CURVEFITTING_CALCULATEGAMMABACKGROUND_H_ */
134 changes: 134 additions & 0 deletions Code/Mantid/Framework/CurveFitting/src/CalculateGammaBackground.cpp
@@ -0,0 +1,134 @@
/*WIKI*
*WIKI*/
#include "MantidCurveFitting/CalculateGammaBackground.h"
#include "MantidAPI/WorkspaceValidators.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/MandatoryValidator.h"


namespace Mantid
{
namespace CurveFitting
{
//--------------------------------------------------------------------------------------------------------
// Public members
//--------------------------------------------------------------------------------------------------------

/// Default constructor
CalculateGammaBackground::CalculateGammaBackground()
: Algorithm(), m_inputWS(), m_npeaks(0), m_masses(), m_amplitudes(), m_widths(),
m_backgroundWS(), m_correctedWS()
{
}

//--------------------------------------------------------------------------------------------------------
// Private members
//--------------------------------------------------------------------------------------------------------

const std::string CalculateGammaBackground::name() const
{
return "CalculateGammaBackground";
}

int CalculateGammaBackground::version() const
{
return 1;
}

const std::string CalculateGammaBackground::category() const
{
return "CorrectionFunctions";
}

void CalculateGammaBackground::initDocs()
{
this->setWikiSummary("Calculates and the background due to gamma rays produced when neutrons are absorbed by shielding");
this->setOptionalMessage("Calculates the background due to gamma rays produced when neutrons are absorbed by shielding.");
}

void CalculateGammaBackground::init()
{
using namespace API;
using namespace Kernel;

declareProperty(new WorkspaceProperty<>("InputWorkspace", "", Direction::Input,
boost::make_shared<WorkspaceUnitValidator>("TOF")),
"An input workspace containing TOF data");

// Peak information
const std::string grpName = "Peaks";
typedef MandatoryValidator<std::vector<double>> NonEmptyDoubleArray;
declareProperty(new ArrayProperty<double>("Masses",boost::make_shared<NonEmptyDoubleArray>()),
"List of mass values for each peak in the input spectrum in atomic mass units");
setPropertyGroup("Masses", grpName);
declareProperty(new ArrayProperty<double>("PeakAmplitudes",boost::make_shared<NonEmptyDoubleArray>()),
"List of amplitudes of the peaks in the same order as the Masses property");
setPropertyGroup("PeakAmplitudes", grpName);
declareProperty(new ArrayProperty<double>("PeakWidths",boost::make_shared<NonEmptyDoubleArray>()),
"List of std deviations of peak widths in the same order as the Masses property");
setPropertyGroup("PeakWidths", grpName);

declareProperty(new WorkspaceProperty<>("BackgroundWorkspace", "", Direction::Output));
declareProperty(new WorkspaceProperty<>("CorrectedWorkspace", "", Direction::Output));
}

void CalculateGammaBackground::exec()
{
retrieveInputs();
createOutputWorkspaces();

const size_t nhist = m_inputWS->getNumberHistograms();
for(size_t i = 0; i < nhist; ++i)
{
applyCorrection(i);
}

setProperty("BackgroundWorkspace",m_backgroundWS);
setProperty("CorrectedWorkspace",m_correctedWS);
}

/**
* Caches input details for the peak information
*/
void CalculateGammaBackground::retrieveInputs()
{
m_inputWS = getProperty("InputWorkspace");
m_masses = getProperty("Masses");
m_amplitudes = getProperty("PeakAmplitudes");
m_widths = getProperty("PeakWidths");
m_npeaks = m_masses.size();

if(m_masses.size() != m_amplitudes.size() || m_masses.size() != m_widths.size())
{
std::ostringstream os;
os << "Invalid peak inputs: No. of masses=" << m_masses.size()
<< ", No. of amplitudes=" << m_amplitudes.size() << ", No. of peaks=" << m_widths.size();
throw std::invalid_argument(os.str());
}
}

/**
* Create & cache output workspaces
*/
void CalculateGammaBackground::createOutputWorkspaces()
{
using namespace API;
m_backgroundWS = WorkspaceFactory::Instance().create(m_inputWS);
m_correctedWS = WorkspaceFactory::Instance().create(m_inputWS);
}

/**
* Calculate & apply gamma correction for the given index of the
* input workspace
* @param index A workspace index that defines the spectrum to correct
*/
void CalculateGammaBackground::applyCorrection(const size_t index)
{

}

} // namespace CurveFitting
} // namespace Mantid
@@ -0,0 +1,95 @@
#ifndef MANTID_CURVEFITTING_CALCULATEGAMMABACKGROUNDTEST_H_
#define MANTID_CURVEFITTING_CALCULATEGAMMABACKGROUNDTEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidCurveFitting/CalculateGammaBackground.h"
#include "ComptonProfileTestHelpers.h"

using Mantid::CurveFitting::CalculateGammaBackground;

class CalculateGammaBackgroundTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static CalculateGammaBackgroundTest *createSuite() { return new CalculateGammaBackgroundTest(); }
static void destroySuite( CalculateGammaBackgroundTest *suite ) { delete suite; }

//------------------------------------ Success cases ---------------------------------------
void test_Input_With_Single_Mass_Gives_Expected_Output_Workspaces()
{
auto alg = createAlgorithm();
alg->setRethrows(true);

double x0(165.0),x1(166.0),dx(0.5);
alg->setProperty("InputWorkspace",ComptonProfileTestHelpers::createSingleSpectrumTestWorkspace(x0,x1,dx));

alg->setPropertyValue("Masses", "1.0079");
alg->setPropertyValue("PeakAmplitudes", "2.9e-2");
alg->setPropertyValue("PeakWidths", "4.29");

TS_ASSERT_THROWS_NOTHING(alg->execute());
TS_ASSERT(alg->isExecuted());

using namespace Mantid::API;
MatrixWorkspace_sptr backgroundWS = alg->getProperty("BackgroundWorkspace");
MatrixWorkspace_sptr correctedWS = alg->getProperty("CorrectedWorkspace");
TS_ASSERT(backgroundWS);
TS_ASSERT(correctedWS);
TS_ASSERT(backgroundWS != correctedWS);
}


//------------------------------------ Error cases ---------------------------------------

void test_Peak_Information_Lists_Of_Zero_Length_Throw_An_Error()
{
auto alg = createAlgorithm();
alg->setRethrows(true);

double x0(165.0),x1(166.0),dx(0.5);
alg->setProperty("InputWorkspace",ComptonProfileTestHelpers::createSingleSpectrumTestWorkspace(x0,x1,dx));

// None set=all empty
TS_ASSERT_THROWS(alg->execute(), std::runtime_error);
// Just set masses
alg->setPropertyValue("Masses", "1,2,3");
TS_ASSERT_THROWS(alg->execute(), std::runtime_error);
// now amplitudes
alg->setPropertyValue("PeakAmplitudes", "1,2,3");
TS_ASSERT_THROWS(alg->execute(), std::runtime_error);
// now widths
alg->setPropertyValue("PeakWidths", "1,2,3");
TS_ASSERT_THROWS_NOTHING(alg->execute());
}

void test_Peak_Information_Lists_Of_Different_Lengths_Throws_Error()
{
auto alg = createAlgorithm();
alg->setRethrows(true);
double x0(165.0),x1(166.0),dx(0.5);
alg->setProperty("InputWorkspace",ComptonProfileTestHelpers::createSingleSpectrumTestWorkspace(x0,x1,dx));

alg->setProperty("Masses", "1,2,3");
alg->setProperty("PeakAmplitudes", "1,2");
alg->setProperty("PeakWidths", "1,2,3,4");
TS_ASSERT_THROWS(alg->execute(), std::invalid_argument);
}

private:

Mantid::API::IAlgorithm_sptr createAlgorithm()
{
Mantid::API::IAlgorithm_sptr alg = boost::make_shared<CalculateGammaBackground>();
alg->initialize();
alg->setChild(true);
alg->setPropertyValue("CorrectedWorkspace", "__UNUSED__");
alg->setPropertyValue("BackgroundWorkspace", "__UNUSED__");
return alg;
}

};


#endif /* MANTID_ALGORITHMS_CalculateGammaBackgroundTEST_H_ */
Expand Up @@ -21,6 +21,7 @@ namespace ComptonProfileTestHelpers
bool isHist(false);

auto ws2d = WorkspaceCreationHelper::Create2DWorkspaceFromFunction(ones(), nhist, x0,x1,dx,isHist);
ws2d->getAxis(0)->setUnit("TOF");
// Requires an instrument.
auto inst = boost::make_shared<Instrument>();
ws2d->setInstrument(inst);
Expand Down

0 comments on commit 0b5f556

Please sign in to comment.