Skip to content

Commit

Permalink
Refactor test helper to allow multiple spectra.
Browse files Browse the repository at this point in the history
Refs #8598
  • Loading branch information
martyngigg committed Dec 16, 2013
1 parent d3a5546 commit 0e5969b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 38 deletions.
Expand Up @@ -17,7 +17,7 @@ class CalculateGammaBackgroundTest : public CxxTest::TestSuite
static void destroySuite( CalculateGammaBackgroundTest *suite ) { delete suite; }

//------------------------------------ Success cases ---------------------------------------
void xtest_Input_With_Spectrum_Number_Inside_Forward_Scatter_Range_Gives_Expected_Correction()
void test_Input_With_Spectrum_Number_Inside_Forward_Scatter_Range_Gives_Expected_Correction()
{
using namespace Mantid::API;
auto inputWS = createTestWorkspaceWithFoilChanger(); //specNo=1
Expand Down Expand Up @@ -148,7 +148,7 @@ class CalculateGammaBackgroundTest : public CxxTest::TestSuite

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

void xtest_Empty_Function_Property_Throws_Error()
void test_Empty_Function_Property_Throws_Error()
{
auto alg = createAlgorithm();
alg->setRethrows(true);
Expand All @@ -158,7 +158,7 @@ class CalculateGammaBackgroundTest : public CxxTest::TestSuite
TS_ASSERT(!alg->isExecuted());
}

void xtest_Function_Property_With_Single_Non_ComptonProfile_Throws_Error()
void test_Function_Property_With_Single_Non_ComptonProfile_Throws_Error()
{
auto alg = createAlgorithm();
alg->setRethrows(true);
Expand All @@ -169,7 +169,7 @@ class CalculateGammaBackgroundTest : public CxxTest::TestSuite
TS_ASSERT(!alg->isExecuted());
}

void xtest_Function_Property_With_Composite_Non_ComptonProfile_Throws_Error()
void test_Function_Property_With_Composite_Non_ComptonProfile_Throws_Error()
{
auto alg = createAlgorithm();
alg->setRethrows(true);
Expand Down Expand Up @@ -210,19 +210,19 @@ class CalculateGammaBackgroundTest : public CxxTest::TestSuite
Mantid::API::MatrixWorkspace_sptr createTestWorkspaceWithFoilChanger()
{
double x0(50.0),x1(300.0),dx(0.5);
return ComptonProfileTestHelpers::createSingleSpectrumWorkspace(x0,x1,dx, true,true);
return ComptonProfileTestHelpers::createTestWorkspace(1,x0,x1,dx, true,true);
}

Mantid::API::MatrixWorkspace_sptr createTestWorkspaceWithNoFoilChanger()
{
double x0(165.0),x1(166.0),dx(0.5);
return ComptonProfileTestHelpers::createSingleSpectrumWorkspace(x0,x1,dx,false);
return ComptonProfileTestHelpers::createTestWorkspace(1,x0,x1,dx,false);
}

Mantid::API::MatrixWorkspace_sptr createTwoSpectrumWorkspaceWithFoilChanger()
{
double x0(50.0),x1(300.0),dx(0.5);
auto singleSpectrum = ComptonProfileTestHelpers::createSingleSpectrumWorkspace(x0,x1,dx, true,true);
auto singleSpectrum = ComptonProfileTestHelpers::createTestWorkspace(1,x0,x1,dx, true,true);
const size_t nhist = 2;
auto twoSpectrum = Mantid::API::WorkspaceFactory::Instance().create(singleSpectrum, nhist);
// Copy data
Expand Down
Expand Up @@ -71,7 +71,7 @@ class ComptonPeakProfileTest : public CxxTest::TestSuite
{
Mantid::API::IFunction_sptr profile = boost::make_shared<ComptonPeakProfile>();
profile->initialize();
auto paramWS = ComptonProfileTestHelpers::createSingleSpectrumWorkspace(300,351,0.5,true,true); // Only using for parameters
auto paramWS = ComptonProfileTestHelpers::createTestWorkspace(1,300,351,0.5,true,true); // Only using for parameters
profile->setAttributeValue("Mass", 1.0079);
TS_ASSERT_THROWS_NOTHING(profile->setWorkspace(paramWS));
profile->setUpForFit();
Expand Down
44 changes: 24 additions & 20 deletions Code/Mantid/Framework/CurveFitting/test/ComptonProfileTestHelpers.h
Expand Up @@ -11,9 +11,9 @@
namespace ComptonProfileTestHelpers
{
// Forward declare all functions
static Mantid::API::MatrixWorkspace_sptr createSingleSpectrumWorkspace(const double x0, const double x1,
const double dx, const bool singleMassSpectrum = false,
const bool addFoilChanger = false);
static Mantid::API::MatrixWorkspace_sptr createTestWorkspace(const size_t nhist,const double x0, const double x1,
const double dx, const bool singleMassSpectrum = false,
const bool addFoilChanger = false);
static Mantid::Geometry::Instrument_sptr createTestInstrumentWithFoilChanger(const Mantid::detid_t id,const Mantid::Kernel::V3D &);
static Mantid::Geometry::Instrument_sptr createTestInstrumentWithNoFoilChanger(const Mantid::detid_t id,
const Mantid::Kernel::V3D &);
Expand All @@ -28,13 +28,11 @@ namespace ComptonProfileTestHelpers
};

static Mantid::API::MatrixWorkspace_sptr
createSingleSpectrumWorkspace(const double x0, const double x1, const double dx,
const bool singleMassSpectrum,const bool addFoilChanger)
createTestWorkspace(const size_t nhist, const double x0, const double x1, const double dx,
const bool singleMassSpectrum,const bool addFoilChanger)
{
int nhist(1);
bool isHist(false);

auto ws2d = WorkspaceCreationHelper::Create2DWorkspaceFromFunction(ones(), nhist, x0,x1,dx,isHist);
auto ws2d = WorkspaceCreationHelper::Create2DWorkspaceFromFunction(ones(), static_cast<int>(nhist), x0,x1,dx,isHist);
ws2d->getAxis(0)->setUnit("TOF");
if(singleMassSpectrum)
{
Expand All @@ -43,14 +41,17 @@ namespace ComptonProfileTestHelpers
const double peakCentre(164.0), sigmaSq(16*16), peakHeight(0.2);
const double noise(0.02);
Mantid::Kernel::MersenneTwister mt1998(123456);
for(size_t i = 0; i < nvalues; ++i)
for(size_t i = 0; i < nhist; ++i)
{
double x= ws2d->dataX(0)[i];
double y = peakHeight * exp(-0.5*pow(x - peakCentre, 2.)/sigmaSq);
double r = mt1998.nextValue();
if(r > 0.5) y += noise*r;
else y -= noise*r;
ws2d->dataY(0)[i] = y;
for(size_t j = 0; j < nvalues; ++j)
{
double x= ws2d->dataX(i)[j];
double y = peakHeight * exp(-0.5*pow(x - peakCentre, 2.)/sigmaSq);
double r = mt1998.nextValue();
if(r > 0.5) y += noise*r;
else y -= noise*r;
ws2d->dataY(i)[j] = y;
}
}
}

Expand Down Expand Up @@ -78,11 +79,14 @@ namespace ComptonProfileTestHelpers
}

// Link workspace with detector
auto *spec0 = ws2d->getSpectrum(0);
spec0->setSpectrumNo(1);
spec0->clearDetectorIDs();
spec0->addDetectorID(id);

for(size_t i = 0; i < nhist; ++i)
{
const Mantid::specid_t specID = static_cast<Mantid::specid_t>(id + i);
auto *spec = ws2d->getSpectrum(i);
spec->setSpectrumNo(specID);
spec->clearDetectorIDs();
spec->addDetectorID(id);
}
return ws2d;
}

Expand Down
Expand Up @@ -56,7 +56,7 @@ class ComptonScatteringCountRateTest : public CxxTest::TestSuite
using namespace Mantid::API;
IFunction_sptr func = createFunctionNoBackground(); // No equality matrix set
double x0(165.0),x1(166.0),dx(0.5);
auto testWS = ComptonProfileTestHelpers::createSingleSpectrumWorkspace(x0,x1,dx);
auto testWS = ComptonProfileTestHelpers::createTestWorkspace(1,x0,x1,dx);

TS_ASSERT_THROWS_NOTHING(func->setWorkspace(testWS));
}
Expand All @@ -66,7 +66,7 @@ class ComptonScatteringCountRateTest : public CxxTest::TestSuite
using namespace Mantid::API;
IFunction_sptr func = createFunctionNoBackground();
double x0(165.0),x1(166.0),dx(0.5);
auto testWS = ComptonProfileTestHelpers::createSingleSpectrumWorkspace(x0,x1,dx);
auto testWS = ComptonProfileTestHelpers::createTestWorkspace(1,x0,x1,dx);
auto & dataX = testWS->dataX(0);
std::transform(dataX.begin(), dataX.end(), dataX.begin(), std::bind2nd(std::multiplies<double>(),1e-06)); // to seconds
func->setWorkspace(testWS);
Expand All @@ -86,7 +86,7 @@ class ComptonScatteringCountRateTest : public CxxTest::TestSuite
using namespace Mantid::API;
IFunction_sptr func = createFunctionWithBackground();
double x0(165.0),x1(166.0),dx(0.5);
auto testWS = ComptonProfileTestHelpers::createSingleSpectrumWorkspace(x0,x1,dx);
auto testWS = ComptonProfileTestHelpers::createTestWorkspace(1,x0,x1,dx);
auto & dataX = testWS->dataX(0);
std::transform(dataX.begin(), dataX.end(), dataX.begin(), std::bind2nd(std::multiplies<double>(),1e-06)); // to seconds
func->setWorkspace(testWS);
Expand All @@ -108,7 +108,7 @@ class ComptonScatteringCountRateTest : public CxxTest::TestSuite
using namespace Mantid::API;
IFunction_sptr func = createFunctionNoBackground();
double x0(165.0),x1(166.0),dx(0.5);
auto testWS = ComptonProfileTestHelpers::createSingleSpectrumWorkspace(x0,x1,dx);
auto testWS = ComptonProfileTestHelpers::createTestWorkspace(1,x0,x1,dx);
auto & dataX = testWS->dataX(0);
std::transform(dataX.begin(), dataX.end(), dataX.begin(), std::bind2nd(std::multiplies<double>(),1e-06)); // to seconds
func->setWorkspace(testWS);
Expand All @@ -127,7 +127,7 @@ class ComptonScatteringCountRateTest : public CxxTest::TestSuite
func->setAttributeValue("IntensityConstraints", "Matrix(1|2)1|-2"); // I_1 = 2I_2

double x0(165.0),x1(166.0),dx(0.5);
auto testWS = ComptonProfileTestHelpers::createSingleSpectrumWorkspace(x0,x1,dx);
auto testWS = ComptonProfileTestHelpers::createTestWorkspace(1,x0,x1,dx);
auto & dataX = testWS->dataX(0);
std::transform(dataX.begin(), dataX.end(), dataX.begin(), std::bind2nd(std::multiplies<double>(),1e-06)); // to seconds
func->setWorkspace(testWS);
Expand All @@ -146,7 +146,7 @@ class ComptonScatteringCountRateTest : public CxxTest::TestSuite
IFunction_sptr func = createFunctionNoBackground(useTwoIntensityFuncAsFirst);

double x0(165.0),x1(166.0),dx(0.5);
auto testWS = ComptonProfileTestHelpers::createSingleSpectrumWorkspace(x0,x1,dx);
auto testWS = ComptonProfileTestHelpers::createTestWorkspace(1,x0,x1,dx);
auto & dataX = testWS->dataX(0);
std::transform(dataX.begin(), dataX.end(), dataX.begin(), std::bind2nd(std::multiplies<double>(),1e-06)); // to seconds

Expand All @@ -168,7 +168,7 @@ class ComptonScatteringCountRateTest : public CxxTest::TestSuite
func->setAttributeValue("IntensityConstraints", "Matrix(1|2)1|-2"); // I_1 = 2I_2

double x0(165.0),x1(166.0),dx(0.5);
auto testWS = ComptonProfileTestHelpers::createSingleSpectrumWorkspace(x0,x1,dx);
auto testWS = ComptonProfileTestHelpers::createTestWorkspace(1,x0,x1,dx);
auto & dataX = testWS->dataX(0);
std::transform(dataX.begin(), dataX.end(), dataX.begin(), std::bind2nd(std::multiplies<double>(),1e-06)); // to seconds

Expand Down
Expand Up @@ -31,7 +31,7 @@ class ConvertToYSpaceTest : public CxxTest::TestSuite

auto alg = createAlgorithm();
double x0(50.0),x1(300.0),dx(0.5);
auto testWS = ComptonProfileTestHelpers::createSingleSpectrumWorkspace(x0,x1,dx, true,true);
auto testWS = ComptonProfileTestHelpers::createTestWorkspace(1,x0,x1,dx, true,true);
alg->setProperty("InputWorkspace", testWS);
alg->setProperty("Mass", 1.0097);
alg->execute();
Expand Down
Expand Up @@ -82,7 +82,7 @@ class GaussianComptonProfileTest : public CxxTest::TestSuite

auto func = createFunctionWithParamsSet();
double x0(370.0),x1(371.0),dx(0.5); //chosen to give put us near the peak for this mass & spectrum
auto testWS = ComptonProfileTestHelpers::createSingleSpectrumWorkspace(x0,x1,dx);
auto testWS = ComptonProfileTestHelpers::createTestWorkspace(1,x0,x1,dx);
auto & dataX = testWS->dataX(0);
std::transform(dataX.begin(), dataX.end(), dataX.begin(), std::bind2nd(std::multiplies<double>(),1e-06)); // to seconds
func->setWorkspace(testWS);
Expand Down
Expand Up @@ -79,7 +79,7 @@ class GramCharlierComptonProfileTest : public CxxTest::TestSuite

auto func = createFunctionWithParamsSet();
double x0(165.0),x1(166.0),dx(0.5); //chosen to give put us near the peak for this mass & spectrum
auto testWS = ComptonProfileTestHelpers::createSingleSpectrumWorkspace(x0,x1,dx);
auto testWS = ComptonProfileTestHelpers::createTestWorkspace(1,x0,x1,dx);
auto & dataX = testWS->dataX(0);
std::transform(dataX.begin(), dataX.end(), dataX.begin(), std::bind2nd(std::multiplies<double>(),1e-06)); // to seconds
func->setWorkspace(testWS);
Expand Down

0 comments on commit 0e5969b

Please sign in to comment.