Beispiel #1
0
def iterate(board):

    """

    Funkcja pobiera planszę game of life i zwraca jej następną iterację.

    Zasady Game of life są takie:

    1. Komórka może być albo żywa albo martwa.
    2. Jeśli komórka jest martwa i ma trzech sąsiadóœ to ożywa.
    3. Jeśli komórka jest żywa i ma mniej niż dwóch sąsiadów to umiera,
       jeśli ma więcej niż trzech sąsiadóœ również umiera. W przeciwnym wypadku
       (dwóch lub trzech sąsiadów) to żyje dalej.

    :param np.ndarray board: Dwuwymiarowa tablica zmiennych logicznych która
    obrazuje aktualny stan game of life. Jeśli w danym polu jest True (lub 1)
    oznacza to że dana komórka jest obsadzona

    """
    from numpy import logical_and as land, logical_or as lor, logical_not as lnot
#    print(board)
    board = board.astype(np.bool)
    neigh = calculate_neighbours(board)
#    print(neigh)
    ozywa = land(board == False, neigh == 3)
    umiera = lnot(land( board == True, lor( neigh < 2, neigh > 3) ))
#    print(ozywa)
#    print(umiera)    
    board2 = land(lor(board, ozywa), umiera)
    return(board2)    
Beispiel #2
0
def recurtime(array0,
              array1,
              border=None,
              exclude_zero=False,
              relative_time=False):
    """Return a list of time bef and time aft array0

    if border is None, put nan for missing values (first and last time)
    if border is "exclude" keep only time with a tbef and a taft
    if exclude_zero is True, do not count synchronous times
    if relative_time,return the shift, otherwise the absolute time
    """
    if not array0.size or not array1.size:
        print("One empty array in recurtime")
        if border is None:
            return [np.ones(array0.size) * np.inf] * 2
        else:
            return [np.array([])] * 2
    # initalise output
    exact_match = np.zeros(array0.shape, dtype=bool)
    afts = np.zeros(array0.shape) + np.nan
    tbefs = np.zeros(array0.shape) + np.nan
    tafts = np.zeros(array0.shape) + np.nan

    # search array0 in array1
    ind = array1.searchsorted(array0)

    # first deal with the middle:
    tbefs[ind > 0] = array1[ind[ind > 0] - 1]
    tafts[ind < len(array1)] = array1[ind[ind < len(array1)]]

    # find exact match:
    exact_match = tafts == array0
    if exclude_zero:
        # pb if the last match is exact
        lastmatch = ind >= len(array1) - 1
        to_push = land(exact_match, lnot(lastmatch))
        to_nan = land(exact_match, lastmatch)
        tafts[to_push] = array1[ind[to_push] + 1]
        tafts[to_nan] = np.nan
    else:
        tbefs[exact_match] = array0[exact_match]
        tafts[exact_match] = array0[exact_match]

    if relative_time:
        tbefs = array0 - tbefs
        tafts = tafts - array0

    if border == 'exclude':
        to_ret = land(lnot(np.isnan(tbefs)), lnot(np.isnan(tbafts)))
    else:
        to_ret = ones(tbefs.shape, dtype=bool)
    return tbefs[to_ret], tafts[to_ret]
Beispiel #3
0
def _groub_by(p, tol, r):
    g, gm, gp = [], [], p - p[0]
    while True:
        if gp[-1] < 0: break
        ndx = where(land(0. <= gp, gp < tol))[0]
        if 0 < len(ndx):
            g.append(ndx)
            gm.append(p[ndx].mean())
        gp -= tol
    return g, array([gm, [r] * len(gm)])
Beispiel #4
0
def _groub_by(p, tol, r):
  g, gm, gp= [], [], p- p[0]
  while True:
    if gp[-1]< 0: break
    ndx= where(land(0.<= gp, gp< tol))[0]
    if 0< len(ndx):
      g.append(ndx)
      gm.append(p[ndx].mean())
    gp-= tol
  return g, array([gm, [r]* len(gm)])
