Improve spline loading

This commit is contained in:
Steven Robertson 2011-10-25 19:03:35 -04:00
parent fb5bdc2a9f
commit 3436291eb6

View File

@ -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