Example #1
0
    def _more_init(self):
        self.shape = tuple(self.shape)
        self.coorv = ndgrid(*self.coor)
        if not isinstance(self.coorv, (list, tuple)):
            # 1D grid, wrap self.coorv as list:
            self.coorv = [self.coorv]

        self.npoints = 1
        for i in range(len(self.shape)):
            self.npoints *= self.shape[i]

        self.tolerance = (max(self.max_coor) - min(self.min_coor)) * 1E-14

        # nicknames: xcoor, ycoor, xcoorv, ycoorv, etc
        for i in range(self.nsd):
            self.__dict__[self.dirnames[i] + 'coor'] = self.coor[i]
            self.__dict__[self.dirnames[i] + 'coorv'] = self.coorv[i]

        if self.nsd == 3:
            # make boundary coordinates for vectorization:
            xdummy, \
            self.ycoorv_xfixed_boundary, \
            self.zcoorv_xfixed_boundary = ndgrid(0, self.ycoor, self.zcoor)

            self.xcoorv_yfixed_boundary, \
            ydummy, \
            self.zcoorv_yfixed_boundary = ndgrid(self.xcoor, 0, self.zcoor)

            self.xcoorv_yfixed_boundary, \
            self.zcoorv_yfixed_boundary, \
            zdummy = ndgrid(self.xcoor, self.ycoor, 0)
Example #2
0
def CreateRow(xmin, xmax, N, kernel, theta):
    dim = N.size

    if dim == 1:
        x = linspace(xmin[0], xmax[0], N[0])
        R = DistanceVector(x, x[0], theta)

    elif dim == 2:
        x1 = linspace(xmin[0], xmax[0], N[0])
        x2 = linspace(xmin[1], xmax[1], N[1])

        xx, yy = ndgrid(x1, x2)

        x = vstack((ravel(xx, order='F'), ravel(yy, order='F'))).transpose()
        R = DistanceVector(x, x[0, :].transpose(), theta)

    elif dim == 3:
        x1 = linspace(xmin[0], xmax[0], N[0])
        x2 = linspace(xmin[1], xmax[1], N[1])
        x3 = linspace(xmin[2], xmax[2], N[2])

        xx, yy, zz = ndgrid(x1, x2, x3)

        x = vstack((ravel(xx, order='F'), ravel(yy, order='F'),
                    ravel(zz, order='F'))).transpose()
        R = DistanceVector(x, x[0, :].transpose(), theta)

    else:
        print 'Wrong dimension'

    row = kernel(R)
    return row, x
Example #3
0
def CreateRow(xmin,xmax,N,kernel,theta):
	dim = N.size
	
	if dim == 1:
		x = linspace(xmin[0],xmax[0],N[0])
		R = DistanceVector(x,x[0],theta)

	elif dim == 2:
		x1 = linspace(xmin[0],xmax[0],N[0])
		x2 = linspace(xmin[1],xmax[1],N[1])

		xx, yy = ndgrid(x1,x2)
	
		x = vstack((ravel(xx, order = 'F'),ravel(yy, order = 'F'))).transpose()
		R = DistanceVector(x,x[0,:].transpose(),theta)

	elif dim == 3:
		x1 = linspace(xmin[0],xmax[0],N[0])
		x2 = linspace(xmin[1],xmax[1],N[1])
		x3 = linspace(xmin[2],xmax[2],N[2])

		xx, yy, zz = ndgrid(x1,x2,x3)
	
		x = vstack((ravel(xx, order = 'F'),ravel(yy, order = 'F'),ravel(zz, order = 'F'))).transpose()
		R = DistanceVector(x,x[0,:].transpose(),theta)

	else:
		print 'Wrong dimension'

	row = kernel(R)
	return row, x