Beispiel #5
0
def cube2world(u, v):
    # [u,v] = meshgrid(0:3/(3*dim-1):3, 0:4/(4*dim-1):4);
    # u and v are in the [0,1] interval, so put them back to [0,3]
    # and [0,4]
    u = u * 3
    v = v * 4

    x = np.zeros(u.shape)
    y = np.zeros(u.shape)
    z = np.zeros(u.shape)
    valid = np.zeros(u.shape, dtype='bool')

    # up
    indUp = land(land(u >= 1, u < 2), v < 1)
    x[indUp] = (u[indUp] - 1.5) * 2
    y[indUp] = 1
    z[indUp] = (v[indUp] - 0.5) * -2

    # left
    indLeft = land(land(u < 1, v >= 1), v < 2)
    x[indLeft] = -1
    y[indLeft] = (v[indLeft] - 1.5) * -2
    z[indLeft] = (u[indLeft] - 0.5) * -2

    # forward
    indForward = land(land(land(u >= 1, u < 2), v >= 1), v < 2)
    x[indForward] = (u[indForward] - 1.5) * 2
    y[indForward] = (v[indForward] - 1.5) * -2
    z[indForward] = -1

    # right
    indRight = land(land(u >= 2, v >= 1), v < 2)
    x[indRight] = 1
    y[indRight] = (v[indRight] - 1.5) * -2
    z[indRight] = (u[indRight] - 2.5) * 2

    # down
    indDown = land(land(land(u >= 1, u < 2), v >= 2), v < 3)
    x[indDown] = (u[indDown] - 1.5) * 2
    y[indDown] = -1
    z[indDown] = (v[indDown] - 2.5) * 2

    # backward
    indBackward = land(land(u >= 1, u < 2), v >= 3)
    x[indBackward] = (u[indBackward] - 1.5) * 2
    y[indBackward] = (v[indBackward] - 3.5) * 2
    z[indBackward] = 1

    # normalize
    # np.hypot(x, y, z) #sqrt(x.^2 + y.^2 + z.^2);
    norm = np.sqrt(x**2 + y**2 + z**2)
    x = x / norm
    y = y / norm
    z = z / norm

    # return valid indices
    valid_ind = lor(
        lor(lor(indUp, indLeft), lor(indForward, indRight)), lor(indDown, indBackward))
    valid[valid_ind] = 1
    return x, y, z, valid
Beispiel #6
0
def world2cube(x, y, z):
    # world -> cube
    u = np.zeros(x.shape)
    v = np.zeros(x.shape)

    # forward
    indForward = np.nonzero(
        land(land(z <= 0, z <= -np.abs(x)), z <= -np.abs(y)))
    u[indForward] = 1.5 - 0.5 * x[indForward] / z[indForward]
    v[indForward] = 1.5 + 0.5 * y[indForward] / z[indForward]

    # backward
    indBackward = np.nonzero(
        land(land(z >= 0,  z >= np.abs(x)),  z >= np.abs(y)))
    u[indBackward] = 1.5 + 0.5 * x[indBackward] / z[indBackward]
    v[indBackward] = 3.5 + 0.5 * y[indBackward] / z[indBackward]

    # down
    indDown = np.nonzero(
        land(land(y <= 0,  y <= -np.abs(x)),  y <= -np.abs(z)))
    u[indDown] = 1.5 - 0.5 * x[indDown] / y[indDown]
    v[indDown] = 2.5 - 0.5 * z[indDown] / y[indDown]

    # up
    indUp = np.nonzero(land(land(y >= 0,  y >= np.abs(x)),  y >= np.abs(z)))
    u[indUp] = 1.5 + 0.5 * x[indUp] / y[indUp]
    v[indUp] = 0.5 - 0.5 * z[indUp] / y[indUp]

    # left
    indLeft = np.nonzero(
        land(land(x <= 0,  x <= -np.abs(y)),  x <= -np.abs(z)))
    u[indLeft] = 0.5 + 0.5 * z[indLeft] / x[indLeft]
    v[indLeft] = 1.5 + 0.5 * y[indLeft] / x[indLeft]

    # right
    indRight = np.nonzero(land(land(x >= 0,  x >= np.abs(y)),  x >= np.abs(z)))
    u[indRight] = 2.5 + 0.5 * z[indRight] / x[indRight]
    v[indRight] = 1.5 - 0.5 * y[indRight] / x[indRight]

    # bring back in the [0,1] intervals
    u = u / 3
    v = v / 4
    return u, v
