# -*- coding: utf-8 -*-
"""
Created on Mon Sep 26 02:14:56 2016

@author: ckirst
"""

### Splines
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import splrep, splev
import interpolation.spline as sp
reload(sp)

s = sp.Spline(nparameter=10, degree=3)

s = sp.Spline(nparameter=10, npoints=141, degree=3)
x = np.sin(10 * s.points) + np.sin(2 * s.points)
#s.set_values(x);
s.parameter
s.values
s.values = x
s.parameter

p = s.parameter
tck = splrep(s.points, x, t=s.knots, task=-1, k=s.degree)
xpp = splev(s.points, tck)
xp = s.get_values()

plt.figure(1)
plt.clf()
Exemple #2
0
def test():

    ### Splines
    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.interpolate import splrep, splev
    import interpolation.spline as sp
    reload(sp)

    s = sp.Spline(nparameter=10, degree=3)

    s = sp.Spline(nparameter=10, npoints=141, degree=3)
    x = np.sin(10 * s.points) + np.sin(2 * s.points)
    s.values = x

    p = s.parameter
    tck = splrep(s.points, x, t=s.knots, task=-1, k=s.degree)
    xpp = splev(s.points, tck)
    xp = s.get_values()

    plt.figure(1)
    plt.clf()
    plt.plot(x)
    plt.plot(xp)
    plt.plot(xpp)

    plt.figure(2)
    plt.clf()
    for i in range(s.nparameter):
        pp = np.zeros(s.nparameter)
        pp[i] = 1
        xp = s.get_values(pp)
        plt.plot(s.points, xp)
    plt.title('basis functions scipy')

    # test spline basis

    reload(sp)
    s = sp.Spline(nparameter=10, npoints=141, degree=3)

    bm = s.basis_matrix()
    plt.figure(3)
    plt.clf()
    plt.plot(bm)
    plt.title('basis functions matrix')

    xb = bm.dot(p)
    xp = s.get_values(p)

    #xb = np.dot(np.invert(bm), pb);

    plt.figure(4)
    plt.clf()
    plt.plot(x)
    #plt.plot(0.1* xb);
    plt.plot(xb)
    plt.plot(xp)

    # invertible case of npoints = nparameter
    s = sp.Spline(nparameter=25, npoints=25, degree=3)
    x = np.sin(10 * s.points) + np.sin(2 * s.points)
    s.from_values(x)
    p = s.parameter

    bm = s.basis
    pb = np.linalg.inv(bm).dot(x)

    plt.figure(5)
    plt.clf()
    plt.plot(p)
    plt.plot(pb)

    xs = s(s.points, p)
    xp = bm.dot(pb)

    plt.figure(6)
    plt.clf()
    plt.plot(x)
    plt.plot(xs)
    plt.plot(xp)

    from utils.timer import timeit

    @timeit
    def ts():
        return s(s.points, p)

    @timeit
    def tb():
        return s.get_values(p)

    ps = ts()
    pb = tb()
    np.allclose(pb, ps)

    # factor ~10 times faster with basis

    # test shifting spline by a value s
    s = sp.Spline(nparameter=25, npoints=25, degree=3)
    x = np.sin(10 * s.points) + np.sin(2 * s.points)
    p = s.from_values(x)
    xnew = s(s.points + 0.05, p)
    import copy
    s2 = copy.deepcopy(s)
    s2.shift(0.05, extrapolation=1)

    plt.figure(7)
    plt.clf()
    s.plot()
    plt.plot(s.points, xnew)
    s2.plot()

    # test integration and derivatives
    reload(sp)
    points = np.linspace(0, 2 * np.pi, 50)
    x = np.sin(points)
    # + np.sin(2 * points);
    s = sp.Spline(values=x, points=points, nparameter=20)
    plt.figure(1)
    plt.clf()
    s.plot()
    d = s.derivative()
    d.plot()
    i = s.integral()
    i.plot()
    return optimize.newton(err, x)


def fun(x):
    return np.sqrt(37) / 6.0 * ellipeinc(6 * x, 36.0 / 37.0)


x = np.linspace(0, 1, nn)
y = fun(x)
y1 = [inverse(fun,
              fun(1) * xx) for xx in x]
y2 = [np.sin(6 * inverse(fun,
                         fun(1) * xx)) for xx in x]

s1 = sp.Spline(y1, x, nparameter=pn)
s2 = sp.Spline(y2, x, nparameter=pn)

plt.figure(1)
plt.clf()
plt.subplot(1, 2, 1)
#plt.plot(x, y)
plt.plot(x, y1)
plt.plot(x, y2)
plt.subplot(1, 2, 2)
s1.plot()
s2.plot()

### Test Curve

