Skip to content

Commit

Permalink
Refs #4313: Fix for repeated time values in ALD
Browse files Browse the repository at this point in the history
addLogDerivative
  • Loading branch information
Janik Zikovsky committed Dec 12, 2011
1 parent b244242 commit 04ead62
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
31 changes: 21 additions & 10 deletions Code/Mantid/Framework/Algorithms/src/AddLogDerivative.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/*WIKI*
This algorithm performs a simple numerical derivative of the values in a sample log.
The 1st order derivative is simply: dy = (y1-y0) / (t1-t0), which is placed in the log at t=(t0+t1)/2
Higher order derivatives are obtained by performing the equation above N times. Since this is a simple numerical derivative, you can expect the result to quickly get noisy at higher derivatives.
Higher order derivatives are obtained by performing the equation above N times.
Since this is a simple numerical derivative, you can expect the result to quickly
get noisy at higher derivatives.
If any of the times in the logs are repeated, then those repeated time values will be skipped,
and the output derivative log will have fewer points than the input.
*WIKI*/
#include "MantidAlgorithms/AddLogDerivative.h"
Expand Down Expand Up @@ -94,21 +95,31 @@ namespace Algorithms
{
dVal.clear();
dTime.clear();
double t0 = times[0];
double y0 = values[0];
for (size_t i=0; i<times.size()-1; i++)
{
double y0 = values[i];
double y1 = values[i+1];
double t0 = times[i];
double t1 = times[i+1];
double dy = (y1-y0) / (t1-t0);
double t = (t0+t1)/2.0;
dVal.push_back( dy );
dTime.push_back( t );
if (t1 != t0)
{
// Avoid repeated time values giving infinite derivatives
double dy = (y1-y0) / (t1-t0);
double t = (t0+t1)/2.0;
dVal.push_back( dy );
dTime.push_back( t );
// For the next time interval
t0 = t1;
y0 = y1;
}
}
times = dTime;
values = dVal;
}

if (times.size() == 0)
throw std::runtime_error("Log " + input->name() + " did not have enough non-repeated time values to make this derivative.");

// Convert time in sec to DateAndTime
DateAndTime start = input->nthTime(0);
std::vector<DateAndTime> timeFull;
Expand Down
22 changes: 20 additions & 2 deletions Code/Mantid/Framework/Algorithms/test/AddLogDerivativeTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class AddLogDerivativeTest : public CxxTest::TestSuite
}

/** Perform test, return result */
TimeSeriesProperty<double> * do_test(int Derivative, bool willFail = false)
TimeSeriesProperty<double> * do_test(int Derivative, bool willFail = false,
bool addRepeatedTimes = false)
{
Workspace2D_sptr ws = WorkspaceCreationHelper::Create2DWorkspace(10, 10);
AnalysisDataService::Instance().addOrReplace("Dummy", ws);
Expand All @@ -46,9 +47,15 @@ class AddLogDerivativeTest : public CxxTest::TestSuite
TS_ASSERT( p->addValue("2007-11-30T16:17:10",2.00) );
TS_ASSERT( p->addValue("2007-11-30T16:17:20",0.00) );
TS_ASSERT( p->addValue("2007-11-30T16:17:30",5.00) );
if (addRepeatedTimes)
{
TS_ASSERT( p->addValue("2007-11-30T16:17:30",10.00) );
TS_ASSERT( p->addValue("2007-11-30T16:17:40",15.00) );
TS_ASSERT( p->addValue("2007-11-30T16:17:50",20.00) );
}
ws->mutableRun().addProperty(p, true);

std::string NewLogName = "doubleProp_derived";
std::string NewLogName = "doubleProp_deriv";

AddLogDerivative alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
Expand Down Expand Up @@ -110,6 +117,17 @@ class AddLogDerivativeTest : public CxxTest::TestSuite
TS_ASSERT_DELTA( p->nthValue(0), 0.01, 1e-5);
}

/** Ticket #4313: Handled repeated time values in logs */
void test_exec_1stDerivative_repeatedValues()
{
TimeSeriesProperty<double> * p = do_test(1, false, true);
if (!p) return;
TS_ASSERT_EQUALS( p->size(), 5);
TS_ASSERT_EQUALS( p->nthTime(3).to_ISO8601_string(), "2007-11-30T16:17:35");
TS_ASSERT_DELTA( p->nthValue(3), 1.0, 1e-5);
TS_ASSERT_EQUALS( p->nthTime(4).to_ISO8601_string(), "2007-11-30T16:17:45");
TS_ASSERT_DELTA( p->nthValue(4), 0.5, 1e-5);
}

void test_exec_failures()
{
Expand Down

0 comments on commit 04ead62

Please sign in to comment.