예제 #1
0
파일: nbody.py 프로젝트: Ektorus/bohrium
def random_galaxy( B, x_max, y_max, z_max, n, dtype ):
    """Generate a galaxy of random bodies"""

    return {            # We let all bodies stand still initially
        'm':    np.random.random(n, dtype=dtype, bohrium=B.bohrium) * (10**6 / (4 * np.pi ** 2)),
        'x':    np.random.random(n, dtype=dtype, bohrium=B.bohrium) * (2*x_max-x_max),
        'y':    np.random.random(n, dtype=dtype, bohrium=B.bohrium) * (2*y_max-y_max),
        'z':    np.random.random(n, dtype=dtype, bohrium=B.bohrium) * (2*z_max-z_max),
        'vx':   np.ones(n, dtype=dtype, bohrium=B.bohrium),
        'vy':   np.ones(n, dtype=dtype, bohrium=B.bohrium),
        'vz':   np.ones(n, dtype=dtype, bohrium=B.bohrium),
    }
예제 #2
0
def simulate(m, x, y, z, vx, vy, vz, timesteps):

    temporaries = (np.ones(
        (size, size), dtype="float64"), np.ones((size, size), dtype="float64"),
                   np.ones((size, size), dtype="float64"),
                   np.ones((size, size), dtype="float64"))

    start = time.time()
    for i in range(timesteps):
        ret = move(m, x, y, z, vx, vy, vz, dt, temporaries)
        np.flush()
        print(x, y, z)
    end = time.time()
    print("Simulation time:", end - start)
예제 #3
0
def directionalBlur(img, size=15):
    """
    blur in a given direction

    https://www.packtpub.com/mapt/book/application_development/9781785283932/2/ch02lvl1sec21/motion-blur#!
    """
    oSize = img.shape
    kernel = np.zeros((size, size))
    kernel[int((size - 1) / 2), :] = np.ones(size)
    kernel = kernel / size
    img = scipy.signal.convolve(img, kernel)
    d = (img.shape[0] - oSize[0]) / 2, (img.shape[1] - oSize[1]) / 2
    img = img[d[0]:-d[0], d[1]:-d[1]]
    return img
예제 #4
0
 def get(self, type='smooth', mode='reflect'):
     """ type can be 'smooth' or 'fine'
         mode can be 'reflect','constant','nearest','mirror', 'wrap' for handling borders """
     gradfn = {'smooth': self.prewittgrad, 'fine': self.basicgrad}[type]
     gradx, grady = gradfn()
     # x, y and z below are now the gradient matrices,
     # each entry from x,y,z is a gradient vector at an image point
     x = filters.convolve(self.img, gradx, mode=mode)
     y = filters.convolve(self.img, grady, mode=mode)
     # norm is the magnitude of the x,y,z vectors,
     # each entry is the magnitude of the gradient at an image point and z*z = 1
     norm = np.sqrt(x * x + y * y + 1)
     # divide by the magnitude to normalise
     # as well scale to an image: negative 0-127, positive 127-255
     x, y = [a / norm * 127.0 + 128.0 for a in (x, y)]
     z = np.ones(
         self.shape) / norm  # generate z, matrix of ones, then normalise
     z = z * 255.0  # all positive
     # x, -y gives blender form
     # convert to int, transpose to rgb and return the normal map
     return np.array([x, -y, z]).transpose(1, 2, 0).astype(np.uint8)
예제 #5
0
#!/usr/bin/env python
import bohrium as np

for i in xrange(0,10):
    a = np.ones((3,3))
    b = np.ones((3))
    a = b
    np.bridge.flush()
    print "[[[[[ %d ]]]]]" % i

print a
def getChannel(img, channel):
    """
    get a channel as a new image.

    :param img: the image to extract a color channel from
    :param channel: name of the channel to extract - supports R,G,B,A,H,S,V

    Returns a grayscale image, or None
    """
    img = numpyArray(img)
    if channel == 'R':
        if len(img.shape) == 2:
            img = img[:, :]
        else:
            img = img[:, :, 0]
    elif channel == 'G':
        if len(img.shape) == 2:
            img = img[:, :]
        else:
            img = img[:, :, 1]
    elif channel == 'B':
        if len(img.shape) == 2:
            img = img[:, :]
        else:
            img = img[:, :, 2]
    elif channel == 'A':
        if len(img.shape) == 2 or len(img[0, 0]) < 4:
            img = np.ones(img.shape)
        else:
            img = img[:, :, 3]
    elif channel == 'H':
        if len(img.shape) == 2:
            img = np.zeros(img.shape)
        else:
            img = rgb2hsvArray(img)[:, :, 0]
    elif channel == 'S':
        if len(img.shape) == 2:
            img = np.zeros(img.shape)
        else:
            img = rgb2hsvArray(img)[:, :, 1]
    elif channel == 'V':
        if len(img.shape) == 2:
            pass
        else:
            img = rgb2hsvArray(img)[:, :, 2]
    elif channel == 'C':
        if len(img.shape) == 2:
            img = np.zeros(img.shape)
        else:
            img = rgb2cmykArray(img)[:, :, 0]
    elif channel == 'M':
        if len(img.shape) == 2:
            img = np.zeros(img.shape)
        else:
            img = rgb2cmykArray(img)[:, :, 1]
    elif channel == 'Y':
        if len(img.shape) == 2:
            img = np.zeros(img.shape)
        else:
            img = rgb2cmykArray(img)[:, :, 2]
    elif channel == 'K':
        if len(img.shape) == 2:
            pass
        else:
            img = rgb2cmykArray(img)[:, :, 3]
    else:
        raise Exception('Unknown channel: ' + channel)
    return img
def line(img, xxx_todo_changeme, xxx_todo_changeme1,thick=1,color=1):
    """
    comes from this neat implementation:
        https://stackoverflow.com/questions/31638651/how-can-i-draw-lines-into-numpy-arrays
    """
    (x,y) = xxx_todo_changeme
    (x2,y2) = xxx_todo_changeme1
    def trapez(y,y0,w):
        return np.clip(np.minimum(y+1+w/2-y0, -y+1+w/2+y0),0,1)

    def weighted_line(r0, c0, r1, c1, w, rmin=0, rmax=np.inf):
        # The algorithm below works fine if c1 >= c0 and c1-c0 >= abs(r1-r0).
        # If either of these cases are violated, do some switches.
        if abs(c1-c0) < abs(r1-r0):
            # Switch x and y, and switch again when returning.
            xx, yy, val = weighted_line(c0, r0, c1, r1, w, rmin=rmin, rmax=rmax)
            return (yy, xx, val)

        # At this point we know that the distance in columns (x) is greater
        # than that in rows (y). Possibly one more switch if c0 > c1.
        if c0 > c1:
            return weighted_line(r1, c1, r0, c0, w, rmin=rmin, rmax=rmax)

        # The following is now always < 1 in abs
        num=r1-r0
        denom=c1-c0
        slope = np.divide(num,denom,out=np.zeros_like(denom), where=denom!=0)

        # Adjust weight by the slope
        w *= np.sqrt(1+np.abs(slope)) / 2

        # We write y as a function of x, because the slope is always <= 1
        # (in absolute value)
        x = np.arange(c0, c1+1, dtype=float)
        y = x * slope + (c1*r0-c0*r1) / (c1-c0)

        # Now instead of 2 values for y, we have 2*np.ceil(w/2).
        # All values are 1 except the upmost and bottommost.
        thickness = np.ceil(w/2)
        yy = (np.floor(y).reshape(-1,1) + np.arange(-thickness-1,thickness+2).reshape(1,-1))
        xx = np.repeat(x, yy.shape[1])
        vals = trapez(yy, y.reshape(-1,1), w).flatten()

        yy = yy.flatten()

        # Exclude useless parts and those outside of the interval
        # to avoid parts outside of the picture
        mask = np.logical_and.reduce((yy >= rmin, yy < rmax, vals > 0))

        return (yy[mask].astype(int), xx[mask].astype(int), vals[mask])
    def naive_line(r0, c0, r1, c1):
        # The algorithm below works fine if c1 >= c0 and c1-c0 >= abs(r1-r0).
        # If either of these cases are violated, do some switches.
        if abs(c1-c0) < abs(r1-r0):
            # Switch x and y, and switch again when returning.
            xx, yy, val = naive_line(c0, r0, c1, r1)
            return (yy, xx, val)

        # At this point we know that the distance in columns (x) is greater
        # than that in rows (y). Possibly one more switch if c0 > c1.
        if c0 > c1:
            return naive_line(r1, c1, r0, c0)

        # We write y as a function of x, because the slope is always <= 1
        # (in absolute value)
        x = np.arange(c0, c1+1, dtype=float)
        y = x * (r1-r0) / (c1-c0) + (c1*r0-c0*r1) / (c1-c0)

        valbot = np.floor(y)-y+1
        valtop = y-np.floor(y)
        return (np.concatenate((np.floor(y), np.floor(y)+1)).astype(int), np.concatenate((x,x)).astype(int),np.concatenate((valbot, valtop)))
    if thick==1:
        rows, cols, weights=naive_line(x,y,x2,y2)
    else:
        rows, cols, weights=weighted_line(x,y,x2,y2,thick,rmin=0,rmax=max(img.shape[0],img.shape[1]))
    w = weights.reshape([-1, 1]) # reshape anti-alias weights
    if len(img.shape)>2:
        img[rows, cols, 0:3] = (
            np.multiply((1 - w) * np.ones([1, 3]),img[rows, cols, 0:3]) +
            w * np.array([color])
        )
    else:
        img[rows, cols] = (
            np.multiply(
                (1 - w) * np.ones([1, ]),
                img[rows, cols]) +
            w *
            color
        )[0]

    return img
