Skip to content

Commit

Permalink
Refs #4036 tests for 6 algos
Browse files Browse the repository at this point in the history
in the boolean-type operation style
  • Loading branch information
Janik Zikovsky committed Nov 8, 2011
1 parent 026651b commit 28e4c2d
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ namespace MDAlgorithms
virtual std::string outputPropName() const { return "OutputWorkspace";}

void init();
virtual void initExtraProperties();
virtual void exec();

/// LHS workspace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ namespace MDAlgorithms

protected:
/// Return true if the algorithm can operate on a scalar.
virtual bool acceptScalar()
virtual bool acceptScalar() const
{ return true; }
virtual bool commutative() const;

virtual void initDocs();
bool commutative() const;
void checkInputs();
void execEvent();
virtual void execHistoScalar(Mantid::MDEvents::MDHistoWorkspace_sptr out, Mantid::DataObjects::WorkspaceSingleValue_const_sptr scalar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace MDAlgorithms
virtual int version() const;

private:
void initExtraProperties();
bool acceptScalar() { return true; }
void execHistoHisto(Mantid::MDEvents::MDHistoWorkspace_sptr out, Mantid::MDEvents::MDHistoWorkspace_const_sptr operand);
void execHistoScalar(Mantid::MDEvents::MDHistoWorkspace_sptr out, Mantid::DataObjects::WorkspaceSingleValue_const_sptr scalar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ namespace MDAlgorithms
virtual int version() const;

private:
bool acceptScalar() { return true; }
bool commutative() { return false; }
bool acceptScalar() const { return true; }
bool commutative() const { return false; }

void execHistoHisto(Mantid::MDEvents::MDHistoWorkspace_sptr out, Mantid::MDEvents::MDHistoWorkspace_const_sptr operand);
void execHistoScalar(Mantid::MDEvents::MDHistoWorkspace_sptr out, Mantid::DataObjects::WorkspaceSingleValue_const_sptr scalar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ namespace MDAlgorithms
virtual int version() const;

private:
bool acceptScalar() { return true; }
bool commutative() { return false; }
bool acceptScalar() const { return true; }
bool commutative() const { return false; }

void execHistoHisto(Mantid::MDEvents::MDHistoWorkspace_sptr out, Mantid::MDEvents::MDHistoWorkspace_const_sptr operand);
void execHistoScalar(Mantid::MDEvents::MDHistoWorkspace_sptr out, Mantid::DataObjects::WorkspaceSingleValue_const_sptr scalar);
Expand Down
7 changes: 7 additions & 0 deletions Code/Mantid/Framework/MDAlgorithms/src/BinaryOperationMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ namespace MDAlgorithms
"An MDEventWorkspace, MDHistoWorkspace or WorkspaceSingleValue as the right-hand side of the operation.");
declareProperty(new WorkspaceProperty<IMDWorkspace>(outputPropName(),"",Direction::Output),
"Name of the output MDEventWorkspace or MDHistoWorkspace.");
this->initExtraProperties();
}


/// Optional extra properties
void BinaryOperationMD::initExtraProperties()
{
}

//----------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ namespace MDAlgorithms
throw std::runtime_error("Cannot perform the " + this->name() + " operation on a MDEventWorkspace.");
if (!acceptScalar() && (m_lhs_scalar || m_rhs_scalar))
throw std::runtime_error("Cannot perform the " + this->name() + " operation on a WorkspaceSingleValue.");
if (!this->commutative() && m_lhs_scalar)
throw std::runtime_error("Cannot perform the " + this->name() + " operation with a scalar on the left-hand side.");
}

//----------------------------------------------------------------------------------------------
Expand All @@ -66,7 +68,7 @@ namespace MDAlgorithms

//----------------------------------------------------------------------------------------------
/// Run the algorithm with a MDHisotWorkspace as output, scalar and operand
void BooleanBinaryOperationMD::execHistoScalar(Mantid::MDEvents::MDHistoWorkspace_sptr out, Mantid::DataObjects::WorkspaceSingleValue_const_sptr scalar)
void BooleanBinaryOperationMD::execHistoScalar(Mantid::MDEvents::MDHistoWorkspace_sptr /*out*/, Mantid::DataObjects::WorkspaceSingleValue_const_sptr /*scalar*/)
{
throw std::runtime_error("Cannot perform the " + this->name() + " operation on a WorkspaceSingleValue.");
}
Expand Down
16 changes: 13 additions & 3 deletions Code/Mantid/Framework/MDAlgorithms/src/EqualToMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Perform the == (equals to) boolean operation on two MDHistoWorkspaces or a MDHistoWorkspace and a scalar.
The output workspace has a signal of 0.0 to mean "false" and a signal of 1.0 to mean "true". Errors are 0.
For two MDHistoWorkspaces, the operation is performed element-by-element.
For two MDHistoWorkspaces, the operation is performed element-by-element. Only the signal is compared.
For a MDHistoWorkspace and a scalar, the operation is performed on each element of the output.
Expand Down Expand Up @@ -42,18 +42,28 @@ namespace MDAlgorithms
/// Algorithm's version for identification. @see Algorithm::version
int EqualToMD::version() const { return 1;};

