From 579071ada97de816bf03ae5a070217df2482254b Mon Sep 17 00:00:00 2001 From: Wenduo Zhou Date: Thu, 17 Apr 2014 13:39:11 -0400 Subject: [PATCH] Added unit test. Refs #9326. Init unit test file; Modified CMake; Reformat .h and .cpp files --- .../Framework/Algorithms/CMakeLists.txt | 1 + .../inc/MantidAlgorithms/ChopEventFilters.h | 103 +++++----- .../Algorithms/src/ChopEventFilters.cpp | 176 +++++++++--------- .../Algorithms/test/ChopEventFiltersTest.h | 162 ++++++++++++++++ 4 files changed, 302 insertions(+), 140 deletions(-) create mode 100644 Code/Mantid/Framework/Algorithms/test/ChopEventFiltersTest.h diff --git a/Code/Mantid/Framework/Algorithms/CMakeLists.txt b/Code/Mantid/Framework/Algorithms/CMakeLists.txt index e69c904d77fe..96f03ff1eb93 100644 --- a/Code/Mantid/Framework/Algorithms/CMakeLists.txt +++ b/Code/Mantid/Framework/Algorithms/CMakeLists.txt @@ -672,6 +672,7 @@ set ( TEST_FILES WeightingStrategyTest.h WorkspaceCreationHelperTest.h WorkspaceGroupTest.h + ChopEventFiltersTest.h ) set ( TEST_PY_FILES NormaliseToUnityTest.py ) diff --git a/Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/ChopEventFilters.h b/Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/ChopEventFilters.h index 70afed4ab1b4..5ecd28625ecd 100644 --- a/Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/ChopEventFilters.h +++ b/Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/ChopEventFilters.h @@ -7,11 +7,11 @@ namespace Mantid { - namespace Algorithms - { - - /** - * Add a peak to a PeaksWorkspace. +namespace Algorithms +{ + + /** + * Add a peak to a PeaksWorkspace. @date 2012-10-16 @@ -35,53 +35,52 @@ namespace Mantid File change history is stored at: Code Documentation is available at: */ - class DLLExport ChopEventFilters : public API::Algorithm - { - public: - ChopEventFilters(); - ~ChopEventFilters(); - - /// Algorithm's name for identification - virtual const std::string name() const { return "ChopEventFilters";}; - /// Algorithm's version for identification - virtual int version() const { return 1;}; - /// Algorithm's category for identification - virtual const std::string category() const { return "Events\\EventFiltering";} - - private: - /// Sets documentation strings for this algorithm - virtual void initDocs(); - /// Initialise the properties - void init(); - /// Run the algorithm - void exec(); - - void processAlgProperties(); - void chopEventFilterByTime(); - - - /// Input (source) time splitters workspace in MatrixWorkspace format - API::MatrixWorkspace_const_sptr m_inputWS; - - /// Output time splitters workspace in MatrixWorkspace format - API::MatrixWorkspace_sptr m_outputWS; - - /// Number of total time slots for chopping - int m_numSlots; - - /// Index of the time slot for output - int m_slotIndex; - - /// Target worskpace group to be chopped - int m_wsGroup; - - std::vector m_outX; - std::vector m_outY; - - }; - - - } // namespace Mantid + class DLLExport ChopEventFilters : public API::Algorithm + { + public: + ChopEventFilters(); + ~ChopEventFilters(); + + /// Algorithm's name for identification + virtual const std::string name() const { return "ChopEventFilters";}; + /// Algorithm's version for identification + virtual int version() const { return 1;}; + /// Algorithm's category for identification + virtual const std::string category() const { return "Events\\EventFiltering";} + + private: + /// Sets documentation strings for this algorithm + virtual void initDocs(); + /// Initialise the properties + void init(); + /// Run the algorithm + void exec(); + + void processAlgProperties(); + void chopEventFilterByTime(); + + + /// Input (source) time splitters workspace in MatrixWorkspace format + API::MatrixWorkspace_const_sptr m_inputWS; + + /// Output time splitters workspace in MatrixWorkspace format + API::MatrixWorkspace_sptr m_outputWS; + + /// Number of total time slots for chopping + int m_numSlots; + + /// Index of the time slot for output + int m_slotIndex; + + /// Target worskpace group to be chopped + int m_wsGroup; + + std::vector m_outX; + std::vector m_outY; + + }; + +} // namespace Mantid } // namespace Algorithms diff --git a/Code/Mantid/Framework/Algorithms/src/ChopEventFilters.cpp b/Code/Mantid/Framework/Algorithms/src/ChopEventFilters.cpp index 032bc9889118..70fcd4cfbe80 100644 --- a/Code/Mantid/Framework/Algorithms/src/ChopEventFilters.cpp +++ b/Code/Mantid/Framework/Algorithms/src/ChopEventFilters.cpp @@ -30,40 +30,40 @@ namespace Mantid namespace Algorithms { -using namespace Mantid::API; -using namespace Mantid::Kernel; + using namespace Mantid::API; + using namespace Mantid::Kernel; -using namespace std; + using namespace std; -const double TOL = 1.0E-5; + const double TOL = 1.0E-5; -DECLARE_ALGORITHM(ChopEventFilters) + DECLARE_ALGORITHM(ChopEventFilters) -ChopEventFilters::ChopEventFilters() -{ -} + ChopEventFilters::ChopEventFilters() + { + } -ChopEventFilters::~ChopEventFilters() -{ -} + ChopEventFilters::~ChopEventFilters() + { + } -/** Wiki documentation - */ -void ChopEventFilters::initDocs() -{ + /** Wiki documentation + */ + void ChopEventFilters::initDocs() + { setWikiSummary("Chop the event filters for a specified workspace group to even time slots " "and output one slot to a new workspace. "); -} - -/** Declare properties - */ -void ChopEventFilters::init() -{ - declareProperty(new WorkspaceProperty("InputWorkspace", "", Direction::Input), - "Name of input event filter workspace to be processed."); + } + + /** Declare properties + */ + void ChopEventFilters::init() + { + declareProperty(new WorkspaceProperty("InputWorkspace", "", Direction::Input), + "Name of input event filter workspace to be processed."); declareProperty(new WorkspaceProperty("OutputWorkspace", "", Direction::Output), - "Name of the output event filter workspace as a result of processing."); + "Name of the output event filter workspace as a result of processing."); declareProperty("WorkspaceGroup", EMPTY_INT(), "Workspace group of the evnet filers to be processed."); @@ -73,16 +73,16 @@ void ChopEventFilters::init() // FIXME - Need a validator for slot index declareProperty("IndexOfSlot", EMPTY_INT(), "Index of the time slot to be kept."); -} + } -/** Main execution - */ -void ChopEventFilters::exec() -{ + /** Main execution + */ + void ChopEventFilters::exec() + { // Process inputs processAlgProperties(); - // Calculating splitted + // Calculating splitted chopEventFilterByTime(); // Finale @@ -90,38 +90,38 @@ void ChopEventFilters::exec() else throw std::runtime_error("Output workspace is a null pointer."); return; -} + } -/** Process input property - */ -void ChopEventFilters::processAlgProperties() -{ + /** Process input property + */ + void ChopEventFilters::processAlgProperties() + { m_inputWS = getProperty("InputWorkspace"); m_wsGroup = getProperty("WorkspaceGroup"); if (isEmpty(m_wsGroup)) - throw std::runtime_error("Workspace must be given."); + throw std::runtime_error("Workspace must be given."); m_numSlots = getProperty("NumberOfSlots"); m_slotIndex = getProperty("IndexOfSlot"); if (m_slotIndex >= m_numSlots) - throw std::runtime_error("Slot index is out of boundary as number of slots."); + throw std::runtime_error("Slot index is out of boundary as number of slots."); return; -} + } -/** Chop the selected splitters by time - */ -void ChopEventFilters::chopEventFilterByTime() -{ + /** Chop the selected splitters by time + */ + void ChopEventFilters::chopEventFilterByTime() + { double targetWSgroup = static_cast (m_wsGroup); const MantidVec& vecX = m_inputWS->readX(0); const MantidVec& vecY = m_inputWS->readY(0); if (vecY.size() == vecX.size()) { - throw runtime_error("Input workspace is wrong at x and y's sizes."); + throw runtime_error("Input workspace is wrong at x and y's sizes."); } // Find out splitters with matched workspace group @@ -130,56 +130,56 @@ void ChopEventFilters::chopEventFilterByTime() size_t numfilters = vecY.size(); for (size_t i = 0; i < numfilters; ++i) { - double tmpgroup = vecY[i]; - if (fabs(tmpgroup - targetWSgroup) < TOL) + double tmpgroup = vecY[i]; + if (fabs(tmpgroup - targetWSgroup) < TOL) + { + // Found a group + double abs_start = vecX[i]; + double abs_end = vecX[i+1]; + double deltaT = (abs_end - abs_start)*invertN; + + // Set up new start time and end time + double newstart = abs_start + slotindex_d * deltaT; + double newend = newstart + deltaT; + + // Append to output vector + if (m_outX.size() == 0) { - // Found a group - double abs_start = vecX[i]; - double abs_end = vecX[i+1]; - double deltaT = (abs_end - abs_start)*invertN; - - // Set up new start time and end time - double newstart = abs_start + slotindex_d * deltaT; - double newend = newstart + deltaT; - - // Append to output vector - if (m_outX.size() == 0) - { - // First entry - m_outX.push_back(newstart); - m_outX.push_back(newend); - m_outY.push_back(targetWSgroup); - } - else - { - // Appending to previous - if (newstart - m_outX.back() > TOL) - { - // This splitter is not adjacent - m_outX.push_back(newstart); - m_outX.push_back(newend); - m_outY.push_back(-1); - m_outY.push_back(targetWSgroup); - } - else - { - // New splitter is same and adjacent to previous one. - m_outX.back() = newend; - g_log.warning("It is not likely to happen that new splitter is continous to previous one."); - } - } - + // First entry + m_outX.push_back(newstart); + m_outX.push_back(newend); + m_outY.push_back(targetWSgroup); } - + else + { + // Appending to previous + if (newstart - m_outX.back() > TOL) + { + // This splitter is not adjacent + m_outX.push_back(newstart); + m_outX.push_back(newend); + m_outY.push_back(-1); + m_outY.push_back(targetWSgroup); + } + else + { + // New splitter is same and adjacent to previous one. + m_outX.back() = newend; + g_log.warning("It is not likely to happen that new splitter is continous to previous one."); + } + } + + } + } /// END(for i) // Check if (m_outX.size() == 0) { - std::stringstream errss; - errss << "Input workspace does not have any splitters with target workspace group " + std::stringstream errss; + errss << "Input workspace does not have any splitters with target workspace group " << m_wsGroup << ", as user specified. "; - throw runtime_error(errss.str()); + throw runtime_error(errss.str()); } // Build output workspace group @@ -190,13 +190,13 @@ void ChopEventFilters::chopEventFilterByTime() MantidVec& dataY = m_outputWS->dataY(0); for (size_t i = 0; i < sizey; ++i) { - dataX[i] = m_outX[i]; - dataY[i] = m_outY[i]; + dataX[i] = m_outX[i]; + dataY[i] = m_outY[i]; } dataX[sizey] = m_outX[sizey]; return; -} + } } } diff --git a/Code/Mantid/Framework/Algorithms/test/ChopEventFiltersTest.h b/Code/Mantid/Framework/Algorithms/test/ChopEventFiltersTest.h new file mode 100644 index 000000000000..a0ef315c15e1 --- /dev/null +++ b/Code/Mantid/Framework/Algorithms/test/ChopEventFiltersTest.h @@ -0,0 +1,162 @@ +#ifndef MANTID_ALGORITHMS_CHOPEVENTFILTERS_H_ +#define MANTID_ALGORITHMS_CHOPEVENTFILTERS_H_ + +#include + +#include "MantidAlgorithms/ChopEventFilters.h" +#include "MantidAPI/MatrixWorkspace.h" +#include "MantidAPI/FrameworkManager.h" +#include "MantidAPI/WorkspaceFactory.h" +#include "MantidDataObjects/Workspace2D.h" + +using Mantid::Algorithms::ChopEventFilters; + +using namespace Mantid; +using namespace Mantid::API; +using namespace Mantid::Kernel; +using namespace Mantid::DataObjects; + +using namespace std; + +class FitPeakTest : public CxxTest::TestSuite +{ +public: + // This pair of boilerplate methods prevent the suite being created statically + // This means the constructor isn't called when running other tests + static FitPeakTest *createSuite() + { + API::FrameworkManager::Instance(); + return new FitPeakTest(); + } + static void destroySuite( FitPeakTest *suite ) { delete suite; } + + //---------------------------------------------------------------------------------------------- + /** Test on init and setup + */ + void test_Init() + { + // Generate input workspace + MatrixWorkspace_sptr dataws = gen_4866P5Data(); + AnalysisDataService::Instance().addOrReplace("PG3_4866Peak5", dataws); + + // Generate peak and background parameters + vector peakparnames, bkgdparnames; + vector peakparvalues, bkgdparvalues; + + gen_BkgdParameters(bkgdparnames, bkgdparvalues); + gen_PeakParameters(peakparnames, peakparvalues); + + // Initialize FitPeak + ChopEventFilters fitpeak; + + fitpeak.initialize(); + TS_ASSERT(fitpeak.isInitialized()); + + // Set properties + TS_ASSERT_THROWS_NOTHING(fitpeak.setPropertyValue("InputWorkspace", "PG3_4866Peak5")); + TS_ASSERT_THROWS_NOTHING(fitpeak.setPropertyValue("OutputWorkspace", "FittedPeak")); + TS_ASSERT_THROWS_NOTHING(fitpeak.setPropertyValue("ParameterTableWorkspace", "Fitted_Peak5_Parameters")); + TS_ASSERT_THROWS_NOTHING(fitpeak.setProperty("WorkspaceIndex", 0)); + TS_ASSERT_THROWS_NOTHING(fitpeak.setProperty("PeakFunctionType", "Gaussian")); + TS_ASSERT_THROWS_NOTHING(fitpeak.setProperty("PeakParameterNames", peakparnames)); + TS_ASSERT_THROWS_NOTHING(fitpeak.setProperty("PeakParameterValues", peakparvalues)); + TS_ASSERT_THROWS_NOTHING(fitpeak.setProperty("BackgroundType", "Linear")); + TS_ASSERT_THROWS_NOTHING(fitpeak.setProperty("BackgroundParameterNames", bkgdparnames)); + TS_ASSERT_THROWS_NOTHING(fitpeak.setProperty("BackgroundParameterValues", bkgdparvalues)); + TS_ASSERT_THROWS_NOTHING(fitpeak.setPropertyValue("FitWindow", "10.0, 20.0")); + TS_ASSERT_THROWS_NOTHING(fitpeak.setPropertyValue("PeakRange", "11.0, 12.0")); + TS_ASSERT_THROWS_NOTHING(fitpeak.setProperty("FitBackgroundFirst", true)); + + // Clean + AnalysisDataService::Instance().remove("PG3_4866Peak5"); + AnalysisDataService::Instance().remove("Peak5_Parameters"); + + return; + } + + //---------------------------------------------------------------------------------------------- + /** Generate a workspace contains PG3_4866 5-th peak + */ + MatrixWorkspace_sptr gen_PG3DiamondData() + { + vector vecx, vecy, vece; + + vecx.push_back(2.050678); vecy.push_back(1.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.051498); vecy.push_back(0.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.052319); vecy.push_back(0.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.053140); vecy.push_back(2.000000 ); vece.push_back(1.41421E+00); + vecx.push_back(2.053961); vecy.push_back(0.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.054783); vecy.push_back(0.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.055605); vecy.push_back(2.000000 ); vece.push_back(1.41421E+00); + vecx.push_back(2.056427); vecy.push_back(2.000000 ); vece.push_back(1.41421E+00); + vecx.push_back(2.057250); vecy.push_back(3.000000 ); vece.push_back(1.73205E+00); + vecx.push_back(2.058072); vecy.push_back(4.000000 ); vece.push_back(2.00000E+00); + vecx.push_back(2.058896); vecy.push_back(5.000000 ); vece.push_back(2.23607E+00); + vecx.push_back(2.059719); vecy.push_back(16.000000); vece.push_back(4.00000E+00); + vecx.push_back(2.060543); vecy.push_back(20.000000); vece.push_back(4.47214E+00); + vecx.push_back(2.061367); vecy.push_back(31.000000); vece.push_back(5.56776E+00); + vecx.push_back(2.062192); vecy.push_back(26.000000); vece.push_back(5.09902E+00); + vecx.push_back(2.063017); vecy.push_back(28.000000); vece.push_back(5.29150E+00); + vecx.push_back(2.063842); vecy.push_back(29.000000); vece.push_back(5.38516E+00); + vecx.push_back(2.064668); vecy.push_back(41.000000); vece.push_back(6.40312E+00); + vecx.push_back(2.065493); vecy.push_back(40.000000); vece.push_back(6.32456E+00); + vecx.push_back(2.066320); vecy.push_back(38.000000); vece.push_back(6.16441E+00); + vecx.push_back(2.067146); vecy.push_back(40.000000); vece.push_back(6.32456E+00); + vecx.push_back(2.067973); vecy.push_back(34.000000); vece.push_back(5.83095E+00); + vecx.push_back(2.068800); vecy.push_back(35.000000); vece.push_back(5.91608E+00); + vecx.push_back(2.069628); vecy.push_back(18.000000); vece.push_back(4.24264E+00); + vecx.push_back(2.070456); vecy.push_back(21.000000); vece.push_back(4.58258E+00); + vecx.push_back(2.071284); vecy.push_back(9.000000 ); vece.push_back(3.00000E+00); + vecx.push_back(2.072112); vecy.push_back(6.000000 ); vece.push_back(2.44949E+00); + vecx.push_back(2.072941); vecy.push_back(6.000000 ); vece.push_back(2.44949E+00); + vecx.push_back(2.073770); vecy.push_back(11.000000); vece.push_back(3.31662E+00); + vecx.push_back(2.074600); vecy.push_back(10.000000); vece.push_back(3.16228E+00); + vecx.push_back(2.075430); vecy.push_back(4.000000 ); vece.push_back(2.00000E+00); + vecx.push_back(2.076260); vecy.push_back(7.000000 ); vece.push_back(2.64575E+00); + vecx.push_back(2.077090); vecy.push_back(0.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.077921); vecy.push_back(1.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.078752); vecy.push_back(1.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.079584); vecy.push_back(1.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.080416); vecy.push_back(0.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.081248); vecy.push_back(0.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.082080); vecy.push_back(0.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.082913); vecy.push_back(0.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.083746); vecy.push_back(2.000000 ); vece.push_back(1.41421E+00); + vecx.push_back(2.084580); vecy.push_back(1.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.085414); vecy.push_back(1.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.086248); vecy.push_back(1.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.087082); vecy.push_back(0.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.087917); vecy.push_back(1.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.088752); vecy.push_back(0.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.089588); vecy.push_back(0.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.090424); vecy.push_back(0.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.091260); vecy.push_back(0.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.092096); vecy.push_back(0.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.092933); vecy.push_back(0.000000 ); vece.push_back(1.00000E+00); + vecx.push_back(2.093770); vecy.push_back(0.000000 ); vece.push_back(1.00000E+00); + + size_t NVectors = 1; + size_t sizex = vecx.size(); + size_t sizey = vecy.size(); + MatrixWorkspace_sptr ws = boost::dynamic_pointer_cast( + WorkspaceFactory::Instance().create("Workspace2D", NVectors, sizex, sizey)); + + MantidVec& vecX = ws->dataX(0); + MantidVec& vecY = ws->dataY(0); + MantidVec& vecE = ws->dataE(0); + + for (size_t i = 0; i < sizex; ++i) + vecX[i] = vecx[i]; + for (size_t i = 0; i < sizey; ++i) + { + vecY[i] = vecy[i]; + vecE[i] = vece[i]; + } + + return ws; + } + +}; + + +#endif /* MANTID_ALGORITHMS_CHOPEVENTFILTERS_H_ */