Skip to content

Commit

Permalink
refs #5626. Performance Test Parallel and Sequential.
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Jul 19, 2012
1 parent a213408 commit b98cc75
Showing 1 changed file with 137 additions and 36 deletions.
173 changes: 137 additions & 36 deletions Code/Mantid/Framework/Algorithms/test/NormaliseByDetectorTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <fstream>

#include "MantidDataHandling/LoadParameterFile.h"
#include "MantidDataHandling/LoadEmptyInstrument.h"
#include "MantidAlgorithms/NormaliseByDetector.h"
#include "MantidTestHelpers/ComponentCreationHelper.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
Expand All @@ -21,11 +22,6 @@ using namespace Mantid;
using namespace Mantid::Algorithms;
using namespace Mantid::API;

class NormaliseByDetectorTest : public CxxTest::TestSuite
{

private:

/// File object type. Provides exception save file creation/destruction.
class FileObject
{
Expand Down Expand Up @@ -60,6 +56,48 @@ class NormaliseByDetectorTest : public CxxTest::TestSuite
void *operator new[](size_t);
};

/**
Helper function. Runs LoadParameterAlg, to get an instrument parameter definition from a file onto a workspace.
*/
void apply_instrument_parameter_file_to_workspace(MatrixWorkspace_sptr ws, const FileObject& file)
{
// Load the Instrument Parameter file over the existing test workspace + instrument.
using DataHandling::LoadParameterFile;
LoadParameterFile loadParameterAlg;
loadParameterAlg.setRethrows(true);
loadParameterAlg.initialize();
loadParameterAlg.setPropertyValue("Filename", file.getFileName());
loadParameterAlg.setProperty("Workspace", ws);
loadParameterAlg.execute();
}

/**
Helper method for running the algorithm and simply verifying that it runs without exception producing an output workspace..
*/
MatrixWorkspace_sptr do_test_doesnt_throw_on_execution(MatrixWorkspace_sptr inputWS, bool parallel = true)
{
NormaliseByDetector alg(parallel);
alg.setRethrows(true);
alg.initialize();
alg.setPropertyValue("OutputWorkspace", "out");
alg.setProperty("InputWorkspace", inputWS);
TS_ASSERT_THROWS_NOTHING(alg.execute());
MatrixWorkspace_sptr outWS =AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("out");
TS_ASSERT(outWS != NULL);
return outWS;
}

/**
Functionality Tests
*/
class NormaliseByDetectorTest : public CxxTest::TestSuite
{

private:


/** Helper function, creates a histogram workspace with an instrument with 2 detectors, and 2 spectra.
Y-values are flat accross the x bins. Which makes it easy to calculate the expected value for any fit function applied to the X-data.
*/
Expand All @@ -80,21 +118,6 @@ class NormaliseByDetectorTest : public CxxTest::TestSuite
return ws;
}

/**
Helper function. Runs LoadParameterAlg, to get an instrument parameter definition from a file onto a workspace.
*/
void apply_instrument_parameter_file_to_workspace(MatrixWorkspace_sptr ws, const FileObject& file)
{
// Load the Instrument Parameter file over the existing test workspace + instrument.
using DataHandling::LoadParameterFile;
LoadParameterFile loadParameterAlg;
loadParameterAlg.setRethrows(true);
loadParameterAlg.initialize();
loadParameterAlg.setPropertyValue("Filename", file.getFileName());
loadParameterAlg.setProperty("Workspace", ws);
loadParameterAlg.execute();
}

/**
Helper function, applies fit functions from a fabricated, fake instrument parameter file ontop of an existing instrument definition.
The fit function is set at the instrument level.
Expand Down Expand Up @@ -263,22 +286,6 @@ class NormaliseByDetectorTest : public CxxTest::TestSuite
TS_ASSERT_THROWS(alg.execute(), std::runtime_error);
}

/**
Helper method for running the algorithm and simply verifying that it runs without exception producing an output workspace..
*/
MatrixWorkspace_sptr do_test_doesnt_throw_on_execution(MatrixWorkspace_sptr inputWS, bool parallel = true)
{
NormaliseByDetector alg(parallel);
alg.setRethrows(true);
alg.initialize();
alg.setPropertyValue("OutputWorkspace", "out");
alg.setProperty("InputWorkspace", inputWS);
TS_ASSERT_THROWS_NOTHING(alg.execute());
MatrixWorkspace_sptr outWS =AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("out");
TS_ASSERT(outWS != NULL);
return outWS;
}

public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
Expand Down Expand Up @@ -493,5 +500,99 @@ class NormaliseByDetectorTest : public CxxTest::TestSuite

};

