Skip to content

Commit

Permalink
Re #10462. Unit test for MDNormSXD properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Nov 10, 2014
1 parent 9929332 commit 6afd7ed
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 28 deletions.
38 changes: 38 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/WorkspaceValidators.h
Expand Up @@ -369,6 +369,44 @@ class DLLExport IncreasingAxisValidator : public MatrixWorkspaceValidator
}
};

//===============================================================================================
/**
* A validator which checks whether data in each spectrum of the input workspace are monotonically increasing.
*/
class DLLExport IncreasingDataValidator : public MatrixWorkspaceValidator
{
public:
///Gets the type of the validator
std::string getType() const { return "increasingdata"; }
/// Clone the current state
Kernel::IValidator_sptr clone() const { return boost::make_shared<IncreasingDataValidator>(*this); }

private:
/** Validate a workspace.
* @param value :: The workspace to test
* @return A message for users with negative results, otherwise ""
*/
std::string checkValidity( const MatrixWorkspace_sptr& value ) const
{
if ( value->blocksize() < 2 )
{
return "Spectra must have two or more data points (bins).";
}
for(size_t spec = 0; spec < value->getNumberHistograms(); ++spec)
{
auto &Y = value->readY( spec );
double y = Y.front();
for(auto it = Y.begin() + 1; it != Y.end(); ++it)
{
if ( y > *it ) return "Data in the workspace must monotonically increase.";
y = *it;
}
}
return "";
}

};

} // namespace API
} // namespace Mantid

Expand Down
7 changes: 6 additions & 1 deletion Code/Mantid/Framework/MDAlgorithms/src/IntegrateFlux.cpp
Expand Up @@ -2,6 +2,9 @@
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidKernel/BoundedValidator.h"

#include <boost/make_shared.hpp>

namespace Mantid
{
Expand Down Expand Up @@ -34,7 +37,9 @@ namespace MDAlgorithms
void IntegrateFlux::init()
{
declareProperty(new WorkspaceProperty<DataObjects::EventWorkspace>("InputWorkspace","",Direction::Input), "An input workspace.");
declareProperty("NPoints", 1000, "Number of points per output spectrum.");
auto validator = boost::make_shared<Kernel::BoundedValidator<int>>();
validator->setLower(2);
declareProperty("NPoints", 1000, validator, "Number of points per output spectrum.");
declareProperty(new WorkspaceProperty<API::Workspace>("OutputWorkspace","",Direction::Output), "An output workspace.");
}

Expand Down
16 changes: 9 additions & 7 deletions Code/Mantid/Framework/MDAlgorithms/src/MDNormSXD.cpp
Expand Up @@ -92,13 +92,15 @@ namespace MDAlgorithms
"Enter it as a comma-separated list of values with the format: 'name,minimum,maximum,number_of_bins'. Leave blank for NONE.");
}

auto wsValidator = boost::make_shared<CompositeValidator>();
wsValidator->add<WorkspaceUnitValidator>("Momentum");
wsValidator->add<InstrumentValidator>();
wsValidator->add<CommonBinsValidator>();

declareProperty(new WorkspaceProperty<>("FluxWorkspace","",Direction::Input,wsValidator), "An input workspace containing momentum dependent flux.");
declareProperty(new WorkspaceProperty<>("SolidAngleWorkspace","",Direction::Input,wsValidator->clone()), "An input workspace containing momentum integrated vanadium (a measure of the solid angle).");
auto fluxValidator = boost::make_shared<CompositeValidator>();
fluxValidator->add<WorkspaceUnitValidator>("Momentum");
fluxValidator->add<InstrumentValidator>();
fluxValidator->add<CommonBinsValidator>();
auto solidAngleValidator = fluxValidator->clone();
fluxValidator->add<IncreasingDataValidator>();

declareProperty(new WorkspaceProperty<>("FluxWorkspace","",Direction::Input,fluxValidator), "An input workspace containing integrated momentum dependent flux.");
declareProperty(new WorkspaceProperty<>("SolidAngleWorkspace","",Direction::Input,solidAngleValidator), "An input workspace containing momentum integrated vanadium (a measure of the solid angle).");

declareProperty(new WorkspaceProperty<Workspace>("OutputWorkspace","",Direction::Output), "A name for the output data MDHistoWorkspace.");
declareProperty(new WorkspaceProperty<Workspace>("OutputNormalizationWorkspace","",Direction::Output), "A name for the output normalization MDHistoWorkspace.");
Expand Down
4 changes: 1 addition & 3 deletions Code/Mantid/Framework/MDAlgorithms/test/IntegrateFluxTest.h
Expand Up @@ -120,12 +120,10 @@ class IntegrateFluxTest : public CxxTest::TestSuite
auto inputWS = createInputWorkspace(inWSName);

