diff --git a/Framework/Crystal/src/PeakIntensityVsRadius.cpp b/Framework/Crystal/src/PeakIntensityVsRadius.cpp index 25841bb6b620..22d67b2398d3 100644 --- a/Framework/Crystal/src/PeakIntensityVsRadius.cpp +++ b/Framework/Crystal/src/PeakIntensityVsRadius.cpp @@ -1,12 +1,13 @@ #include "MantidCrystal/PeakIntensityVsRadius.h" -#include "MantidDataObjects/PeaksWorkspace.h" #include "MantidAPI/Axis.h" #include "MantidAPI/IMDEventWorkspace.h" #include "MantidAPI/MatrixWorkspace.h" #include "MantidAPI/TextAxis.h" #include "MantidAPI/WorkspaceFactory.h" +#include "MantidDataObjects/PeaksWorkspace.h" + +#include "MantidKernel/BoundedValidator.h" -#include "MantidKernel/ListValidator.h" #include "MantidKernel/Strings.h" using namespace Mantid::Kernel; @@ -49,8 +50,10 @@ void PeakIntensityVsRadius::init() { declareProperty("RadiusStart", 0.0, "Radius at which to start integrating."); declareProperty("RadiusEnd", 1.0, "Radius at which to stop integrating."); + auto mustBePositive = boost::make_shared>(); + mustBePositive->setLower(0); declareProperty( - "NumSteps", 10, + "NumSteps", 10, mustBePositive, "Number of steps, between start and end, to calculate radius."); declareProperty( diff --git a/Framework/Crystal/test/PeakIntensityVsRadiusTest.h b/Framework/Crystal/test/PeakIntensityVsRadiusTest.h index 9cae1b19c9f0..121ae7e32716 100644 --- a/Framework/Crystal/test/PeakIntensityVsRadiusTest.h +++ b/Framework/Crystal/test/PeakIntensityVsRadiusTest.h @@ -1,9 +1,10 @@ #ifndef MANTID_CRYSTAL_PEAKINTENSITYVSRADIUSTEST_H_ #define MANTID_CRYSTAL_PEAKINTENSITYVSRADIUSTEST_H_ -#include "MantidCrystal/PeakIntensityVsRadius.h" #include "MantidAPI/AnalysisDataService.h" +#include "MantidAPI/FrameworkManager.h" #include "MantidAPI/MatrixWorkspace.h" +#include "MantidCrystal/PeakIntensityVsRadius.h" #include "MantidDataObjects/Peak.h" #include "MantidDataObjects/PeaksWorkspace.h" #include "MantidGeometry/Instrument.h" @@ -11,7 +12,6 @@ #include "MantidKernel/Timer.h" #include "MantidKernel/V3D.h" #include "MantidTestHelpers/ComponentCreationHelper.h" -#include "MantidAPI/FrameworkManager.h" #include using namespace Mantid; @@ -25,7 +25,11 @@ class PeakIntensityVsRadiusTest : public CxxTest::TestSuite { public: void test_Init() { PeakIntensityVsRadius alg; - TS_ASSERT_THROWS_NOTHING(alg.initialize()) + assertSuccessfulInitialization(alg); + } + + void assertSuccessfulInitialization(PeakIntensityVsRadius &alg) { + TS_ASSERT_THROWS_NOTHING(alg.initialize()); TS_ASSERT(alg.isInitialized()) } @@ -67,22 +71,71 @@ class PeakIntensityVsRadiusTest : public CxxTest::TestSuite { "PeakIntensityVsRadiusTest_peaks", peakWS); } + void test_worksWithValidInputs() { + ensureExecutionNoThrow(1.0, 2.0, 0, 0); + ensureExecutionNoThrow(0, 0, 0.12, 0.15); + ensureExecutionNoThrow(1.0, 0, 0, 0.15); + ensureExecutionNoThrow(0, 1.5, 0.15, 0); + // Can't specify fixed and variable + } + + void test_throwsWhenExecutingForInvalidInputs() { + ensureExecutionThrows(1.0, 0, 0.15, 0); + ensureExecutionThrows(0, 1.0, 0, 0.15); + ensureExecutionThrows(1.0, 0, 0.15, 0); + ensureExecutionThrows(1.0, 1.0, 0.12, 0.15); + } + + void ensureExecutionThrows(double BackgroundInnerFactor, + double BackgroundOuterFactor, + double BackgroundInnerRadius, + double BackgroundOuterRadius) { + doTestValid(false, BackgroundInnerFactor, BackgroundOuterFactor, + BackgroundInnerRadius, BackgroundOuterRadius); + } + + void ensureExecutionNoThrow(double BackgroundInnerFactor, + double BackgroundOuterFactor, + double BackgroundInnerRadius, + double BackgroundOuterRadius) { + doTestValid(true, BackgroundInnerFactor, BackgroundOuterFactor, + BackgroundInnerRadius, BackgroundOuterRadius); + } + /** Check the validateInputs() calls */ - void doTestValid(bool pass, double BackgroundInnerFactor, + void doTestValid(bool assertExecuteSuccess, double BackgroundInnerFactor, double BackgroundOuterFactor, double BackgroundInnerRadius, double BackgroundOuterRadius) { PeakIntensityVsRadius alg; - TS_ASSERT_THROWS_NOTHING(alg.initialize()) - TS_ASSERT(alg.isInitialized()) + // Name of the output workspace. + std::string outWSName("PeakIntensityVsRadiusTest_OutputWS"); + assertSuccessfulInitialization(alg); + assertNoThrowWhenSettingProperties( + alg, outWSName, BackgroundInnerFactor, BackgroundOuterFactor, + BackgroundInnerRadius, BackgroundOuterRadius); + if (assertExecuteSuccess) { + TS_ASSERT_THROWS_NOTHING(alg.execute();); + } else { + TS_ASSERT_THROWS_ANYTHING(alg.execute();); + } + } + + void assertNoThrowWhenSettingProperties(PeakIntensityVsRadius &alg, + std::string &outWSName, + double BackgroundInnerFactor, + double BackgroundOuterFactor, + double BackgroundInnerRadius, + double BackgroundOuterRadius) { + auto constexpr DEFAULT_NUM_STEPS = 16; TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue( "InputWorkspace", "PeakIntensityVsRadiusTest_MDEWS")); TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue( "PeaksWorkspace", "PeakIntensityVsRadiusTest_peaks")); - TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue( - "OutputWorkspace", "PeakIntensityVsRadiusTest_OutputWS")); + TS_ASSERT_THROWS_NOTHING( + alg.setPropertyValue("OutputWorkspace", outWSName)); TS_ASSERT_THROWS_NOTHING(alg.setProperty("RadiusStart", 0.0)); TS_ASSERT_THROWS_NOTHING(alg.setProperty("RadiusEnd", 1.5)); - TS_ASSERT_THROWS_NOTHING(alg.setProperty("NumSteps", 16)); + TS_ASSERT_THROWS_NOTHING(alg.setProperty("NumSteps", DEFAULT_NUM_STEPS)); TS_ASSERT_THROWS_NOTHING( alg.setProperty("BackgroundInnerFactor", BackgroundInnerFactor)); TS_ASSERT_THROWS_NOTHING( @@ -91,23 +144,29 @@ class PeakIntensityVsRadiusTest : public CxxTest::TestSuite { alg.setProperty("BackgroundInnerRadius", BackgroundInnerRadius)); TS_ASSERT_THROWS_NOTHING( alg.setProperty("BackgroundOuterRadius", BackgroundOuterRadius)); - if (!pass) { - TS_ASSERT_THROWS_ANYTHING(alg.execute();); - } else { - TS_ASSERT_THROWS_NOTHING(alg.execute();); - } } - void test_validateInputs() { - doTestValid(true, 1.0, 2.0, 0, 0); - doTestValid(true, 0, 0, 0.12, 0.15); - doTestValid(true, 1.0, 0, 0, 0.15); - doTestValid(true, 0, 1.5, 0.15, 0); - // Can't specify fixed and variable - doTestValid(false, 1.0, 0, 0.15, 0); - doTestValid(false, 0, 1.0, 0, 0.15); - doTestValid(false, 1.0, 0, 0.15, 0); - doTestValid(false, 1.0, 1.0, 0.12, 0.15); + void test_throwsWhenSettingInvalidPropertyValues() { + PeakIntensityVsRadius alg; + assertSuccessfulInitialization(alg); + assertInvalidPropertyValue(alg, "NumSteps", -8); + } + + template + void assertInvalidPropertyValue(PeakIntensityVsRadius &alg, + std::string const &name, PropertyType value) { + TS_ASSERT_THROWS_ANYTHING(alg.setProperty(name, value)) + } + + void test_VariableBackground() { + MatrixWorkspace_sptr ws = doTest(1.0, 2.0, 0, 0); + // Check the results + TSM_ASSERT_EQUALS("Two peaks", ws->getNumberHistograms(), 2); + assertFirstFourYValuesCloseToZero(ws); + + // Points before 0.5 are approximately zero because the background shell is + // in the peak. + assertFlatAfter1(ws); } MatrixWorkspace_sptr doTest(double BackgroundInnerFactor, @@ -118,25 +177,10 @@ class PeakIntensityVsRadiusTest : public CxxTest::TestSuite { std::string outWSName("PeakIntensityVsRadiusTest_OutputWS"); PeakIntensityVsRadius alg; - TS_ASSERT_THROWS_NOTHING(alg.initialize()) - TS_ASSERT(alg.isInitialized()) - TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue( - "InputWorkspace", "PeakIntensityVsRadiusTest_MDEWS")); - TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue( - "PeaksWorkspace", "PeakIntensityVsRadiusTest_peaks")); - TS_ASSERT_THROWS_NOTHING( - alg.setPropertyValue("OutputWorkspace", outWSName)); - TS_ASSERT_THROWS_NOTHING(alg.setProperty("RadiusStart", 0.0)); - TS_ASSERT_THROWS_NOTHING(alg.setProperty("RadiusEnd", 1.5)); - TS_ASSERT_THROWS_NOTHING(alg.setProperty("NumSteps", 16)); - TS_ASSERT_THROWS_NOTHING( - alg.setProperty("BackgroundInnerFactor", BackgroundInnerFactor)); - TS_ASSERT_THROWS_NOTHING( - alg.setProperty("BackgroundOuterFactor", BackgroundOuterFactor)); - TS_ASSERT_THROWS_NOTHING( - alg.setProperty("BackgroundInnerRadius", BackgroundInnerRadius)); - TS_ASSERT_THROWS_NOTHING( - alg.setProperty("BackgroundOuterRadius", BackgroundOuterRadius)); + assertSuccessfulInitialization(alg); + assertNoThrowWhenSettingProperties( + alg, outWSName, BackgroundInnerFactor, BackgroundOuterFactor, + BackgroundInnerRadius, BackgroundOuterRadius); TS_ASSERT_THROWS_NOTHING(alg.execute();); TS_ASSERT(alg.isExecuted()); @@ -149,48 +193,40 @@ class PeakIntensityVsRadiusTest : public CxxTest::TestSuite { return ws; } - void test_NoBackground() { - MatrixWorkspace_sptr ws = doTest(0, 0, 0, 0); + void test_FixedBackground() { + // Background + MatrixWorkspace_sptr ws = doTest(0, 0, 0.4, 0.5); // Check the results TSM_ASSERT_EQUALS("Two peaks", ws->getNumberHistograms(), 2); - TSM_ASSERT_EQUALS("16 radii specified", ws->blocksize(), 16); - TS_ASSERT_DELTA(ws->x(0)[1], 0.1, 1e-6); - TS_ASSERT_DELTA(ws->x(0)[2], 0.2, 1e-6); - TS_ASSERT_LESS_THAN(ws->y(0)[5], 1000); - TSM_ASSERT_DELTA("After 1.0, the signal is flat", ws->y(0)[12], 1000, 1e-6); - TSM_ASSERT_DELTA("After 1.0, the signal is flat", ws->y(0)[15], 1000, 1e-6); + // Points before 0.5 are approximately zero because the background shell is + // in the peak. + assertFirstFourYValuesCloseToZero(ws); + assertFlatAfter1(ws); } - void test_VariableBackground() { - MatrixWorkspace_sptr ws = doTest(1.0, 2.0, 0, 0); - // Check the results - TSM_ASSERT_EQUALS("Two peaks", ws->getNumberHistograms(), 2); + void assertFlatAfter1(MatrixWorkspace_sptr ws) { + TSM_ASSERT_DELTA("After 1.0, the signal is flat", ws->y(0)[12], 1000, 1e-6); + TSM_ASSERT_DELTA("After 1.0, the signal is flat", ws->y(0)[15], 1000, 1e-6) + } - // Points before 0.5 are approximately zero because the background shell is - // in the peak. + void assertFirstFourYValuesCloseToZero(MatrixWorkspace_sptr ws) { TS_ASSERT_DELTA(ws->y(0)[0], 0, 10); TS_ASSERT_DELTA(ws->y(0)[1], 0, 10); TS_ASSERT_DELTA(ws->y(0)[2], 0, 10); TS_ASSERT_DELTA(ws->y(0)[3], 0, 10); - TSM_ASSERT_DELTA("After 1.0, the signal is flat", ws->y(0)[12], 1000, 1e-6); - TSM_ASSERT_DELTA("After 1.0, the signal is flat", ws->y(0)[15], 1000, 1e-6); } - void test_FixedBackground() { - // Background - MatrixWorkspace_sptr ws = doTest(0, 0, 0.4, 0.5); + void test_NoBackground() { + MatrixWorkspace_sptr ws = doTest(0, 0, 0, 0); // Check the results TSM_ASSERT_EQUALS("Two peaks", ws->getNumberHistograms(), 2); + TSM_ASSERT_EQUALS("16 radii specified", ws->blocksize(), 16); + TS_ASSERT_DELTA(ws->x(0)[1], 0.1, 1e-6); + TS_ASSERT_DELTA(ws->x(0)[2], 0.2, 1e-6); - // Points before 0.5 are approximately zero because the background shell is - // in the peak. - TS_ASSERT_DELTA(ws->y(0)[0], 0, 10); - TS_ASSERT_DELTA(ws->y(0)[1], 0, 10); - TS_ASSERT_DELTA(ws->y(0)[2], 0, 10); - TS_ASSERT_DELTA(ws->y(0)[3], 0, 10); - TSM_ASSERT_DELTA("After 1.0, the signal is flat", ws->y(0)[12], 1000, 1e-6); - TSM_ASSERT_DELTA("After 1.0, the signal is flat", ws->y(0)[15], 1000, 1e-6); + TS_ASSERT_LESS_THAN(ws->y(0)[5], 1000); + assertFlatAfter1(ws); } }; diff --git a/docs/source/algorithms/PeakIntensityVsRadius-v1.rst b/docs/source/algorithms/PeakIntensityVsRadius-v1.rst index d6179a78564e..3c2dfb61ec14 100644 --- a/docs/source/algorithms/PeakIntensityVsRadius-v1.rst +++ b/docs/source/algorithms/PeakIntensityVsRadius-v1.rst @@ -46,6 +46,12 @@ with the following parameters filled in: Usage ----- +**Example - PeakIntensityVsRadius:** + +The code itself works but disabled from doc tests as takes too long to complete. User should provide its own +event nexus file instead of **TOPAZ_3132_event.nxs** used within this example. The original **TOPAZ_3132_event.nxs** +file is availible in `Mantid system tests repository `_. + .. code-block:: python # Load a SCD data set from systemtests Data and find the peaks