예제 #1
0
def square_and_sum(a,s):
    """
    Writes np.sum(a**2,axis=0) into s
    """
    cmin, cmax = thread_partition_array(a)
    map_noreturn(asqs, [(a,s,cmin[i],cmax[i]) for i in xrange(len(cmax))])
    return a
예제 #2
0
    def __call__(self,x,y,amp=1.,scale=1.,symm=None,*args,**kwargs):

        if amp<0. or scale<0.:
            raise ValueError, 'The amp and scale parameters must be positive.'

        if symm is None:
            symm = (x is y)

        # Figure out how to divide job up between threads.
        nx = x.shape[0]
        ny = y.shape[0]
        n_threads = min(get_threadpool_size(), nx*ny / 10000)

        if n_threads > 1:
            if not symm:
                bounds = np.linspace(0,ny,n_threads+1)
            else:
                bounds = np.array(np.sqrt(np.linspace(0,ny*ny,n_threads+1)),dtype=int)

        # Split off the distance arguments
        distance_arg_dict = {}
        if hasattr(self.distance_fun, 'extra_parameters'):
            for key in self.extra_distance_params.iterkeys():
                if key in kwargs.keys():
                    distance_arg_dict[key] = kwargs.pop(key)

        # Allocate the matrix
        C = np.asmatrix(np.empty((nx,ny),dtype=float,order='F'))

        def targ(C,x,y, cmin, cmax,symm, d_kwargs=distance_arg_dict, c_args=args, c_kwargs=kwargs):
            # Compute distance for this bit
            self.distance_fun(C, x, y, cmin=cmin, cmax=cmax, symm=symm, **d_kwargs)
            imul(C, 1./scale, cmin=cmin, cmax=cmax, symm=symm)
            # Compute covariance for this bit
            if self.with_x:
                self.cov_fun(C,x,y,cmin=cmin, cmax=cmax,symm=symm,*c_args,**c_kwargs)
            else:
                self.cov_fun(C, cmin=cmin, cmax=cmax,symm=symm, *c_args, **c_kwargs)
            imul(C, amp*amp, cmin=cmin, cmax=cmax, symm=symm)

        if n_threads <= 1:
            targ(C,x,y,0,-1,symm)
        else:
            thread_args = [(C,x,y,bounds[i],bounds[i+1],symm) for i in xrange(n_threads)]
            map_noreturn(targ, thread_args)

        if symm:
            symmetrize(C)


        return C
예제 #3
0
파일: spear.py 프로젝트: askielboe/JAVELIN
def spear_threading(x,y,idx,idy,sigma,tau,lags,wids,scales,symm=None,set_pmap=False,blocksize=10000) :
    """
    threaded version, divide matrix into subblocks with *blocksize* 
    elements each. Do not use it when multiprocessing is on (e.g., in emcee MCMC
    sampling).

    set_pmap needs to be turned on for the Pmap_Model.
    """
    if (sigma<0. or tau<0.) :
        raise ValueError, 'The amp and scale parameters must be positive.'
    if (symm is None) :
        symm = (x is y) and (idx is idy)
    x = regularize_array(x)
    y = regularize_array(y)
    nx = x.shape[0]
    ny = y.shape[0]
    if np.isscalar(idx) :
        idx = np.ones(nx, dtype="int", order="F")*idx
    if np.isscalar(idy) :
        idy = np.ones(nx, dtype="int", order="F")*idy
    # Figure out how to divide job up between threads (along y)
    n_threads = min(get_threadpool_size(), nx*ny/blocksize)
    if n_threads > 1 :
        if not symm:
            # divide ny evenly if x is not y
            bounds = np.linspace(0,ny,n_threads+1)
        else :
            # divide ny*ny evenly in quadrature if x is y
            bounds = np.array(np.sqrt(np.linspace(0,ny*ny,n_threads+1)),dtype=int)
    # Allocate the matrix
    C = np.asmatrix(np.empty((nx,ny),dtype=float,order='F'))
    if set_pmap :
        def targ(C,x,y,idx,idy,cmin,cmax,symm) :
            SCF.covmatpmap_bit(C,x,y,idx,idy,sigma,tau,lags,wids,scales,cmin,cmax,symm)
    else :
        def targ(C,x,y,idx,idy,cmin,cmax,symm) :
            SCF.covmat_bit(C,x,y,idx,idy,sigma,tau,lags,wids,scales,cmin,cmax,symm)
    if n_threads <= 1 :
        targ(C,x,y,idx,idy,0,-1,symm)
    else :
        thread_args = [(C,x,y,idx,idy,bounds[i],bounds[i+1],symm) for i in xrange(n_threads)]
        map_noreturn(targ, thread_args)
    if symm:
        isotropic_cov_funs.symmetrize(C)
    return(C)
예제 #4
0
def brownian(x,y,amp=1.,scale=1.,origin=None,h=.5,symm=None):
    """
    brownian(x,y,amp=1., scale=1.,h=.5,origin=None)

    Fractional n-dimensional brownian motion. h=.5 corresponds to standard
    Brownian motion.

    A covariance function. Remember, broadcasting for covariance functions works
    differently than for numpy universal functions. C(x,y) returns a matrix, and
    C(x) returns a vector.

    :Parameters:

        - `amp`: The pointwise standard deviation of f.

        - `scale`: The factor by which to scale the distance between points.
                 Large value implies long-range correlation.

        - `h': The fractional parameter.


        - `x and y` are arrays of points in Euclidean coordinates
          formatted as follows:

          [[x_{0,0} ... x_{0,ndim}],
           [x_{1,0} ... x_{1,ndim}],
           ...
           [x_{N,0} ... x_{N,ndim}]]

        - `symm` indicates whether x and y are references to
          the same array.

        - `cmin' and `cmax' indicate which columns to compute.
          These are used for multithreaded evaluation.

    :Reference: http://en.wikipedia.org/wiki/Fractional_brownian_motion
    """
    # Thanks to Anne Archibald for handythread.py, the model for the
    # multithreaded call.

    if h<0 or h>1:
        raise ValueError, 'Parameter h must be between 0 and 1.'

    if amp<0. or scale<0.:
        raise ValueError, 'The amp and scale parameters must be positive.'

    if symm is None:
        symm = (x is y)

    # Figure out how to divide job up between threads.
    nx = x.shape[0]
    ny = y.shape[0]
    n_threads = min(get_threadpool_size(), nx*ny / 10000)

    if n_threads > 1:
        if not symm:
            bounds = np.linspace(0,ny,n_threads+1)
        else:
            bounds = np.array(np.sqrt(np.linspace(0,ny*ny,n_threads+1)),dtype=int)

    # Allocate the matrix
    C = np.asmatrix(np.empty((nx,ny),dtype=float,order='F'))
    if origin is not None:
        x = x-origin
        y = y-origin
    x = x / float(scale)
    y = y / float(scale)

    if n_threads <= 1:
        brownian_targ(C,x,y,h,amp,0,-1,symm)
    else:
        thread_args=[(C,x,y,h,amp,bounds[i],bounds[i+1],symm) for i in xrange(n_threads)]
        map_noreturn(brownian_targ, thread_args)

    return C
