Skip to content

Commit

Permalink
Basic functionality works with tests.
Browse files Browse the repository at this point in the history
Refs #9227
  • Loading branch information
martyngigg committed Mar 31, 2014
1 parent 9d51c79 commit a43f69a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
29 changes: 24 additions & 5 deletions Code/Mantid/Framework/Algorithms/src/AddTimeSeriesLog.cpp
Expand Up @@ -15,12 +15,16 @@ ISO8601 format, e.g. 2010-09-14T04:20:12."
*WIKI_USAGE*/

#include "MantidAlgorithms/AddTimeSeriesLog.h"
#include "MantidKernel/DateTimeValidator.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidKernel/MandatoryValidator.h"

namespace Mantid
{
namespace Algorithms
{
using namespace API;
using namespace Kernel;

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(AddTimeSeriesLog)
Expand Down Expand Up @@ -49,16 +53,13 @@ namespace Mantid
*/
void AddTimeSeriesLog::init()
{
using namespace API;
using namespace Kernel;
declareProperty(new WorkspaceProperty<MatrixWorkspace>("Workspace", "", Direction::InOut),
"In/out workspace that will store the new log information");

auto nonEmptyString = boost::make_shared<MandatoryValidator<std::string>>();
declareProperty("Name", "", nonEmptyString,
declareProperty("Name", "", boost::make_shared<MandatoryValidator<std::string>>(),
"A string name for either a new time series log to be created "
"or an existing name to update", Direction::Input);
declareProperty("Time", "", nonEmptyString,
declareProperty("Time", "", boost::make_shared<DateTimeValidator>(),
"An ISO formatted date/time string specifying the timestamp for "
"the given log value, e.g 2010-09-14T04:20:12",
Direction::Input);
Expand All @@ -73,6 +74,24 @@ namespace Mantid
*/
void AddTimeSeriesLog::exec()
{
MatrixWorkspace_sptr logWS = getProperty("Workspace");
std::string name = getProperty("Name");
std::string time = getProperty("Time");
double value = getProperty("Value");

auto & run = logWS->mutableRun();
TimeSeriesProperty<double> *timeSeries(NULL);
if(run.hasProperty(name))
{
timeSeries = dynamic_cast<TimeSeriesProperty<double>*>(run.getLogData(name));
if(!timeSeries) throw std::invalid_argument("Log '" + name + "' already exists but is not a time series.");
}
else
{
timeSeries = new TimeSeriesProperty<double>(name);
run.addProperty(timeSeries);
}
timeSeries->addValue(time, value);
}


Expand Down
58 changes: 58 additions & 0 deletions Code/Mantid/Framework/Algorithms/test/AddTimeSeriesLogTest.h
Expand Up @@ -3,6 +3,7 @@

#include <cxxtest/TestSuite.h>
#include "MantidAlgorithms/AddTimeSeriesLog.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"

class AddTimeSeriesLogTest : public CxxTest::TestSuite
Expand All @@ -13,6 +14,25 @@ class AddTimeSeriesLogTest : public CxxTest::TestSuite
static AddTimeSeriesLogTest *createSuite() { return new AddTimeSeriesLogTest(); }
static void destroySuite( AddTimeSeriesLogTest *suite ) { delete suite; }

void test_Workspace2D()
{
auto ws = WorkspaceCreationHelper::Create2DWorkspace(10,10);
TS_ASSERT_THROWS_NOTHING(executeAlgorithm(ws, "Test Name", "2010-09-14T04:20:12", 20.0));
checkLogWithEntryExists(ws, "Test Name", "2010-09-14T04:20:12", 20.0, 0);
TS_ASSERT_THROWS_NOTHING(executeAlgorithm(ws, "Test Name", "2010-09-14T04:20:19", 40.0));
checkLogWithEntryExists(ws, "Test Name", "2010-09-14T04:20:19", 40.0, 1);

}

void test_EventWorkspace()
{
auto ws = WorkspaceCreationHelper::CreateEventWorkspace(10,10);
TS_ASSERT_THROWS_NOTHING(executeAlgorithm(ws, "Test Name", "2010-09-14T04:20:12", 20.0));
checkLogWithEntryExists(ws, "Test Name", "2010-09-14T04:20:12", 20.0, 0);
TS_ASSERT_THROWS_NOTHING(executeAlgorithm(ws, "Test Name", "2010-09-14T04:20:19", 40.0));
checkLogWithEntryExists(ws, "Test Name", "2010-09-14T04:20:19", 40.0, 1);
}

//-------------------------- Failure cases ------------------------------------
void test_empty_log_name_not_allowed()
{
Expand All @@ -38,13 +58,32 @@ class AddTimeSeriesLogTest : public CxxTest::TestSuite
TS_ASSERT_THROWS(alg.setPropertyValue("Value", ""), std::invalid_argument);
}

void test_time_as_non_iso_formatted_string_throws_invalid_argument()
{
Mantid::Algorithms::AddTimeSeriesLog alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize());

TS_ASSERT_THROWS(alg.setPropertyValue("Time", "NotATime"), std::invalid_argument);
TS_ASSERT_THROWS(alg.setPropertyValue("Time", "2014 03 31 09 30"), std::invalid_argument);
TS_ASSERT_THROWS(alg.setPropertyValue("Time", "09:30:00"), std::invalid_argument);
}

void test_algorithm_fails_if_log_exists_but_is_not_a_time_series()
{
auto ws = WorkspaceCreationHelper::Create2DWorkspace(10,10);
auto & run = ws->mutableRun();
run.addProperty<double>("Test Name", 1.0);
TS_ASSERT_THROWS(executeAlgorithm(ws, "Test Name", "2010-09-14T04:20:12", 20.0), std::invalid_argument);
}

private:

void executeAlgorithm(Mantid::API::MatrixWorkspace_sptr testWS, const std::string & logName, const std::string & logTime,
const double logValue)
{
//execute algorithm
Mantid::Algorithms::AddTimeSeriesLog alg;
alg.setChild(true); // not in ADS
TS_ASSERT_THROWS_NOTHING(alg.initialize());
TS_ASSERT( alg.isInitialized() )

Expand All @@ -56,7 +95,26 @@ class AddTimeSeriesLogTest : public CxxTest::TestSuite
alg.execute();
}

void checkLogWithEntryExists(Mantid::API::MatrixWorkspace_sptr testWS, const std::string & logName, const std::string & logTime,
const double logValue, const size_t position)
{
using Mantid::Kernel::DateAndTime;
using Mantid::Kernel::TimeSeriesProperty;

const auto & run = testWS->run();
TSM_ASSERT("Run does not contain the expected log entry", run.hasProperty(logName));

auto *prop = run.getLogData(logName);
auto *timeSeries = dynamic_cast<TimeSeriesProperty<double>*>(prop);
TSM_ASSERT("A log entry with the given name exists but it is not a time series", timeSeries);
auto times = timeSeries->timesAsVector();
TS_ASSERT(times.size() >= position + 1);
auto values = timeSeries->valuesAsVector();
TS_ASSERT_EQUALS(DateAndTime(logTime), times[position]);

TS_ASSERT(values.size() >= position + 1);
TS_ASSERT_EQUALS(logValue, values[position]);
}
};


Expand Down

0 comments on commit a43f69a

Please sign in to comment.