s = np.linspace(0, 1, nn)
Exemple #4
0
def test():
    """Test Curve class"""

    ### Curves
    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.interpolate import splrep, splev, splprep
    import interpolation.spline as sp
    reload(sp)

    s = np.linspace(0, 1, 50) * 2 * np.pi
    xy = np.vstack([np.sin(2 * s), np.cos(s)]).T

    plt.figure(10)
    plt.clf()
    plt.plot(xy[:, 0], xy[:, 1])

    reload(sp)
    c = sp.Curve(xy, nparameter=20)

    c.plot()

    sp = c.to_spline(0)
    sp.plot()

    sp = c.to_spline(1)
    sp.plot()

    c.parameter.shape

    tck = c.tck()
    pts = splev(np.linspace(0.3, 0.5, 10), tck)
    pts = np.vstack(pts).T
    spts = c(np.linspace(0.3, 0.5, 10))
    np.allclose(pts, spts)

    # curve intersections
    import numpy as np
    import matplotlib.pyplot as plt
    import interpolation.spline as sp
    reload(sp)

    s = 2 * np.pi * np.linspace(0, 1, 50)
    xy1 = np.vstack([np.cos(s), np.sin(2 * s)]).T
    xy2 = np.vstack([0.5 * s, 0.2 * s]).T + [-2, -0.5]
    c1 = sp.Curve(xy1)
    c2 = sp.Curve(xy2)

    xy0, p1, p2 = c1.intersections(c2, with_xy=True)

    plt.figure(1)
    plt.clf()
    c1.plot()
    c2.plot()
    plt.scatter(xy0[:, 0], xy0[:, 1], c='m', s=40)
    xy0s = c1(p1)
    plt.scatter(xy0s[:, 0], xy0s[:, 1], c='k', s=40)
    plt.axis('equal')
    xys = c1()
    plt.scatter(xys[:, 0], xys[:, 1], c='r', s=40)
    plt.axis('equal')

    # intersection with line segment
    pt0 = [-1.5, -0.5]
    pt1 = [1.5, 1]
    xy0, p1, p2 = c1.intersections_with_line(pt0, pt1, with_xy=True)

    plt.figure(1)
    plt.clf()
    c1.plot()
    xyp = np.vstack([pt0, pt1])
    plt.plot(xyp[:, 0], xyp[:, 1])
    plt.scatter(xy0[:, 0], xy0[:, 1], c='m', s=40)
    plt.axis('equal')

    # intersections with resampling

    s = 2 * np.pi * np.linspace(0, 1, 25)
    xy1 = np.vstack([np.cos(s), np.sin(2 * s)]).T
    xy2 = np.vstack([0.5 * s, 0.2 * s]).T + [-2, -0.5]
    c1 = sp.Curve(xy1)
    c2 = sp.Curve(xy2)

    xy0, p1, p2 = c1.intersections(c2, with_xy=True)
    xy0r, p1r, p2r = c1.intersections(c2, with_xy=True, nsamples=50)

    plt.figure(1)
    plt.clf()
    c1.plot()
    c2.plot()
    plt.scatter(xy0[:, 0], xy0[:, 1], c='m', s=40)
    xy0s = c1(p1)
    plt.scatter(xy0s[:, 0], xy0s[:, 1], c='k', s=40)
    plt.axis('equal')
    xys = c1(p1r)
    plt.scatter(xys[:, 0], xys[:, 1], c='r', s=40)
    plt.axis('equal')
    c1.resample(npoints=50)
    c1.plot()

    ### Resampling
    reload(sp)
    s = 2 * np.pi * np.linspace(0, 1, 25)
    xy1 = np.vstack([np.cos(s), np.sin(2 * s)]).T
    c1 = sp.Curve(xy1)
    c2 = c1.copy()
    c2.resample(npoints=150)
    c2.values.shape

    plt.figure(2)
    plt.clf()
    c1.plot()
    c2.plot()

    ### Uniform Sampling and knotting
    import numpy as np
    import matplotlib.pyplot as plt
    #from scipy.interpolate import splprep
    import interpolation.spline as sp
    reload(sp)

    s = 2 * np.pi * np.linspace(0, 1, 50)
    xy1 = np.vstack([np.cos(s), np.sin(2 * s)]).T
    c1 = sp.Curve(xy1)

    tck = c1.tck()
    tck1, u = splprep(c1.values.T, s=0)
    c2 = sp.Curve(tck=tck1, points=u)
    c2.resample(npoints=c2.npoints)

    #c2 = c1.copy();
    #c2.resample_uniform();
    c3 = c1.copy()
    c3.resample_uniform()

    c4 = c2.copy()
    c4.reknot_uniform()

    plt.figure(1)
    plt.clf()
    #plt.subplot(1,2,1);
    c1.plot()
    xy = c1.values
    plt.scatter(xy[:, 0], xy[:, 1])
    #plt.subplot(1,2,2);
    c2.plot()
    xy = c2.values
    plt.scatter(xy[:, 0], xy[:, 1])
    c3.plot()
    xy = c3.values
    plt.scatter(xy[:, 0], xy[:, 1])
    c4.plot()
    xy = c4.values
    plt.scatter(xy[:, 0], xy[:, 1])

    plt.figure(2)
    plt.clf()
    #plt.plot(c1.points);
    #plt.plot(c2.points);
    plt.plot(np.diff(c1.knots) - np.mean(np.diff(c1.knots)), '+')
    plt.plot(np.diff(c2.knots) - np.mean(np.diff(c1.knots)), 'o')
    plt.plot(np.diff(c3.knots) - np.mean(np.diff(c1.knots)), '.')
    plt.plot(np.diff(c4.knots) - np.mean(np.diff(c1.knots)), 'x')

    ### Theta
    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.interpolate import splprep, splev  #, splrep
    import interpolation.spline as sp
    reload(sp)

    s = np.linspace(0, 1, 150)
    xy1 = np.vstack([s, np.sin(2 * np.pi * s)]).T
    c1 = sp.Curve(xy1, nparameter=20)
    c1.uniform()
    c1.nparameter

    tck = c1.derivative()

    plt.figure(78)
    plt.clf()
    plt.subplot(1, 2, 1)
    c1.plot(dim=0)
    c1.plot(dim=1)
    c1.plot()
    plt.plot(xy1[:, 0], xy1[:, 1])

    dc = c1.derivative()
    plt.subplot(1, 2, 2)
    dc.plot(dim=0)
    dc.plot(dim=1)

    #plt.plot(xy1[:,0], xy1[:,1])

    #get the tangents  and tangent angles
    #tgs = splev(c1.points, c1.tck(), der = 1);
    #tgs = np.vstack(tgs).T;

    tgs = dc.values
    phi = np.arctan2(tgs[:, 1], tgs[:, 0])
    phi = np.mod(phi + np.pi, 2 * np.pi) - np.pi

    plt.figure(5)
    plt.clf()
    plt.plot(phi)

    #return Spline(phi, points = self.points, knots = self.knots, degree = self.degree - 1);
    tck = splrep(c1.points, phi, s=0.0, k=c1.degree + 1)
    phi = Spline(tck=tck)

    theta, xy, o = c1.theta(with_xy=True, with_orientation=True)
    phi = c1.phi()
    theta2 = phi.derivative()

    plt.figure(1)
    plt.clf()
    plt.subplot(1, 3, 1)
    c1.plot()
    plt.subplot(1, 3, 2)
    phi.plot()
    plt.subplot(1, 3, 3)
    theta.plot()
    theta2.plot()

    c2 = sp.theta_to_curve(theta)

    plt.figure(21)
    plt.clf()
    c1.plot()
    c2.plot()

    orientation = o
    reference = 0.5
    phi = theta.integral()
    p0 = phi(reference)
    phi.parameter += orientation - p0
    # add a constant = add constant to parameter
    phi.values += orientation - p0

    phi2 = c1.phi()

    plt.figure(23)
    plt.clf()
    phi.plot()
    phi2.plot()

    points = phi.points
    phiv = phi(points)
    dt = 1.0 / (points.shape[0] - 1)
    #dt = 1;
    tgtv = dt * np.vstack([np.cos(phiv), np.sin(phiv)]).T
    xycv = np.cumsum(tgtv, axis=0) * np.pi

    plt.figure(100)
    plt.clf()
    plt.plot(xycv[:, 0], xycv[:, 1])
    c1.plot()

    tgt = sp.Curve(tgtv, points=points)
    xyc = tgt.integral()
    #xyc0 = xyc(reference);
    #xyc.parameter += xy - xyc0;
    #xyc.values += xy - xyc0;

    plt.figure(24)
    plt.clf()
    xyc.plot()

    plt.figure(25)
    plt.clf()
    tgt.plot(dim=0)
    tgt.plot(dim=1)

    ### Calculus
    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.interpolate import splprep, splev, splrep
    import interpolation.spline as sp
    reload(sp)

    s = np.linspace(0, 2 * np.pi, 150)
    y = np.sin(s)

    s = sp.Spline(values=y, points=s, nparameter=20)

    si = s.integral()
    sd = s.derivative()

    plt.figure(1)
    plt.clf()
    s.plot()
    si.plot()
    sd.plot()

    # for curve
    s = np.linspace(0, 2 * np.pi, 130)
    xy = np.vstack([s, np.sin(s)]).T

    c = sp.Curve(xy, nparameter=20)
    c.uniform()

    ci = c.integral()
    cd = c.derivative()

    plt.figure(2)
    plt.clf()
    plt.subplot(1, 3, 1)
    c.plot(dim=0)
    ci.plot(dim=0)
    cd.plot(dim=0)
    plt.subplot(1, 3, 2)
    c.plot(dim=1)
    ci.plot(dim=1)
    cd.plot(dim=1)
    plt.subplot(1, 3, 3)
    c.plot()
    plt.scatter(c.values[:, 0], c.values[:, 1], c='m', s=40)
    plt.plot(xy[:, 0], xy[:, 1])
    plt.axis('equal')
import interpolation.curve as curve;
import interpolation.spline as spline;

import worm.geometry as wgeo;

from utils.timer import timeit;

reload(curve); reload(spline);

#make the data

npoints = 21;
theta = np.linspace(0,1,npoints);
theta = np.sin(theta);

c = spline.Spline(theta);


@timeit
def d():
  return wgeo.center_from_theta(theta);

@timeit
def s():
  return curve.theta_to_curve(c);
  

c1 = d();
c2 = s();