Example #1
0
def avalanche(x, A, f, N):

    count = 0
    degree = np.sum(A, 0, dtype=np.int32)
    #print(degree.shape)
    xc = degree + (degree == 0)
    spikes = x >= xc
    ava = spikes.copy()
    while np.dot(degree, spikes) > 0:
        ava = (ava + spikes) > 0
        spikes = x >= xc

        add_particles = np.dot(A, spikes)
        #subtract=np.multiply(np.random.rand(N),spikes)>f
        nonzero = add_particles.nonzero()[0]
        add_particles[nonzero] = np.random.rand(
            len(nonzero)) > f  #leak of avalanching particles

        x = x + add_particles
        x = x - np.multiply(spikes, degree)
        #x = x - (np.random.rand(N)>f)
        #x = np.multiply(x,(x>0))
        count = count + 1

    return [x, ava]
Example #2
0
def avalanche(x, A, f, N):

    count = 0
    degree = np.sum(A, 0, dtype=np.int32)
    #print(degree.shape)
    xc = degree + (degree == 0)
    spikes = x >= xc
    ava = spikes.copy()
    while np.sum(np.multiply(degree, spikes)) > 0:
        ava = (ava + spikes) > 0
        spikes = x >= xc

        x = x + np.dot(A, spikes)
        x = x - np.multiply(spikes, degree)
        x = x - (np.random.rand(N) > f)
        x = np.multiply(x, (x > 0))
        count = count + 1

    return [x, ava]
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
Example #4
0
    temp1 = np.zeros(N)
    ava = spikes.copy()
    if np.sum(np.multiply(degree,spikes))>0: 
        [x,temp1] = avalanche(x,A,f,N)
        count = count + 1
    ava = (ava+temp1)>0
    """
    degree = np.sum(A, 0, dtype=np.int32)
    print("Particles", np.sum(x))
    #degree = np.reshape(degree, (N,1))
    #print(degree.shape)
    xc = degree + (degree == 0)
    spikes = x >= xc
    ava = spikes.copy()
    temp1 = np.zeros(N, dtype=np.bool)
    if np.sum(np.multiply(degree, spikes)) > 0:
        [x, temp1] = avalanche(x, A, f, N)
        count = count + 1

    #xsave[i] = x
    ava = (ava + temp1) > 0
    a = int(np.sum(ava))
    avalanche_sizes.append(a)
    print('Avalanche size', a)
    WRITE("Avalanche processing over. Avalanche size:" + str(a))

    ##Rewiring
    """
    for c1 in range(N):
        for c2 in range(N):
            if Ruse[c1,c2] <= Ruse[c2,c1]: