Skip to content

Commit

Permalink
refs #9194. Complete set of functional tests.
Browse files Browse the repository at this point in the history
Clearly more testing is always possible, but the functional tests present
here, as well as those on the worker ConnectedComponentLabelingTest suite
provide a decent and useful saftey-net for my upcoming refactoring and
performance improvement work.
  • Loading branch information
OwenArnold committed Mar 28, 2014
1 parent d3049f0 commit bfc39ce
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 9 deletions.
Expand Up @@ -179,7 +179,10 @@ namespace Mantid
}

const SpecialCoordinateSystem mdCoordinates = mdWS->getSpecialCoordinateSystem();
// TODO. check special coordinates have been set. If unknown throw.
if(mdCoordinates == None)
{
throw std::invalid_argument("The coordinate system of the input MDWorkspace cannot be established. Run SetSpecialCoordinates on InputWorkspace.");
}

const double threshold = getProperty("Threshold");
const double radiusEstimate = getProperty("RadiusEstimate");
Expand Down
116 changes: 108 additions & 8 deletions Code/Mantid/Framework/Crystal/test/IntegratePeaksUsingClustersTest.h
Expand Up @@ -50,9 +50,9 @@ namespace
}

// Make a fake peaks workspace and corresponding mdhistoworkspace and return both
MDHistoPeaksWSTuple make_peak_and_md_ws(const std::vector<V3D>& hklValues,
const double& min, const double& max, const double& peakRadius=1,
const size_t nEventsInPeak=1000, const size_t& nBins=100)
MDHistoPeaksWSTuple make_peak_and_md_ws(const std::vector<V3D>& hklValuesVec,
const double& min, const double& max, const std::vector<double>& peakRadiusVec,
const std::vector<size_t>& nEventsInPeakVec, const size_t& nBins=100)
{
Instrument_sptr inst = ComponentCreationHelper::createTestInstrumentRectangular(1, 100, 0.05);

Expand Down Expand Up @@ -89,18 +89,18 @@ namespace
coordsAlg->setProperty("SpecialCoordinates", "HKL");
coordsAlg->execute();

for(size_t i = 0; i<hklValues.size(); ++i)
for(size_t i = 0; i<hklValuesVec.size(); ++i)
{
Peak peak(inst, 15050, 1.0);

const double h = hklValues[i][0];
const double k = hklValues[i][1];
const double l = hklValues[i][2];
const double& h = hklValuesVec[i][0];
const double& k = hklValuesVec[i][1];
const double& l = hklValuesVec[i][2];

peak.setHKL(h, k, l);
peakWS->addPeak(peak);

add_fake_md_peak(mdws, nEventsInPeak, h, k, l, peakRadius);
add_fake_md_peak(mdws, nEventsInPeakVec[i], h, k, l, peakRadiusVec[i]);
}

auto binMDAlg = AlgorithmManager::Instance().create("BinMD");
Expand All @@ -123,6 +123,16 @@ namespace
return MDHistoPeaksWSTuple(outMDWS, peakWS);
}

// Make a fake peaks workspace and corresponding mdhistoworkspace and return both
MDHistoPeaksWSTuple make_peak_and_md_ws(const std::vector<V3D>& hklValues,
const double& min, const double& max, const double& peakRadius=1,
const size_t nEventsInPeak=1000, const size_t& nBins=100)
{
std::vector<size_t> nEventsInPeakVec(hklValues.size(), nEventsInPeak);
std::vector<double> peakRadiusVec(hklValues.size(), peakRadius);
return make_peak_and_md_ws(hklValues, min, max, peakRadiusVec, nEventsInPeakVec, nBins);
}

// Execute the clustering integration algorithm
MDHistoPeaksWSTuple execute_integration(const MDHistoPeaksWSTuple& inputWorkspaces, const double& peakRadius, const double& threshold)
{
Expand All @@ -147,6 +157,9 @@ namespace
}
}

//=====================================================================================
// Functional Tests
//=====================================================================================
class IntegratePeaksUsingClustersTest : public CxxTest::TestSuite
{

Expand Down Expand Up @@ -194,6 +207,48 @@ class IntegratePeaksUsingClustersTest : public CxxTest::TestSuite
TSM_ASSERT_THROWS("InputWorkspace required", alg.execute(), std::runtime_error&);
}

void test_throw_if_special_coordinates_unknown()
{
auto peaksws = WorkspaceCreationHelper::createPeaksWorkspace();
IMDHistoWorkspace_sptr mdws = MDEventsTestHelper::makeFakeMDHistoWorkspace(1,1);

IntegratePeaksUsingClusters alg;
alg.setRethrows(true);
alg.initialize();
alg.setProperty("InputWorkspace", mdws);
alg.setProperty("PeaksWorkspace", peaksws);
alg.setPropertyValue("OutputWorkspaceMD", "out_md");
alg.setPropertyValue("OutputWorkspace", "out_peaks");
TSM_ASSERT_THROWS("Unknown special coordinates", alg.execute(), std::invalid_argument&);
}

void test_threshold_too_high_gives_no_peaks()
{
// ------- Make the fake input
std::vector<V3D> hklValues;
// Add a single peak.
hklValues.push_back(V3D(2,2,2));
const double peakRadius = 1;
const double threshold = 10000; // Threshold will filter out everything given the nEventsInPeak restriction.
const size_t nEventsInPeak = 10000;
MDHistoPeaksWSTuple inputWorkspaces = make_peak_and_md_ws(hklValues, -10, 10, peakRadius, nEventsInPeak);
//-------- Execute the integratioin
MDHistoPeaksWSTuple integratedWorkspaces = execute_integration(inputWorkspaces, peakRadius, threshold);
// ------- Get the integrated results
IMDHistoWorkspace_sptr outClustersWS = integratedWorkspaces.get<0>();
IPeaksWorkspace_sptr outPeaksWS = integratedWorkspaces.get<1>();

std::set<Mantid::signal_t> labelIds;
for(size_t i = 0; i < outClustersWS->getNPoints(); ++i)
{
labelIds.insert(outClustersWS->getSignalAt(i));
}
TSM_ASSERT_EQUALS("Should only have one type of label", 1, labelIds.size());
TSM_ASSERT("Should have 'empy' label", does_contain(labelIds, 0));

TSM_ASSERT_EQUALS("Integrated intensity should be zero since no integration has occured", 0, outPeaksWS->getPeak(0).getIntensity());
TSM_ASSERT_EQUALS("Integrated intensity should be zero since no integration has occured", 0, outPeaksWS->getPeak(0).getSigmaIntensity());
}

void test_integrate_single_peak()
{
Expand Down Expand Up @@ -271,6 +326,51 @@ class IntegratePeaksUsingClustersTest : public CxxTest::TestSuite
TSM_ASSERT_EQUALS("Peaks are identical, so integrated error values should be identical", outPeaksWS->getPeak(0).getSigmaIntensity(), outPeaksWS->getPeak(1).getSigmaIntensity());
}

void test_integrate_two_peaks_of_different_magnitude()
{
// ------- Make the fake input
std::vector<V3D> hklValues;
// Add several peaks. These are NOT overlapping.
hklValues.push_back(V3D(1,1,1));
hklValues.push_back(V3D(6,6,6));
const double peakRadius = 1;
const double threshold = 100;
std::vector<size_t> nEventsInPeakVec;
nEventsInPeakVec.push_back(10000);
nEventsInPeakVec.push_back(20000); // Second peak has DOUBLE the intensity of the firse one.

MDHistoPeaksWSTuple inputWorkspaces = make_peak_and_md_ws(hklValues, -10, 10, std::vector<double>(hklValues.size(), peakRadius), nEventsInPeakVec);
//-------- Execute the integratioin
MDHistoPeaksWSTuple integratedWorkspaces = execute_integration(inputWorkspaces, peakRadius, threshold);
// ------- Get the integrated results
IMDHistoWorkspace_sptr outClustersWS = integratedWorkspaces.get<0>();
IPeaksWorkspace_sptr outPeaksWS = integratedWorkspaces.get<1>();

// ------- Check the results.
// Basic checks
auto mdWS = inputWorkspaces.get<0>();
auto peaksWS = inputWorkspaces.get<1>();
TS_ASSERT_EQUALS(outPeaksWS->getNumberPeaks(), peaksWS->getNumberPeaks());
TS_ASSERT_EQUALS(mdWS->getNPoints(), outClustersWS->getNPoints());
// Check clusters by extracting unique label ids.
std::set<Mantid::signal_t> labelIds;
for(size_t i = 0; i < outClustersWS->getNPoints(); ++i)
{
labelIds.insert(outClustersWS->getSignalAt(i));
}
TSM_ASSERT_EQUALS("N peaks present, so should only have n+1 unique label ids", 3, labelIds.size());

TSM_ASSERT("Should have 'empy' label", does_contain(labelIds, 0));
TSM_ASSERT("Should have non-empy label", does_contain(labelIds, 1));
TSM_ASSERT("Should have non-empy label", does_contain(labelIds, 2));

// Two peaks are identical, so integrated values should be the same.
TSM_ASSERT("Integrated intensity should be greater than zero", outPeaksWS->getPeak(0).getIntensity() > 0);
TSM_ASSERT("Integrated error should be greater than zero", outPeaksWS->getPeak(0).getSigmaIntensity() > 0);
TSM_ASSERT_EQUALS("Second peak is twice as 'bright'", outPeaksWS->getPeak(0).getIntensity() * 2, outPeaksWS->getPeak(1).getIntensity());
TSM_ASSERT_EQUALS("Second peak is twice as 'bright'", outPeaksWS->getPeak(0).getSigmaIntensity() * 2, outPeaksWS->getPeak(1).getSigmaIntensity());
}




Expand Down

0 comments on commit bfc39ce

Please sign in to comment.