예제 #5
0
def spear_threading(x,
                    y,
                    idx,
                    idy,
                    sigma,
                    tau,
                    lags,
                    wids,
                    scales,
                    symm=None,
                    set_pmap=False,
                    blocksize=10000):
    """
    threaded version, divide matrix into subblocks with *blocksize*
    elements each. Do not use it when multiprocessing is on (e.g., in emcee MCMC
    sampling).

    set_pmap needs to be turned on for the Pmap_Model.
    """
    if (sigma < 0. or tau < 0.):
        raise ValueError, 'The amp and scale parameters must be positive.'
    if (symm is None):
        symm = (x is y) and (idx is idy)
    x = regularize_array(x)
    y = regularize_array(y)
    nx = x.shape[0]
    ny = y.shape[0]
    if np.isscalar(idx):
        idx = np.ones(nx, dtype="int", order="F") * idx
    if np.isscalar(idy):
        idy = np.ones(nx, dtype="int", order="F") * idy
    # Figure out how to divide job up between threads (along y)
    n_threads = min(get_threadpool_size(), nx * ny / blocksize)
    if n_threads > 1:
        if not symm:
            # divide ny evenly if x is not y
            bounds = np.linspace(0, ny, n_threads + 1)
        else:
            # divide ny*ny evenly in quadrature if x is y
            bounds = np.array(np.sqrt(np.linspace(0, ny * ny, n_threads + 1)),
                              dtype=int)
    # Allocate the matrix
    C = np.asmatrix(np.empty((nx, ny), dtype=float, order='F'))
    if set_pmap:

        def targ(C, x, y, idx, idy, cmin, cmax, symm):
            SCF.covmatpmap_bit(C, x, y, idx, idy, sigma, tau, lags, wids,
                               scales, cmin, cmax, symm)
    else:

        def targ(C, x, y, idx, idy, cmin, cmax, symm):
            SCF.covmat_bit(C, x, y, idx, idy, sigma, tau, lags, wids, scales,
                           cmin, cmax, symm)

    if n_threads <= 1:
        targ(C, x, y, idx, idy, 0, -1, symm)
    else:
        thread_args = [(C, x, y, idx, idy, bounds[i], bounds[i + 1], symm)
                       for i in xrange(n_threads)]
        map_noreturn(targ, thread_args)
    if symm:
        isotropic_cov_funs.symmetrize(C)
    return (C)
예제 #6
0
    def __call__(self, x, y, amp=1., scale=1., symm=None, *args, **kwargs):

        if amp < 0. or scale < 0.:
            raise ValueError, 'The amp and scale parameters must be positive.'

        if symm is None:
            symm = (x is y)

        # Figure out how to divide job up between threads.
        nx = x.shape[0]
        ny = y.shape[0]
        n_threads = min(get_threadpool_size(), nx * ny / 10000)

        if n_threads > 1:
            if not symm:
                bounds = np.linspace(0, ny, n_threads + 1)
            else:
                bounds = np.array(np.sqrt(
                    np.linspace(0, ny * ny, n_threads + 1)),
                                  dtype=int)

        # Split off the distance arguments
        distance_arg_dict = {}
        if hasattr(self.distance_fun, 'extra_parameters'):
            for key in self.extra_distance_params.iterkeys():
                if key in kwargs.keys():
                    distance_arg_dict[key] = kwargs.pop(key)

        # Allocate the matrix
        C = np.asmatrix(np.empty((nx, ny), dtype=float, order='F'))

        def targ(C,
                 x,
                 y,
                 cmin,
                 cmax,
                 symm,
                 d_kwargs=distance_arg_dict,
                 c_args=args,
                 c_kwargs=kwargs):
            # Compute distance for this bit
            self.distance_fun(C,
                              x,
                              y,
                              cmin=cmin,
                              cmax=cmax,
                              symm=symm,
                              **d_kwargs)
            imul(C, 1. / scale, cmin=cmin, cmax=cmax, symm=symm)
            # Compute covariance for this bit
            if self.with_x:
                self.cov_fun(C,
                             x,
                             y,
                             cmin=cmin,
                             cmax=cmax,
                             symm=symm,
                             *c_args,
                             **c_kwargs)
            else:
                self.cov_fun(C,
                             cmin=cmin,
                             cmax=cmax,
                             symm=symm,
                             *c_args,
                             **c_kwargs)
            imul(C, amp * amp, cmin=cmin, cmax=cmax, symm=symm)

        if n_threads <= 1:
            targ(C, x, y, 0, -1, symm)
        else:
            thread_args = [(C, x, y, bounds[i], bounds[i + 1], symm)
                           for i in xrange(n_threads)]
            map_noreturn(targ, thread_args)

        if symm:
            symmetrize(C)

        return C