From fb691333b544aa1ac31a27ff482593c213975df5 Mon Sep 17 00:00:00 2001 From: Anthony Lim <anthony.lim@stfc.ac.uk> Date: Tue, 3 Oct 2017 14:09:19 +0100 Subject: [PATCH] refs #20714 added tests and clean up --- .../inc/MantidAlgorithms/PhaseQuadMuon.h | 2 - Framework/Algorithms/src/PhaseQuadMuon.cpp | 42 +++--- Framework/Algorithms/test/PhaseQuadMuonTest.h | 122 ++++++++++++++---- 3 files changed, 121 insertions(+), 45 deletions(-) diff --git a/Framework/Algorithms/inc/MantidAlgorithms/PhaseQuadMuon.h b/Framework/Algorithms/inc/MantidAlgorithms/PhaseQuadMuon.h index 4f0fe6b9187..d47ca50770d 100644 --- a/Framework/Algorithms/inc/MantidAlgorithms/PhaseQuadMuon.h +++ b/Framework/Algorithms/inc/MantidAlgorithms/PhaseQuadMuon.h @@ -63,8 +63,6 @@ private: API::MatrixWorkspace_sptr squash(const API::MatrixWorkspace_sptr &ws, const API::ITableWorkspace_sptr &phase, const std::vector<double> &n0); - int findName(const std::vector<std::string> patterns, - const std::vector<std::string> &names); }; } // namespace Algorithms diff --git a/Framework/Algorithms/src/PhaseQuadMuon.cpp b/Framework/Algorithms/src/PhaseQuadMuon.cpp index acdacde2aab..c8973d4d07b 100644 --- a/Framework/Algorithms/src/PhaseQuadMuon.cpp +++ b/Framework/Algorithms/src/PhaseQuadMuon.cpp @@ -7,8 +7,28 @@ #include "MantidKernel/Unit.h" namespace { -std::vector<std::string> phaseNames = {"phase", "phi"}; -std::vector<std::string> asymmNames = {"asymmetry", "asymm", "asym"}; +const std::vector<std::string> phaseNames = {"phase", "phi"}; +const std::vector<std::string> asymmNames = {"asymmetry", "asymm", "asym"}; + +int findName(const std::vector<std::string> &patterns, + const std::vector<std::string> &names) { + for ( std::string pattern : patterns){ + auto it = std::find_if(names.begin(), names.end(), + [pattern](const std::string &s) { + if (s == pattern) { + return true; + } else { + return false; + } + }); + if (it != names.end()) { + return static_cast<int>(std::distance(names.begin(), it)); + } + } + return -1; +} + + } namespace Mantid { @@ -144,24 +164,6 @@ std::map<std::string, std::string> PhaseQuadMuon::validateInputs() { return result; } -int PhaseQuadMuon::findName(const std::vector<std::string> patterns, - const std::vector<std::string> &names) { - for (std::string pattern : patterns) { - auto it = std::find_if(names.begin(), names.end(), - [pattern](const std::string &s) { - if (s == pattern) { - return true; - } else { - return false; - } - }); - if (it != names.end()) { - return static_cast<int>(std::distance(names.begin(), it)); - } - } - return -1; -} - //---------------------------------------------------------------------------------------------- /** Calculates the normalization constant for the exponential decay * @param ws :: [input] Workspace containing the spectra to remove exponential diff --git a/Framework/Algorithms/test/PhaseQuadMuonTest.h b/Framework/Algorithms/test/PhaseQuadMuonTest.h index e1a5a45ce7e..b3e7bf068c5 100644 --- a/Framework/Algorithms/test/PhaseQuadMuonTest.h +++ b/Framework/Algorithms/test/PhaseQuadMuonTest.h @@ -1,6 +1,8 @@ #ifndef MANTID_ALGORITHMS_PHASEQUADMUONTEST_H_ #define MANTID_ALGORITHMS_PHASEQUADMUONTEST_H_ +#include <math.h> + #include <cxxtest/TestSuite.h> #include "MantidAPI/AlgorithmManager.h" #include "MantidAPI/AnalysisDataService.h" @@ -13,27 +15,25 @@ using namespace Mantid::DataObjects; using namespace Mantid::API; namespace { + void populatePhaseTable(ITableWorkspace_sptr phaseTable, std::vector<std::string> names) { phaseTable->addColumn("int", names[0]); phaseTable->addColumn("double", names[1]); phaseTable->addColumn("double", names[2]); + for (int i = 0; i < 16; i++) { TableRow phaseRow1 = phaseTable->appendRow(); - phaseRow1 << i << 1. << 0.; + phaseRow1 << i << 1. << 2.*M_PI*double(i)/16.; TableRow phaseRow2 = phaseTable->appendRow(); - phaseRow2 << i << 1. << 1.57; + phaseRow2 << i << 1. << 2.*M_PI*double(i)/16.; } } void populatePhaseTable(ITableWorkspace_sptr phaseTable) { populatePhaseTable(phaseTable, {"DetectorID", "Asymmetry", "Phase"}); } -IAlgorithm_sptr setupAlg(MatrixWorkspace_sptr inputWs, bool isChildAlg) { - // Create and populate a detector table - boost::shared_ptr<ITableWorkspace> phaseTable( - new Mantid::DataObjects::TableWorkspace); - populatePhaseTable(phaseTable); +IAlgorithm_sptr setupAlg(MatrixWorkspace_sptr inputWs, bool isChildAlg,ITableWorkspace_sptr phaseTable ) { // Set up PhaseQuad IAlgorithm_sptr phaseQuad = AlgorithmManager::Instance().create("PhaseQuad"); phaseQuad->setChild(isChildAlg); @@ -44,6 +44,25 @@ IAlgorithm_sptr setupAlg(MatrixWorkspace_sptr inputWs, bool isChildAlg) { return phaseQuad; } + +IAlgorithm_sptr setupAlg(MatrixWorkspace_sptr inputWs, bool isChildAlg) { + // Create and populate a detector table + boost::shared_ptr<ITableWorkspace> phaseTable( + new Mantid::DataObjects::TableWorkspace); + populatePhaseTable(phaseTable); + + return setupAlg(inputWs, isChildAlg,phaseTable ); +} + +IAlgorithm_sptr setupAlg(MatrixWorkspace_sptr inputWs, bool isChildAlg,std::vector<std::string> names) { + // Create and populate a detector table + boost::shared_ptr<ITableWorkspace> phaseTable( + new Mantid::DataObjects::TableWorkspace); + populatePhaseTable(phaseTable,names); + + return setupAlg(inputWs, isChildAlg,phaseTable );} + + MatrixWorkspace_sptr loadMuonDataset() { IAlgorithm_sptr loader = AlgorithmManager::Instance().create("Load"); loader->setChild(true); @@ -90,30 +109,87 @@ public: const auto specImY = outputWs->getSpectrum(1).y(); const auto specImE = outputWs->getSpectrum(1).e(); // Check real Y values - TS_ASSERT_DELTA(specReY[0], -1.0019, 0.0001); - TS_ASSERT_DELTA(specReY[20], -0.0289, 0.0001); - TS_ASSERT_DELTA(specReY[50], 0.0227, 0.0001); + TS_ASSERT_DELTA(specReY[0], -0.0531, 0.0001); + TS_ASSERT_DELTA(specReY[20], -0.0013, 0.0001); + TS_ASSERT_DELTA(specReY[50], 0.0048, 0.0001); // Check real E values - TS_ASSERT_DELTA(specReE[0], 0.0010, 0.0001); - TS_ASSERT_DELTA(specReE[20], 0.0021, 0.0001); - TS_ASSERT_DELTA(specReE[50], 0.0024, 0.0001); + TS_ASSERT_DELTA(specReE[0], 0.0016, 0.0001); + TS_ASSERT_DELTA(specReE[20], 0.0025, 0.0001); + TS_ASSERT_DELTA(specReE[50], 0.0029, 0.0001); // Check imaginary Y values - TS_ASSERT_DELTA(specImY[0], -1.0011, 0.0001); - TS_ASSERT_DELTA(specImY[20], 0.0079, 0.0001); - TS_ASSERT_DELTA(specImY[50], 0.0280, 0.0001); + TS_ASSERT_DELTA(specImY[0], -0.7030, 0.0001); + TS_ASSERT_DELTA(specImY[20], -0.0096, 0.0001); + TS_ASSERT_DELTA(specImY[50], 0.0254, 0.0001); // Check imaginary E values - TS_ASSERT_DELTA(specImE[0], 0.0029, 0.0001); - TS_ASSERT_DELTA(specImE[20], 0.0031, 0.0001); - TS_ASSERT_DELTA(specImE[50], 0.0035, 0.0001); + TS_ASSERT_DELTA(specImE[0], 0.0012, 0.0001); + TS_ASSERT_DELTA(specImE[20], 0.0027, 0.0001); + TS_ASSERT_DELTA(specImE[50], 0.0031, 0.0001); + } + void testNoPhase() { + std::vector<std::string> names ={"ID","Asym","dummy"}; + MatrixWorkspace_sptr inputWs = loadMuonDataset(); + IAlgorithm_sptr phaseQuad = setupAlg(inputWs, true,names); + TS_ASSERT_THROWS(phaseQuad->execute(),std::runtime_error); + + } + void testNoAsymm() { + std::vector<std::string> names ={"ID","AsYMg","phase"}; + MatrixWorkspace_sptr inputWs = loadMuonDataset(); + IAlgorithm_sptr phaseQuad = setupAlg(inputWs, true,names); + TS_ASSERT_THROWS(phaseQuad->execute(),std::runtime_error); } - // add test for no phase + void testTwoPhases() { + std::vector<std::string> names ={"ID","Phase","phi"}; + MatrixWorkspace_sptr inputWs = loadMuonDataset(); + IAlgorithm_sptr phaseQuad = setupAlg(inputWs, true,names); + TS_ASSERT_THROWS(phaseQuad->execute(),std::runtime_error); - // add test for no asymm + } + void testTwoAsymm() { + std::vector<std::string> names ={"ID","Asym","Asymm"}; + MatrixWorkspace_sptr inputWs = loadMuonDataset(); + IAlgorithm_sptr phaseQuad = setupAlg(inputWs, true,names); + TS_ASSERT_THROWS(phaseQuad->execute(),std::runtime_error); - // add test for two phase + } + void testSwapOrder() { + std::vector<std::string> names ={"ID","phase","Asymm"}; + MatrixWorkspace_sptr inputWs = loadMuonDataset(); + IAlgorithm_sptr phaseQuad = setupAlg(inputWs, true,names); + TS_ASSERT_THROWS_NOTHING(phaseQuad->execute()); + TS_ASSERT(phaseQuad->isExecuted()); + + // Get the output ws + MatrixWorkspace_sptr outputWs = phaseQuad->getProperty("OutputWorkspace"); - // add test for two asymm + TS_ASSERT_EQUALS(outputWs->getNumberHistograms(), 2); + TS_ASSERT_EQUALS( + outputWs->getSpectrum(0).readX(), + inputWs->getSpectrum(0).readX()); // Check outputWs X values + TS_ASSERT_EQUALS(outputWs->getSpectrum(1).readX(), + inputWs->getSpectrum(1).readX()); + const auto specReY = outputWs->getSpectrum(0).y(); + const auto specReE = outputWs->getSpectrum(0).e(); + const auto specImY = outputWs->getSpectrum(1).y(); + const auto specImE = outputWs->getSpectrum(1).e(); + // Check real Y values + TS_ASSERT_DELTA(specReY[0], -3.9473, 0.0001); + TS_ASSERT_DELTA(specReY[20], -0.1183, 0.0001); + TS_ASSERT_DELTA(specReY[50], 0.0504, 0.0001); + // Check real E values + TS_ASSERT_DELTA(specReE[0], 0.0054, 0.0001); + TS_ASSERT_DELTA(specReE[20], 0.0081, 0.0001); + TS_ASSERT_DELTA(specReE[50], 0.0093, 0.0001); + // Check imaginary Y values + TS_ASSERT_DELTA(specImY[0], 0.4317, 0.0001); + TS_ASSERT_DELTA(specImY[20], 0.0058, 0.0001); + TS_ASSERT_DELTA(specImY[50], 0.0058, 0.0001); + // Check imaginary E values + TS_ASSERT_DELTA(specImE[0], 0.0017, 0.0001); + TS_ASSERT_DELTA(specImE[20], 0.0034, 0.0001); + TS_ASSERT_DELTA(specImE[50], 0.0039, 0.0001); + } // add test for different order }; -- GitLab