IntegrateFlux alg;
alg.setRethrows(true);
alg.initialize();
alg.setPropertyValue("InputWorkspace", inWSName);
alg.setPropertyValue("OutputWorkspace", outWSName);
alg.setProperty("NPoints", 1);
TS_ASSERT_THROWS( alg.execute(), std::runtime_error );
TS_ASSERT_THROWS( alg.setProperty("NPoints", 1), std::invalid_argument );

// Remove workspace from the data service.
AnalysisDataService::Instance().clear();
Expand Down
96 changes: 79 additions & 17 deletions Code/Mantid/Framework/MDAlgorithms/test/MDNormSXDTest.h
Expand Up @@ -4,6 +4,10 @@
#include <cxxtest/TestSuite.h>

#include "MantidMDAlgorithms/MDNormSXD.h"
#include "MantidMDAlgorithms/CreateMDWorkspace.h"
#include "MantidAPI/IMDHistoWorkspace.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"

using Mantid::MDAlgorithms::MDNormSXD;
using namespace Mantid::API;
Expand All @@ -24,32 +28,90 @@ class MDNormSXDTest : public CxxTest::TestSuite
TS_ASSERT( alg.isInitialized() )
}

//No test for now. Should be part of ticket #9105
void test_exec()
void test_properties()
{
// Name of the output workspace.
/*std::string outWSName("MDNormSXDTest_OutputWS");
std::string mdWsName = "__temp_InputMDWorkspaceName";
createMDWorkspace(mdWsName);
std::string fluxGoodWsName = "__temp_InputGoodFluxWorkspaceName";
createGoodFluxWorkspace(fluxGoodWsName);
std::string fluxBadWsName = "__temp_InputBadFluxWorkspaceName";
createBadFluxWorkspace(fluxBadWsName);
std::string saWsName = "__temp_InputSAWorkspaceName";
createBadFluxWorkspace(saWsName);

MDNormSXD alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("REPLACE_PROPERTY_NAME_HERE!!!!", "value") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", outWSName) );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InputWorkspace", mdWsName) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("FluxWorkspace", fluxGoodWsName) );
TS_ASSERT_THROWS( alg.setProperty("FluxWorkspace", fluxBadWsName), std::invalid_argument );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("SolidAngleWorkspace", saWsName) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", "OutWSName") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputNormalizationWorkspace", "OutNormWSName") );

// Retrieve the workspace from data service. TODO: Change to your desired type
Workspace_sptr ws;
TS_ASSERT_THROWS_NOTHING( ws = AnalysisDataService::Instance().retrieveWS<Workspace>(outWSName) );
TS_ASSERT(ws);
if (!ws) return;
AnalysisDataService::Instance().clear();
}

private:

void createMDWorkspace(const std::string& wsName)
{
const int ndims = 2;
std::string bins = "2,2";
std::string extents = "0,1,0,1";
std::vector<std::string> names(ndims);
names[0] = "A"; names[1] = "B";
std::vector<std::string> units(ndims);
units[0] = "a"; units[1] = "b";

// TODO: Check the results
Mantid::MDAlgorithms::CreateMDWorkspace alg;
alg.initialize();
TS_ASSERT_THROWS_NOTHING( alg.setProperty("Dimensions", ndims) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Extents", extents) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("Names", names) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("Units", units) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", wsName) );
alg.execute();

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


void createGoodFluxWorkspace(const std::string& wsName)
{
auto flux = WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument( 2, 10 );
auto &x = flux->dataX(0);
auto &y1 = flux->dataY(1);

for(size_t i = 0; i < y1.size(); ++i)
{
y1[i] = 2 * x[i];
}
flux->setX(1,x);
flux->getAxis(0)->setUnit("Momentum");

AnalysisDataService::Instance().addOrReplace( wsName, flux );
}

void createBadFluxWorkspace(const std::string& wsName)
{
auto flux = WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument( 2, 10 );
auto &x = flux->dataX(0);
auto &y1 = flux->dataY(1);

for(size_t i = 0; i < y1.size(); ++i)
{
y1[i] = -2 * x[i];
}
flux->setX(1,x);
flux->getAxis(0)->setUnit("Momentum");

AnalysisDataService::Instance().addOrReplace( wsName, flux );
}

void createSolidAngleWorkspace(const std::string& wsName)
{
auto sa = WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument( 2, 10 );
AnalysisDataService::Instance().addOrReplace( wsName, sa );
}

};

Expand Down

0 comments on commit 6afd7ed

Please sign in to comment.