Beispiel #7
0
    def _p_poly_dists(xs,ys,xv,yv):
        """
        http://www.mathworks.com/matlabcentral/fileexchange/19398-distance-from-a-point-to-polygon/content/p_poly_dist.m
        function: p_poly_dist
        Description:  distance from many points to polygon whose vertices are specified by the
                     vectors xv and yv
        Input:  
           xs - points' x coordinates
           ys - points' y coordinates
           xv - vector of polygon vertices x coordinates
           yv - vector of polygon vertices x coordinates
        Output: 
           d - distance from point to polygon (defined as a minimal distance from 
               point to any of polygon's ribs, positive if the point is outside the
               polygon and negative otherwise)
           x_poly: x coordinate of the point in the polygon closest to x,y
           y_poly: y coordinate of the point in the polygon closest to x,y
        
        Routines: p_poly_dist.m
        Revision history:
           03/31/2008 - return the point of the polygon closest to x,y
                      - added the test for the case where a polygon rib is 
                        either horizontal or vertical. From Eric Schmitz.
                      - Changes by Alejandro Weinstein
           7/9/2006  - case when all projections are outside of polygon ribs
           23/5/2004 - created by Michael Yoshpe 
        function [d,x_poly,y_poly] = p_poly_dist(x, y, xv, yv) 
        """

        xs = array(xs)
        ys = array(ys)
        xv = array(xv)
        yv = array(yv)
        Nv = len(xv)-1
        if ((xv[0] != xv[Nv]) or (yv[0] != yv[Nv])):
            xv = append(xv,xv[0])
            yv = append(yv,yv[0])
        #     Nv = Nv + 1

        # linear parameters of segments that connect the vertices
        # Ax + By + C = 0
        A = -diff(yv)
        B =  diff(xv)
        C = yv[1:]*xv[:-1] - xv[1:]*yv[:-1]

        # find the projection of each point (x,y) on each rib
        AB = 1./(A**2 + B**2)
        vv = outer(xs,A)+outer(ys,B)+C
        xps = (xs - (A*AB*vv).T).T
        yps = (ys - (B*AB*vv).T).T

        # Test for the case where a polygon rib is 
        # either horizontal or vertical. From Eric Schmitz
        i = where(diff(xv)==0)
        xps[:,i]=xv[i]
        i = where(diff(yv)==0)
        yps[:,i]=yv[i]

        # find all cases where projected point is inside the segment
        idx_x = lor(land(xv[:-1]<xps,xps<xv[1:]),land(xv[1:]<xps,xps<xv[:-1]))
        idx_y = lor(land(yv[:-1]<yps,yps<yv[1:]),land(yv[1:]<yps,yps<yv[:-1]))
        idx = land(idx_x,idx_y)

        idxsum = idx.sum(axis=1)

        ds = zeros(len(xs))
        x_polys = zeros(len(xs))
        y_polys = zeros(len(xs))

        offribs = where(idxsum==0)[0] #all projections outside of polygon ribs
        if len(offribs) > 0:
            dvs = hypot(subouter(xv[:-1],xs[offribs]), 
                        subouter(yv[:-1],ys[offribs]))
            I = argmin(dvs,axis=0)
            ds[offribs] = dvs[I,range(len(I))]
            x_polys[offribs] = xv[I]
            y_polys[offribs] = yv[I]

        onrib = where(idxsum!=0)[0]
        idx2 = idx[onrib]
        if len(onrib) > 0:
            dps = ma.masked_array(empty(idx2.shape), mask=lnot(idx2))
            dps[idx2] = hypot(xps[idx]-xs[where(idx)[0]], 
                        yps[idx]-ys[where(idx)[0]])
            # minds = dps.min(axis=0)
            # idxs = where(dps == minds)
            I = argmin(dps,axis=1)
            ds[onrib] = dps[range(len(I)),I]
            x_polys[onrib] = xps[onrib,I]
            y_polys[onrib] = yps[onrib,I]


        # if(inpolygon(x, y, xv, yv)) 
        #    d = -d
        # end
        return ds,x_polys,y_polys
Beispiel #8
0
    def _p_poly_dist(x,y,xv,yv):
        """
        function: p_poly_dist
        Description:  distance from point to polygon whose vertices are specified by the
                     vectors xv and yv
        Input:  
           x - point's x coordinate
           y - point's y coordinate
           xv - vector of polygon vertices x coordinates
           yv - vector of polygon vertices x coordinates
        Output: 
           d - distance from point to polygon (defined as a minimal distance from 
               point to any of polygon's ribs, positive if the point is outside the
               polygon and negative otherwise)
           x_poly: x coordinate of the point in the polygon closest to x,y
           y_poly: y coordinate of the point in the polygon closest to x,y
        
        Routines: p_poly_dist.m
        Revision history:
           03/31/2008 - return the point of the polygon closest to x,y
                      - added the test for the case where a polygon rib is 
                        either horizontal or vertical. From Eric Schmitz.
                      - Changes by Alejandro Weinstein
           7/9/2006  - case when all projections are outside of polygon ribs
           23/5/2004 - created by Michael Yoshpe 
        function [d,x_poly,y_poly] = p_poly_dist(x, y, xv, yv) 
        """

        Nv = len(xv)-1
        if ((xv[0] != xv[Nv]) or (yv[0] != yv[Nv])):
            xv = append(xv,xv[0])
            yv = append(yv,yv[0])
        #     Nv = Nv + 1

        # linear parameters of segments that connect the vertices
        # Ax + By + C = 0
        A = -diff(yv)
        B =  diff(xv)
        C = yv[1:]*xv[:-1] - xv[1:]*yv[:-1]

        # find the projection of point (x,y) on each rib
        AB = 1./(A**2 + B**2)
        vv = (A*x+B*y+C)
        xp = x - (A*AB)*vv
        yp = y - (B*AB)*vv

        # Test for the case where a polygon rib is 
        # either horizontal or vertical. From Eric Schmitz
        i = where(diff(xv)==0)
        xp[i]=xv[i]
        i = where(diff(yv)==0)
        yp[i]=yv[i]

        # find all cases where projected point is inside the segment
        idx_x = lor(land(xv[:-1]<xp,xp<xv[1:]),land(xv[1:]<xp,xp<xv[:-1]))
        idx_y = lor(land(yv[:-1]<yp,yp<yv[1:]),land(yv[1:]<yp,yp<yv[:-1]))
        idx = land(idx_x,idx_y)


        if idx.sum()==0:#no True, all projections are outside of polygon ribs
            # distance from point (x,y) to the vertices
            dv = hypot(xv[:-1]-x,yv[:-1]-y)
            I = argmin(dv)
            d = dv[I]
            x_poly = xv[I]
            y_poly = yv[I]
        else:
            # distance from point (x,y) to the projection on ribs
            dp = hypot(xp[idx]-x,yp[idx]-y)
            # d = min(dp)
            # idxs = where(dp == d)
            I = argmin(dp)
            d = dp[I]
            x_poly = xp[idx][I]
            y_poly = yp[idx][I]

        # if(inpolygon(x, y, xv, yv)) 
        #    d = -d
        # end
        return d,x_poly,y_poly
Beispiel #9
0
def cube2world(u, v):
    # [u,v] = meshgrid(0:3/(3*dim-1):3, 0:4/(4*dim-1):4);
    # u and v are in the [0,1] interval, so put them back to [0,3]
    # and [0,4]
    u = u * 3
    v = v * 4

    x = np.zeros(u.shape)
    y = np.zeros(u.shape)
    z = np.zeros(u.shape)
    valid = np.zeros(u.shape, dtype='bool')

    # up
    indUp = land(land(u >= 1, u < 2), v < 1)
    x[indUp] = (u[indUp] - 1.5) * 2
    y[indUp] = 1
    z[indUp] = (v[indUp] - 0.5) * -2

    # left
    indLeft = land(land(u < 1, v >= 1), v < 2)
    x[indLeft] = -1
    y[indLeft] = (v[indLeft] - 1.5) * -2
    z[indLeft] = (u[indLeft] - 0.5) * -2

    # forward
    indForward = land(land(land(u >= 1, u < 2), v >= 1), v < 2)
    x[indForward] = (u[indForward] - 1.5) * 2
    y[indForward] = (v[indForward] - 1.5) * -2
    z[indForward] = -1

    # right
    indRight = land(land(u >= 2, v >= 1), v < 2)
    x[indRight] = 1
    y[indRight] = (v[indRight] - 1.5) * -2
    z[indRight] = (u[indRight] - 2.5) * 2

    # down
    indDown = land(land(land(u >= 1, u < 2), v >= 2), v < 3)
    x[indDown] = (u[indDown] - 1.5) * 2
    y[indDown] = -1
    z[indDown] = (v[indDown] - 2.5) * 2

    # backward
    indBackward = land(land(u >= 1, u < 2), v >= 3)
    x[indBackward] = (u[indBackward] - 1.5) * 2
    y[indBackward] = (v[indBackward] - 3.5) * 2
    z[indBackward] = 1

    # normalize
    # np.hypot(x, y, z) #sqrt(x.^2 + y.^2 + z.^2);
    norm = np.sqrt(x**2 + y**2 + z**2)
    x = x / norm
    y = y / norm
    z = z / norm

    # return valid indices
    valid_ind = lor(
        lor(lor(indUp, indLeft), lor(indForward, indRight)), lor(indDown, indBackward))
    valid[valid_ind] = 1
    return x, y, z, valid
