示例#1
0
def exponential(mean, shape=[]):
    """exponential(mean, n) or exponential(mean, [n, m, ...])

    Returns array of random numbers exponentially distributed with
    specified mean
    """
    # If U is a random number uniformly distributed on [0,1], then
    #      -ln(U) is exponentially distributed with mean 1, and so
    #      -ln(U)*M is exponentially distributed with mean M.
    (mean,), shape = _broadcast_arguments((mean,), shape)
    x = random(shape)
    num.log(x, x)
    num.subtract(0.0, x, x)
    num.multiply(mean, x, x)
    return x
示例#2
0
def exponential(mean, shape=[]):
    """exponential(mean, n) or exponential(mean, [n, m, ...])

    Returns array of random numbers exponentially distributed with
    specified mean
    """
    # If U is a random number uniformly distributed on [0,1], then
    #      -ln(U) is exponentially distributed with mean 1, and so
    #      -ln(U)*M is exponentially distributed with mean M.
    (mean, ), shape = _broadcast_arguments((mean, ), shape)
    x = random(shape)
    num.log(x, x)
    num.subtract(0.0, x, x)
    num.multiply(mean, x, x)
    return x
示例#3
0
文件: Convolve.py 项目: joshfermin/AI
def _correlate2d_fft(data0, kernel0, output=None, mode="nearest", cval=0.0):
    """_correlate2d_fft does 2d correlation of 'data' with 'kernel', storing
    the result in 'output' using the FFT to perform the correlation.

    supported 'mode's include:
        'nearest'   elements beyond boundary come from nearest edge pixel.
        'wrap'      elements beyond boundary come from the opposite array edge.
        'reflect'   elements beyond boundary come from reflection on same array edge.
        'constant'  elements beyond boundary are set to 'cval' 
    """
    shape = data0.shape
    kshape = kernel0.shape
    oversized = (num.array(shape) + num.array(kshape))

    dy = kshape[0] // 2
    dx = kshape[1] // 2

    kernel = num.zeros(oversized, typecode=num.Float64)
    kernel[:kshape[0], :kshape[1]] = kernel0[::-1, ::
                                             -1]  # convolution <-> correlation
    data = iraf_frame.frame(data0, oversized, mode=mode, cval=cval)

    complex_result = (isinstance(data, _nt.ComplexType)
                      or isinstance(kernel, _nt.ComplexType))

    Fdata = fft.fft2d(data)
    del data

    Fkernel = fft.fft2d(kernel)
    del kernel

    num.multiply(Fdata, Fkernel, Fdata)
    del Fkernel

    if complex_result:
        convolved = fft.inverse_fft2d(Fdata, s=oversized)
    else:
        convolved = fft.inverse_real_fft2d(Fdata, s=oversized)

    result = convolved[kshape[0] - 1:shape[0] + kshape[0] - 1,
                       kshape[1] - 1:shape[1] + kshape[1] - 1]

    if output is not None:
        output._copyFrom(result)
    else:
        return result
示例#4
0
文件: Convolve.py 项目: fxia22/ASM_xf
def _correlate2d_fft(data0, kernel0, output=None, mode="nearest", cval=0.0):
    """_correlate2d_fft does 2d correlation of 'data' with 'kernel', storing
    the result in 'output' using the FFT to perform the correlation.

    supported 'mode's include:
        'nearest'   elements beyond boundary come from nearest edge pixel.
        'wrap'      elements beyond boundary come from the opposite array edge.
        'reflect'   elements beyond boundary come from reflection on same array edge.
        'constant'  elements beyond boundary are set to 'cval' 
    """
    shape = data0.shape
    kshape = kernel0.shape
    oversized = (num.array(shape) + num.array(kshape))

    dy = kshape[0] // 2
    dx = kshape[1] // 2

    kernel = num.zeros(oversized, typecode=num.Float64)
    kernel[:kshape[0], :kshape[1]] = kernel0[::-1,::-1]   # convolution <-> correlation
    data = iraf_frame.frame(data0, oversized, mode=mode, cval=cval)

    complex_result = (isinstance(data, _nt.ComplexType) or 
                      isinstance(kernel, _nt.ComplexType))

    Fdata = fft.fft2d(data)
    del data
    
    Fkernel = fft.fft2d(kernel)
    del kernel
    
    num.multiply(Fdata, Fkernel, Fdata)
    del Fkernel

    if complex_result:
        convolved = fft.inverse_fft2d( Fdata, s=oversized)
    else:
        convolved = fft.inverse_real_fft2d( Fdata, s=oversized)
        
    result = convolved[ kshape[0]-1:shape[0]+kshape[0]-1, kshape[1]-1:shape[1]+kshape[1]-1 ]

    if output is not None:
        output._copyFrom( result )
    else:
        return result