/**
Performance Tests
*/
class NormaliseByDetectorTestPerformance : public CxxTest::TestSuite
{
private:

MatrixWorkspace_sptr ws;

/// Helper method to run common sanity checks.
void do_basic_checks(MatrixWorkspace_sptr normalisedWS)
{
TS_ASSERT(normalisedWS != NULL);
TS_ASSERT(ws->getNumberHistograms() == normalisedWS->getNumberHistograms());
TS_ASSERT(ws->readX(0).size() == normalisedWS->readX(0).size());
TS_ASSERT(ws->readY(0).size() == normalisedWS->readY(0).size());
TS_ASSERT(ws->readE(0).size() == normalisedWS->readE(0).size());
}

public:

NormaliseByDetectorTestPerformance()
{
// Load some data
IAlgorithm* loadalg = FrameworkManager::Instance().createAlgorithm("Load");
loadalg->setRethrows(true);
loadalg->initialize();
loadalg->setPropertyValue("Filename", "POLREF00004699.nxs");
loadalg->setPropertyValue("OutputWorkspace", "testws");
loadalg->execute();

// Convert units to wavelength
IAlgorithm* unitsalg = FrameworkManager::Instance().createAlgorithm("ConvertUnits");
unitsalg->initialize();
unitsalg->setPropertyValue("InputWorkspace", "testws");
unitsalg->setPropertyValue("OutputWorkspace", "testws");
unitsalg->setPropertyValue("Target", "Wavelength");
unitsalg->execute();

// Convert the specturm axis ot signed_theta
IAlgorithm* specaxisalg = FrameworkManager::Instance().createAlgorithm("ConvertSpectrumAxis");
specaxisalg->initialize();
specaxisalg->setPropertyValue("InputWorkspace", "testws");
specaxisalg->setPropertyValue("OutputWorkspace", "testws");
specaxisalg->setPropertyValue("Target", "signed_theta");
specaxisalg->execute();

WorkspaceGroup_sptr wsGroup = API::AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>("testws");
ws = boost::dynamic_pointer_cast<MatrixWorkspace>(wsGroup->getItem(0));

const std::string instrumentName = ws->getInstrument()->getName();

// Create a parameter file, with a root equation that will apply to all detectors.
const std::string parameterFileContents = boost::str(boost::format(
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n\
<parameter-file instrument = \"%1%\" date = \"2012-01-31T00:00:00\">\n\
<component-link name=\"%1%\">\n\
<parameter name=\"LinearBackground:A0\" type=\"fitting\">\n\
<formula eq=\"1\" result-unit=\"Wavelength\"/>\n\
<fixed />\n\
</parameter>\n\
<parameter name=\"LinearBackground:A1\" type=\"fitting\">\n\
<formula eq=\"2\" result-unit=\"Wavelength\"/>\n\
<fixed />\n\
</parameter>\n\
</component-link>\n\
</parameter-file>\n") % instrumentName);

// Create a temporary Instrument Parameter file.
FileObject file(parameterFileContents, instrumentName + "_Parameters.xml");

// Apply parameter file to workspace.
apply_instrument_parameter_file_to_workspace(ws, file);
}

void testSequential()
{
bool parallel = false;
MatrixWorkspace_sptr normalisedWS = do_test_doesnt_throw_on_execution(ws, parallel);
// Run some basic sanity checks
do_basic_checks(normalisedWS);
}

void testParallel()
{
bool parallel = true;
MatrixWorkspace_sptr normalisedWS = do_test_doesnt_throw_on_execution(ws, parallel);
// Run some basic sanity checks
do_basic_checks(normalisedWS);
}
};


#endif /* MANTID_ALGORITHMS_NORMALISEBYDETECTORTEST_H_ */

0 comments on commit b98cc75

Please sign in to comment.