From 9b2de3257b756b4d4c758ec31fb359e169a9745b Mon Sep 17 00:00:00 2001 From: Martyn Gigg Date: Tue, 1 Apr 2014 09:45:49 +0100 Subject: [PATCH] Add a DeleteLog algorithm. Refs #9227 --- .../Framework/Algorithms/CMakeLists.txt | 3 + .../inc/MantidAlgorithms/DeleteLog.h | 49 ++++++++++ .../Framework/Algorithms/src/DeleteLog.cpp | 66 ++++++++++++++ .../Framework/Algorithms/test/DeleteLogTest.h | 90 +++++++++++++++++++ 4 files changed, 208 insertions(+) create mode 100644 Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/DeleteLog.h create mode 100644 Code/Mantid/Framework/Algorithms/src/DeleteLog.cpp create mode 100644 Code/Mantid/Framework/Algorithms/test/DeleteLogTest.h diff --git a/Code/Mantid/Framework/Algorithms/CMakeLists.txt b/Code/Mantid/Framework/Algorithms/CMakeLists.txt index 0d4999da78e3..805a9d834e56 100644 --- a/Code/Mantid/Framework/Algorithms/CMakeLists.txt +++ b/Code/Mantid/Framework/Algorithms/CMakeLists.txt @@ -67,6 +67,7 @@ set ( SRC_FILES src/CrossCorrelate.cpp src/CuboidGaugeVolumeAbsorption.cpp src/CylinderAbsorption.cpp + src/DeleteLog.cpp src/DeleteWorkspace.cpp src/DetectorDiagnostic.cpp src/DetectorEfficiencyCor.cpp @@ -294,6 +295,7 @@ set ( INC_FILES inc/MantidAlgorithms/CrossCorrelate.h inc/MantidAlgorithms/CuboidGaugeVolumeAbsorption.h inc/MantidAlgorithms/CylinderAbsorption.h + inc/MantidAlgorithms/DeleteLog.h inc/MantidAlgorithms/DeleteWorkspace.h inc/MantidAlgorithms/DetectorDiagnostic.h inc/MantidAlgorithms/DetectorEfficiencyCor.h @@ -532,6 +534,7 @@ set ( TEST_FILES CropWorkspaceTest.h CuboidGaugeVolumeAbsorptionTest.h CylinderAbsorptionTest.h + DeleteLogTest.h DeleteWorkspaceTest.h DetectorEfficiencyCorTest.h DetectorEfficiencyCorUserTest.h diff --git a/Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/DeleteLog.h b/Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/DeleteLog.h new file mode 100644 index 000000000000..c657578faa06 --- /dev/null +++ b/Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/DeleteLog.h @@ -0,0 +1,49 @@ +#ifndef MANTID_ALGORITHMS_DELETELOG_H_ +#define MANTID_ALGORITHMS_DELETELOG_H_ + +#include "MantidAPI/Algorithm.h" + +namespace Mantid +{ + namespace Algorithms + { + + /** + Copyright © 2014 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory + + This file is part of Mantid. + + Mantid is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + Mantid is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + File change history is stored at: + Code Documentation is available at: + */ + class DLLExport DeleteLog : public API::Algorithm + { + public: + virtual const std::string name() const; + virtual int version() const; + virtual const std::string category() const; + + private: + virtual void initDocs(); + void init(); + void exec(); + }; + + + } // namespace Algorithms +} // namespace Mantid + +#endif /* MANTID_ALGORITHMS_DELETELOG_H_ */ diff --git a/Code/Mantid/Framework/Algorithms/src/DeleteLog.cpp b/Code/Mantid/Framework/Algorithms/src/DeleteLog.cpp new file mode 100644 index 000000000000..ab0e20f627b6 --- /dev/null +++ b/Code/Mantid/Framework/Algorithms/src/DeleteLog.cpp @@ -0,0 +1,66 @@ +/*WIKI* +Removes a named log from the run attached to the input workspace. If the log does not exist then the algorithm simply +emits a warning and does not fail. +*WIKI*/ +#include "MantidAlgorithms/DeleteLog.h" +#include "MantidKernel/MandatoryValidator.h" + +namespace Mantid +{ +namespace Algorithms +{ + using namespace API; + using namespace Kernel; + + // Register the algorithm into the AlgorithmFactory + DECLARE_ALGORITHM(DeleteLog) + + //---------------------------------------------------------------------------------------------- + /// @copydoc Algorithm::name + const std::string DeleteLog::name() const { return "DeleteLog"; } + + /// @copydoc Algorithm::version + int DeleteLog::version() const { return 1; } + + /// @copydoc Algorithm::category + const std::string DeleteLog::category() const { return "DataHandling\\Logs";} + + //---------------------------------------------------------------------------------------------- + /// Sets documentation strings for this algorithm + void DeleteLog::initDocs() + { + this->setWikiSummary("Removes a named log from a run"); + this->setOptionalMessage("Removes a named log from a run"); + } + + //---------------------------------------------------------------------------------------------- + /** Initialize the algorithm's properties. + */ + void DeleteLog::init() + { + declareProperty(new WorkspaceProperty<>("Workspace","",Direction::InOut), + "In/out workspace containing the logs. The workspace is modified in place"); + declareProperty("Name","", boost::make_shared>(), + "", Direction::Input); + } + + //---------------------------------------------------------------------------------------------- + /** Execute the algorithm. + */ + void DeleteLog::exec() + { + MatrixWorkspace_sptr logWS = getProperty("Workspace"); + std::string logName = getProperty("Name"); + auto & run = logWS->mutableRun(); + if(run.hasProperty(logName)) + { + run.removeLogData(logName); + } + else + { + g_log.warning() << "Unable to delete log '" << logName << "' from the given workspace as it does not exist.\n"; + } + } + +} // namespace Algorithms +} // namespace Mantid diff --git a/Code/Mantid/Framework/Algorithms/test/DeleteLogTest.h b/Code/Mantid/Framework/Algorithms/test/DeleteLogTest.h new file mode 100644 index 000000000000..972b04161fa1 --- /dev/null +++ b/Code/Mantid/Framework/Algorithms/test/DeleteLogTest.h @@ -0,0 +1,90 @@ +#ifndef MANTID_ALGORITHMS_DELETELOGTEST_H_ +#define MANTID_ALGORITHMS_DELETELOGTEST_H_ + +#include + +#include "MantidAlgorithms/DeleteLog.h" +#include "MantidKernel/TimeSeriesProperty.h" +#include "MantidTestHelpers/WorkspaceCreationHelper.h" + + +class DeleteLogTest : 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 DeleteLogTest *createSuite() { return new DeleteLogTest(); } + static void destroySuite( DeleteLogTest *suite ) { delete suite; } + + // -------------------------- Success tests -------------------------- + + void test_Init() + { + Mantid::Algorithms::DeleteLog alg; + TS_ASSERT_THROWS_NOTHING( alg.initialize() ) + TS_ASSERT( alg.isInitialized() ) + } + + void test_non_existant_log_does_not_throw_error() + { + Mantid::Algorithms::DeleteLog alg; + alg.initialize(); + alg.setChild(true); // no ADS storage + alg.setRethrows(true); + TS_ASSERT_THROWS_NOTHING(alg.setProperty("Name", "NotALog")); + auto ws = WorkspaceCreationHelper::Create2DWorkspace(10,10); + alg.setProperty("Workspace", ws); + TS_ASSERT_THROWS_NOTHING(alg.execute()); + } + + void test_single_value_log_is_deleted() + { + Mantid::Algorithms::DeleteLog alg; + alg.initialize(); + alg.setChild(true); // no ADS storage + alg.setRethrows(true); + auto ws = WorkspaceCreationHelper::Create2DWorkspace(10,10); + std::string logName("SingleValue"); + ws->mutableRun().addProperty(logName, 1.0); + alg.setProperty("Workspace", ws); + + TS_ASSERT_THROWS_NOTHING(alg.setProperty("Name", logName)); + TS_ASSERT_THROWS_NOTHING(alg.execute()); + + TS_ASSERT(!ws->run().hasProperty(logName)); + } + + void test_time_series_log_is_deleted() + { + Mantid::Algorithms::DeleteLog alg; + alg.initialize(); + alg.setChild(true); // no ADS storage + alg.setRethrows(true); + auto ws = WorkspaceCreationHelper::Create2DWorkspace(10,10); + std::string logName("TimeSeries"); + + auto *tsp = new Mantid::Kernel::TimeSeriesProperty(logName); + tsp->addValue("2010-09-14T04:20:12", 20.0); + ws->mutableRun().addProperty(tsp); + alg.setProperty("Workspace", ws); + + TS_ASSERT_THROWS_NOTHING(alg.setProperty("Name", logName)); + TS_ASSERT_THROWS_NOTHING(alg.execute()); + + TS_ASSERT(!ws->run().hasProperty(logName)); + } + + + // -------------------------- Failure tests -------------------------- + + void test_empty_log_name_throws_invalid_argument() + { + Mantid::Algorithms::DeleteLog alg; + alg.initialize(); + TS_ASSERT_THROWS(alg.setProperty("Name", ""), std::invalid_argument); + } + +}; + + +#endif /* MANTID_ALGORITHMS_DELETELOGTEST_H_ */