Skip to content

Commit

Permalink
Add a DeleteLog algorithm.
Browse files Browse the repository at this point in the history
Refs #9227
  • Loading branch information
martyngigg committed Apr 1, 2014
1 parent a43f69a commit 9b2de32
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/Algorithms/CMakeLists.txt
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -532,6 +534,7 @@ set ( TEST_FILES
CropWorkspaceTest.h
CuboidGaugeVolumeAbsorptionTest.h
CylinderAbsorptionTest.h
DeleteLogTest.h
DeleteWorkspaceTest.h
DetectorEfficiencyCorTest.h
DetectorEfficiencyCorUserTest.h
Expand Down
49 changes: 49 additions & 0 deletions 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 <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
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_ */
66 changes: 66 additions & 0 deletions 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<MandatoryValidator<std::string>>(),
"", 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
90 changes: 90 additions & 0 deletions Code/Mantid/Framework/Algorithms/test/DeleteLogTest.h
@@ -0,0 +1,90 @@
#ifndef MANTID_ALGORITHMS_DELETELOGTEST_H_
#define MANTID_ALGORITHMS_DELETELOGTEST_H_

#include <cxxtest/TestSuite.h>

#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<double>(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<double>(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_ */

0 comments on commit 9b2de32

Please sign in to comment.