Skip to content
Snippets Groups Projects
Commit c9e59bdf authored by Janik Zikovsky's avatar Janik Zikovsky
Browse files

Fixes #3124: AddSampleLog extended to create other log types (numbers and number series)

parent 021a2aa5
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
//---------------------------------------------------------------------- //----------------------------------------------------------------------
#include "MantidAlgorithms/AddSampleLog.h" #include "MantidAlgorithms/AddSampleLog.h"
#include "MantidKernel/Exception.h" #include "MantidKernel/Exception.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/Strings.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include <string> #include <string>
namespace Mantid namespace Mantid
...@@ -16,8 +19,8 @@ DECLARE_ALGORITHM(AddSampleLog) ...@@ -16,8 +19,8 @@ DECLARE_ALGORITHM(AddSampleLog)
/// Sets documentation strings for this algorithm /// Sets documentation strings for this algorithm
void AddSampleLog::initDocs() void AddSampleLog::initDocs()
{ {
this->setWikiSummary("Used to insert a single string into the sample in a workspace "); this->setWikiSummary("Used to insert a value into the sample logs in a workspace.");
this->setOptionalMessage("Used to insert a single string into the sample in a workspace"); this->setOptionalMessage("Used to insert a value into the sample logs in a workspace.");
} }
using namespace Kernel; using namespace Kernel;
...@@ -29,6 +32,15 @@ void AddSampleLog::init() ...@@ -29,6 +32,15 @@ void AddSampleLog::init()
"Workspace to add the log entry to"); "Workspace to add the log entry to");
declareProperty("LogName", "", new MandatoryValidator<std::string>, declareProperty("LogName", "", new MandatoryValidator<std::string>,
"The name that will identify the log entry"); "The name that will identify the log entry");
std::vector<std::string> propOptions;
propOptions.push_back("String");
propOptions.push_back("Number");
propOptions.push_back("Number Series");
declareProperty("LogType", "String",new ListValidator(propOptions),
"The type that the log data will be."
);
declareProperty("LogText", "", declareProperty("LogText", "",
"The content of the log"); "The content of the log");
} }
...@@ -43,8 +55,33 @@ void AddSampleLog::exec() ...@@ -43,8 +55,33 @@ void AddSampleLog::exec()
// get the data that the user wants to add // get the data that the user wants to add
std::string propName = getProperty("LogName"); std::string propName = getProperty("LogName");
std::string propValue = getProperty("LogText"); std::string propValue = getProperty("LogText");
std::string propType = getPropertyValue("LogType");
// Remove any existing log
if (theRun.hasProperty(propName))
theRun.removeLogData(propName);
theRun.addLogData(new PropertyWithValue<std::string>(propName, propValue)); if (propType == "String")
{
theRun.addLogData(new PropertyWithValue<std::string>(propName, propValue));
}
else if (propType == "Number")
{
double val;
if (!Strings::convert(propValue, val))
throw std::invalid_argument("Error interpreting string '" + propValue + "' as a number.");
theRun.addLogData(new PropertyWithValue<double>(propName, val));
}
else if (propType == "Number Series")
{
double val;
if (!Strings::convert(propValue, val))
throw std::invalid_argument("Error interpreting string '" + propValue + "' as a number.");
Kernel::DateAndTime now = Kernel::DateAndTime::get_current_time();
TimeSeriesProperty<double> * tsp = new TimeSeriesProperty<double>(propName);
tsp->addValue(now, val);
theRun.addLogData(tsp);
}
setProperty("Workspace", wSpace); setProperty("Workspace", wSpace);
} }
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "MantidTestHelpers/WorkspaceCreationHelper.h" #include "MantidTestHelpers/WorkspaceCreationHelper.h"
#include "MantidAPI/MatrixWorkspace.h" #include "MantidAPI/MatrixWorkspace.h"
#include "MantidAlgorithms/AddSampleLog.h" #include "MantidAlgorithms/AddSampleLog.h"
#include "MantidKernel/TimeSeriesProperty.h"
using namespace Mantid::Kernel; using namespace Mantid::Kernel;
using namespace Mantid::API; using namespace Mantid::API;
...@@ -17,20 +18,56 @@ class AddSampleLogTest : public CxxTest::TestSuite ...@@ -17,20 +18,56 @@ class AddSampleLogTest : public CxxTest::TestSuite
{ {
public: public:
void testInsertion2D() void test_Workspace2D()
{ {
ExecuteAlgorithm(WorkspaceCreationHelper::Create2DWorkspace(10,10)); MatrixWorkspace_sptr ws = WorkspaceCreationHelper::Create2DWorkspace(10,10);
ExecuteAlgorithm(ws, "My Name", "String", "My Value", 0.0);
} }
void testInsertionEvent() void test_EventWorkspace()
{ {
ExecuteAlgorithm(WorkspaceCreationHelper::CreateEventWorkspace(10,10)); MatrixWorkspace_sptr ws = WorkspaceCreationHelper::CreateEventWorkspace(10,10);
ExecuteAlgorithm(ws, "My Name", "String", "My Value", 0.0);
} }
void ExecuteAlgorithm(MatrixWorkspace_sptr testWS) void test_CanOverwrite()
{
MatrixWorkspace_sptr ws = WorkspaceCreationHelper::Create2DWorkspace(10,10);
ExecuteAlgorithm(ws, "My Name", "String", "My Value", 0.0);
ExecuteAlgorithm(ws, "My Name", "String", "My New Value", 0.0);
}
void test_Number()
{
MatrixWorkspace_sptr ws = WorkspaceCreationHelper::Create2DWorkspace(10,10);
ExecuteAlgorithm(ws, "My Name", "Number", "1.234", 1.234);
ExecuteAlgorithm(ws, "My Name", "Number", "2.456", 2.456);
}
void test_BadNumber()
{
MatrixWorkspace_sptr ws = WorkspaceCreationHelper::Create2DWorkspace(10,10);
ExecuteAlgorithm(ws, "My Name", "Number", "OneTwoThreeFour", 0.0, true);
}
void test_BadNumberSeries()
{
MatrixWorkspace_sptr ws = WorkspaceCreationHelper::Create2DWorkspace(10,10);
ExecuteAlgorithm(ws, "My Name", "Number Series", "FiveSixSeven", 0.0, true);
}
void test_NumberSeries()
{
MatrixWorkspace_sptr ws = WorkspaceCreationHelper::Create2DWorkspace(10,10);
ExecuteAlgorithm(ws, "My Name", "Number Series", "1.234", 1.234);
ExecuteAlgorithm(ws, "My Name", "Number Series", "2.456", 2.456);
}
void ExecuteAlgorithm(MatrixWorkspace_sptr testWS, std::string LogName, std::string LogType, std::string LogText,
double expectedValue, bool fails=false)
{ {
//add the workspace to the ADS //add the workspace to the ADS
AnalysisDataService::Instance().add("AddSampleLogTest_Temporary", testWS); AnalysisDataService::Instance().addOrReplace("AddSampleLogTest_Temporary", testWS);
//execute algorithm //execute algorithm
AddSampleLog alg; AddSampleLog alg;
...@@ -38,20 +75,45 @@ public: ...@@ -38,20 +75,45 @@ public:
TS_ASSERT( alg.isInitialized() ) TS_ASSERT( alg.isInitialized() )
alg.setPropertyValue("Workspace", "AddSampleLogTest_Temporary"); alg.setPropertyValue("Workspace", "AddSampleLogTest_Temporary");
alg.setPropertyValue("LogName", "my name"); alg.setPropertyValue("LogName", LogName);
alg.setPropertyValue("LogText", "my data"); alg.setPropertyValue("LogText", LogText);
alg.setPropertyValue("LogType", LogType);
TS_ASSERT_THROWS_NOTHING(alg.execute()) TS_ASSERT_THROWS_NOTHING(alg.execute())
TS_ASSERT( alg.isExecuted() ) if (fails)
{
TS_ASSERT( !alg.isExecuted() )
return;
}
else
{
TS_ASSERT( alg.isExecuted() )
}
//check output //check output
MatrixWorkspace_sptr output = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve(alg.getProperty("Workspace"))); MatrixWorkspace_sptr output = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve(alg.getProperty("Workspace")));
const Run& wSpaceRun = output->run(); const Run& wSpaceRun = output->run();
PropertyWithValue<std::string> *testProp = dynamic_cast<PropertyWithValue<std::string>*>(wSpaceRun.getLogData("my name")); Property * prop;
TS_ASSERT_THROWS_NOTHING(prop = wSpaceRun.getLogData(LogName);)
if (!prop) return;
if (LogType == "String")
{
TS_ASSERT_EQUALS( prop->value(), LogText);
}
else if (LogType == "Number")
{
PropertyWithValue<double> *testProp = dynamic_cast<PropertyWithValue<double>*>(prop);
TS_ASSERT(testProp);
TS_ASSERT_DELTA((*testProp)(), expectedValue, 1e-5);
}
else if (LogType == "Number Series")
{
TimeSeriesProperty<double> *testProp = dynamic_cast<TimeSeriesProperty<double>*>(prop);
TS_ASSERT(testProp);
TS_ASSERT_DELTA(testProp->firstValue(), expectedValue, 1e-5);
}
TS_ASSERT(testProp)
TS_ASSERT_EQUALS(testProp->value(), "my data")
//cleanup //cleanup
AnalysisDataService::Instance().remove(output->getName()); AnalysisDataService::Instance().remove(output->getName());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment