Newer
Older
#ifndef WBVMEDIANTESTTEST_H_
#define WBVMEDIANTESTTEST_H_
#include <cxxtest/TestSuite.h>
Gigg, Martyn Anthony
committed
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
Steve Williams
committed
#include "MantidAlgorithms/MedianDetectorTest.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/Workspace2D.h"
Peterson, Peter
committed
#include "MantidDataHandling/LoadInstrument.h"
//#include "MantidDataHandling/LoadEmptyInstrument.h"
Peterson, Peter
committed
#include <boost/shared_ptr.hpp>
#include <boost/lexical_cast.hpp>
#include <Poco/File.h>
#include <Poco/Path.h>
Peterson, Peter
committed
#include <cmath>
Steve Williams
committed
#include <iostream>
#include <sstream>
#include <fstream>
Steve Williams
committed
#include <ios>
#include <string>
using namespace Mantid::Kernel;
using namespace Mantid::Geometry;
using namespace Mantid::API;
using namespace Mantid::Algorithms;
using namespace Mantid::DataObjects;
const int THEMASKED(40);
const int SAVEDBYERRORBAR(143);
const int Nhist(144);
//these values must match the values in MedianDetectorTest.h
const double BAD_VAL(1.);
const double GOOD_VAL(0.);
Steve Williams
committed
class MedianDetectorTestTest : public CxxTest::TestSuite
{
public:
Steve Williams
committed
Michael Whitty
committed
static MedianDetectorTestTest *createSuite() { return new MedianDetectorTestTest(); }
static void destroySuite(MedianDetectorTestTest *suite) { delete suite; }
void testWorkspaceAndArray()
Steve Williams
committed
{
Peterson, Peter
committed
MedianDetectorTest alg;
Gigg, Martyn Anthony
committed
TS_ASSERT_EQUALS( alg.name(), "MedianDetectorTest" );
TS_ASSERT_EQUALS( alg.version(), 1 );
// The spectra were setup in the constructor and passed to our algorithm through this function
TS_ASSERT_THROWS_NOTHING(TS_ASSERT( runInit(alg) ));
Steve Williams
committed
Peterson, Peter
committed
alg.setProperty("SignificanceTest", 1.0);
Gigg, Martyn Anthony
committed
// These are realistic values that I just made up
Peterson, Peter
committed
alg.setProperty( "LowThreshold", 0.5 );
alg.setProperty( "HighThreshold", 1.3333 );
Steve Williams
committed
Steve Williams
committed
TS_ASSERT_THROWS_NOTHING( alg.execute());
TS_ASSERT( alg.isExecuted() );
Workspace_sptr output;
Steve Williams
committed
TS_ASSERT_THROWS_NOTHING(output = AnalysisDataService::Instance().retrieve("MedianDetectorTestOutput"));
Gigg, Martyn Anthony
committed
Steve Williams
committed
MatrixWorkspace_const_sptr input;
input = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(m_IWSName);
Gigg, Martyn Anthony
committed
TS_ASSERT(input);
//TS_ASSERT(input->getInstrument()->isDetectorMasked(input->getSpectrum(THEMASKED)->getDetectorIDs()));
Gigg, Martyn Anthony
committed
MatrixWorkspace_sptr outputMat = boost::dynamic_pointer_cast<MatrixWorkspace>(output);;
TS_ASSERT( outputMat );
TS_ASSERT_EQUALS( outputMat->YUnit(), "" );
Steve Williams
committed
Steve Williams
committed
//There are three outputs, a workspace (tested in the next code block), an array (later, this test) and a file (next test)
Steve Williams
committed
//were all the spectra output?
const size_t numberOfSpectra = outputMat->getNumberHistograms();
Steve Williams
committed
TS_ASSERT_EQUALS(numberOfSpectra, (int)Nhist);
Gigg, Martyn Anthony
committed
const int numFailed = alg.getProperty("NumberOfFailures");
Gigg, Martyn Anthony
committed
// the numbers below are threshold values that were found by trial and error running these tests
const int firstGoodSpec = 36;
Steve Williams
committed
const int lastGoodSpec = 95;
for (int lHist=0; lHist < Nhist; lHist++)
Steve Williams
committed
{
// std::cout << " " << lHist << " " << outputMat->readY(lHist).front() << std::endl;
double expected = BAD_VAL;
if (lHist >= firstGoodSpec && lHist <= lastGoodSpec)
expected = GOOD_VAL;
if (lHist == THEMASKED)
expected = BAD_VAL;
else if (lHist == SAVEDBYERRORBAR)
expected = GOOD_VAL;
TS_ASSERT_EQUALS(outputMat->readY(lHist).front(), expected );
Steve Williams
committed
}
Peterson, Peter
committed
}
Steve Williams
committed
void testSolidAngle()
{
Mantid::DataObjects::Workspace2D_sptr ws=WorkspaceCreationHelper::create2DWorkspaceWithRectangularInstrument(5, 10, 1);
for(size_t i=0;i<ws->getNumberHistograms();i++)
{
ws->dataY(i)[0]=std::floor(1e9*ws->getDetector(i)->solidAngle(V3D(0,0,0)));
}
AnalysisDataService::Instance().addOrReplace("MDTSolidAngle",ws);
MedianDetectorTest alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize());
TS_ASSERT(alg.isInitialized());
// Set the properties
alg.setPropertyValue("InputWorkspace", "MDTSolidAngle");
alg.setPropertyValue("OutputWorkspace","MedianDetectorTestOutput");
// set significance test to small
alg.setProperty("SignificanceTest", 0.00001);
alg.setProperty("CorrectForSolidAngle",false);
TS_ASSERT_THROWS_NOTHING( alg.execute());
TS_ASSERT( alg.isExecuted() );
int numFailed = alg.getProperty("NumberOfFailures");
TS_ASSERT_EQUALS(numFailed, 200);
alg.setProperty("CorrectForSolidAngle",true);
TS_ASSERT_THROWS_NOTHING( alg.execute());
TS_ASSERT( alg.isExecuted() );
numFailed = alg.getProperty("NumberOfFailures");
TS_ASSERT_EQUALS(numFailed, 0);
AnalysisDataService::Instance().remove("MDTSolidAngle");
void testLevelsUp()
{
Mantid::DataObjects::Workspace2D_sptr ws=WorkspaceCreationHelper::create2DWorkspaceWithRectangularInstrument(5, 10, 1);
for(size_t i=0;i<ws->getNumberHistograms();i++)
{
ws->dataY(i)[0]=std::floor(1e9*ws->getDetector(i)->solidAngle(V3D(0,0,0)));
}
AnalysisDataService::Instance().addOrReplace("MDTLevelsUp",ws);
MedianDetectorTest alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize());
TS_ASSERT(alg.isInitialized());
// Set the properties
alg.setPropertyValue("InputWorkspace", "MDTLevelsUp");
alg.setPropertyValue("OutputWorkspace","MedianDetectorTestOutput");
// set significance test to small
alg.setProperty("SignificanceTest", 0.00001);
alg.setProperty("CorrectForSolidAngle",false);
alg.setProperty("LevelsUp","0");
TS_ASSERT_THROWS_NOTHING( alg.execute());
TS_ASSERT( alg.isExecuted() );
int numFailed = alg.getProperty("NumberOfFailures");
TS_ASSERT_EQUALS(numFailed, 200);
alg.setProperty("LevelsUp","1");
TS_ASSERT_THROWS_NOTHING( alg.execute());
TS_ASSERT( alg.isExecuted() );
numFailed = alg.getProperty("NumberOfFailures");
TS_ASSERT_EQUALS(numFailed, 0);
AnalysisDataService::Instance().remove("MDTLevelsUp");
Gigg, Martyn Anthony
committed
MedianDetectorTestTest() : m_IWSName("MedianDetectorTestInput")
{
using namespace Mantid;
// Set up a small workspace for testing
Workspace_sptr space = WorkspaceFactory::Instance().create("Workspace2D",Nhist,11,10);
Steve Williams
committed
m_2DWS = boost::dynamic_pointer_cast<Workspace2D>(space);
Peterson, Peter
committed
const short specLength = 22;
Steve Williams
committed
boost::shared_ptr<MantidVec> x(new MantidVec(specLength));
for (int i = 0; i < specLength; ++i)
{
(*x)[i]=i*1000;
}
Peterson, Peter
committed
// the data will be 21 random numbers
Gigg, Martyn Anthony
committed
double yArray[specLength-1] = {0.2,4,50,0.001,0,0,0,1,0,15,4,0,0.001,2e-10,0,8,0,1e-4,1,7,11};
m_YSum = 0;
for (int i = 0; i < specLength-1; i++)
{
m_YSum += yArray[i];
}
Peterson, Peter
committed
// most error values will be small so that they wont affect the tests
Gigg, Martyn Anthony
committed
boost::shared_ptr<MantidVec> smallErrors(new MantidVec( specLength-1, 0.01*m_YSum/specLength ) );
Peterson, Peter
committed
// if the SignificanceTest property is set to one, knowing what happens in the loop below, these errors will just make or break the tests
boost::shared_ptr<MantidVec> almostBigEnough( new MantidVec( specLength-1, 0) );
(*almostBigEnough)[0] = 0.9*m_YSum*(0.5*Nhist-1);
boost::shared_ptr<MantidVec> bigEnough( new MantidVec( specLength-1, 0 ) );
(*bigEnough)[0] = 1.2*m_YSum*(0.5*Nhist);
Peterson, Peter
committed
for (int j = 0; j < Nhist; ++j)
{
m_2DWS->setX(j, x);
boost::shared_ptr<MantidVec> spectrum( new MantidVec );
//the spectravalues will be multiples of the random numbers above
for ( int l = 0; l < specLength-1; ++l )
{
spectrum->push_back( j*yArray[l] );
}
boost::shared_ptr<MantidVec> errors = smallErrors;
if ( j == Nhist-2 ) errors = almostBigEnough;
if ( j == SAVEDBYERRORBAR ) errors = bigEnough;
m_2DWS->setData( j, spectrum, errors );
// Just set the spectrum number to match the index
m_2DWS->getAxis(1)->setValue(j, j+1);
Steve Williams
committed
}
Peterson, Peter
committed
// Register the workspace in the data service
AnalysisDataService::Instance().add(m_IWSName, space);
Peterson, Peter
committed
// Load the instrument data
Mantid::DataHandling::LoadInstrument loader;
loader.initialize();
// Path to test input file assumes Test directory checked out from SVN
std::string inputFile = "INES_Definition.xml";
Peterson, Peter
committed
loader.setPropertyValue("Filename", inputFile);
loader.setPropertyValue("Workspace", m_IWSName);
loader.execute();
Steve Williams
committed
m_2DWS->getAxis(0)->unit() = UnitFactory::Instance().create("TOF");
Steve Williams
committed
// mask the detector
m_2DWS->maskWorkspaceIndex(THEMASKED);
Gigg, Martyn Anthony
committed
private:
Steve Williams
committed
bool runInit(MedianDetectorTest &alg)
{
TS_ASSERT_THROWS_NOTHING(alg.initialize());
Steve Williams
committed
bool good = alg.isInitialized();
// Set the properties
Steve Williams
committed
alg.setPropertyValue("InputWorkspace", m_IWSName);
alg.setPropertyValue("OutputWorkspace","MedianDetectorTestOutput");
Steve Williams
committed
return good;
Steve Williams
committed
std::string m_IWSName, m_OFileName;
Workspace2D_sptr m_2DWS;
Steve Williams
committed
double m_YSum;
};
#endif /*WBVMEDIANTESTTEST_H_*/