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]
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)
def circular(self, shape, center=None, radius=None, bigger=0, auto=min, rev=False): """Creates a circular mask""" self.logger.log("Creating circular mask") try: h, w = shape if center is None: center = [int(w / 2), int(h / 2)] if radius is None: radius = auto(center[0], center[1], w - center[0], h - center[1]) Y, X = ogrid[:h, :w] dist_from_center = sqrt( power(X - center[0], 2) + power(Y - center[1], 2)) the_mask = dist_from_center <= radius + bigger if rev: return lnot(the_mask) else: return the_mask except Exception as excpt: self.logger.log(excpt)
def polygon(self, shape, points, rev=False): img = Image.new('L', shape, 0) ImageDraw.Draw(img).polygon(points, outline=1, fill=1) mask = ar(img) the_mask = mask == 1 if rev: return(lnot(the_mask)) else: return(the_mask)
def altaz(self, shape, altitude_range, azimut_range, center=None, radius=None, offset=90, auto=min, rev=False): """Creates an AltAz mask""" self.logger.log("Creating AltAz mask") try: w, h = shape if center is None: center = [int(w / 2), int(h / 2)] if radius is None: radius = auto(center[0], center[1], w - center[0], h - center[1]) r1 = radius * self.ima.projection(deg2rad(max(altitude_range))) r2 = radius * self.ima.projection(deg2rad(min(altitude_range))) r3 = radius mask1 = self.pizza(shape, azimut_range, center=center, radius=r1, offset=offset, auto=auto) * 1 mask2 = self.pizza(shape, azimut_range, center=center, radius=r2, offset=offset, auto=auto) * 1 mask3 = self.pizza(shape, azimut_range, center=center, radius=r3, offset=offset, auto=auto) * 1 the_sum = mask1 + mask2 + mask3 if rev: return lnot(the_sum == 2) else: return the_sum == 2 except Exception as excpt: self.logger.log(excpt)
def polygon(self, shape, points, rev=False): """Creates a poligonial mask""" self.logger.log("Creating ploy mask") try: img = PInew('L', (shape[1], shape[0]), 0) PIDraw(img).polygon(points, outline=1, fill=1) mask = ar(img) the_mask = mask == 1 if rev: return lnot(the_mask) else: return the_mask except Exception as excpt: self.logger.log(excpt)
def circular(self, shape, center=None, radius=None, bigger=0, auto=min, rev=False): h, w = shape if center is None: center = [int(w/2), int(h/2)] if radius is None: radius = auto(center[0], center[1], w-center[0], h-center[1]) Y, X = ogrid[:h, :w] dist_from_center = sqrt( power(X - center[0], 2) + power(Y-center[1], 2)) the_mask = dist_from_center <= radius + bigger if rev: return(lnot(the_mask)) else: return(the_mask)
def pizza(self, shape, angle_range, center=None, radius=None, offset=90, auto=min, rev=False): """Creates a pizza slice mask""" self.logger.log("Creating pizza mask") try: w, h = shape x, y = ogrid[:w, :h] if center is None: center = [int(w / 2), int(h / 2)] if radius is None: radius = auto(center[0], center[1], w - center[0], h - center[1]) cx, cy = center tmin, tmax = deg2rad(ar(angle_range) - offset) if tmax < tmin: tmax += 2 * pi r2 = (x - cx) * (x - cx) + (y - cy) * (y - cy) theta = arctan2(x - cx, y - cy) - tmin theta %= (2 * pi) circmask = r2 <= radius * radius anglemask = theta <= (tmax - tmin) the_mask = circmask * anglemask if rev: return lnot(the_mask) else: return the_mask except Exception as excpt: self.logger.log(excpt)
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