Commit b396588f authored by Lamar Moore's avatar Lamar Moore
Browse files

Re #15542 merge with master

parent 8ecd3c4f
......@@ -18,53 +18,64 @@
using namespace Mantid::Algorithms;
using namespace Mantid::API;
/**
* This is a test class that exists to test the method validateInputs()
*/
class TestAsymmetryCalc : public Mantid::Algorithms::AsymmetryCalc {
public:
std::map<std::string, std::string> wrapValidateInputs() {
return this->validateInputs();
}
};
class AsymmetryCalcTest : public CxxTest::TestSuite {
public:
void testName() { TS_ASSERT_EQUALS(asymCalc.name(), "AsymmetryCalc") }
void testName() {
AsymmetryCalc asymCalc;
TS_ASSERT_EQUALS(asymCalc.name(), "AsymmetryCalc")
}
void testCategory() { TS_ASSERT_EQUALS(asymCalc.category(), "Muon") }
void testCategory() {
AsymmetryCalc asymCalc;
TS_ASSERT_EQUALS(asymCalc.category(), "Muon")
}
void testInit() {
AsymmetryCalc asymCalc;
asymCalc.initialize();
TS_ASSERT(asymCalc.isInitialized())
}
void testLoadNexusAndSetProperties() {
// Load the muon nexus file
loader.initialize();
loader.setPropertyValue("Filename", "emu00006473.nxs");
loader.setPropertyValue("OutputWorkspace", "EMU6473");
TS_ASSERT_THROWS_NOTHING(loader.execute());
TS_ASSERT_EQUALS(loader.isExecuted(), true);
asymCalc.setPropertyValue("InputWorkspace", "EMU6473");
asymCalc.setPropertyValue("OutputWorkspace", "Result");
asymCalc.setPropertyValue("Alpha", "1.0");
asymCalc.setPropertyValue("ForwardSpectra",
"1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16");
asymCalc.setPropertyValue(
"BackwardSpectra", "17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32");
}
void testProperties() {
// this test is badly written and requires the previous test to have
// succeeded
AsymmetryCalc asymCalc;
asymCalc.initialize();
asymCalc.setProperty("Alpha", "1.0");
TS_ASSERT_EQUALS(asymCalc.getPropertyValue("Alpha"), "1");
}
void testExecute() {
// this test is badly written and requires the previous test to have
// succeeded
void testExecuteOnDataFile() {
MatrixWorkspace_sptr data;
TS_ASSERT_THROWS_NOTHING(data = loadDataFile());
TS_ASSERT(data);
AsymmetryCalc asymCalc;
try {
asymCalc.initialize();
asymCalc.setChild(true);
asymCalc.setProperty("InputWorkspace", data);
asymCalc.setPropertyValue("OutputWorkspace", "__Unused");
asymCalc.setPropertyValue("Alpha", "1.0");
asymCalc.setPropertyValue("ForwardSpectra",
"1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16");
asymCalc.setPropertyValue(
"BackwardSpectra", "17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32");
TS_ASSERT_EQUALS(asymCalc.execute(), true);
} catch (std::runtime_error &e) {
TS_FAIL(e.what());
} catch (std::runtime_error &err) {
TS_FAIL(err.what());
}
// Check the result
MatrixWorkspace_const_sptr outputWS =
AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("Result");
// Use a range as cxxtest seems to complain about the accuracy
asymCalc.getProperty("OutputWorkspace");
TS_ASSERT_DELTA(outputWS->dataY(0)[100], 0.2965, 0.005);
TS_ASSERT(!outputWS->isHistogramData());
}
......@@ -78,14 +89,14 @@ public:
AsymmetryCalc alg;
alg.initialize();
alg.setChild(true);
alg.setProperty("InputWorkspace", ws);
alg.setPropertyValue("OutputWorkspace", "Result");
alg.setPropertyValue("OutputWorkspace", "__Unused");
alg.setPropertyValue("ForwardSpectra", "1");
alg.setPropertyValue("BackwardSpectra", "3");
alg.execute();
MatrixWorkspace_const_sptr outputWS =
AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("Result");
MatrixWorkspace_const_sptr outputWS = alg.getProperty("OutputWorkspace");
TS_ASSERT_EQUALS(outputWS->readY(0)[0], -0.5); // == (1 - 3)/(1 + 3)
TS_ASSERT_EQUALS(outputWS->readY(0)[6], -0.5); // == (1 - 3)/(1 + 3)
TS_ASSERT_EQUALS(outputWS->readY(0)[9], -0.5); // == (1 - 3)/(1 + 3)
......@@ -93,33 +104,89 @@ public:
}
void test_yUnitLabel() {
const std::string outputWSName = "AsymmetryCalcTest_yUnitLabel_OutputWS";
auto ws = WorkspaceCreationHelper::Create2DWorkspace(2, 1);
AsymmetryCalc alg;
alg.initialize();
alg.setChild(true);
alg.setProperty("InputWorkspace", ws);
alg.setProperty("OutputWorkspace", outputWSName);
alg.setPropertyValue("OutputWorkspace", "__Unused");
alg.execute();
auto result = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
outputWSName);
MatrixWorkspace_const_sptr result = alg.getProperty("OutputWorkspace");
TS_ASSERT(result);
if (result) {
TS_ASSERT_EQUALS(result->YUnitLabel(), "Asymmetry");
}
}
AnalysisDataService::Instance().remove(outputWSName);
void test_validateInputs() {
auto ws = WorkspaceCreationHelper::Create2DWorkspace(2, 1);
AsymmetryCalc asymCalc;
asymCalc.initialize();
asymCalc.setChild(true);
asymCalc.setProperty("InputWorkspace", ws);
asymCalc.setPropertyValue("OutputWorkspace", "__Unused");
asymCalc.setPropertyValue("ForwardSpectra", "1");
asymCalc.setPropertyValue("BackwardSpectra", "3");
// Bad spectrum number for BackwardSpectra
TS_ASSERT_THROWS(asymCalc.execute(), std::runtime_error);
asymCalc.setPropertyValue("BackwardSpectra", "1");
asymCalc.setPropertyValue("ForwardSpectra", "3");
// Bad spectrum number for ForwardSpectra
TS_ASSERT_THROWS(asymCalc.execute(), std::runtime_error);
}
/**
* Test that the algorithm can handle a WorkspaceGroup as input without
* crashing
* We have to use the ADS to test WorkspaceGroups
*/
void testValidateInputsWithWSGroup() {
auto ws1 = boost::static_pointer_cast<Workspace>(
WorkspaceCreationHelper::Create2DWorkspace(2, 1));
auto ws2 = boost::static_pointer_cast<Workspace>(
WorkspaceCreationHelper::Create2DWorkspace(2, 1));
AnalysisDataService::Instance().add("workspace1", ws1);
AnalysisDataService::Instance().add("workspace2", ws2);
auto group = boost::make_shared<WorkspaceGroup>();
AnalysisDataService::Instance().add("group", group);
group->add("workspace1");
group->add("workspace2");
TestAsymmetryCalc calc;
calc.initialize();
calc.setChild(true);
TS_ASSERT_THROWS_NOTHING(calc.setPropertyValue("InputWorkspace", "group"));
calc.setPropertyValue("OutputWorkspace", "__Unused");
calc.setPropertyValue("ForwardSpectra", "1");
calc.setPropertyValue("BackwardSpectra", "2");
TS_ASSERT_THROWS_NOTHING(calc.wrapValidateInputs());
AnalysisDataService::Instance().clear();
}
private:
AsymmetryCalc asymCalc;
Mantid::DataHandling::LoadMuonNexus2 loader;
Mantid::DataHandling::GroupDetectors group1;
Mantid::DataHandling::GroupDetectors group2;
/// Load data from file
MatrixWorkspace_sptr loadDataFile() {
Mantid::DataHandling::LoadMuonNexus2 loader;
loader.initialize();
loader.setChild(true);
loader.setPropertyValue("Filename", "emu00006473.nxs");
loader.setPropertyValue("OutputWorkspace", "__Unused");
loader.execute();
if (loader.isExecuted()) {
Workspace_sptr outWS = loader.getProperty("OutputWorkspace");
auto matrixWS = boost::dynamic_pointer_cast<MatrixWorkspace>(outWS);
if (matrixWS) {
return matrixWS;
} else {
throw std::runtime_error("Failed to cast loaded workspace");
}
} else {
throw std::runtime_error("Failed to load test data file");
}
}
};
#endif /*ASYMMETRYCALCTEST_H_*/
......@@ -3,10 +3,13 @@
#include <cxxtest/TestSuite.h>
#include "MantidAlgorithms/CalMuonDetectorPhases.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/Workspace.h"
#include "MantidKernel/cow_ptr.h"
#include "MantidKernel/PhysicalConstants.h"
......@@ -15,6 +18,17 @@ using Mantid::MantidVec;
const std::string outputName = "MuonRemoveExpDecay_Output";
/**
* This is a test class that exists to test the method validateInputs()
*/
class TestCalMuonDetectorPhases
: public Mantid::Algorithms::CalMuonDetectorPhases {
public:
std::map<std::string, std::string> wrapValidateInputs() {
return this->validateInputs();
}
};
class CalMuonDetectorPhasesTest : public CxxTest::TestSuite {
public:
// This pair of boilerplate methods prevent the suite being created statically
......@@ -98,6 +112,34 @@ public:
TS_ASSERT(!calc->isExecuted());
}
/**
* Test that the algorithm can handle a WorkspaceGroup as input without
* crashing
* We have to use the ADS to test WorkspaceGroups
*/
void testValidateInputsWithWSGroup() {
auto ws1 = boost::static_pointer_cast<Workspace>(
createWorkspace(2, 4, "Microseconds"));
auto ws2 = boost::static_pointer_cast<Workspace>(
createWorkspace(2, 4, "Microseconds"));
AnalysisDataService::Instance().add("workspace1", ws1);
AnalysisDataService::Instance().add("workspace2", ws2);
auto group = boost::make_shared<WorkspaceGroup>();
AnalysisDataService::Instance().add("group", group);
group->add("workspace1");
group->add("workspace2");
TestCalMuonDetectorPhases calc;
calc.initialize();
calc.setChild(true);
TS_ASSERT_THROWS_NOTHING(calc.setPropertyValue("InputWorkspace", "group"));
calc.setPropertyValue("DataFitted", "fit");
calc.setPropertyValue("DetectorTable", "tab");
calc.setProperty("ForwardSpectra", std::vector<int>{1});
calc.setProperty("BackwardSpectra", std::vector<int>{2});
TS_ASSERT_THROWS_NOTHING(calc.wrapValidateInputs());
AnalysisDataService::Instance().clear();
}
MatrixWorkspace_sptr createWorkspace(size_t nspec, size_t maxt,
std::string units) {
......
......@@ -236,7 +236,7 @@ public:
void test_exception_is_thrown_for_missing_proton_charge_and_absolute_time() {
// Arrange
const double timeShiftDouble = 1000;
DateAndTime abosluteTimeShift = m_startTime + timeShiftDouble;
DateAndTime absoluteTimeShift = m_startTime + timeShiftDouble;
auto ws = provideWorkspace2D(LogType::NOPROTONCHARGE, "in_ws", m_startTime,
m_length);
......@@ -250,7 +250,7 @@ public:
ScopedWorkspace scopedWorkspace(ws);
alg.setPropertyValue("OutputWorkspace", scopedWorkspace.name());
alg.setPropertyValue("AbsoluteTimeOffset",
abosluteTimeShift.toISO8601String());
absoluteTimeShift.toISO8601String());
// We expect to see an exception because we are using absolute times and
// there is no proton charge log
......@@ -397,6 +397,37 @@ public:
inputEqualsOutputWorkspace);
}
/**
* Test that the algorithm can handle a WorkspaceGroup as input without
* crashing
* We have to use the ADS to test WorkspaceGroups
*
* Need to use absolute time to test this part of validateInputs
*/
void testValidateInputsWithWSGroup() {
const double timeShiftDouble = 1000;
DateAndTime absoluteTimeShift = m_startTime + timeShiftDouble;
auto ws1 = provideWorkspace2D(LogType::NOPROTONCHARGE, "in_ws", m_startTime,
m_length);
auto ws2 = provideWorkspace2D(LogType::NOPROTONCHARGE, "in_ws", m_startTime,
m_length);
AnalysisDataService::Instance().add("workspace1", ws1);
AnalysisDataService::Instance().add("workspace2", ws2);
auto group = boost::make_shared<WorkspaceGroup>();
AnalysisDataService::Instance().add("group", group);
group->add("workspace1");
group->add("workspace2");
ChangeTimeZero alg;
alg.initialize();
alg.setChild(true);
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("InputWorkspace", "group"));
alg.setPropertyValue("OutputWorkspace", "__NoName");
alg.setPropertyValue("AbsoluteTimeOffset",
absoluteTimeShift.toISO8601String());
TS_ASSERT_THROWS_NOTHING(alg.validateInputs());
AnalysisDataService::Instance().clear();
}
private:
DateAndTime m_startTime;
DateAndTime m_stringPropertyTime;
......
......@@ -10,6 +10,13 @@
using namespace Mantid::API;
using Mantid::Algorithms::ConvertToDistribution;
class TestConvertToDistribution : public ConvertToDistribution {
public:
std::map<std::string, std::string> wrapValidateInputs() {
return this->validateInputs();
}
};
class ConvertToDistributionTest : public CxxTest::TestSuite {
public:
static ConvertToDistributionTest *createSuite() {
......@@ -17,35 +24,31 @@ public:
}
static void destroySuite(ConvertToDistributionTest *suite) { delete suite; }
ConvertToDistributionTest() : dist("notDist") {
Workspace_sptr WS =
WorkspaceCreationHelper::Create2DWorkspaceBinned(1, 10, 0, 0.5);
AnalysisDataService::Instance().add(dist, WS);
void testName() {
ConvertToDistribution conv;
TS_ASSERT_EQUALS(conv.name(), "ConvertToDistribution")
}
void testName() { TS_ASSERT_EQUALS(conv.name(), "ConvertToDistribution") }
void testVersion() { TS_ASSERT_EQUALS(conv.version(), 1) }
void testVersion() {
ConvertToDistribution conv;
TS_ASSERT_EQUALS(conv.version(), 1)
}
void testInit() {
ConvertToDistribution conv;
TS_ASSERT_THROWS_NOTHING(conv.initialize())
TS_ASSERT(conv.isInitialized())
}
void testExec() {
if (!conv.isInitialized())
conv.initialize();
TS_ASSERT_THROWS_NOTHING(conv.setPropertyValue("Workspace", dist))
TS_ASSERT_THROWS_NOTHING(conv.execute())
TS_ASSERT(conv.isExecuted())
MatrixWorkspace_const_sptr output;
TS_ASSERT_THROWS_NOTHING(
output =
AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(dist))
ConvertToDistribution conv;
conv.initialize();
conv.setChild(true);
auto dist = createTestWorkspace();
TS_ASSERT_THROWS_NOTHING(conv.setProperty("Workspace", dist));
TS_ASSERT_THROWS_NOTHING(conv.execute());
TS_ASSERT(conv.isExecuted());
auto output = boost::dynamic_pointer_cast<MatrixWorkspace>(dist);
const Mantid::MantidVec &X = output->dataX(0);
const Mantid::MantidVec &Y = output->dataY(0);
const Mantid::MantidVec &E = output->dataE(0);
......@@ -55,13 +58,34 @@ public:
TS_ASSERT_EQUALS(E[i], sqrt(2.0) / 0.5)
}
TS_ASSERT(output->isDistribution())
}
AnalysisDataService::Instance().remove(dist);
/**
* Test that the algorithm can handle a WorkspaceGroup as input without
* crashing
* We have to use the ADS to test WorkspaceGroups
*/
void testValidateInputsWithWSGroup() {
auto ws1 = createTestWorkspace();
auto ws2 = createTestWorkspace();
AnalysisDataService::Instance().add("workspace1", ws1);
AnalysisDataService::Instance().add("workspace2", ws2);
auto group = boost::make_shared<WorkspaceGroup>();
AnalysisDataService::Instance().add("group", group);
group->add("workspace1");
group->add("workspace2");
TestConvertToDistribution conv;
conv.initialize();
conv.setChild(true);
TS_ASSERT_THROWS_NOTHING(conv.setPropertyValue("Workspace", "group"));
TS_ASSERT_THROWS_NOTHING(conv.wrapValidateInputs());
AnalysisDataService::Instance().clear();
}
private:
ConvertToDistribution conv;
std::string dist;
Workspace_sptr createTestWorkspace() {
return WorkspaceCreationHelper::Create2DWorkspaceBinned(1, 10, 0, 0.5);
}
};
#endif /*CONVERTTODISTRIBUTIONTEST_H_*/
......@@ -16,6 +16,16 @@
using namespace Mantid;
using namespace Mantid::API;
/**
* This is a test class that exists to test the method validateInputs()
*/
class TestFFT : public Mantid::Algorithms::FFT {
public:
std::map<std::string, std::string> wrapValidateInputs() {
return this->validateInputs();
}
};
class FFTTest : public CxxTest::TestSuite {
public:
// This pair of boilerplate methods prevent the suite being created statically
......@@ -530,6 +540,31 @@ public:
FrameworkManager::Instance().deleteWorkspace("FFT_WS_imag_mismatch");
}
/**
* Test that the algorithm can handle a WorkspaceGroup as input without
* crashing
* We have to use the ADS to test WorkspaceGroups
*/
void testValidateInputsWithWSGroup() {
auto ws1 =
boost::static_pointer_cast<Workspace>(createWS(100, 0, "real_1"));
auto ws2 =
boost::static_pointer_cast<Workspace>(createWS(100, 0, "real_2"));
auto group = boost::make_shared<WorkspaceGroup>();
AnalysisDataService::Instance().add("group", group);
group->addWorkspace(ws1);
group->addWorkspace(ws2);
TestFFT fft;
fft.initialize();
fft.setChild(true);
TS_ASSERT_THROWS_NOTHING(fft.setPropertyValue("InputWorkspace", "group"));
fft.setPropertyValue("OutputWorkspace", "__NotUsed");
fft.setPropertyValue("Real", "0");
fft.setPropertyValue("Imaginary", "0");
TS_ASSERT_THROWS_NOTHING(fft.wrapValidateInputs());
AnalysisDataService::Instance().clear();
}
private:
MatrixWorkspace_sptr createWS(int n, int dn, const std::string &name) {
FrameworkManager::Instance();
......
......@@ -5,9 +5,9 @@
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
#include "MantidAlgorithms/FindPeaks.h"
#include "MantidAlgorithms/CreateSampleWorkspace.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/TableRow.h"
#include "MantidAPI/WorkspaceFactory.h"
......@@ -284,8 +284,6 @@ private:
*/
class FindPeaksTestPerformance : public CxxTest::TestSuite {
Mantid::API::MatrixWorkspace_sptr m_dataWS;
public:
static FindPeaksTestPerformance *createSuite() {
return new FindPeaksTestPerformance();
......@@ -309,6 +307,8 @@ public:
m_dataWS = boost::dynamic_pointer_cast<Mantid::API::MatrixWorkspace>(
AnalysisDataService::Instance().retrieve("FindPeaksTest_peaksWS"));
m_syntheticWS = createSyntheticWS();
return;
}
......@@ -320,6 +320,7 @@ public:
if (!finder.isInitialized())
finder.initialize();
// The data file has 5 spectra, each of them with up to 5-6 peaks
if (!m_dataWS)
throw std::runtime_error("Unable to get input matrix workspace. ");
finder.setPropertyValue("InputWorkspace", "FindPeaksTest_peaksWS");
......@@ -330,6 +331,66 @@ public:
finder.execute();
}
/*
* Gives FindPeaks a synthetic spectrum with 10 peaks of various
* shapes, without 'PeakPositions' hint.
*/
void test_singleSpectrumMultiplePeaksNoPeaksPositions() {
FindPeaks fp;
fp.setChild(true);
fp.initialize();
fp.setChild(true);
fp.setProperty("InputWorkspace", m_syntheticWS);
fp.setPropertyValue("PeaksList", "FindPeaksTest_foundpeaks");
fp.execute();
auto tbl = fp.getProperty("PeaksList");
}
private:
/**
* Creates a synthetic single-spectrum workspace with synthetic data
* containing some peaks.
*/
Mantid::API::MatrixWorkspace_sptr createSyntheticWS() const {
Mantid::Algorithms::CreateSampleWorkspace create;
create.initialize();
create.setChild(true);
create.setPropertyValue("Function", "User Defined");
create.setPropertyValue("UserDefinedFunction", makeSpectrum10Peaks());
create.setProperty("NumBanks", 1);
create.setProperty("BankPixelWidth", 1);
create.setProperty("XMin", 0.0);
create.setProperty("XMax", 200.0);
create.setProperty("BinWidth", 0.1);
create.setPropertyValue("OutputWorkspace", "FindPeaksTestPerf_peaks_ws");
create.execute();
return create.getProperty("OutputWorkspace");
}
/**
* Produces a user defined function with 10 peaks to keep FindPeaks
* busy for a small fraction of a second. Some peaks are not easy
* (simulated here with different shapes).
*/
std::string makeSpectrum10Peaks() const {
const std::string def =
"name=LinearBackground, A0=101.3, A1=8510.3;"
"name=Gaussian, PeakCentre=0.1, Height=200000, Sigma=0.007;"
"name=Lorentzian, PeakCentre=0.45, Amplitude=6000, FWHM=0.017;"
"name=PseudoVoigt, PeakCentre=0.75, Height=85050, FWHM=0.04;"
"name=Gaussian, PeakCentre=0.95, Height=110000, Sigma=0.007;"
"name=Gaussian, PeakCentre=1.15, Height=110000, Sigma=0.007;"
"name=BackToBackExponential, X0=1.30, I=7000, A=1500.1, B=1800.2, "
"S=0.01;"
"name=Gaussian, PeakCentre=1.50, Height=29000, Sigma=0.01;"
"name=Gaussian, PeakCentre=1.70, Height=90000, Sigma=0.02;"
"name=Gaussian, PeakCentre=1.90, Height=80000, Sigma=0.007;"
"name=Gaussian, PeakCentre=2.1, Height=150000, Sigma=0.007;";
return def;
}
Mantid::API::MatrixWorkspace_sptr m_dataWS;
Mantid::API::MatrixWorkspace_sptr m_syntheticWS;
}; // end of class FindPeaksTestPerformance
#endif /*FINDPEAKSTEST_H_*/
......@@ -36,7 +36,7 @@ public:
MaxentData_sptr maxentData =
boost::make_shared<MaxentData>(boost::shared_ptr<MockEntropy>(entropy));
// Bad image size (should be 2*data.size())
// Bad image size (should be at least 2*data.size())
std::vector<double> dat = {0, 1};
std::vector<double> err = {1, 1};
std::vector<double> img = {0, 0};
......@@ -228,6 +228,42 @@ public:
// Should have been updated