예제 #8
0
파일: vizviz.py 프로젝트: Ektorus/bohrium
#!/usr/bin/env python
import bohrium as np
from bohrium import visualization

a = np.ones((100,100))
visualization.plot_surface(a)
예제 #9
0
파일: slicing.py 프로젝트: Ektorus/bohrium
np.bridge.flush()

print "\n\n>>>> first row\n"
a[0:9:, 0] = 1.0
np.bridge.flush()

print "\n\n>>>> last row\n"
a[0:9:,-1] = 1.0
np.bridge.flush()

print "\n\n>>>> inbetween row\n"
a[0:9:, 4] = 1.0
np.bridge.flush()
"""
print "Assign to slice2 single-element (MATRIX)"
a = np.ones((9,9))
np.bridge.flush()

print "\n\n>>>> first row\n"
a[0, 0] = 3.0
np.bridge.flush()

print "\n\n>>>> inbetween row\n"
a[4, 4] = 3.0
np.bridge.flush()

print "\n\n>>>> last row\n"
a[-1,-1] = 3.0
np.bridge.flush()

예제 #10
0
"""
Execute this with::

    BHPY_BACKEND="chapel" python chapel.numpy.implicit.py


"""
from __future__ import print_function
import bohrium as np

a = np.ones(10)

print(a)
예제 #11
0
파일: gentest.py 프로젝트: Ektorus/bohrium
def test_reduce(np, bohrium, shape):

    return np.sum(np.ones(shape))

if __name__ == "__main__":

    """
    print("NUMPY")
    import numpy as np
    print test_reduce(np, False, (10, 10, 10))
    print test_reduce(np, False, (10, 10, 10))
    """

    print("BOHRIUM")
    import bohrium as np
    shape = (10,10,10)
    a = np.sum(np.ones(shape))
    b = np.sum(np.ones(shape))
    c = np.add.reduce(np.add.reduce(np.add.reduce(np.ones(shape))))
    d = np.sum(np.ones(shape))
    print a,b,c,d

    #print np.add.reduce(np.add.reduce(np.add.reduce(np.ones(shape))))

    #t1 = np.add.reduce(np.ones(shape))
    #t2 = np.add.reduce(t1)
    #t3 = np.add.reduce(t2)
    #del t1, t2
    #print t3
예제 #12
0
파일: locate.py 프로젝트: Ektorus/bohrium
def main():
    B = util.Benchmark()

    nx      = B.size[0]
    ny      = B.size[1]
    nz      = B.size[2]
    ITER    = B.size[3]

    NO_OBST = 1
    omega   = 1.0
    density = 1.0
    deltaU  = 1e-7
    t1      = 1/3.0
    t2      = 1/18.0
    t3      = 1/36.0

    B.start()
    F       = np.ones((19, nx, ny, nz), dtype=np.float64)
    F[:]    = density/19.0
    FEQ     = np.ones((19, nx, ny, nz), dtype=np.float64)
    FEQ[:]  = density/19.0
    T       = np.zeros((19, nx, ny, nz), dtype=np.float64)

    #Create the scenery.
    BOUND   = np.zeros((nx, ny, nz), dtype=np.float64)
    BOUNDi  = np.ones((nx, ny, nz), dtype=np.float64)
    """
    if not NO_OBST:
        for i in xrange(nx):
            for j in xrange(ny):
                for k in xrange(nz):
                    if ((i-4)**2+(j-5)**2+(k-6)**2) < 6:
                        BOUND[i,j,k] += 1.0
                        BOUNDi[i,j,k] += 0.0
    BOUND[:,0,:]    += 1.0
    BOUNDi[:,0,:]   *= 0.0
    """

    if util.Benchmark().bohrium:
        np.flush()
    for ts in xrange(0, ITER):

        ##Propagate / Streaming step
        T[:] = F
        #nearest-neighbours
        F[1,:,:,0]   = T[1,:,:,-1]
        F[1,:,:,1:]  = T[1,:,:,:-1]
        F[2,:,:,:-1] = T[2,:,:,1:]
        F[2,:,:,-1]  = T[2,:,:,0]
        F[3,:,0,:]   = T[3,:,-1,:]
        F[3,:,1:,:]  = T[3,:,:-1,:]
        F[4,:,:-1,:] = T[4,:,1:,:]
        F[4,:,-1,:]  = T[4,:,0,:]
        F[5,0,:,:]   = T[5,-1,:,:]
        F[5,1:,:,:]  = T[5,:-1,:,:]
        F[6,:-1,:,:] = T[6,1:,:,:]
        F[6,-1,:,:]  = T[6,0,:,:]
        #next-nearest neighbours
        F[7,0 ,0 ,:] = T[7,-1 , -1,:]
        F[7,0 ,1:,:] = T[7,-1 ,:-1,:]
        F[7,1:,0 ,:] = T[7,:-1, -1,:]
        F[7,1:,1:,:] = T[7,:-1,:-1,:]

        F[8,0 ,:-1,:] = T[8,-1 ,1:,:]
        F[8,0 , -1,:] = T[8,-1 ,0 ,:]
        F[8,1:,:-1,:] = T[8,:-1,1:,:]
        F[8,1:, -1,:] = T[8,:-1,0 ,:]

        F[9,:-1,0 ,:] = T[9,1:, -1,:]
        F[9,:-1,1:,:] = T[9,1:,:-1,:]
        F[9,-1 ,0 ,:] = T[9,0 ,  0,:]
        F[9,-1 ,1:,:] = T[9,0 ,:-1,:]

        F[10,:-1,:-1,:] = T[10,1:,1:,:]
        F[10,:-1, -1,:] = T[10,1:,0 ,:]
        F[10,-1 ,:-1,:] = T[10,0 ,1:,:]
        F[10,-1 , -1,:] = T[10,0 ,0 ,:]

        F[11,0 ,:,0 ] = T[11,0  ,:, -1]
        F[11,0 ,:,1:] = T[11,0  ,:,:-1]
        F[11,1:,:,0 ] = T[11,:-1,:, -1]
        F[11,1:,:,1:] = T[11,:-1,:,:-1]

        F[12,0 ,:,:-1] = T[12, -1,:,1:]
        F[12,0 ,:, -1] = T[12, -1,:,0 ]
        F[12,1:,:,:-1] = T[12,:-1,:,1:]
        F[12,1:,:, -1] = T[12,:-1,:,0 ]

        F[13,:-1,:,0 ] = T[13,1:,:, -1]
        F[13,:-1,:,1:] = T[13,1:,:,:-1]
        F[13, -1,:,0 ] = T[13,0 ,:, -1]
        F[13, -1,:,1:] = T[13,0 ,:,:-1]

        F[14,:-1,:,:-1] = T[14,1:,:,1:]
        F[14,:-1,:, -1] = T[14,1:,:,0 ]
        F[14,-1 ,:,:-1] = T[14,0 ,:,1:]
        F[14,-1 ,:, -1] = T[14,0 ,:,0 ]

        F[15,:,0 ,0 ] = T[15,:, -1, -1]
        F[15,:,0 ,1:] = T[15,:, -1,:-1]
        F[15,:,1:,0 ] = T[15,:,:-1, -1]
        F[15,:,1:,1:] = T[15,:,:-1,:-1]

        F[16,:,0 ,:-1] = T[16,:, -1,1:]
        F[16,:,0 , -1] = T[16,:, -1,0 ]
        F[16,:,1:,:-1] = T[16,:,:-1,1:]
        F[16,:,1:, -1] = T[16,:,:-1,0 ]

        F[17,:,:-1,0 ] = T[17,:,1:, -1]
        F[17,:,:-1,1:] = T[17,:,1:,:-1]
        F[17,:, -1,0 ] = T[17,:,0 , -1]
        F[17,:, -1,1:] = T[17,:,0 ,:-1]

        F[18,:,:-1,:-1] = T[18,:,1:,1:]
        F[18,:,:-1, -1] = T[18,:,1:,0 ]
        F[18,:,-1 ,:-1] = T[18,:,0 ,1:]
        F[18,:,-1 , -1] = T[18,:,0 ,0 ]
        #Densities bouncing back at next timestep
        BB = np.empty(F.shape)
        T[:] = F
        T[1:,:,:,:] *= BOUND[np.newaxis,:,:,:]
        BB[2 ,:,:,:] += T[1 ,:,:,:]
        BB[1 ,:,:,:] += T[2 ,:,:,:]
        BB[4 ,:,:,:] += T[3 ,:,:,:]
        BB[3 ,:,:,:] += T[4 ,:,:,:]
        BB[6 ,:,:,:] += T[5 ,:,:,:]
        BB[5 ,:,:,:] += T[6 ,:,:,:]
        BB[10,:,:,:] += T[7 ,:,:,:]
        BB[9 ,:,:,:] += T[8 ,:,:,:]
        BB[8 ,:,:,:] += T[9 ,:,:,:]
        BB[7 ,:,:,:] += T[10,:,:,:]
        BB[14,:,:,:] += T[11,:,:,:]
        BB[13,:,:,:] += T[12,:,:,:]
        BB[12,:,:,:] += T[13,:,:,:]
        BB[11,:,:,:] += T[14,:,:,:]
        BB[18,:,:,:] += T[15,:,:,:]
        BB[17,:,:,:] += T[16,:,:,:]
        BB[16,:,:,:] += T[17,:,:,:]
        BB[15,:,:,:] += T[18,:,:,:]

        # Relax calculate equilibrium state (FEQ) with equivalent speed and density to F
        DENSITY = np.add.reduce(F)

        #UX = F[5,:,:,:].copy()
        UX = np.ones(F[5,:,:,:].shape, dtype=np.float64)
        UX[:,:,:] = F[5,:,:,:]

        UX += F[7,:,:,:]
        UX += F[8,:,:,:]
        UX += F[11,:,:,:]
        UX += F[12,:,:,:]
        UX -= F[6,:,:,:]
        UX -= F[9,:,:,:]
        UX -= F[10,:,:,:]
        UX -= F[13,:,:,:]
        UX -= F[14,:,:,:]
        UX /=DENSITY

        #UY = F[3,:,:,:].copy()
        UY = np.ones(F[3,:,:,:].shape, dtype=np.float64)
        UY[:,:,:] = F[3,:,:,:]

        UY += F[7,:,:,:]
        UY += F[9,:,:,:]
        UY += F[15,:,:,:]
        UY += F[16,:,:,:]
        UY -= F[4,:,:,:]
        UY -= F[8,:,:,:]
        UY -= F[10,:,:,:]
        UY -= F[17,:,:,:]
        UY -= F[18,:,:,:]
        UY /=DENSITY

        #UZ = F[1,:,:,:].copy()
        UZ = np.ones(F[1,:,:,:].shape, dtype=np.float64)
        UZ[:,:,:] = F[1,:,:,:]

        UZ += F[11,:,:,:]
        UZ += F[13,:,:,:]
        UZ += F[15,:,:,:]
        UZ += F[17,:,:,:]
        UZ -= F[2,:,:,:]
        UZ -= F[12,:,:,:]
        UZ -= F[14,:,:,:]
        UZ -= F[16,:,:,:]
        UZ -= F[18,:,:,:]
        UZ /=DENSITY

        UX[0,:,:] += deltaU #Increase inlet pressure
                            #Set bourderies to zero.
        UX[:,:,:] *= BOUNDi
        UY[:,:,:] *= BOUNDi
        UZ[:,:,:] *= BOUNDi
        DENSITY[:,:,:] *= BOUNDi

        U_SQU = UX**2 + UY**2 + UZ**2

        # Calculate equilibrium distribution: stationary
        FEQ[0,:,:,:] = (t1*DENSITY)*(1.0-3.0*U_SQU/2.0)
        # nearest-neighbours
        T1 = 3.0/2.0*U_SQU
        tDENSITY = t2*DENSITY
        FEQ[1,:,:,:]=tDENSITY*(1.0 + 3.0*UZ + 9.0/2.0*UZ**2 - T1)
        FEQ[2,:,:,:]=tDENSITY*(1.0 - 3.0*UZ + 9.0/2.0*UZ**2 - T1)
        FEQ[3,:,:,:]=tDENSITY*(1.0 + 3.0*UY + 9.0/2.0*UY**2 - T1)
        FEQ[4,:,:,:]=tDENSITY*(1.0 - 3.0*UY + 9.0/2.0*UY**2 - T1)
        FEQ[5,:,:,:]=tDENSITY*(1.0 + 3.0*UX + 9.0/2.0*UX**2 - T1)
        FEQ[6,:,:,:]=tDENSITY*(1.0 - 3.0*UX + 9.0/2.0*UX**2 - T1)
        # next-nearest neighbours
        T1 = 3.0*U_SQU/2.0
        tDENSITY = t3*DENSITY
        U8 = UX+UY
        FEQ[7,:,:,:] =tDENSITY*(1.0 + 3.0*U8  + 9.0/2.0*(U8)**2  - T1)
        U9 = UX-UY
        FEQ[8,:,:,:] =tDENSITY*(1.0 + 3.0*U9  + 9.0/2.0*(U9)**2  - T1)
        U10 = -UX+UY
        FEQ[9,:,:,:] =tDENSITY*(1.0 + 3.0*U10 + 9.0/2.0*(U10)**2 - T1)
        U8 *= -1.0
        FEQ[10,:,:,:]=tDENSITY*(1.0 + 3.0*U8 + 9.0/2.0*(U8)**2 - T1)
        U12 = UX+UZ
        FEQ[11,:,:,:]=tDENSITY*(1.0 + 3.0*U12 + 9.0/2.0*(U12)**2 - T1)
        U12 *= 1.0
        FEQ[14,:,:,:]=tDENSITY*(1.0 + 3.0*U12 + 9.0/2.0*(U12)**2 - T1)
        U13 = UX-UZ
        FEQ[12,:,:,:]=tDENSITY*(1.0 + 3.0*U13 + 9.0/2.0*(U13)**2 - T1)
        U13 *= -1.0
        FEQ[13,:,:,:]=tDENSITY*(1.0 + 3.0*U13 + 9.0/2.0*(U13)**2 - T1)
        U16 = UY+UZ
        FEQ[15,:,:,:]=tDENSITY*(1.0 + 3.0*U16 + 9.0/2.0*(U16)**2 - T1)
        U17 = UY-UZ
        FEQ[16,:,:,:]=tDENSITY*(1.0 + 3.0*U17 + 9.0/2.0*(U17)**2 - T1)
        U17 *= -1.0
        FEQ[17,:,:,:]=tDENSITY*(1.0 + 3.0*U17 + 9.0/2.0*(U17)**2 - T1)
        U16 *= -1.0
        FEQ[18,:,:,:]=tDENSITY*(1.0 + 3.0*U16 + 9.0/2.0*(U16)**2 - T1)
        F *= (1.0-omega)
        F += omega * FEQ

        #Densities bouncing back at next timestep
        F[1:,:,:,:] *= BOUNDi[np.newaxis,:,:,:]
        F[1:,:,:,:] += BB[1:,:,:,:]

        del BB
        del T1
        del UX, UY, UZ
        del U_SQU
        del DENSITY, tDENSITY
        del U8, U9, U10, U12, U13, U16, U17
        if util.Benchmark().bohrium:
            np.flush()

    B.stop()
    B.pprint()

    if B.outputfn:
        B.tofile(B.outputfn, {'res': UX})

    """