Skip to content

Commit

Permalink
Added some basic unit tests
Browse files Browse the repository at this point in the history
Refs #11833
  • Loading branch information
DanNixon committed May 28, 2015
1 parent 1c14b88 commit 4bcd7b7
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 8 deletions.
39 changes: 33 additions & 6 deletions Code/Mantid/Framework/Algorithms/src/VesuvioL1ThetaResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ void VesuvioL1ThetaResolution::init() {
declareProperty("SpectrumMax", 198,
"Index of maximum spectrum");

declareProperty("NumEvents", 10000, positiveInt,
declareProperty("NumEvents", 1000000, positiveInt,
"Number of scattering events");
declareProperty("Seed", 123456789, positiveInt,
"Seed for random number generator");

declareProperty("L1BinWidth", 0.01, positiveDouble,
declareProperty("L1BinWidth", 0.05, positiveDouble,
"Bin width for L1 distribution.");
declareProperty("ThetaBinWidth", 0.01, positiveDouble,
declareProperty("ThetaBinWidth", 0.05, positiveDouble,
"Bin width for theta distribution.");

declareProperty(
Expand Down Expand Up @@ -138,12 +138,31 @@ void VesuvioL1ThetaResolution::exec() {

// Create output workspaces for distributions if required
if(!l1DistributionWsName.empty()) {
m_l1DistributionWs = WorkspaceFactory::Instance().create("Workspace2D", numHist, numEvents, numEvents);
m_l1DistributionWs = WorkspaceFactory::Instance().create(m_instWorkspace, numHist, numEvents, numEvents);

// Set Y axis
m_l1DistributionWs->setYUnitLabel("Events");

// Set X axis
auto xAxis = m_l1DistributionWs->getAxis(0);
xAxis->setUnit("Label");
auto labelUnit = boost::dynamic_pointer_cast<Units::Label>(xAxis->unit());
if(labelUnit)
labelUnit->setLabel("l1");
}

if(!thetaDistributionWsName.empty()) {
m_thetaDistributionWs = WorkspaceFactory::Instance().create("Workspace2D", numHist, numEvents, numEvents);
m_thetaDistributionWs = WorkspaceFactory::Instance().create(m_instWorkspace, numHist, numEvents, numEvents);

// Set Y axis
m_thetaDistributionWs->setYUnitLabel("Events");

// Set X axis
auto xAxis = m_thetaDistributionWs->getAxis(0);
xAxis->setUnit("Label");
auto labelUnit = boost::dynamic_pointer_cast<Units::Label>(xAxis->unit());
if(labelUnit)
labelUnit->setLabel("theta");
}

// Set up progress reporting
Expand All @@ -159,7 +178,7 @@ void VesuvioL1ThetaResolution::exec() {
std::stringstream report;
report << "Detector " << det->getID();
prog.report(report.str());
g_log.information() << "Detector " << det->getID() << std::endl;
g_log.information() << "Detector ID " << det->getID() << std::endl;

// Do simulation
calculateDetector(det, l1, theta);
Expand Down Expand Up @@ -193,6 +212,10 @@ void VesuvioL1ThetaResolution::exec() {
std::copy(l1.begin(), l1.end(), x.begin());

m_l1DistributionWs->dataY(i) = y;

auto spec = m_l1DistributionWs->getSpectrum(i);
spec->setSpectrumNo(specNo);
spec->addDetectorID(det->getID());
}

// Process data for theta distribution
Expand All @@ -204,6 +227,10 @@ void VesuvioL1ThetaResolution::exec() {
std::copy(theta.begin(), theta.end(), x.begin());

m_thetaDistributionWs->dataY(i) = y;

auto spec = m_thetaDistributionWs->getSpectrum(i);
spec->setSpectrumNo(specNo);
spec->addDetectorID(det->getID());
}
}

Expand Down
113 changes: 112 additions & 1 deletion Code/Mantid/Framework/Algorithms/test/VesuvioL1ThetaResolutionTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

#include "MantidAlgorithms/VesuvioL1ThetaResolution.h"

#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/SpectraAxis.h"
#include "MantidAPI/TextAxis.h"

using Mantid::Algorithms::VesuvioL1ThetaResolution;
using namespace Mantid::API;

Expand Down Expand Up @@ -35,6 +39,7 @@ class VesuvioL1ThetaResolutionTest : public CxxTest::TestSuite {
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", outWSName) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("NumEvents", 1000) );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );

Expand All @@ -43,7 +48,113 @@ class VesuvioL1ThetaResolutionTest : public CxxTest::TestSuite {
TS_ASSERT(ws);
if (!ws) return;

// TODO: Check the results
NumericAxis * xAxis = dynamic_cast<NumericAxis *>(ws->getAxis(0));
TS_ASSERT( xAxis );
if(xAxis)
{
TS_ASSERT_EQUALS( xAxis->length(), 196 );
TS_ASSERT_EQUALS( xAxis->getValue(0), 3 );
TS_ASSERT_EQUALS( xAxis->getValue(xAxis->length() - 1), 198 );
TS_ASSERT_EQUALS( xAxis->unit()->unitID(), "Label" );
TS_ASSERT_EQUALS( xAxis->unit()->caption(), "Spectrum Number" );
}

TextAxis * vAxis = dynamic_cast<TextAxis *>(ws->getAxis(1));
TS_ASSERT( vAxis );
if(vAxis)
{
TS_ASSERT_EQUALS( vAxis->length(), 4 );
}

// Remove workspace from the data service.
AnalysisDataService::Instance().remove(outWSName);
}


/**
* Tests execution outputting the L1 distribution workspace.
*/
void test_runL1Distribution()
{
// Name of the output workspace.
std::string outWSName("VesuvioL1ThetaResolutionTest_OutputWS");
std::string l1WSName("VesuvioL1ThetaResolutionTest_OutputWS_L1");

VesuvioL1ThetaResolution alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", outWSName) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("L1Distribution", l1WSName) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("NumEvents", 1000) );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );

MatrixWorkspace_sptr ws;
TS_ASSERT_THROWS_NOTHING( ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(l1WSName) );
TS_ASSERT(ws);
if (!ws) return;

NumericAxis * xAxis = dynamic_cast<NumericAxis *>(ws->getAxis(0));
TS_ASSERT( xAxis );
if(xAxis)
{
TS_ASSERT_EQUALS( xAxis->unit()->unitID(), "Label" );
TS_ASSERT_EQUALS( xAxis->unit()->caption(), "l1" );
}

SpectraAxis * vAxis = dynamic_cast<SpectraAxis *>(ws->getAxis(1));
TS_ASSERT( vAxis );
if(vAxis)
{
TS_ASSERT_EQUALS( vAxis->length(), 196 );
TS_ASSERT_EQUALS( vAxis->getValue(0), 3 );
TS_ASSERT_EQUALS( vAxis->getValue(vAxis->length() - 1), 198 );
}

// Remove workspace from the data service.
AnalysisDataService::Instance().remove(outWSName);
}


/**
* Tests execution outputting the theta distribution workspace.
*/
void test_runThetaDistribution()
{
// Name of the output workspace.
std::string outWSName("VesuvioL1ThetaResolutionTest_OutputWS");
std::string thetaWSName("VesuvioL1ThetaResolutionTest_OutputWS_Theta");

VesuvioL1ThetaResolution alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", outWSName) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("ThetaDistribution", thetaWSName) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("NumEvents", 1000) );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );

MatrixWorkspace_sptr ws;
TS_ASSERT_THROWS_NOTHING( ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(thetaWSName) );
TS_ASSERT(ws);
if (!ws) return;

NumericAxis * xAxis = dynamic_cast<NumericAxis *>(ws->getAxis(0));
TS_ASSERT( xAxis );
if(xAxis)
{
TS_ASSERT_EQUALS( xAxis->unit()->unitID(), "Label" );
TS_ASSERT_EQUALS( xAxis->unit()->caption(), "theta" );
}

SpectraAxis * vAxis = dynamic_cast<SpectraAxis *>(ws->getAxis(1));
TS_ASSERT( vAxis );
if(vAxis)
{
TS_ASSERT_EQUALS( vAxis->length(), 196 );
TS_ASSERT_EQUALS( vAxis->getValue(0), 3 );
TS_ASSERT_EQUALS( vAxis->getValue(vAxis->length() - 1), 198 );
}

// Remove workspace from the data service.
AnalysisDataService::Instance().remove(outWSName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Usage

.. testcode:: VesuvioL1ThetaResolutionExample

resolution, l1_dist, theta_dist = VesuvioL1ThetaResolution()
resolution, l1_dist, theta_dist = VesuvioL1ThetaResolution(NumEvents=1000)

resolution_spec_names = resolution.getAxis(1).extractValues()
print "Resolution spectra: %s" % (', '.join(resolution_spec_names))
Expand Down

0 comments on commit 4bcd7b7

Please sign in to comment.