Skip to content

Commit

Permalink
Refs #12585 unit tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
VickieLynch committed Jul 1, 2016
1 parent c205a6c commit e9b2f68
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
#include "MantidDataObjects/MDEventFactory.h"
#include "MantidDataObjects/MDEventWorkspace.h"
#include "MantidGeometry/MDGeometry/MDFrame.h"
#include "MantidMDAlgorithms/DllConfig.h"

namespace Mantid {
namespace MDAlgorithms {


std::vector<std::string> MANTID_MDALGORITHMS_DLL
parseNames(const std::string &names_string);

/** CreateMDWorkspace :
*
* Algorithm to create an empty MDEventWorkspace with a given number of
Expand Down
36 changes: 34 additions & 2 deletions Framework/MDAlgorithms/src/CreateMDWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/Memory.h"
#include "MantidKernel/System.h"
#include "MantidKernel/MandatoryValidator.h"
#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp>
#include <cmath>

namespace Mantid {
Expand All @@ -22,6 +25,32 @@ using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::Geometry;
using namespace Mantid::DataObjects;
using boost::regex;

/*
* The list of dimension names often looks like "[H,0,0],[0,K,0]" with "[H,0,0]"
* being the first dimension but getProperty returns a vector of
* the string split on every comma
* This function parses the string, and does not split on commas within brackets
*/
std::vector<std::string> parseNames(const std::string &names_string) {

// This regex has two parts which are separated by the "|" (or)
// The first part matches anything which is bounded by square brackets
// unless they contain square brackets (so that it only matches inner pairs)
// The second part matches anything that doesn't contain a comma
// NB, the order of the two parts matters

regex expression("\\[([^\\[]*)\\]|[^,]+");

boost::sregex_token_iterator iter(names_string.begin(), names_string.end(),
expression, 0);
boost::sregex_token_iterator end;

std::vector<std::string> names_result(iter, end);

return names_result;
}

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(CreateMDWorkspace)
Expand All @@ -43,7 +72,8 @@ void CreateMDWorkspace::init() {
"A comma separated list of min, max for each dimension,\n"
"specifying the extents of each dimension.");

declareProperty(make_unique<ArrayProperty<std::string>>("Names"),
declareProperty(make_unique<ArrayProperty<std::string>>("Names",
Direction::Input),
"A comma separated list of the name of each dimension.");

declareProperty(make_unique<ArrayProperty<std::string>>("Units"),
Expand Down Expand Up @@ -131,7 +161,9 @@ void CreateMDWorkspace::exec() {
size_t ndims = static_cast<size_t>(ndims_prop);

std::vector<double> extents = getProperty("Extents");
std::vector<std::string> names = getProperty("Names");
std::string dimensions_string = getPropertyValue("Names");
std::vector<std::string> names = parseNames(dimensions_string);

std::vector<std::string> units = getProperty("Units");
std::vector<std::string> frames = getProperty("Frames");

Expand Down
44 changes: 20 additions & 24 deletions Framework/MDAlgorithms/test/IntegratePeaksMDHKLTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class IntegratePeaksMDHKLTest : public CxxTest::TestSuite {
TS_ASSERT_THROWS_NOTHING(algC.setProperty("Dimensions", "3"));
TS_ASSERT_THROWS_NOTHING(
algC.setProperty("Extents", "-10,10,-10,10,-10,10"));

TS_ASSERT_THROWS_NOTHING(algC.setProperty("Names", "[H,0,0],[0,K,0],[0,0,L]"));
std::string units = Mantid::Kernel::Units::Symbol::RLU.ascii() + "," +
Mantid::Kernel::Units::Symbol::RLU.ascii() + "," +
Expand Down Expand Up @@ -102,6 +103,10 @@ class IntegratePeaksMDHKLTest : public CxxTest::TestSuite {
algF.setPropertyValue("InputWorkspace", "IntegratePeaksMDHKLTest_MDEWS"));
TS_ASSERT_THROWS_NOTHING(
algF.setProperty("PeakParams", mess.str().c_str()));
TS_ASSERT_THROWS_NOTHING(
algF.setProperty("RandomSeed", "63759"));
TS_ASSERT_THROWS_NOTHING(
algF.setProperty("RandomizeSignal", "1"));
TS_ASSERT_THROWS_NOTHING(algF.execute());
TS_ASSERT(algF.isExecuted());
}
Expand All @@ -111,9 +116,9 @@ class IntegratePeaksMDHKLTest : public CxxTest::TestSuite {
void test_exec() {
// --- Fake workspace with 3 peaks ------
createMDEW();
addPeak(1000, 0., 0., 0., 1.0);
addPeak(1000, 2., 3., 4., 0.5);
addPeak(1000, 6., 6., 6., 2.0);
addPeak(1000, 1., 1., 1., 0.1);
addPeak(1000, 2., 3., 4., 0.15);
addPeak(1000, 6., 6., 6., 0.2);

MDEventWorkspace3Lean::sptr mdews =
AnalysisDataService::Instance().retrieveWS<MDEventWorkspace3Lean>(
Expand All @@ -122,7 +127,7 @@ class IntegratePeaksMDHKLTest : public CxxTest::TestSuite {
TSM_ASSERT_EQUALS("Should be HKL", Mantid::Geometry::HKL::HKLName,
frame.name());
TS_ASSERT_EQUALS(mdews->getNPoints(), 3000);
TS_ASSERT_DELTA(mdews->getBox()->getSignal(), 3000.0, 1e-2);
TS_ASSERT_DELTA(mdews->getBox()->getSignal(), 3021.7071, 1e-2);

// Make a fake instrument - doesn't matter, we won't use it really
Instrument_sptr inst =
Expand All @@ -131,18 +136,20 @@ class IntegratePeaksMDHKLTest : public CxxTest::TestSuite {
// --- Make a fake PeaksWorkspace ---
PeaksWorkspace_sptr peakWS0(new PeaksWorkspace());
peakWS0->setInstrument(inst);
peakWS0->addPeak(Peak(inst, 15050, 1.0));
Peak Pin(inst, 15050, 1.0);
Pin.setHKL(V3D(1,1,1));
peakWS0->addPeak(Pin);

TS_ASSERT_EQUALS(peakWS0->getPeak(0).getIntensity(), 0.0);
AnalysisDataService::Instance().add("IntegratePeaksMDHKLTest_peaks", peakWS0);

// ------------- Integrating with cylinder ------------------------
doRun("IntegratePeaksMDHKLTest_peaks");

TS_ASSERT_DELTA(peakWS0->getPeak(0).getIntensity(), 2.0, 1e-2);
TS_ASSERT_DELTA(peakWS0->getPeak(0).getIntensity(), 29.4284, 1e-2);

// Error is also calculated
TS_ASSERT_DELTA(peakWS0->getPeak(0).getSigmaIntensity(), M_SQRT2, 1e-2);
TS_ASSERT_DELTA(peakWS0->getPeak(0).getSigmaIntensity(), 5.2813, 1e-2);

}

Expand All @@ -155,7 +162,7 @@ class IntegratePeaksMDHKLTest : public CxxTest::TestSuite {
* 1 < r < 2 : density 1/2
* 2 < r < 3 : density 1/3
*/
addPeak(1000, 0., 0., 0., 1.0);
addPeak(1000, 1., 1., 1., 0.1);
addPeak(1000 * 4, 0., 0., 0.,
2.0); // 8 times the volume / 4 times the counts = 1/2 density
addPeak(1000 * 9, 0., 0., 0.,
Expand All @@ -165,31 +172,20 @@ class IntegratePeaksMDHKLTest : public CxxTest::TestSuite {
PeaksWorkspace_sptr peakWS(new PeaksWorkspace());
Instrument_sptr inst =
ComponentCreationHelper::createTestInstrumentCylindrical(5);
peakWS->addPeak(Peak(inst, 1, 1.0, V3D(0., 0., 0.)));
Peak Pin(inst, 1, 1.0);
Pin.setHKL(V3D(1,1,1));
peakWS->addPeak(Pin);
TS_ASSERT_EQUALS(peakWS->getPeak(0).getIntensity(), 0.0);
AnalysisDataService::Instance().addOrReplace("IntegratePeaksMDHKLTest_peaks",
peakWS);

// Set background from 2.0 to 3.0.
// So the 1/2 density background remains, we subtract the 1/3 density =
// about 1500 counts
doRun("IntegratePeaksMDHKLTest_peaks");
TS_ASSERT_DELTA(peakWS->getPeak(0).getIntensity(), 1000 + 500, 80.0);
TS_ASSERT_DELTA(peakWS->getPeak(0).getIntensity(), 29.4275, 0.1);
// Error is larger, since it is error of peak + error of background
TSM_ASSERT_DELTA("Error has increased",
peakWS->getPeak(0).getSigmaIntensity(), sqrt(1830.0), 2);

}

void test_writes_out_selected_algorithm_parameters() {
createMDEW();

doRun("OutWS");

auto outWS =
AnalysisDataService::Instance().retrieveWS<PeaksWorkspace>("OutWS");
peakWS->getPeak(0).getSigmaIntensity(), 5.2814, 0.1);

TS_ASSERT(outWS->hasIntegratedPeaks());
}

};
Expand Down

0 comments on commit e9b2f68

Please sign in to comment.