//----------------------------------------------------------------------------------------------
/// Extra properties
void EqualToMD::initExtraProperties()
{
declareProperty("Tolerance", 1e-5,
"Tolerance when performing the == comparison. Default 10^-5.");
}

//----------------------------------------------------------------------------------------------
/// Run the algorithm with a MDHisotWorkspace as output and operand
void EqualToMD::execHistoHisto(Mantid::MDEvents::MDHistoWorkspace_sptr out, Mantid::MDEvents::MDHistoWorkspace_const_sptr operand)
{
out->equalTo(*operand);
double tolerance = getProperty("Tolerance");
out->equalTo(*operand, tolerance);
}

//----------------------------------------------------------------------------------------------
/// Run the algorithm with a MDHisotWorkspace as output and a scalar on the RHS
void EqualToMD::execHistoScalar(Mantid::MDEvents::MDHistoWorkspace_sptr out, Mantid::DataObjects::WorkspaceSingleValue_const_sptr scalar)
{
out->equalTo(scalar->dataY(0)[0]);
double tolerance = getProperty("Tolerance");
out->equalTo(scalar->dataY(0)[0], tolerance);
}


Expand Down
46 changes: 15 additions & 31 deletions Code/Mantid/Framework/MDAlgorithms/test/AndMDTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,42 @@
#include <iomanip>

#include "MantidMDAlgorithms/AndMD.h"
#include "MantidTestHelpers/BinaryOperationMDTestHelper.h"
#include "MantidMDEvents/MDHistoWorkspace.h"

using namespace Mantid;
using namespace Mantid::MDAlgorithms;
using namespace Mantid::API;
using Mantid::MDEvents::MDHistoWorkspace_sptr;

class AndMDTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static AndMDTest *createSuite() { return new AndMDTest(); }
static void destroySuite( AndMDTest *suite ) { delete suite; }


void test_Init()
{
AndMD alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}
void test_exec()

void test_histo_histo()
{
// Name of the output workspace.
std::string outWSName("AndMDTest_OutputWS");

AndMD 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() );

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

// TODO: Check the results

// Remove workspace from the data service.
AnalysisDataService::Instance().remove(outWSName);
MDHistoWorkspace_sptr out;
out = BinaryOperationMDTestHelper::doTest("AndMD", "histo_A", "histo_zero", "out");
TS_ASSERT_DELTA( out->getSignalAt(0), 0.0, 1e-5);
out = BinaryOperationMDTestHelper::doTest("AndMD", "histo_A", "histo_B", "out");
TS_ASSERT_DELTA( out->getSignalAt(0), 1.0, 1e-5);
}
void test_Something()

void test_scalar_or_event_fails()
{
BinaryOperationMDTestHelper::doTest("AndMD", "histo_A", "scalar", "out", false /*fails*/);
BinaryOperationMDTestHelper::doTest("AndMD", "event_A", "event_B", "out", false /*fails*/);
}


};


#endif /* MANTID_MDALGORITHMS_ANDMDTEST_H_ */
#endif /* MANTID_MDALGORITHMS_ANDMDTEST_H_ */
65 changes: 33 additions & 32 deletions Code/Mantid/Framework/MDAlgorithms/test/EqualToMDTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,59 @@
#include <iomanip>

#include "MantidMDAlgorithms/EqualToMD.h"
#include "MantidTestHelpers/BinaryOperationMDTestHelper.h"
#include "MantidMDEvents/MDHistoWorkspace.h"

using namespace Mantid;
using namespace Mantid::MDAlgorithms;
using namespace Mantid::API;
using Mantid::MDEvents::MDHistoWorkspace_sptr;

class EqualToMDTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static EqualToMDTest *createSuite() { return new EqualToMDTest(); }
static void destroySuite( EqualToMDTest *suite ) { delete suite; }


void test_Init()
{
EqualToMD alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}
void test_exec()

void test_histo_histo()
{
// Name of the output workspace.
std::string outWSName("EqualToMDTest_OutputWS");

EqualToMD 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() );

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