Example #4
0
    def _more_init(self):
        self.shape = tuple(self.shape)
        self.coorv = ndgrid(*self.coor)
        if not isinstance(self.coorv, (list,tuple)):
            # 1D grid, wrap self.coorv as list:
            self.coorv = [self.coorv]

        self.npoints = 1
        for i in range(len(self.shape)):
            self.npoints *= self.shape[i]

        self.tolerance = (max(self.max_coor) - min(self.min_coor))*1E-14

        # nicknames: xcoor, ycoor, xcoorv, ycoorv, etc
        for i in range(self.nsd):
            self.__dict__[self.dirnames[i]+'coor'] = self.coor[i]
            self.__dict__[self.dirnames[i]+'coorv'] = self.coorv[i]

        if self.nsd == 3:
            # make boundary coordinates for vectorization:
            xdummy, \
            self.ycoorv_xfixed_boundary, \
            self.zcoorv_xfixed_boundary = ndgrid(0, self.ycoor, self.zcoor)

            self.xcoorv_yfixed_boundary, \
            ydummy, \
            self.zcoorv_yfixed_boundary = ndgrid(self.xcoor, 0, self.zcoor)

            self.xcoorv_yfixed_boundary, \
            self.zcoorv_yfixed_boundary, \
            zdummy = ndgrid(self.xcoor, self.ycoor, 0)
Example #5
0
def simplex_quad(n, N):
    """
    n: number of dimensions (bins)
    N: number of integration points per bin
    """
    vert=eye(n+1, n)
    
    if n==1:
        q, w = rquad(N, 0)
    else:
        q, w=zip(*[rquad(N, n-(k+1)) for k in range(n)])
        Q=ndgrid(*q); W=ndgrid(*w)
        shaper=np.zeros([N, N, N*n])
        for i in range(n):
            shaper[:,:,i*N:(i+1)*N] = Q[i]
        qu = reshape(shaper,[N**n,n], order='F')
        shaper=np.zeros([N, N, N*n])
        for i in range(n):
            shaper[:,:,i*N:(i+1)*N] = W[i]
        wu = reshape(shaper,[N**n,n], order='F')
        m=eye(n+1)
        m[1:n+1,0]=-1
        c=np.dot(m, vert)
        W=abs(det(c[1:n+1, :]))*prod(wu, axis=1)
        qp=cumprod(qu, 1)
        e=np.ones([N**n, 1])
        np.append((1-qu[:, :n-1]), array(e), 1)
        array([e, qp[:, :n-2], qp[:,n-1]])
        X=np.dot(np.append(array(e),np.append((1-qu[:, :n-1]), array(e), 1)*array([array(e), qp[:, :n-2], qp[:,n-1]]).T,1), c)
        return W, X
Example #6
0
def flow(*args):
    # xx,yy,zz,vv = flow()
    # xx,yy,zz,vv = flow(n)
    # xx,yy,zz,vv = flow(xx,yy,zz)
    if len(args) == 0:
        xx, yy, zz = ndgrid(linspace(0.1, 10, 50),
                            linspace(-3, 3, 25),
                            linspace(-3, 3, 25),
                            sparse=False)
    elif len(args) == 1:
        n = int(args[0])
        xx, yy, zz = ndgrid(linspace(0.1, 10, 2 * n),
                            linspace(-3, 3, n),
                            linspace(-3, 3, n),
                            sparse=False)
    elif len(args) == 3:
        xx, yy, zz = args
    else:
        raise SyntaxError("Invalid number of arguments.")

    # convert to spherical coordinates:
    theta = arctan2(zz, yy)
    phi = arctan2(xx, sqrt(yy**2 + zz**2))
    r = sqrt(xx**2 + yy**2 + zz**2)

    rv = 2 / r * (3 / (2 - cos(phi))**2 - 1)
    phiv = -2 * sin(phi) / (2 - cos(phi)) / r
    thetav = zeros(shape(r))

    # convert back to cartesian coordinates:
    xv = rv * cos(phiv) * cos(thetav)
    yv = rv * cos(phiv) * sin(thetav)
    zv = rv * sin(phiv)

    vv = log(sqrt(xv**2 + yv**2 + zv**2))

    return xx, yy, zz, vv