Beispiel #10
0
def world2cube(x, y, z):
    # world -> cube
    u = np.zeros(x.shape)
    v = np.zeros(x.shape)

    # forward
    indForward = np.nonzero(
        land(land(z <= 0, z <= -np.abs(x)), z <= -np.abs(y)))
    u[indForward] = 1.5 - 0.5 * x[indForward] / z[indForward]
    v[indForward] = 1.5 + 0.5 * y[indForward] / z[indForward]

    # backward
    indBackward = np.nonzero(
        land(land(z >= 0,  z >= np.abs(x)),  z >= np.abs(y)))
    u[indBackward] = 1.5 + 0.5 * x[indBackward] / z[indBackward]
    v[indBackward] = 3.5 + 0.5 * y[indBackward] / z[indBackward]

    # down
    indDown = np.nonzero(
        land(land(y <= 0,  y <= -np.abs(x)),  y <= -np.abs(z)))
    u[indDown] = 1.5 - 0.5 * x[indDown] / y[indDown]
    v[indDown] = 2.5 - 0.5 * z[indDown] / y[indDown]

    # up
    indUp = np.nonzero(land(land(y >= 0,  y >= np.abs(x)),  y >= np.abs(z)))
    u[indUp] = 1.5 + 0.5 * x[indUp] / y[indUp]
    v[indUp] = 0.5 - 0.5 * z[indUp] / y[indUp]

    # left
    indLeft = np.nonzero(
        land(land(x <= 0,  x <= -np.abs(y)),  x <= -np.abs(z)))
    u[indLeft] = 0.5 + 0.5 * z[indLeft] / x[indLeft]
    v[indLeft] = 1.5 + 0.5 * y[indLeft] / x[indLeft]

    # right
    indRight = np.nonzero(land(land(x >= 0,  x >= np.abs(y)),  x >= np.abs(z)))
    u[indRight] = 2.5 + 0.5 * z[indRight] / x[indRight]
    v[indRight] = 1.5 - 0.5 * y[indRight] / x[indRight]

    # bring back in the [0,1] intervals
    u = u / 3
    v = v / 4
    return u, v
Beispiel #11
0
            # The masks apply to ranges you don't want, they're MASKED!
            #   the result is an array with the values you do want
            #
            # Include points not exceeding the input threshold
            mx_include = ma.masked_array(tt1, mask=(tt1 > threshold))

            # Exclude points that exceed the input threshold
            mx_exclude = ma.masked_array(tt1, mask=(tt1 < threshold))
            exclude_counts.append(mx_exclude.count())
        else:
            mx_include = tt1
            mx_exclude = ma.masked_array([])
            exclude_counts.append(0)

        r1counts.append(
            ma.masked_array(tt1, land(tt1 >= s1Left, tt1 <= s1Right)).count())
        r2counts.append(
            ma.masked_array(
                tt1,
                lor(land(tt1 <= s1Left, tt1 >= s2Left),
                    land(tt1 >= s1Right, tt1 <= s2Right))).count())
        r3counts.append(
            ma.masked_array(
                tt1,
                lor(land(tt1 <= s2Left, tt1 >= s3Left),
                    land(tt1 >= s2Right, tt1 <= s3Right))).count())

        if threshold is not None:
            print '\tExtreme counts greater than %f = %d' % (
                threshold, mx_exclude.count())
Beispiel #12
0
 def contains_points(self, points):
     """Like contains_point but takes a list or array of points."""
     xs, ys = array(points).T
     return land( land(self._min.x <= xs, xs < self._max.x),
                  land(self._min.y < ys, ys <= self._max.y) )