Skip to content

Commit

Permalink
Merge pull request mantidproject#20053 from mantidproject/8394_fix_Pe…
Browse files Browse the repository at this point in the history
…akIntensityVsRadius

Add Validator to NumSteps in PeakintensityVsRadius
  • Loading branch information
martyngigg authored Jul 26, 2017
2 parents 4b786a8 + b684506 commit 7a3a83f
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 73 deletions.
9 changes: 6 additions & 3 deletions Framework/Crystal/src/PeakIntensityVsRadius.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<BoundedValidator<int>>();
mustBePositive->setLower(0);
declareProperty(
"NumSteps", 10,
"NumSteps", 10, mustBePositive,
"Number of steps, between start and end, to calculate radius.");

declareProperty(
Expand Down
176 changes: 106 additions & 70 deletions Framework/Crystal/test/PeakIntensityVsRadiusTest.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#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"
#include "MantidKernel/System.h"
#include "MantidKernel/Timer.h"
#include "MantidKernel/V3D.h"
#include "MantidTestHelpers/ComponentCreationHelper.h"
#include "MantidAPI/FrameworkManager.h"
#include <cxxtest/TestSuite.h>

using namespace Mantid;
Expand All @@ -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())
}

Expand Down Expand Up @@ -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(
Expand All @@ -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 <typename PropertyType>
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,
Expand All @@ -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());

Expand All @@ -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);
}
};

Expand Down
6 changes: 6 additions & 0 deletions docs/source/algorithms/PeakIntensityVsRadius-v1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/mantidproject/systemtests/tree/master/Data/TOPAZ_3132_event.nxs>`_.

.. code-block:: python
# Load a SCD data set from systemtests Data and find the peaks
Expand Down

0 comments on commit 7a3a83f

Please sign in to comment.