Example #7
0
def flow(*args):
    # xx,yy,zz,vv = flow()
    # xx,yy,zz,vv = flow(n)
    # xx,yy,zz,vv = flow(xx,yy,zz)
    if len(args) == 0:
        xx, yy, zz = ndgrid(linspace(0.1, 10, 50),
                            linspace(-3, 3, 25),
                            linspace(-3, 3, 25),
                            sparse=False)
    elif len(args) == 1:
        n = int(args[0])
        xx, yy, zz = ndgrid(linspace(0.1, 10, 2*n),
                            linspace(-3, 3, n),
                            linspace(-3, 3, n),
                            sparse=False)
    elif len(args) == 3:
        xx, yy, zz = args
    else:
        raise SyntaxError("Invalid number of arguments.")
    
    # convert to spherical coordinates:
    theta = arctan2(zz, yy)
    phi = arctan2(xx, sqrt(yy**2 + zz**2))
    r = sqrt(xx**2 + yy**2 + zz**2)

    rv = 2/r*(3/(2-cos(phi))**2 - 1)
    phiv = -2*sin(phi)/(2-cos(phi))/r
    thetav = zeros(shape(r))

    # convert back to cartesian coordinates:
    xv = rv*cos(phiv)*cos(thetav)
    yv = rv*cos(phiv)*sin(thetav)
    zv = rv*sin(phiv)

    vv = log(sqrt(xv**2 + yv**2 + zv**2))

    return xx, yy, zz, vv
Example #8
0
def peaks(*args):
    # z = peaks()
    # z = peaks(n)
    # z = peaks(x,y)
    n = 49
    nargs = len(args)
    if nargs in (0, 1):
        if nargs == 1:
            n = int(args[0])
        x, y = ndgrid(linspace(-3, 3, n), linspace(-3, 3, n))
    elif nargs == 2:
        x, y = args
    else:
        raise SyntaxError("Invalid number of arguments.")
    return 3*(1-x)**2*exp(-x**2-(y+1)**2) \
           - 10*(x/5-x**3-y**5)*exp(-x**2-y**2) - 1/3*exp(-(x+1)**2-y**2)
Example #9
0
def peaks(*args):
    # z = peaks()
    # z = peaks(n)
    # z = peaks(x,y)
    n = 49
    nargs = len(args)
    if nargs in (0,1):
        if nargs == 1:
            n = int(args[0])
        x, y = ndgrid(linspace(-3,3,n),linspace(-3,3,n))
    elif nargs == 2:
        x, y = args
    else:
        raise SyntaxError("Invalid number of arguments.")
    return 3*(1-x)**2*exp(-x**2-(y+1)**2) \
           - 10*(x/5-x**3-y**5)*exp(-x**2-y**2) - 1/3*exp(-(x+1)**2-y**2)
    # Initial target size.
    target_sz = np.array([initstate[3], initstate[2]])
    
    # Parameters according to the paper.
    padding = 1                               # Extra area.
    rho = 0.075                               # Learning parameter rho.
    sz = target_sz * (1 + padding)            # Context region size.


    # Parameters of scale update - scale ratio, lambda, average frames.
    scale, lambada, num = 1, 0.55, 15
    # Pre-computed confidence map.
    alapha = 2.25

    rs, cs = sn.ndgrid(np.array(range(sz[0])) + 1 - sz[0]/2.,
                       np.array(range(sz[1])) + 1  - sz[1]/2.)

    dist = rs**2. + cs**2.

    conf = np.exp(-0.5 * np.sqrt(dist) / alapha)

    # normalization
    conf = conf / conf.sum(axis=0).sum()

    # frequency
    conff = np.fft.fft2(conf)

    # store pre-computed weight window
    hamming_window = np.outer(np.hamming(sz[0]),
                              np.hanning(sz[1]).T)