Python scipy.interpolate.splprep() Examples

The following are 19 code examples of scipy.interpolate.splprep(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module scipy.interpolate , or try the search function .
Example #1
Source File: rover_utils.py    From Ensemble-Bayesian-Optimization with MIT License 6 votes vote down vote up
def set_params(self, params, start=None, goal=None):

        points = params.reshape((-1, self.d)).T

        if start is not None:
            points = np.hstack((start[:, None], points))

        if goal is not None:
            points = np.hstack((points, goal[:, None]))

        self.tck, u = si.splprep(points, k=3)

        if start is not None:
            for a, sv in izip(self.tck[1], start):
                a[0] = sv

        if goal is not None:
            for a, gv in izip(self.tck[1], goal):
                a[-1] = gv 
Example #2
Source File: resampling.py    From visual_dynamics with MIT License 6 votes vote down vote up
def get_velocities(positions, times, tol):
    positions = np.atleast_2d(positions)
    n = len(positions)
    deg = min(3, n - 1)

    good_inds = np.r_[True, (abs(times[1:] - times[:-1]) >= 1e-6)]
    good_positions = positions[good_inds]
    good_times = times[good_inds]

    if len(good_inds) == 1:
        return np.zeros(positions[0:1].shape)

    (tck, _) = si.splprep(good_positions.T, s=tol ** 2 * (n + 1), u=good_times, k=deg)
    # smooth_positions = np.r_[si.splev(times,tck,der=0)].T
    velocities = np.r_[si.splev(times, tck, der=1)].T
    return velocities 
Example #3
Source File: nail.py    From Virtual-Makeup with Apache License 2.0 5 votes vote down vote up
def getBoundaryPoints(x = [], y = []):
	tck,u = interpolate.splprep([x, y], s=0, per=1)
	unew = np.linspace(u.min(), u.max(), 1000)
	xnew,ynew = interpolate.splev(unew, tck, der=0)
	tup = c_[xnew.astype(int),ynew.astype(int)].tolist()
	coord = list(set(tuple(map(tuple, tup))))
	coord = np.array([list(elem) for elem in coord])
	return coord[:,0],coord[:,1] 
Example #4
Source File: SplineInterpolation-test-3.py    From 3D_Path_Planning with MIT License 5 votes vote down vote up
def fitSpline(points):
	pts = np.array(points)
	tck, u = splprep(pts.T, u=None, s=0.0, per=1)
	u_new = np.linspace(u.min(), u.max(), 1000)
	x_new, y_new = splev(u_new, tck, der=0)
	return list(zip(x_new,y_new)) 
Example #5
Source File: rrt-star_SplineFitting_NodePruning.py    From 3D_Path_Planning with MIT License 5 votes vote down vote up
def fitSpline(points):
    pts = np.array(points)
    #print("points",pts.T)
    #pts = np.delete(pts,-1)
    #print("x = ",pts.T[:][0])
    x1 = pts.T[:][0]
    y1 = pts.T[:][1]
    #print("x1 = ",x1.T)
    #print("shape x:- ",np.shape(x1))
    #print("shape y:- ",np.shape(y1))
    x1 = x1[::-1]
    y1 = y1[::-1]    
    #tck, u = splprep(pts.T, u=None, s=0.0, per=1, k=3)
    tck = splrep(x1,y1, s=1,  k=3)
    #tck,u = splprep(pts.T[:][0],pts.T[:][1], s=1, k=3)
    #tck,u = splprep(pts.T, s=0.0, k=3,per = 0)
    #u_new = np.linspace(u.min(), u.max(), 1000)
    u_new = np.arange(x1[0], x1[len(x1)-1]+1,0.1)
    #print(x1[0])
    #print(x1[len(x1)-1] +1 )
    #print(u_new)
    #u_new = np.arange(pts.T[0][0], pts.T[len[pts]-1][0],0.001)
    #x_new, y_new = splev(u_new, tck, der=0)
    y_new = splev(u_new, tck, der=0)
    #return list(zip(x_new,y_new))
    return list(zip(u_new,y_new)) 
Example #6
Source File: rrt-star_SplineFitting.py    From 3D_Path_Planning with MIT License 5 votes vote down vote up
def fitSpline(points):
    pts = np.array(points)
    #print("points",pts.T)
    #pts = np.delete(pts,-1)
    #print("x = ",pts.T[:][0])
    x1 = pts.T[:][0]
    y1 = pts.T[:][1]
    #print("x1 = ",x1.T)
    #print("shape x:- ",np.shape(x1))
    #print("shape y:- ",np.shape(y1))
    x1 = x1[::-1]
    y1 = y1[::-1]    
    #tck, u = splprep(pts.T, u=None, s=0.0, per=1, k=3)
    tck = splrep(x1,y1, s=1,  k=3)
    #tck,u = splprep(pts.T[:][0],pts.T[:][1], s=1, k=3)
    #tck,u = splprep(pts.T, s=0.0, k=3,per = 0)
    #u_new = np.linspace(u.min(), u.max(), 1000)
    u_new = np.arange(x1[0], x1[len(x1)-1]+1,0.1)
    #print(x1[0])
    #print(x1[len(x1)-1] +1 )
    #print(u_new)
    #u_new = np.arange(pts.T[0][0], pts.T[len[pts]-1][0],0.001)
    #x_new, y_new = splev(u_new, tck, der=0)
    y_new = splev(u_new, tck, der=0)
    #return list(zip(x_new,y_new))
    return list(zip(u_new,y_new)) 
Example #7
Source File: rrt-star_SplineFitting_NodePruning_2PhaseSampling.py    From 3D_Path_Planning with MIT License 5 votes vote down vote up
def fitSpline(points):
    pts = np.array(points)
    #print("points",pts.T)
    #pts = np.delete(pts,-1)
    #print("x = ",pts.T[:][0])
    x1 = pts.T[:][0]
    y1 = pts.T[:][1]
    #print("x1 = ",x1.T)
    #print("shape x:- ",np.shape(x1))
    #print("shape y:- ",np.shape(y1))
    x1 = x1[::-1]
    y1 = y1[::-1]    
    #tck, u = splprep(pts.T, u=None, s=0.0, per=1, k=3)
    tck = splrep(x1,y1, s=1,  k=2)
    #tck,u = splprep(pts.T[:][0],pts.T[:][1], s=1, k=3)
    #tck,u = splprep(pts.T, s=0.0, k=3,per = 0)
    #u_new = np.linspace(u.min(), u.max(), 1000)
    u_new = np.arange(x1[0], x1[len(x1)-1]+1,0.1)
    #print(x1[0])
    #print(x1[len(x1)-1] +1 )
    #print(u_new)
    #u_new = np.arange(pts.T[0][0], pts.T[len[pts]-1][0],0.001)
    #x_new, y_new = splev(u_new, tck, der=0)
    y_new = splev(u_new, tck, der=0)
    #return list(zip(x_new,y_new))
    return list(zip(u_new,y_new)) 
Example #8
Source File: helpers.py    From sand-spline with MIT License 5 votes vote down vote up
def _rnd_interpolate(xy, num_points, ordered=False):
  tck,u = splprep([
    xy[:,0],
    xy[:,1]],
    s=0
  )
  unew = random(num_points)
  if sort:
    unew = sort(unew)
  out = splev(unew, tck)
  return column_stack(out) 
Example #9
Source File: helpers.py    From sand-spline with MIT License 5 votes vote down vote up
def _interpolate(xy, num_points):
  tck,u = splprep([
    xy[:,0],
    xy[:,1]],
    s=0
  )
  unew = linspace(0, 1, num_points)
  out = splev(unew, tck)
  return column_stack(out) 
Example #10
Source File: coonswarp.py    From RasterFairy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def getCircularBounds(fitCloud=None,width=64,height=64,smoothing=0.01):
    circumference = 2*(width+height)
    
    if not fitCloud is None:
        cx = np.mean(fitCloud[:,0])
        cy = np.mean(fitCloud[:,1])
        r = 0.5* max( np.max(fitCloud[:,0])- np.min(fitCloud[:,0]),np.max(fitCloud[:,1])- np.min(fitCloud[:,1]))
    else:
        r = circumference /(2.0*math.pi)
        cx = cy = r
    perimeterPoints = np.zeros((circumference,2),dtype=float)
    for i in range(circumference):
        angle = (2.0*math.pi)*float(i) / circumference - math.pi * 0.5 
        perimeterPoints[i][0] = cx + r * math.cos(angle)
        perimeterPoints[i][1] = cy + r * math.sin(angle)
        
        
    bounds = {'top':perimeterPoints[0:width],
              'right':perimeterPoints[width-1:width+height-1],
              'bottom':perimeterPoints[width+height-2:2*width+height-2],
              'left':perimeterPoints[2*width+height-3:]}
    
    bounds['s_top'],u = interpolate.splprep([bounds['top'][:,0], bounds['top'][:,1]],s=smoothing)
    bounds['s_right'],u = interpolate.splprep([bounds['right'][:,0],bounds['right'][:,1]],s=smoothing)
    bounds['s_bottom'],u = interpolate.splprep([bounds['bottom'][:,0],bounds['bottom'][:,1]],s=smoothing)
    bounds['s_left'],u = interpolate.splprep([bounds['left'][:,0],bounds['left'][:,1]],s=smoothing)
   
    
    return bounds 
Example #11
Source File: foundation.py    From Virtual-Makeup with Apache License 2.0 5 votes vote down vote up
def getBoundaryPoints(x , y):
    tck,u = interpolate.splprep([x, y], s=0, per=1)
    unew = np.linspace(u.min(), u.max(), 10000)
    xnew,ynew = interpolate.splev(unew, tck, der=0)
    tup = c_[xnew.astype(int),ynew.astype(int)].tolist()
    coord = list(set(tuple(map(tuple, tup))))
    coord = np.array([list(elem) for elem in coord])
    return coord[:,0],coord[:,1] 
Example #12
Source File: utils.py    From sand-glyphs with MIT License 5 votes vote down vote up
def _interpolate(xy, num_points):
  tck,u = splprep([
    xy[:,0],
    xy[:,1]],
    s=0
  )
  unew = linspace(0, 1, num_points)
  out = splev(unew, tck)
  return column_stack(out) 
Example #13
Source File: blush.py    From Virtual-Makeup with MIT License 5 votes vote down vote up
def get_boundary_points(x, y):
    tck, u = interpolate.splprep([x, y], s=0, per=1)
    unew = np.linspace(u.min(), u.max(), 1000)
    xnew, ynew = interpolate.splev(unew, tck, der=0)
    tup = c_[xnew.astype(int), ynew.astype(int)].tolist()
    coord = list(set(tuple(map(tuple, tup))))
    coord = np.array([list(elem) for elem in coord])
    return np.array(coord[:, 0], dtype=np.int32), np.array(coord[:, 1], dtype=np.int32) 
Example #14
Source File: nail.py    From Virtual-Makeup with MIT License 5 votes vote down vote up
def get_boundary_points(x, y):
    tck, u = interpolate.splprep([x, y], s=0, per=1)
    unew = np.linspace(u.min(), u.max(), 1000)
    xnew, ynew = interpolate.splev(unew, tck, der=0)
    tup = c_[xnew.astype(int), ynew.astype(int)].tolist()
    coord = list(set(tuple(map(tuple, tup))))
    coord = np.array([list(elem) for elem in coord])
    return np.array(coord[:, 0], dtype=np.int32), np.array(coord[:, 1], dtype=np.int32) 
Example #15
Source File: SplineRefine.py    From PyAero with MIT License 5 votes vote down vote up
def spline(self, x, y, points=200, degree=2, evaluate=False):
        """Interpolate spline through given points

        Args:
            spline (int, optional): Number of points on the spline
            degree (int, optional): Degree of the spline
            evaluate (bool, optional): If True, evaluate spline just at
                                       the coordinates of the knots
        """

        # interpolate B-spline through data points
        # returns knots of control polygon
        # tck ... tuple (t,c,k) containing the vector of knots,
        # the B-spline coefficients, and the degree of the spline.
        # u ... array of the parameters for each knot
        # NOTE: s=0.0 is important as no smoothing should be done on the spline
        # after interpolating it
        tck, u = interpolate.splprep([x, y], s=0.0, k=degree)

        # number of points on interpolated B-spline (parameter t)
        t = np.linspace(0.0, 1.0, points)

        # if True, evaluate spline just at the coordinates of the knots
        if evaluate:
            t = u

        # evaluate B-spline at given parameters
        # der=0: returns point coordinates
        coo = interpolate.splev(t, tck, der=0)

        # evaluate 1st derivative at given parameters
        der1 = interpolate.splev(t, tck, der=1)

        # evaluate 2nd derivative at given parameters
        der2 = interpolate.splev(t, tck, der=2)

        spline_data = [coo, u, t, der1, der2, tck]

        return spline_data 
Example #16
Source File: Meshing.py    From PyAero with MIT License 5 votes vote down vote up
def distribute(self, direction='u', number=0, type='constant'):

        if direction == 'u':
            line = np.array(self.getULines()[number])
        elif direction == 'v':
            line = np.array(self.getVLines()[number])

        # interpolate B-spline through data points
        # here, a linear interpolant is derived "k=1"
        # splprep returns:
        # tck ... tuple (t,c,k) containing the vector of knots,
        #         the B-spline coefficients, and the degree of the spline.
        #   u ... array of the parameters for each given point (knot)
        tck, u = interpolate.splprep(line.T, s=0, k=1)

        if type == 'constant':
            t = np.linspace(0.0, 1.0, num=len(line))
        if type == 'transition':
            first = np.array(self.getULines()[0])
            last = np.array(self.getULines()[-1])
            tck_first, u_first = interpolate.splprep(first.T, s=0, k=1)
            tck_last, u_last = interpolate.splprep(last.T, s=0, k=1)
            if number < 0.0:
                number = len(self.getVLines())
            v = float(number) / float(len(self.getVLines()))
            t = (1.0 - v) * u_first + v * u_last

        # evaluate function at any parameter "0<=t<=1"
        line = interpolate.splev(t, tck, der=0)
        line = list(zip(line[0].tolist(), line[1].tolist()))

        if direction == 'u':
            self.getULines()[number] = line
        elif direction == 'v':
            for i, uline in enumerate(self.getULines()):
                self.getULines()[i][number] = line[i] 
Example #17
Source File: utils.py    From sand-glyphs with MIT License 5 votes vote down vote up
def _rnd_interpolate(xy, num_points, ordered=False):
  tck,u = splprep([
    xy[:,0],
    xy[:,1]],
    s=0
  )
  unew = random(num_points)
  if ordered:
    unew = sort(unew)
  out = splev(unew, tck)
  return column_stack(out) 
Example #18
Source File: pYAAPT.py    From AMFM_decompy with MIT License 4 votes vote down vote up
def upsample(self, samp_values, file_size, first_samp=0, last_samp=0,
                 interp_tech='pchip'):
        if interp_tech is 'step':
            beg_pad = int((self.noverlap)/2)
            up_version = np.zeros((file_size))
            up_version[:beg_pad] = first_samp
            up_version[beg_pad:beg_pad+self.frame_jump*self.nframes] = \
                                    np.repeat(samp_values, self.frame_jump)
            up_version[beg_pad+self.frame_jump*self.nframes:] = last_samp

        elif interp_tech is 'pchip' or 'spline':
            if np.amin(samp_values) > 0:
                if interp_tech is 'pchip':
                    up_version = scipy_interp.pchip(self.frames_pos,
                                                    samp_values)(range(file_size))

                elif interp_tech is 'spline':
                    tck, u_original = scipy_interp.splprep(
                                                [self.frames_pos, samp_values],
                                                u=self.frames_pos)
                    up_version = scipy_interp.splev(range(file_size), tck)[1]
            else:
                beg_pad = int((self.noverlap)/2)
                up_version = np.zeros((file_size))
                up_version[:beg_pad] = first_samp
                voiced_frames = np.nonzero(samp_values)[0]
                edges = np.nonzero((voiced_frames[1:]-voiced_frames[:-1]) > 1)[0]
                edges = np.insert(edges, len(edges), len(voiced_frames)-1)
                voiced_frames = np.split(voiced_frames, edges+1)[:-1]

                for frame in voiced_frames:
                    up_interval = self.frames_pos[frame]
                    tot_interval = np.arange(int(up_interval[0]-(self.frame_jump/2)),
                                          int(up_interval[-1]+(self.frame_jump/2)))

                    if interp_tech is 'pchip' and len(frame) > 2:
                        up_version[tot_interval] = scipy_interp.pchip(
                                                    up_interval,
                                                    samp_values[frame])(tot_interval)

                    elif interp_tech is 'spline' and len(frame) > 3:
                        tck, u_original = scipy_interp.splprep(
                                            [up_interval, samp_values[frame]],
                                             u=up_interval)
                        up_version[tot_interval] = scipy_interp.splev(tot_interval, tck)[1]

                    # MD: In case len(frame)==2, above methods fail.
                    #Use linear interpolation instead.
                    elif len(frame) > 1:
                        up_version[tot_interval] = scipy_interp.interp1d(
                                                    up_interval,
                                                    samp_values[frame],
                                        fill_value='extrapolate')(tot_interval)

                    elif len(frame) == 1:
                        up_version[tot_interval] = samp_values[frame]


                up_version[beg_pad+self.frame_jump*self.nframes:] = last_samp

        return up_version 
Example #19
Source File: Orthogonal.py    From PyAero with MIT License 4 votes vote down vote up
def transfinite(north, south, west, east):
        """Make a transfinite interpolation.
        http://en.wikipedia.org/wiki/Transfinite_interpolation
        """

        south = np.array(south)
        north = np.array(north)
        west = np.array(west)
        east = np.array(east)

        # convert the block boundary curves into parametric form
        # as curves need to be between 0 and 1
        # interpolate B-spline through data points
        # here, a linear interpolant is derived "k=1"
        # splprep returns:
        # tck ... tuple (t,c,k) containing the vector of knots,
        #         the B-spline coefficients, and the degree of the spline.
        #   u ... array of the parameters for each given point (knot)
        tck_lower, u_lower = interpolate.splprep(south.T, s=0, k=1)
        tck_upper, u_upper = interpolate.splprep(north.T, s=0, k=1)
        tck_left, u_left = interpolate.splprep(west.T, s=0, k=1)
        tck_right, u_right = interpolate.splprep(east.T, s=0, k=1)

        # evaluate function at any parameter "0<=t<=1"
        def eta_left(t):
            return np.array(interpolate.splev(t, tck_left, der=0))

        def eta_right(t):
            return np.array(interpolate.splev(t, tck_right, der=0))

        def xi_bottom(t):
            return np.array(interpolate.splev(t, tck_lower, der=0))

        def xi_top(t):
            return np.array(interpolate.splev(t, tck_upper, der=0))

        nodes = np.zeros((len(west) * len(south), 2))

        # corner points
        c1 = xi_bottom(0.0)
        c2 = xi_top(0.0)
        c3 = xi_bottom(1.0)
        c4 = xi_top(1.0)

        for i, xi in enumerate(u_lower):
            xi_t = u_upper[i]
            for j, eta in enumerate(u_left):
                eta_r = u_right[j]

                node = i * len(u_left) + j

                # formula for the transinite interpolation
                point = (1.0 - xi) * eta_left(eta) + xi * eta_right(eta_r) + \
                    (1.0 - eta) * xi_bottom(xi) + eta * xi_top(xi_t) - \
                    ((1.0 - xi) * (1.0 - eta) * c1 + (1.0 - xi) * eta * c2 +
                     xi * (1.0 - eta) * c3 + xi * eta * c4)

                nodes[node, 0] = point[0]
                nodes[node, 1] = point[1]

        return nodes