From 3436291eb623042ddf41068eb6577b1adabdc986 Mon Sep 17 00:00:00 2001 From: Steven Robertson Date: Tue, 25 Oct 2011 19:03:35 -0400 Subject: [PATCH] Improve spline loading --- cuburn/genome.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cuburn/genome.py b/cuburn/genome.py index bb0b041..c52f0e2 100644 --- a/cuburn/genome.py +++ b/cuburn/genome.py @@ -8,13 +8,21 @@ class SplEval(object): # If a single value is passed, normalize to a constant set of knots if isinstance(knots, (int, float)): knots = [-0.1, knots, 0.0, knots, 1.0, knots, 1.1, knots] + elif not np.all(np.diff(np.float32(np.asarray(knots))[::2]) > 0): + raise ValueError("Spline times are non-monotonic. (Use " + "nextafterf()-spaced times to anchor tangents.)") # If stabilizing knots are missing before or after the edges of the - # [0,1] interval, add them. TODO: choose better knots + # [0,1] interval, add them. We choose knots that preserve the first + # differential, which is probably what is expected when two knots are + # present but may be less accurate for three. Of course, one can just + # add stabilizing knots to the genome to change this. if knots[0] >= 0: - knots = [-0.1, knots[1]] + knots + m = (knots[3] - knots[1]) / (knots[2] - knots[0]) + knots = [-0.1, knots[1] + -0.1 * m] + knots if knots[-2] <= 1: - knots.extend([1.1, knots[-1]]) + m = (knots[-1] - knots[-3]) / (knots[-2] - knots[-4]) + knots.extend([1.1, knots[-1] + 0.1 * m]) self.knots = np.zeros((2, len(knots)/2)) self.knots.T.flat[:] = knots