Skip to content

Commit

Permalink
Merge pull request #6 from timosachsenberg/feature/eol-bspline
Browse files Browse the repository at this point in the history
[TEST] added bspline derivative and coefficent test
  • Loading branch information
aiche committed Sep 10, 2014
2 parents f1b1d60 + 8cbc0a4 commit cdccfc8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/openms/include/OpenMS/MATH/MISC/BSpline2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ namespace OpenMS
* @p x values. A wavelength of zero disables
* the derivative constraint.
* @param bc_type The enumerated boundary condition type. If
* omitted it defaults to BC_ZERO_ENDPOINTS.
* omitted it defaults to BC_ZERO_SECOND.
* @param num_nodes The number of nodes to use for the cubic b-spline.
* If less than 2 a "reasonable" number will be
* calculated automatically, taking into account
* the given cutoff wavelength.
**/
BSpline2d(const std::vector<double>& x, const std::vector<double>& y, double wave_length = 0, BoundaryCondition boundary_condition = BC_ZERO_ENDPOINTS, Size num_nodes = 0);
BSpline2d(const std::vector<double>& x, const std::vector<double>& y, double wave_length = 0, BoundaryCondition boundary_condition = BC_ZERO_SECOND, Size num_nodes = 0);

/**
* Destructor
Expand Down
40 changes: 37 additions & 3 deletions src/tests/class_tests/openms/source/BSpline2d_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ START_SECTION((double eval(double x)))
for (Size i = 0; i != x.size(); ++i)
{
double error = y[i] - 10.0 * sin(x[i]);
//cout << "Original Error: " << error << endl;
mean_squared_error_noisy += error * error;
}
mean_squared_error_noisy /= (double)x.size();
Expand All @@ -123,6 +124,7 @@ START_SECTION((double eval(double x)))
for (Size i = 0; i != x.size(); ++i)
{
double error = b.eval(x[i]) - 10.0 * sin(x[i]);
//cout << "Smoothed Error: " << error << endl;
mean_squared_error_smoothed += error * error;
}
mean_squared_error_smoothed /= (double)x.size();
Expand Down Expand Up @@ -150,17 +152,49 @@ END_SECTION

START_SECTION((double derivative(double x)))
{
// TODO
{
// calculate error on first derivative of smoothed points.
// preserve curvature - otherwise we get large errors on derivative
BSpline2d b(x, y, 0, BSpline2d::BC_ZERO_SECOND);
double mean_absolute_derivative_error(0.0);
for (Size i = 0; i != x.size(); ++i)
{
double error = fabs(b.derivative(x[i]) - 10.0 * cos(x[i]));
mean_absolute_derivative_error += error;
}
mean_absolute_derivative_error /= (double)x.size();

//cout << mean_absolute_derivative_error << endl;
TEST_EQUAL(mean_absolute_derivative_error < 10.0 * 0.2, true)
}

}
END_SECTION

START_SECTION((double coefficient(int n)))
{
// TODO
vector<double> x2;
vector<double> y2;
for (Size i = 0; i != 100; ++i)
{
x2.push_back(i);
y2.push_back(i);
}

// b-spline coefficients should increase monotopically for this example (checked with R)
BSpline2d b(x2, y2, 0, BSpline2d::BC_ZERO_SECOND, 100);
bool coeff_mono_increase = true;
for (Size i = 1; i < 100; ++i)
{
if (b.coefficient(i) <= b.coefficient(i - 1))
{
coeff_mono_increase = false;
}
}
TEST_EQUAL(coeff_mono_increase, true);
}
END_SECTION


/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
END_TEST
Expand Down

0 comments on commit cdccfc8

Please sign in to comment.