// TODO: Check the results

// Remove workspace from the data service.
AnalysisDataService::Instance().remove(outWSName);
MDHistoWorkspace_sptr out;
out = BinaryOperationMDTestHelper::doTest("EqualToMD", "histo_A", "histo_B", "out");
TS_ASSERT_DELTA( out->getSignalAt(0), 0.0, 1e-5);
out = BinaryOperationMDTestHelper::doTest("EqualToMD", "histo_B", "histo_B", "out");
TS_ASSERT_DELTA( out->getSignalAt(0), 1.0, 1e-5);
}

void test_histo_scalar()
{
MDHistoWorkspace_sptr out;
out = BinaryOperationMDTestHelper::doTest("EqualToMD", "histo_A", "scalar", "out");
TS_ASSERT_DELTA( out->getSignalAt(0), 0.0, 1e-5);
out = BinaryOperationMDTestHelper::doTest("EqualToMD", "scalar", "histo_B", "out");
TS_ASSERT_DELTA( out->getSignalAt(0), 1.0, 1e-5);
}
void test_Something()

void test_event_fails()
{
BinaryOperationMDTestHelper::doTest("EqualToMD", "event_A", "scalar", "out", false /*fails*/);
BinaryOperationMDTestHelper::doTest("EqualToMD", "event_A", "event_B", "out", false /*fails*/);
}

void test_Tolerance()
{
MDHistoWorkspace_sptr out;
out = BinaryOperationMDTestHelper::doTest("EqualToMD", "histo_A", "histo_B", "out", true, "Tolerance", "1.5");
// Large enough tolerance to say that 2 == 3 (give or take 1.5)
TS_ASSERT_DELTA( out->getSignalAt(0), 1.0, 1e-5);
}



};


#endif /* MANTID_MDALGORITHMS_EQUALTOMDTEST_H_ */
#endif /* MANTID_MDALGORITHMS_EQUALTOMDTEST_H_ */
60 changes: 28 additions & 32 deletions Code/Mantid/Framework/MDAlgorithms/test/GreaterThanMDTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,54 @@
#include <iomanip>

#include "MantidMDAlgorithms/GreaterThanMD.h"
#include "MantidTestHelpers/BinaryOperationMDTestHelper.h"
#include "MantidMDEvents/MDHistoWorkspace.h"

using namespace Mantid;
using namespace Mantid::MDAlgorithms;
using namespace Mantid::API;
using Mantid::MDEvents::MDHistoWorkspace_sptr;

class GreaterThanMDTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static GreaterThanMDTest *createSuite() { return new GreaterThanMDTest(); }
static void destroySuite( GreaterThanMDTest *suite ) { delete suite; }


void test_Init()
{
GreaterThanMD alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}
void test_exec()

void test_histo_histo()
{
// Name of the output workspace.
std::string outWSName("GreaterThanMDTest_OutputWS");

GreaterThanMD 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() );

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

// TODO: Check the results

// Remove workspace from the data service.
AnalysisDataService::Instance().remove(outWSName);
MDHistoWorkspace_sptr out;
out = BinaryOperationMDTestHelper::doTest("GreaterThanMD", "histo_A", "histo_B", "out");
TS_ASSERT_DELTA( out->getSignalAt(0), 0.0, 1e-5);
out = BinaryOperationMDTestHelper::doTest("GreaterThanMD", "histo_B", "histo_A", "out");
TS_ASSERT_DELTA( out->getSignalAt(0), 1.0, 1e-5);
}

void test_histo_scalar()
{
MDHistoWorkspace_sptr out;
out = BinaryOperationMDTestHelper::doTest("GreaterThanMD", "histo_A", "scalar", "out");
TS_ASSERT_DELTA( out->getSignalAt(0), 0.0, 1e-5);
}
void test_Something()

void test_event_fails()
{
BinaryOperationMDTestHelper::doTest("GreaterThanMD", "event_A", "scalar", "out", false /*fails*/);
BinaryOperationMDTestHelper::doTest("GreaterThanMD", "event_A", "event_B", "out", false /*fails*/);
}

void test_scalar_histo_fails()
{
BinaryOperationMDTestHelper::doTest("GreaterThanMD", "scalar", "histo_A", "out", false /*fails*/);
}



};


#endif /* MANTID_MDALGORITHMS_GREATERTHANMDTEST_H_ */
#endif /* MANTID_MDALGORITHMS_GREATERTHANMDTEST_H_ */

0 comments on commit 28e4c2d

Please sign in to comment.