def pca_var(sub_dims):
        data = np.array([df[d] for d in sub_dims]).T
        try: pca = PCA(data, standardize=True)
        except: return 0,1,0,1,None,None,None,sub_dims

        classed_points = zip(classes, pca.Y)
        pos = [(it[0], it[1]) for c, it in classed_points if c]
        neg = [(it[0], it[1]) for c, it in classed_points if not c]
        P_hull = [pos[i] for i in ConvexHull(pos).vertices]; P_hull.append(P_hull[0])
        N_hull = [neg[i] for i in ConvexHull(neg).vertices]; N_hull.append(N_hull[0])
        P_hull = np.array(P_hull)
        N_hull = np.array(N_hull)
        P_path = Path(P_hull)
        N_path = Path(N_hull)

        N_sep = 0
        for it in neg:
            if not P_path.contains_point(it):
                N_sep += 1

        P_sep = 0
        for it in pos:
            if not N_path.contains_point(it):
                P_sep += 1

        return N_sep, float(len(neg)), P_sep, float(len(pos)), P_hull, N_hull, pca, sub_dims
def reverse_geocode(tweet):
	# print index
	# latlong = json.load(tweets)[index]['coordinates']
	latlong = tweet["coordinates"]
	if latlong == [0.0, 0.0]:
		return False
	# latlong.reverse()
	with open('world-countries.json.txt', 'r') as countries_json:
		found_country = None
		countries = json.load(countries_json)['features']
		for country in countries:
			country_name = country['properties']['name']
			if country['geometry']['type'] == 'Polygon':
				country_vertices = country['geometry']['coordinates'][0]
				country_path = Path(country_vertices)
				if country_path.contains_point(latlong):
					found_country = country_name
					break
			if country['geometry']['type'] == 'MultiPolygon':
				country_polygons = country['geometry']['coordinates']
				for polygon in country_polygons:
					country_vertices = polygon[0]
					country_path = Path(country_vertices)
					if country_path.contains_point(latlong):
						found_country = country_name
						break
		if not found_country:
			found_country = False
	return found_country
# with open('Ferguson_tweets.txt', 'r') as tweets:
# 	print reverse_geocode(tweets, 0)
def contained_in(lat, lng, bound_coords):
    """
    Returns true if (lat, lng) is contained within the polygon formed by the
    points in bound_coords.
    """
    bound_path = Path(np.array(bound_coords))
    return bound_path.contains_point((lat, lng))
예제 #4
0
파일: box.py 프로젝트: gogrean/PyXel
 def distribute_pixels(self, edges, length, width):
     corners = self.get_corners()
     reg_path = Path(corners)
     # Get region boundaries.
     bounds = reg_path.get_extents().get_points()
     [[x_min_bound, y_min_bound], [x_max_bound, y_max_bound]] = bounds
     # For cases when the boundary pixels are not integers:
     x_min_bound = floor(x_min_bound)
     y_min_bound = floor(y_min_bound)
     x_max_bound = ceil(x_max_bound)
     y_max_bound = ceil(y_max_bound)
     pixels_in_bins = []
     for x in range(max(0, x_min_bound), min(x_max_bound+1, width)):
         for y in range(max(0, y_min_bound), min(y_max_bound+1, length)):
             if reg_path.contains_point((x, y)):
                 x_nonrotated, y_nonrotated = rotate_point(self.x0, self.y0,
                                                           x - self.x0,
                                                           y - self.y0,
                                                           -self.angle)
                 dist_from_box_bottom = self.height/2. - \
                                        (self.y0 - y_nonrotated)
                 for i, edge in enumerate(edges[1:]):
                     if edge > dist_from_box_bottom:
                         pixels_in_bins.append((y, x, i))
                         break
     return pixels_in_bins
예제 #5
0
 def onselect(self, verts):
     path = Path(verts)
     self.ind = np.nonzero([path.contains_point(xy) for xy in self.xys])[0]
     self.fc[:, -1] = self.alpha_other
     self.fc[self.ind, -1] = 1
     self.collection.set_facecolors(self.fc)
     self.canvas.draw_idle()
예제 #6
0
    def path_contains_points(verts, points):
        p = Path(verts, closed=True)
        result = num.zeros(points.shape[0], dtype=num.bool)
        for i in range(result.size):
            result[i] = p.contains_point(points[i, :])

        return result
예제 #7
0
파일: calc.py 프로젝트: moghimis/okean
def inpolygon(x,y,xp,yp):
  '''
  Points inside polygon test.

  Based on matplotlib nxutils for old matplotlib versions
  http://matplotlib.sourceforge.net/faq/howto_faq.html#test-whether-a-point-is-inside-a-polygon

  Can also use Path.contains_point, for recent versions, or
  the pnpoly extension - point in polyon test - by W R Franklin,
  http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
  '''

  try:
    from matplotlib.nxutils import pnpoly, points_inside_poly
    _use_mpl = 1
  except ImportError:
    from matplotlib.path import Path
    _use_mpl = 2
  except:
    import pnpoly
    _use_mpl = False

  shape=False
  try:
    if x.ndim>1:
      shape=x.shape
      x=x.flat
      y=y.flat
  except: pass

  if _use_mpl==1:
    verts=np.array(zip(xp,yp))
    if isiterable(x,y):
      points=np.array(zip(x,y))
      res=points_inside_poly(points,verts)
    else:
      res=pnpoly(x,y,verts)==1

  elif _use_mpl==2:
    verts=np.array(zip(xp,yp))
    p=Path(verts)
    if not isiterable(x,y): x,y,itr=[x],[y],0
    else: itr=1
    res=[p.contains_point((x[i],y[i]), radius=0.) for i in range(len(x))]
    res=np.asarray(res,'bool')
    if not itr: res=res[0]

  else:
    if not isiterable(x,y): x,y,itr=[x],[y],0
    else: itr=1
    res=np.zeros(len(x))
    res=[pnpoly.pnpoly(x[i],y[i],xp,yp,len(xp)) for i in range(len(x))]
    res=np.asarray(res)==1
    if not itr: res=res[0]

  if not shape is False: res.shape=shape
  return res
def insert_fake_stars(d,h,mags,all_poly,WCS,sex,survey="UKIDSS",zp=25.):
    fake_stars = []
    xsize = h['NAXIS1']
    ysize = h['NAXIS2']
    #Insert fake star
    if survey== "UKIDSS":
        size = 11
        sigma = 2.5/2.35
    if survey== "VISTA":
        size = 11
        sigma = 2.5/2.35 #FWHM ~ 2.5 pixels
    if survey == "2MASS":
        size = 5

    for mag in mags:
        flag_in = False
        while flag_in == False:
            poly = all_poly #We now only have one contour
            #for poly in all_poly:
            #print(poly)
            verts = np.array(poly,float)
            #print(verts)
            x = np.random.random_sample()*(xsize-size-6)+(size)
            y = np.random.random_sample()*(ysize-size-6)+(size)
            #print(WCS)
            #print(x,y)

            pixcrd  = np.array([[x,y]], np.float_)
            radec = np.array(WCS.wcs_pix2world(pixcrd,0))
            #print(radec)

            gc = coordinates.ICRS(radec[0][0],radec[0][1], unit=(u.degree, u.degree))
            galcoords = gc.galactic
            #L.append(galcoords.l.degrees)
            #B.append(galcoords.b.degrees)
            path = Path(verts)
            yo = path.contains_point((galcoords.l.degree,galcoords.b.degree))
            #yo = nx.pnpoly(galcoords.l.degrees,galcoords.b.degrees,verts)
            if yo == 1:
            #print(te)
                #print("a star is in the contour")
                flag_in = True
            else:
                pass
                #print("a star is outside the contour")
        magnitude = mag
        #zp = sex.config['MAG_ZEROPOINT']
        #Now we pass in zp instead
        expfactor = (magnitude - zp)/(-2.5)
        counts = math.pow(10.,expfactor)
        g = gauss_kern(size,sigma,counts) #5 is rough guess for FWHM
    #       print d[y-size:y+size+1,x-size:x+size+1].size
    #       print g.size
        d[y-size:y+size+1,x-size:x+size+1] += g #Damn backward numpy arrays
        fake_stars.append((x,y,mag))
    fits.writeto("TestOuput.fits",d,h,clobber=True)
    return(fake_stars)
예제 #9
0
파일: track.py 프로젝트: marjakm/common
class Track(object):

    def __init__(self):
        self.inner_path = Path(inner)
        self.outer_path = Path(outer)

        self.lines = list()
        for arr in (inner, outer):
            for i in range(len(arr)-1):
                self.lines.append(self.get_line_from_two_points(arr[i], arr[i+1]))

    def get_line_from_two_points(self, p1, p2):
        pa,pb = (p1,p2) if p1[0]<p2[0] else (p2,p1)
        a = (pb[1]-pa[1])/(pb[0]-pa[0])
        c = pa[1]-a*pa[0]
        return a, c, pa[0], pb[0]

    def contains(self, points, edges):
        res = True
        for name,point in points.items():
            if self.inner_path.contains_point(point):
                res = False
                print("Point {} iside inner track boundary".format(name))
            elif not self.outer_path.contains_point(point):
                res = False
                print("Point {} outside outer track boundary".format(name))

        for name,edge in edges.items():
            p = Path(edge, [Path.MOVETO, Path.LINETO])
            for k,v in (('inner',self.inner_path),('outer',self.outer_path)):
                if v.intersects_path(p, filled=False):
                    res = False
                    print("Edge {} intersects {} track boundary".format(name,k))
        return res

    def plot(self):
        fig, ax = plt.subplots()
        ax.add_patch(PathPatch(self.outer_path, facecolor='k', alpha=0.8))
        ax.add_patch(PathPatch(self.inner_path, facecolor='w', alpha=0.9))
        ax.grid(which='both')
        ax.axis('equal')
        ax.set_ylabel("Y (cm)")
        ax.set_xlabel("X (cm)")
        plt.show()
예제 #10
0
def is_in_sector(sector_coords, point_coords):
    
    codes = [Path.MOVETO]
    for i in range(len(sector_coords)-2):
        codes.append(Path.LINETO)
    codes.append(Path.CLOSEPOLY)

    bbPath = Path(sector_coords, codes)
    
    return bool(bbPath.contains_point(point_coords))
예제 #11
0
파일: radar.py 프로젝트: martalmeida/tabs
def inpolygon(x,y,xp,yp):
  from matplotlib.path import Path
  verts=np.array(zip(xp,yp))
  p=Path(verts)
  if not isiterable(x,y): x,y,itr=[x],[y],0
  else: itr=1
  res=[p.contains_point((x[i],y[i]), radius=0.) for i in range(len(x))]
  res=np.asarray(res,'bool')
  if not itr: res=res[0]
  return res
예제 #12
0
파일: main.py 프로젝트: bobbyluig/Eclipse
    def is_supported(self, vertices):
        """
        Checks if a given support triangle contains the center of mass.
        This assumes the robot is not on a slant or hill.
        :param vertices: The transformed vertices as a 3 x 2 numpy matrix.
        :return: True if center of mass is in triangle, else False.
        """

        triangle = Path(vertices)
        return triangle.contains_point(self.com[:2])
예제 #13
0
def in_rhealpix_image(x, y, north_square=0, south_square=0):
    r"""
    Return True if and only if the point `(x, y)` lies in the image of 
    the rHEALPix projection of the unit sphere.
            
    EXAMPLES::
    
        >>> eps = 0     # Test boundary points.
        >>> north_square, south_square = 0, 0
        >>> rhp = [
        ... (-pi - eps, pi/4 + eps),
        ... (-pi + north_square*pi/2 - eps, pi/4 + eps),
        ... (-pi + north_square*pi/2 - eps, 3*pi/4 + eps),
        ... (-pi + (north_square + 1)*pi/2 + eps, 3*pi/4 + eps),
        ... (-pi + (north_square + 1)*pi/2 + eps, pi/4 + eps),
        ... (pi + eps, pi/4 + eps),
        ... (pi + eps,-pi/4 - eps),
        ... (-pi + (south_square + 1)*pi/2 + eps,-pi/4 - eps),
        ... (-pi + (south_square + 1)*pi/2 + eps,-3*pi/4 - eps),
        ... (-pi + south_square*pi/2 - eps,-3*pi/4 - eps),
        ... (-pi + south_square*pi/2 -eps,-pi/4 - eps),
        ... (-pi - eps,-pi/4 - eps)
        ... ]
        >>> for p in rhp:
        ...     if not in_rhealpix_image(*p):
        ...             print('Fail')
        ... 
        >>> print(in_rhealpix_image(0, 0))
        True
        >>> print(in_rhealpix_image(0, pi/4 + 0.1))
        False
        
    """    
    # matplotlib is a third-party module.
    from matplotlib.path import Path
    
    # Fuzz to slightly expand rHEALPix image so that 
    # points on the boundary count as lying in the image.
    eps = 1e-15
    vertices = [
      (-pi - eps, pi/4 + eps),
      (-pi + north_square*pi/2 - eps, pi/4 + eps),
      (-pi + north_square*pi/2 - eps, 3*pi/4 + eps),
      (-pi + (north_square + 1)*pi/2 + eps, 3*pi/4 + eps),
      (-pi + (north_square + 1)*pi/2 + eps, pi/4 + eps),
      (pi + eps, pi/4 + eps),
      (pi + eps, -pi/4 - eps),
      (-pi + (south_square + 1)*pi/2 + eps, -pi/4 - eps),
      (-pi + (south_square + 1)*pi/2 + eps, -3*pi/4 - eps),
      (-pi + south_square*pi/2 - eps, -3*pi/4 - eps),
      (-pi + south_square*pi/2 -eps, -pi/4 - eps),
      (-pi - eps, -pi/4 - eps)
    ]
    poly = Path(vertices)
    return bool(poly.contains_point([x, y]))    
def convert(x_prime,y_prime,rad,x_start,y_start):
    for j in range(0,hex_num):
        for i in range (0,hex_num):
            vertex_set = []
            x_center = x_start+(rad*2*i)
            y_center = y_start+(rad*math.sqrt(3)*j)
            for k in range(0,6):
                vertex_set.append([x_center+rad*math.cos((2*math.pi*k)/6),y_center+rad*math.sin((2*math.pi*k)/6)])
            vertex_path = Path(vertex_set)
            result = vertex_path.contains_point([x_prime,y_prime],radius=0.1)
            if (result==1):
                return x_prime, y_prime, (j, i)
def polyCluster(jumps, bisectingSlopes):
	cluster = -1*np.ones(len(jumps))
	codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY]
	vertices = polygonVertices(bisectingSlopes)
	nClusters = len(bisectingSlopes)+1
	for iv, v in enumerate(vertices):
		poly = Path(v, codes, closed = True)
		for ij, j in enumerate(jumps):
			if poly.contains_point(j)==1:
				cluster[ij] = iv
	
	return cluster
def extract_galaxy(image, flux_sigma, gcontour=None, zero_outside=True):
    if gcontour is None:
        # find the contour defining the boundary of the galaxy
        cpoint = np.array((image.shape[1], image.shape[0])) / 2
        floor = np.median(image)
        threshold = np.max([2.0 * flux_sigma, floor + 0.075 * (image[cpoint[0], cpoint[1]] - floor)])
        contour = find_contours(image, threshold)
        gcount = 0
        for c in contour:
            # find the contour that contains the central pixel: points interior to this belong to the galaxy
            this_path = Path(c, closed=True)
            if this_path.contains_point(cpoint):
                gcontour = this_path
                gcount += 1

    if debug:
        plt.clf()
        plt.imshow(image, cmap='hot')
        plt.plot(gcontour.vertices[:, 1], gcontour.vertices[:, 0], 'b')
        plt.show()

    if gcontour is None:
        # could not find the galaxy
        return None, gcontour, None
    else:
        # return the flux along the border of the galaxy, since we will use this to add noise later
        border = image[gcontour.vertices[:, 0].astype(int), gcontour.vertices[:, 1].astype(int)]
        # crop the image
        # note image is indexed (row, column) = (y, x), so gcontour.vertices[:, 0] = set of row values for the contour
        rmin, rmax = np.floor(gcontour.vertices[:, 0].min()), np.ceil(gcontour.vertices[:, 0].max())
        cmin, cmax = np.floor(gcontour.vertices[:, 1].min()), np.ceil(gcontour.vertices[:, 1].max())
        cropped = image[int(rmin):int(rmax), int(cmin):int(cmax)]
        cropped_contour = gcontour.deepcopy()
        cropped_contour.vertices[:, 0] -= rmin
        cropped_contour.vertices[:, 1] -= cmin

        if debug:
            plt.clf()
            plt.imshow(cropped, cmap='hot')
            plt.plot(cropped_contour.vertices[:, 1], cropped_contour.vertices[:, 0], 'b')
            plt.show()

        if zero_outside:
            # now find the pixels outside of the contour and set them to zero
            y, x = np.mgrid[:cropped.shape[0], :cropped.shape[1]]
            pixels = np.column_stack((y.ravel(), x.ravel()))
            galaxy_pixels = cropped_contour.contains_points(pixels)
            outside = np.where(~galaxy_pixels)[0]
            outside = np.unravel_index(outside, cropped.shape)
            cropped[outside] = 0.0

        return cropped, gcontour, border
예제 #17
0
def mapper():
    agg = {}
    polygon = [[-73.98830652236938,40.74913859730561] ,[-73.98512542247772,40.75359253503799],[-73.98491084575653,40.753490943068066],[-73.98817241191864,40.74907763805909],[-73.98830652236938,40.74913859730561]]# 6th av from 34th to 40th st
    path = Path(polygon)
    i = 0
    for values in parseInput():
        i += 1
        try:
            pickup_location = [float(values[10]), float(values[11])]
            if path.contains_point(pickup_location):
                print ','.join(values)
        except Exception,e:
            pass#print e,'line at',i,'11',values[11],'12',values[12]
    def area_name(self,point):
        # point = (latitude, longitude)
        area_found_result = False
        area_name = ""
        for i in areas_dict:
            path = Path(areas_dict[i])

            if path.contains_point(point) == 1:
                area_name = i
                area_found_result = True
                break
        if area_found_result == False:
            area_name = "NO area Found"
        return area_name
예제 #19
0
def match_fields(fields, centers):
    matches = {}
    for i in xrange(centers.shape[0]):
        for field in fields:
            # poly = Polygon(field['poly'])
            poly = Path(field['poly'])
            if poly.contains_point((centers[i, 0], centers[i, 1])):
                match = {'brick': field['brick'],
                         'field': field['field'],
                         'ra': centers[i, 0],
                         'dec': centers[i, 1],
                         'poly': field['poly']}
                matches[i + 1] = match
    return matches
예제 #20
0
def square_convert(x_prime, y_prime,x_start,y_start,sq_size,lim):
    for j in range(0,lim):
        for i in range(0,lim):
            vertex_set = []
            x_square = x_start + sq_size*i
            y_square = y_start + sq_size*j
            vertex_set.append([x_square,y_square])
            vertex_set.append([x_square,y_square+sq_size])
            vertex_set.append([x_square+sq_size,y_square+sq_size])
            vertex_set.append([x_square+sq_size,y_square])
            vertex_path = Path(vertex_set)
            result = vertex_path.contains_point([x_prime,y_prime],radius=0.1)
            if (result==1):
                return (i, j)
예제 #21
0
 def onselect(self, verts):
     path = Path(verts)
     
     old_ind = self.ind
     
     self.ind = np.nonzero([path.contains_point(xy) for xy in self.xys])[0]
     
     if self.shift_is_held:
         self.ind = np.unique(np.append(self.ind, old_ind))
     
     self.fc[:, :] = self.unselected_color
     self.fc[self.ind, :] = self.selected_color
     self.collection.set_facecolors(self.fc)
     self.canvas.draw_idle()
예제 #22
0
def inside_polygon(vertices, point):
  """Checks if a point is inside a polygon
  
  Arguments:
    vertices (nx2 array): vertices of the polygon
    point (2 array or dx2 array): coordinates of the point
    
  Returns:
    bool: True if point is inside the polygon
  """
  
  p = Path(vertices);
  if point.ndim == 1:
    return p.contains_point(point);
  else:
    return p.contains_points(point);
예제 #23
0
파일: build.py 프로젝트: liubenyuan/pyEIT
def refinement_func_anomaly(tri_points, area):
    """
    refine triangles within the anomaly regions.
    you have to specify the points which consist the polygon,
    you need to set the enclosing facet before refining,
    i.e., refinement_func.polygon = polygon

    this function is low-performance
    """
    polygon = Path(refinement_func_anomaly.polygon)
    center_tri = np.sum(np.array(tri_points), axis=0)/3.
    if area > 0.005:
        return True
    elif (area > 0.002) and polygon.contains_point(center_tri):
        return True
    else:
        return False
예제 #24
0
def contains_point(polygon, point):
    rot = rot_to_00(point[0], point[1])
    points_xyz = latlon_to_xyz(polygon)
    points_rot = num.dot(rot, points_xyz.T).T
    groups = spoly_cut([points_rot], axis=0)
    for group in groups:
        for points_g in group:
            try:
                points2 = stereographic_poly(points_g)
                p = Path(points2, closed=True)
                if p.contains_point((0., 0.)):
                    return True

            except Farside:
                pass

    return False
예제 #25
0
def integrate_contour_area(data, lvl, maskflag=False):
    """ Turns a set of contours into a series of vertices. You can then get the integrated area inside. """

    # Create a contour at the given level
    contours = PLT.contour(data,N.array([lvl]))
    PLT.close('all')

    # Get the needed path
    try:
        vertices = contours.collections[0].get_paths()[0].vertices
    except IndexError:
        return 0.0

    # Find the x and y extent
    xmin = N.amin(vertices[:,0])
    xmax = N.amax(vertices[:,0])
    ymin = N.amin(vertices[:,1])
    ymax = N.amax(vertices[:,1])

    # Because a path connects end point to start point, and we want the entire area inside, add origin
    # This is not correct for all setups. Fix later
    if vertices[0,1] == 0.0 and vertices[-1,0] == 0.0:
        vertices=N.vstack((N.array([0,0]),vertices))
        vertices=N.vstack((vertices,N.array([0,0])))
    path     = Path(vertices, closed=True)

    # Make an array of points to compare to the path
    ny,nx        = data.shape
    ygrid, xgrid = SP.mgrid[:ny, :nx]
    xypix        = SP.vstack((xgrid.ravel(), ygrid.ravel())).T

    # Make the mask
    mask = N.array([])
    for ipix in xypix:
        mask = N.append(mask,path.contains_point(ipix))
    mask = mask.reshape(data.shape)

    # Get the masked data
    masked_data     = mask*data
    masked_data_sum = N.sum(masked_data)

    if maskflag:
        return mask,xmin,xmax,ymin,ymax
    else:
        return masked_data_sum
예제 #26
0
class VoronoiCell():
    def __init__(self):
        self.id=-1
        self.neighbors = []
        self.path = None
        self.vertices = np.array((np.inf,np.inf))
        self.center = np.array((np.inf,np.inf))
        self.hull_point=False
        self.color = (0.5,0.7,0.7)#np.random.rand(3)
        self.elevation = 0
        self.name = None
        self.value = 0
        self.owner = -1
        self.owner_color = (0,0,0)

    #find the VoronoiCell containg (x,y)
    def contains(self,pos):
        self.path = Path(self.vertices)
        return self.path.contains_point(pos)
    
    def draw(self):
        if self.owner != -1:
            color = self.owner_color
        else:
            color = self.color
        patch = mpatches.Polygon(self.vertices,ec='none',facecolor = color)
        patch.set_picker(True)
        return patch
    
    def increase_elevation(self, increment):    
        if self.name == 'water':
            self.name = 'land'
            self.elevation+=increment
        if self.name == 'lake':
            self.name = 'land'
            self.elevation+=increment
        elif self.name == 'land':
            self.name = 'mountain'
            self.elevation+=increment
        self.color = biomes[self.name]
        
    def __lt__(self, other):
         return self.center[1] < other.center[1]
                 
            
예제 #27
0
	def InsidePolygon(self,RuleList,numpoly, xS, yC):
		"Determining if a point lies in or out a polygon (texture triangle)"
		NODATA=-9999
		for p in range(numpoly):
			polygon=[]
			isInside=False
			num_vert= int(RuleList[p]['Num_vert'][0])
			texture=RuleList[p]['Texture'][0] #numeric code define texture
			sand=RuleList[p]['Sand_vert']# coord. list of texturale polugons
			clay=RuleList[p]['Clay_vert']#
			if (xS <0 or yC <0 ):
				return NODATA
			else:
				polygon=np.asarray([[sand[i],clay[i]] for i in range(len(clay))])
				path = Path(polygon)
				isInside = path.contains_point([xS, yC])
				if isInside==True:
					return texture
예제 #28
0
def find_radio_stations(con, route, var_dict):
  """ This is the meatiest and most heavy lifting-est method in this project.
  This will loop over each point in the route (node) and determine the radio
  stations (if any) that it can receive.
  """
  cur = con.cursor()
  antennas_for_each_node = []
  i = -1
  for node in route:
    #print "Considering node:", i, 'of', len(route)
    i+= 1
    result = query_db(
        cur, node[0], node[1], var_dict['genre'], var_dict['subgenre'])
    # No radio towers exist near this node (based on the rectangular contour
    # approximation:
    if not result: 
      antennas_for_each_node.append(None)
      #print 'XX', i, result
      continue
    #print '>>', i, str(result[:3]).replace('\n', '')

    # There is at least one station whos rectangular coverage includes the node.
    found_in_contour=False
    antlons, antlats, scss, cats, separations, geodesics, contour_lons, contour_lats, frequencies = result
    antenna_dict = {}
    for antenna_num in xrange(len(contour_lons)):
      #print 'antenna_num', antenna_num
      lons = contour_lons[antenna_num]
      lats = contour_lats[antenna_num]
      path = Path(zip(lons, lats))
      #if i > 53 and i < 62:
        #print '\t', antenna_num, 'of', len(contour_lons), 'antennas.',
        #print '\tContour LonLat:', lons[0], lats[0], 'Node LonLat:', node[0], node[1]
      if path.contains_point(node) and scss[antenna_num] != 'NA':
        #print '\tQQQ'
        antennas_for_each_node.append(zip(*result)[antenna_num])
        found_in_contour = True
        #print '\tBreaking!'
        break
    if not found_in_contour:
      #print '\tDid not find found_in_contour'
      antennas_for_each_node.append(None)
    #print str(antennas_for_each_node[-1])[0]
  return antennas_for_each_node
예제 #29
0
파일: room.py 프로젝트: 5nafu/c3nav
class Room(Location):
    ltype = 'room'
    priority = 2

    def __init__(self, graph, name, level, titles, shape):
        super().__init__(name, titles)
        self.graph = graph
        self.level = level
        self.shape = shape
        self.nodes = []
        self.pois = []
        self.barriers = []
        self.groups = []

        mpl_xy = self.shape+self.shape[-1:]
        mpl_codes = [Path.MOVETO] + [Path.LINETO]*len(self.shape)
        self.mpl_path = Path(np.array(mpl_xy), codes=mpl_codes)

    @property
    def priority(self):
        return 1 if self.groups else 2

    def contains_position(self, position):
        if position.level != self.level:
            return False

        return self.mpl_path.contains_point((position.x, position.y))

    def get_barriers(self):
        return (b for b in self.graph.barriers
                if b.level == self.level and self.mpl_path.intersects_path(b.mpl_path, True))

    def barrier_paths(self):
        return [self.mpl_path] + [b.mpl_path for b in self.barriers]

    @property
    def subtitle(self):
        if not self.groups:
            return _('Level %(level)d', level=self.level)
        else:
            return _('%(roomgroup)s, Level %(level)d', roomgroup=self.groups[0].collection_title, level=self.level)

    def __repr__(self):
        return 'Room(%s)' % repr(self.name)
예제 #30
0
    def _finalize_selection(self):
        verts = np.array(self.points, float)
 
        path = Path(verts) 
        
        for batch in self.PCAData.batchs:
            if (self.plot.active_scores_combobox == "Post Scores"):
                y1 = batch.postscores[int(self.plot.Y_PCA)]
                x1 = batch.postscores[int(self.plot.X_PCA)]                        
            else:
                x1 = batch.prescores[int(self.plot.X_PCA)]
                y1 = batch.prescores[int(self.plot.Y_PCA)]
                
            #if nx.pnpoly(x1, y1, verts)==1:
            
            if path.contains_point([x1,y1],None)==1:
                batch.isSelected = True    
        self.plot._create_2D_plot()
        self.plot._create_1D1_plot()
예제 #31
0
   def on_click(self,event):
       if not event.inaxes: 
           self.xy=[]
           return
       self.x,self.y=int(event.xdata), int(event.ydata)
       self.key=event.key
       self.xx.append([self.x])
       self.yy.append([self.y])
       self.xy.append([self.y,self.x])
       self.lc.set_data(self.xx,self.yy)
       if self.key=='m': 
           print 'masking'
           self.xx[-1]=self.xx[0]
           self.yy[-1]=self.yy[0]
           self.xy[-1]=self.xy[0]
           previously_masked = self.mymask.sum()
           #ind=p.nonzero(points_inside_poly(self.points,self.xy))
           verts = []
           codes = []
           #print self.xy # these are the masked vertices
           for xy in self.xy:
              verts.append(xy)
              codes.append(Path.LINETO)
           codes[0] = Path.MOVETO
           codes[-1] = Path.CLOSEPOLY # need extra empty element?
           masked_path = Path(verts, codes)
           self.mymask=self.mymask.reshape(self.lx*self.ly,1)
           inds = []
           #icnt = 0
           #print self.points # these are the whole detector array
           for point in self.points:
              #ind = p.nonzero(masked_path.contains_point(point))
              inds.append(masked_path.contains_point(point))
              #icnt += 1
           self.mymask[p.nonzero(inds)]=1
           self.mymask=self.mymask.reshape(self.lx,self.ly)
           #print icnt
           print "masked out %d pixels (%d already masked)" % (self.mymask.sum()-previously_masked, n.sum(inds)+previously_masked-self.mymask.sum())
           datamasked=masked_array(self.data,self.mymask+self.automask+self.anisotropic_mask)
           self.im.set_data(datamasked)
           self.xx=[]
           self.yy=[]
           self.xy=[] 
           self.lc.set_data(self.xx,self.yy)
           self.lm.set_data(self.xx,self.yy)
#           self.im.set_clim(vmax=(2*self.data.mean()))
           self.im.autoscale()
           p.draw()
           self.x=0
           self.y=0 
       if self.key=='u':
           print 'unmasking'
           self.xx[-1]=self.xx[0]
           self.yy[-1]=self.yy[0]
           self.xy[-1]=self.xy[0]
           previously_masked = self.mymask.sum()
           #ind=p.nonzero(points_inside_poly(self.points,self.xy))
           verts = []
           codes = []
           #print self.xy # these are the masked vertices
           for xy in self.xy:
              verts.append(xy)
              codes.append(Path.LINETO)
           codes[0] = Path.MOVETO
           codes[-1] = Path.CLOSEPOLY # need extra empty element?
           masked_path = Path(verts, codes)
           self.mymask=self.mymask.reshape(self.lx*self.ly,1)
           inds = []
           #print self.points # these are the whole detector array
           for point in self.points:
              inds.append(masked_path.contains_point(point))
           self.mymask[p.nonzero(inds)]=0
           self.mymask=self.mymask.reshape(self.lx,self.ly)
           print "ummasked %d pixels (%d already unmasked)" % (previously_masked-self.mymask.sum(), n.sum(inds)-previously_masked+self.mymask.sum())
           datanew=masked_array(self.data,self.mymask+self.automask+self.anisotropic_mask)
           self.im.set_data(datanew)
           self.xx=[]
           self.yy=[]
           self.xy=[]
           self.lc.set_data(self.xx,self.yy)
           self.lm.set_data(self.xx,self.yy)
#           self.im.set_clim(vmax=(2*self.data.mean()))
           self.im.autoscale()
           p.draw()
           self.x=0
           self.y=0

       if self.key=='r':
           print 'unmasking all'
           self.mymask=0*self.mymask
           datanew=masked_array(self.data,self.mymask+self.automask+self.anisotropic_mask)
           self.im.set_data(datanew)
           self.xx=[]
           self.yy=[]
           self.xy=[] 
           self.lc.set_data(self.xx,self.yy)
           self.lm.set_data(self.xx,self.yy)

#           self.im.set_clim(vmax=(2*self.data.mean()))
           self.im.autoscale()
           p.draw()
           self.x=0
           self.y=0 
       if self.key=='k':
          print 'save and exit'
          self.save_mask()
          print 'Mask saved in file:', self.mask_file
#          mask_f=EdfFile.EdfFile(self.mask_file)
#          mask_f.WriteImage({},self.mymask,0)
#          mask_f.WriteImage({},self.automask,1)
#          mask_f.WriteImage({},self.anisotropic_mask,2)
#          del(mask_f)
          p.close()
          return self.mymask+self.automask
       if self.key=='q':
          print 'exit without saving'
          p.close()
          return self.old_mymask+self.old_automask
예제 #32
0
class Scenario:
    def __init__(self, parameters={}):

        self.parameters = parameters

        self.timestep = constants.timestep
        self.parameters['timestep'] = self.timestep
        self.simulation_duration = constants.total_monitoring_duration_uni_direction

        self.parameters['constant_target'] = 1
        self.parameters['constant_target_magnitude'] = 2.6

        self.frame_save_frequency = int(
            constants.frame_store_sample_frequency / self.timestep)
        self.spawn_frequency = int(constants.spawn_pedestrian_frequency /
                                   self.timestep)
        self.adaptive_context_dir = constants.adaptive_dir
        self.monitor_point = self.parameters['monitor_point']

        self.turning_area = Path([(20.0, 41.0), (50.0, 41.0), (50.0, 9.0),
                                  (20.0, 9.0)])
        self.target_final = (35.0, -1000.0)
        self.turning_up = 9.0
        self.target_original = self.parameters['targets'][0]

    def run_aggregate(self,
                      in_group_a_strength,
                      in_group_a_range,
                      in_group_r_strength,
                      in_group_r_range,
                      out_group_a_strength,
                      out_group_a_range,
                      out_group_r_strength,
                      out_group_r_range,
                      target_a_strength,
                      target_a_range,
                      context,
                      spawn_new_pedestrians=False,
                      simulation=True,
                      drawing=True):
        """ initialize social force model """
        force_model.set_parameters(self.parameters)
        self.spawn_new_pedestrians = spawn_new_pedestrians

        self.drawing = drawing
        total_group_num = len(self.parameters['group_num'])
        ''' set parameter '''
        self.parameters['in_group_a_strength'] = in_group_a_strength
        self.parameters['in_group_a_range'] = in_group_a_range
        self.parameters['in_group_r_strength'] = in_group_r_strength
        self.parameters['in_group_r_range'] = in_group_r_range
        self.parameters['out_group_a_strength'] = out_group_a_strength
        self.parameters['out_group_a_range'] = out_group_a_range
        self.parameters['out_group_r_strength'] = out_group_r_strength
        self.parameters['out_group_r_range'] = out_group_r_range
        self.parameters['target_a_strength'] = target_a_strength
        self.parameters['target_a_range'] = target_a_range

        self.flowrate_simulations = []
        self.flowrate = 0

        #we only measure pedestrians inside this area
        self.turning_angles = []
        self.effective_evacuation = []

        self.simulation_index = "%s" % str(datetime.now().microsecond)

        population_generator = PopulationGenerator(
            self.parameters, in_group_a_strength, in_group_a_range,
            in_group_r_strength, in_group_r_range, out_group_a_strength,
            out_group_a_range, out_group_r_strength, out_group_r_range,
            target_a_strength, target_a_range)
        """ perform simulation over context_placement_num"""
        radii_generators = context._get_radii_generators()
        placement_generators = context._get_placement_generators()
        t = len(placement_generators)
        current_simulation_run = 0
        while current_simulation_run < 1:  #len(placement_generators)/total_group_num :

            simulation_id = "%s" % (self.simulation_index + "_" +
                                    str(current_simulation_run + 1))
            print(">> running simulation %s" % (simulation_id))

            self._init_observation_plots()

            index = current_simulation_run * total_group_num
            radii_generator = radii_generators[index:index + total_group_num]
            placement_generator = placement_generators[index:index +
                                                       total_group_num]
            generated_group_member_index = population_generator._generate_population(
                radii_generator, placement_generator)
            group_pedestrians1 = population_generator._get_generated_group_pedestrians_population(
            )

            bio_index = int(len(radii_generators) / 2) + index
            radii_generator = radii_generators[bio_index:bio_index +
                                               total_group_num]
            placement_generator = placement_generators[bio_index:bio_index +
                                                       total_group_num]
            population_generator._generate_population(
                radii_generator, placement_generator,
                generated_group_member_index)
            group_pedestrians2 = population_generator._get_generated_group_pedestrians_population(
            )

            group_pedestrians = group_pedestrians1 + group_pedestrians2

            self._run(index, simulation_id, group_pedestrians)

            self.plots._dump_influential_matrix(current_simulation_run)

            self.flowrate_simulations.append(self.flowrate)

            #turning_angels
            angels = self.plots.get_turning_angles()
            #print(angels)
            for item in angels:
                self.turning_angles.append(item)

            #get effective evacuation
            effectiveness = self.plots.get_effective_evacuation()
            #print(effectiveness)
            for item in effectiveness:
                self.effective_evacuation.append(item)

            self.plots.reset_sample()

            force_model.reset_model()
            self.flowrate = 0

            current_simulation_run += 1

    def _init_observation_plots(self):
        self.sample_frequency = int(constants.plot_sample_frequency /
                                    self.timestep)
        self.plots = observer_plot(self.parameters)
        self.observation_plot_prefix = os.path.join(constants.observation_dir,
                                                    self.parameters['name'])

    def _init_drawing(self, simulation_id):
        self.show_canvas = image_canvas(self.parameters['drawing_width'],
                                        self.parameters['drawing_height'],
                                        self.parameters['pixel_factor'])

    def _tick(self):
        return self._canvas("tick", constants.framerate_limit)

    def _canvas(self, method, *args):
        return getattr(self.show_canvas, method)(*args)

    def _draw(self):
        self._canvas("clear_screen")

        group_population_number = int(force_model.get_population_size())
        for i in range(group_population_number):
            (x, y) = force_model.group_pedestrian_a_property(i, "position")
            if math.isnan(x) == False and math.isnan(y) == False:
                r = force_model.group_pedestrian_a_property(i, "radius")
                group_id = force_model.group_pedestrian_a_property(
                    i, "groupid")
                self._canvas("draw_pedestrian", x, y, r, group_id)
            else:
                print("Position is unidentified")
                sys.exit()

        for s in self.parameters['start_areas']:
            self._canvas("draw_start_area", s)

        turning_draw = (20.0, 9.0, 50.0, 41.0)
        self._canvas("draw_start_area", turning_draw)

        for w in self.parameters['walls']:
            self._canvas("draw_wall", w)

        monitor = (-1000, self.monitor_point, 1000.0, self.monitor_point)
        self._canvas("draw_wall", monitor)

        #self._canvas("draw_target", self.parameters['targets'][0][0], self.parameters['targets'][0][1])

        self._canvas("draw_text", "t = %.2f" % self.time)

        self.show_canvas.update()

    def _uninit_drawing(self):
        self._canvas("quit")

    def _done(self):
        if self.time > self.simulation_duration:

            pedestrian_escaped = 0
            # count pedestrians over the monitor point and printout the flow rate
            population_number = int(force_model.get_population_size())
            for i in range(population_number):
                (x, y) = force_model.group_pedestrian_a_property(i, "position")
                if y <= self.monitor_point:
                    pedestrian_escaped += 1.0

            self.flowrate = pedestrian_escaped / self.simulation_duration
            print("flowrate " +
                  str(pedestrian_escaped / self.simulation_duration))

            return True

        return False

    def _spawn_pedestrians(self, context_index):  #re-change by spawn by file

        #first we get current position so that they are not overlap
        population_number = int(force_model.get_population_size())
        current_pedestrian_position = []
        group_num = [
            int(constants.spawn_rate / 2),
            constants.spawn_rate - int(constants.spawn_rate / 2)
        ]

        for i in range(population_number):
            (x, y) = force_model.group_pedestrian_a_property(i, "position")
            current_pedestrian_position.append([x, y])

        if self.spawn_new_pedestrians == True:
            context1 = adaptive_context(self.parameters)
            context1._generateContext(current_pedestrian_position, group_num,
                                      self.parameters['start_areas'][0])

            context2 = adaptive_context(self.parameters)
            context2._generateContext(current_pedestrian_position, group_num,
                                      self.parameters['start_areas'][1])

        else:
            #we get from json file rather than add manually by code
            disp_level = self.parameters[
                'out_group_r_strength'] / self.parameters['in_group_r_strength']
            #frame_filename = "%s" % (str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames)))
            frame_filename = "%s" % (str(context_index) + "_" +
                                     str(int(self.frames)))

            adaptivecontext_file1 = open(
                "%s_1.json" %
                os.path.join(self.adaptive_context_dir, frame_filename))
            json_str = adaptivecontext_file1.read()
            context1 = json.loads(json_str, cls=AdaptiveContextLog_Decoder)
            adaptivecontext_file1.close()

            adaptivecontext_file2 = open(
                "%s_2.json" %
                os.path.join(self.adaptive_context_dir, frame_filename))
            json_str = adaptivecontext_file2.read()
            context2 = json.loads(json_str, cls=AdaptiveContextLog_Decoder)
            adaptivecontext_file2.close()

        population_generator = PopulationGenerator(
            self.parameters, self.parameters['in_group_a_strength'],
            self.parameters['in_group_a_range'],
            self.parameters['in_group_r_strength'],
            self.parameters['in_group_r_range'],
            self.parameters['out_group_a_strength'],
            self.parameters['out_group_a_range'],
            self.parameters['out_group_r_strength'],
            self.parameters['out_group_r_range'],
            self.parameters['target_a_strength'],
            self.parameters['target_a_range'], group_num)

        radii_generator1 = context1._get_radii_generators()
        placement_generator1 = context1._get_placement_generators()

        pedestrian_current_id = population_generator._generate_population(
            radii_generator1, placement_generator1, population_number)
        additional_pedestrians1 = population_generator._get_generated_group_pedestrians_population(
        )
        if additional_pedestrians1 is not None and len(
                additional_pedestrians1) > 0:
            for group_member in additional_pedestrians1:
                force_model.add_group_pedestrian(group_member)

        radii_generator2 = context2._get_radii_generators()
        placement_generator2 = context2._get_placement_generators()

        population_generator._generate_population(radii_generator2,
                                                  placement_generator2,
                                                  pedestrian_current_id)
        additional_pedestrians2 = population_generator._get_generated_group_pedestrians_population(
        )
        if additional_pedestrians2 is not None and len(
                additional_pedestrians2) > 0:
            for group_member in additional_pedestrians2:
                force_model.add_group_pedestrian(group_member)

        if self.spawn_new_pedestrians == True:
            #we then dump these context into folder temp_additional context
            disp_level = self.parameters[
                'out_group_r_strength'] / self.parameters['in_group_r_strength']
            frame_filename = "%s" % (str(disp_level) + "_" + str(context_index)
                                     + "_" + str(int(self.frames)))

            log_file = open(
                "%s_1.json" %
                os.path.join(self.adaptive_context_dir, frame_filename), "w")
            json_obj = json.dumps(context1, cls=AdaptiveContextLog_Encoder)
            log_file.write(json_obj)
            log_file.close()

            frame_filename = "%s" % (str(disp_level) + "_" + str(context_index)
                                     + "_" + str(int(self.frames)))

            log_file = open(
                "%s_2.json" %
                os.path.join(self.adaptive_context_dir, frame_filename), "w")
            json_obj = json.dumps(context2, cls=AdaptiveContextLog_Encoder)
            log_file.write(json_obj)
            log_file.close()

    def _plot_sample(self):

        population_number = int(force_model.get_population_size())

        effectiveness = []
        turning_angle = []

        #here we also measure the influential matrix of pedestrian in narrowing corridor
        influential_matrix = dict()
        crowd_info = [[] for i in range(2)]  #since we have 2 groups
        #initialize the position and id of each pedestrian

        for ped in range(population_number):
            (x, y) = force_model.group_pedestrian_a_property(ped, "position")
            ped_id = int(force_model.group_pedestrian_a_property(
                ped, "ped_id"))
            group_id = int(
                force_model.group_pedestrian_a_property(ped, "groupid"))
            crowd_info[group_id].append([ped_id, x, y])

        for i in range(population_number):
            (x, y) = force_model.group_pedestrian_a_property(i, "position")
            if math.isnan(x) == False and math.isnan(y) == False:

                test_in_area = self.turning_area.contains_point((x, y))
                if test_in_area:
                    (v_x, v_y) = force_model.group_pedestrian_a_property(
                        i, "velocity_direction")

                    #select vector from desired velocity direction
                    (desired_x,
                     desired_y) = force_model.group_pedestrian_a_property(
                         i, "desired_direction")

                    angle = self.angle_between((v_x, v_y),
                                               (desired_x, desired_y))
                    turning_angle.append(angle)

                    effect = (desired_x * v_x) + (desired_y * v_y)
                    effectiveness.append(effect)

                    ped_id = int(
                        force_model.group_pedestrian_a_property(i, "ped_id"))
                    group_id = int(
                        force_model.group_pedestrian_a_property(i, "groupid"))

                    closest_distance = 999
                    closest_ped = -1
                    #find the nearest pedestrian in the list
                    for member in crowd_info[group_id]:
                        if member[0] != ped_id:
                            temp_distance = math.sqrt((x - member[1])**2 +
                                                      (y - member[2])**2)
                            if temp_distance < closest_distance:
                                closest_distance = temp_distance
                                closest_ped = member[0]

                    influential_matrix[ped_id] = closest_ped

        total_effective = 0
        if len(effectiveness) > 0:
            total_effective = np.sum(effectiveness)
            total_effective /= len(effectiveness)

        self.plots._add_new_sample(turning_angle, total_effective,
                                   influential_matrix)

    def _revise_target(self):

        population_number = int(force_model.get_population_size())
        for i in range(population_number):
            (x, y) = force_model.group_pedestrian_a_property(i, "position")
            test_in_area = self.turning_area.contains_point((x, y))
            if test_in_area or y < self.turning_up:
                data = dict(ped_index=int(i), target=self.target_final)
                force_model.target_changed(data)

            else:
                if y > self.turning_up:
                    data = dict(ped_index=int(i), target=self.target_original)
                    force_model.target_changed(data)

    def _run(self, context_index, simulation_id, group_pedestrians):

        self.time = 0.0
        self.frames = 0

        if group_pedestrians is not None and len(group_pedestrians) > 0:
            for group_member in group_pedestrians:
                force_model.add_group_pedestrian(group_member)

        self._init_drawing(simulation_id)

        finished = False

        try:
            while self._tick() and not finished:
                force_model.update_pedestrians()

                if self.drawing:
                    self._draw()

                if (not self.frames % self.sample_frequency):
                    self._plot_sample()

                if (not self.frames % self.frame_save_frequency):
                    self._dump_frame(context_index, self.frames)

                self._revise_target()

                if (not self.frames % self.spawn_frequency):
                    self._spawn_pedestrians(context_index)

                self.time += self.timestep
                self.frames += 1

                if self._done():
                    finished = True

        except KeyboardInterrupt:
            pass

        if self.drawing:
            self._uninit_drawing()

    def _dump_frame(self, context_index, current_frame):
        population_number = int(force_model.get_population_size())

        pedestrians_frame = []

        for i in range(population_number):
            (x, y) = force_model.group_pedestrian_a_property(i, "position")
            r = force_model.group_pedestrian_a_property(i, "radius")
            group_id = force_model.group_pedestrian_a_property(i, "groupid")
            ped_id = int(force_model.group_pedestrian_a_property(i, "ped_id"))
            (target_x,
             target_y) = force_model.group_pedestrian_a_property(i, "target")

            pedestrians_frame.append(
                dict(
                    pedestrian_id=ped_id,  #
                    group_id=group_id,  #
                    radius=r,  #
                    position=(x, y),  #
                    in_group_a_strength=self.
                    parameters['in_group_a_strength'],  #
                    in_group_a_range=self.parameters['in_group_a_range'],  #
                    in_group_r_strength=self.
                    parameters['in_group_r_strength'],  #
                    in_group_r_range=self.parameters['in_group_r_range'],
                    out_group_a_strength=self.
                    parameters['out_group_a_strength'],  #
                    out_group_a_range=self.parameters['out_group_a_range'],
                    out_group_r_strength=self.
                    parameters['out_group_r_strength'],
                    out_group_r_range=self.parameters['out_group_r_range'],

                    #target point
                    target=(target_x, target_y),  #
                    target_a_strength=self.parameters['target_a_strength'],
                    target_a_range=self.parameters['target_a_range']))  #

        frame_generator = frame_context(current_frame, pedestrians_frame)
        disp_level = self.parameters['out_group_r_strength'] / self.parameters[
            'in_group_r_strength']
        frame_filename = "%s" % (str(disp_level) + "_" + str(context_index) +
                                 "_" + str(current_frame))

        log_file = open(
            "%s.json" %
            os.path.join(constants.framecontext_dir, frame_filename), "w")
        json_obj = json.dumps(frame_generator, cls=FrameContextLog_Encoder)
        log_file.write(json_obj)
        log_file.close()

    def _get_parameters(self):
        return self.parameters

    def _get_flowrate_simulations(self):
        return self.flowrate_simulations

    def _get_turning_angles(self):
        return self.turning_angles

    def _get_effective(self):
        return self.effective_evacuation

    """ Returns the unit vector of the vector.  """

    def unit_vector(self, vector):
        return vector / np.linalg.norm(vector)

    def angle_between(self, v1, v2):
        v1_u = self.unit_vector(v1)
        v2_u = self.unit_vector(v2)
        rad = np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0))
        return math.degrees(rad)
예제 #33
0
                im_rotated = rotate(diag.im_corrected,
                                    alpha,
                                    center=(o[0], o[1]))
                im_cropped,o,r=imtools.crop_shape(im_rotated,one_shape,\
                                                    diag.param.rgb_norm,diag.measures['nucleus_median_rgb'],\
                                                    scale=1,adjust=True)

                if im_cropped is not None and r[0] > diag.param.rbcR * 1.5:

                    wbc_type = 'un'
                    for each_bb in annotations_bb:
                        #            if each_bb[0] in list(wbc_types.keys()):
                        # only if in wbc list to be detected
                        bb = Path(np.array(each_bb[2]))
                        intersect = bb.contains_point(np.asarray(o))
                        if intersect > 0:
                            # automatic detection is within manual annotation
                            is_pos_detect = True
                            if each_bb[0] in list(wbc_types.keys()):
                                wbc_type = diag.param.wbc_type_dict[each_bb[0]]
                            if wbc_type not in list(wbc_basic_types.keys()):
                                wbc_type = 'un'  # unknown
                                break

                    # SAVE
                    crop_file = os.path.join(
                        output_dir, wbc_type + '_' + str(i_detected) + '_' +
                        str(alpha) + '.png')
                    io.imsave(crop_file, im_cropped)
예제 #34
0
class get_fvcom():
    def __init__(self, mod):
        self.modelname = mod

    def points_square(self, point, hside_length):
        '''point = (lat,lon); length: units is decimal degrees.
           return a squre points(lats,lons) on center point,without center point'''
        ps = []
        (lat, lon) = point
        length = float(hside_length)
        #lats=[lat]; lons=[lon]
        #lats=[]; lons=[]
        bbox = [lon - length, lon + length, lat - length, lat + length]
        bbox = np.array(bbox)
        self.points = np.array([bbox[[0, 1, 1, 0]], bbox[[2, 2, 3, 3]]])
        #print points
        pointt = self.points.T
        for i in pointt:
            ps.append((i[1], i[0]))
        ps.append((pointt[0][1],
                   pointt[0][0]))  # add first point one more time for Path.
        #lats.extend(points[1]); lons.extend(points[0])
        #bps = np.vstack((lon,lat)).T
        #return lats,lons
        return ps

    def nearest_point(self, lon, lat, lons, lats, length):  #0.3/5==0.06
        '''Find the nearest point to (lon,lat) from (lons,lats),
           return the nearest-point (lon,lat)
           author: Bingwei'''
        p = Path.circle((lon, lat), radius=length)
        #numpy.vstack(tup):Stack arrays in sequence vertically
        points = np.vstack((lons.flatten(), lats.flatten())).T

        insidep = []
        #collect the points included in Path.
        for i in xrange(len(points)):
            if p.contains_point(points[i]):  # .contains_point return 0 or 1
                insidep.append(points[i])
        # if insidep is null, there is no point in the path.
        if not insidep:
            print 'There is no model-point near the given-point.'
            raise Exception()
        #calculate the distance of every points in insidep to (lon,lat)
        distancelist = []
        for i in insidep:
            ss = math.sqrt((lon - i[0])**2 + (lat - i[1])**2)
            distancelist.append(ss)
        # find index of the min-distance
        mindex = np.argmin(distancelist)
        # location the point
        lonp = insidep[mindex][0]
        latp = insidep[mindex][1]

        return lonp, latp

    def get_url(self, starttime, endtime):
        '''
        get different url according to starttime and endtime.
        urls are monthly.
        '''
        self.hours = int(round(
            (endtime - starttime).total_seconds() / 60 / 60))
        #print self.hours

        if self.modelname == "GOM3":
            timeurl = '''http://www.smast.umassd.edu:8080/thredds/dodsC/FVCOM/NECOFS/Forecasts/NECOFS_GOM3_FORECAST.nc?Times[0:1:144]'''
            url = '''http://www.smast.umassd.edu:8080/thredds/dodsC/FVCOM/NECOFS/Forecasts/NECOFS_GOM3_FORECAST.nc?
            lon[0:1:51215],lat[0:1:51215],lonc[0:1:95721],latc[0:1:95721],siglay[0:1:39][0:1:51215],h[0:1:51215],nbe[0:1:2][0:1:95721],
            u[{0}:1:{1}][0:1:39][0:1:95721],v[{0}:1:{1}][0:1:39][0:1:95721],zeta[{0}:1:{1}][0:1:51215]'''
            '''urll = http://www.smast.umassd.edu:8080/thredds/dodsC/FVCOM/NECOFS/Forecasts/NECOFS_GOM3_FORECAST.nc?
            u[{0}:1:{1}][0:1:39][0:1:95721],v[{0}:1:{1}][0:1:39][0:1:95721],zeta[{0}:1:{1}][0:1:51215]'''
            try:
                mTime = netCDF4.Dataset(timeurl).variables['Times'][:]
            except:
                print '"GOM3" database is unavailable!'
                raise Exception()
            Times = []
            for i in mTime:
                strt = '201' + i[3] + '-' + i[5] + i[6] + '-' + i[8] + i[
                    9] + ' ' + i[11] + i[12] + ':' + i[14] + i[15]
                Times.append(datetime.strptime(strt, '%Y-%m-%d %H:%M'))
            fmodtime = Times[0]
            emodtime = Times[-1]
            if starttime < fmodtime or starttime > emodtime or endtime < fmodtime or endtime > emodtime:
                print 'Time: Error! Model(GOM3) only works between %s with %s(UTC).' % (
                    fmodtime, emodtime)
                raise Exception()
            npTimes = np.array(Times)
            tm1 = npTimes - starttime
            #tm2 = mtime-t2
            index1 = np.argmin(abs(tm1))
            index2 = index1 + self.hours  #'''
            url = url.format(index1, index2)
            self.mTime = Times[index1:index2 + 1]

            self.url = url

        elif self.modelname == "massbay":
            timeurl = '''http://www.smast.umassd.edu:8080/thredds/dodsC/FVCOM/NECOFS/Forecasts/NECOFS_FVCOM_OCEAN_MASSBAY_FORECAST.nc?Times[0:1:144]'''
            url = """http://www.smast.umassd.edu:8080/thredds/dodsC/FVCOM/NECOFS/Forecasts/NECOFS_FVCOM_OCEAN_MASSBAY_FORECAST.nc?
            lon[0:1:98431],lat[0:1:98431],lonc[0:1:165094],latc[0:1:165094],siglay[0:1:9][0:1:98431],h[0:1:98431],
            nbe[0:1:2][0:1:165094],u[{0}:1:{1}][0:1:9][0:1:165094],v[{0}:1:{1}][0:1:9][0:1:165094],zeta[{0}:1:{1}][0:1:98431]"""

            try:
                mTime = netCDF4.Dataset(timeurl).variables['Times'][:]
            except:
                print '"massbay" database is unavailable!'
                raise Exception()
            Times = []
            for i in mTime:
                strt = '201' + i[3] + '-' + i[5] + i[6] + '-' + i[8] + i[
                    9] + ' ' + i[11] + i[12] + ':' + i[14] + i[15]
                Times.append(datetime.strptime(strt, '%Y-%m-%d %H:%M'))
            fmodtime = Times[0]
            emodtime = Times[-1]
            if starttime < fmodtime or starttime > emodtime or endtime < fmodtime or endtime > emodtime:
                print 'Time: Error! Model(massbay) only works between %s with %s(UTC).' % (
                    fmodtime, emodtime)
                raise Exception()
            npTimes = np.array(Times)
            tm1 = npTimes - starttime
            #tm2 = mtime-t2
            index1 = np.argmin(abs(tm1))
            index2 = index1 + self.hours  #'''
            url = url.format(index1, index2)
            self.mTime = Times[index1:index2 + 1]

            self.url = url

        elif self.modelname == "30yr":  #start at 1977/12/31 23:00, end at 2014/1/1 0:0, time units:hours
            timeurl = """http://www.smast.umassd.edu:8080/thredds/dodsC/fvcom/hindcasts/30yr_gom3?time[0:1:316008]"""
            url = '''http://www.smast.umassd.edu:8080/thredds/dodsC/fvcom/hindcasts/30yr_gom3?h[0:1:48450],
            lat[0:1:48450],latc[0:1:90414],lon[0:1:48450],lonc[0:1:90414],nbe[0:1:2][0:1:90414],siglay[0:1:44][0:1:48450],
            u[{0}:1:{1}][0:1:44][0:1:90414],v[{0}:1:{1}][0:1:44][0:1:90414],zeta[{0}:1:{1}][0:1:48450]'''

            try:
                mtime = netCDF4.Dataset(timeurl).variables['time'][:]
            except:
                print '"30yr" database is unavailable!'
                raise Exception
            # get model's time horizon(UTC).
            '''fmodtime = datetime(1858,11,17) + timedelta(float(mtime[0]))
            emodtime = datetime(1858,11,17) + timedelta(float(mtime[-1]))
            mstt = fmodtime.strftime('%m/%d/%Y %H:%M')
            mett = emodtime.strftime('%m/%d/%Y %H:%M') #'''
            # get number of days from 11/17/1858
            t1 = (starttime - datetime(1858, 11, 17)).total_seconds() / 86400
            t2 = (endtime - datetime(1858, 11, 17)).total_seconds() / 86400
            if not mtime[0] < t1 < mtime[-1] or not mtime[0] < t2 < mtime[-1]:
                #print 'Time: Error! Model(massbay) only works between %s with %s(UTC).'%(mstt,mett)
                print 'Time: Error! Model(massbay) only works between 1978-1-1 with 2014-1-1(UTC).'
                raise Exception()

            tm1 = mtime - t1
            #tm2 = mtime-t2
            index1 = np.argmin(abs(tm1))
            #index2 = np.argmin(abs(tm2)); print index1,index2
            index2 = index1 + self.hours
            url = url.format(index1, index2)
            Times = []
            for i in range(self.hours + 1):
                Times.append(starttime + timedelta(hours=i))
            self.mTime = Times
            self.url = url
        #print url
        return url, self.mTime

    def get_data(self, url):
        '''
        "get_data" not only returns boundary points but defines global attributes to the object
        '''
        self.data = get_nc_data(url, 'lat', 'lon', 'latc', 'lonc', 'siglay',
                                'h', 'nbe', 'u', 'v', 'zeta')  #,'nv'
        self.lonc, self.latc = self.data['lonc'][:], self.data[
            'latc'][:]  #quantity:165095
        self.lons, self.lats = self.data['lon'][:], self.data['lat'][:]
        self.h = self.data['h'][:]
        self.siglay = self.data['siglay'][:]
        #nv = self.data['nv'][:]
        self.u = self.data['u']
        self.v = self.data['v']
        self.zeta = self.data['zeta']

        nbe1 = self.data['nbe'][0]
        nbe2 = self.data['nbe'][1]
        nbe3 = self.data['nbe'][2]
        pointt = np.vstack((nbe1, nbe2, nbe3)).T
        self.pointt = pointt
        wl = []
        for i in pointt:
            if 0 in i:
                wl.append(1)
            else:
                wl.append(0)
        self.wl = wl
        tf = np.array(wl)
        inde = np.where(tf == True)
        #b_index = inde[0]
        lonb = self.lonc[inde]
        latb = self.latc[inde]
        self.b_points = np.vstack((lonb, latb)).T  #'''
        #self.b_points = b_points
        return self.b_points  #,nv lons,lats,lonc,latc,,h,siglay

    def shrink_data(self, lon, lat, lons, lats, le):
        lont = []
        latt = []
        #p = Path.circle((lon,lat),radius=rad)
        self.psqus = self.points_square(
            (lon, lat),
            le)  # Got four point of rectangle with center point (lon,lat)
        codes = [
            Path.MOVETO,
            Path.LINETO,
            Path.LINETO,
            Path.LINETO,
            Path.CLOSEPOLY,
        ]
        #print psqus
        self.sp = Path(self.psqus, codes)
        pints = np.vstack((lons, lats)).T
        for i in range(len(pints)):
            if self.sp.contains_point(pints[i]):
                lont.append(pints[i][0])
                latt.append(pints[i][1])
        lonl = np.array(lont)
        latl = np.array(latt)  #'''
        if not lont:
            print 'Given point out of model area.'
            sys.exit()
        return lonl, latl

    def eline_path(self, lon, lat):
        '''
        When drifter close to boundary(less than 0.1),find one nearest point to drifter from boundary points, 
        then find two nearest boundary points to previous boundary point, create a boundary path using that 
        three boundary points.
        '''
        def boundary_location(locindex, pointt, wl):
            '''
            Return the index of boundary points nearest to 'locindex'.
            '''
            loca = []
            dx = pointt[locindex]
            #print 'func',dx
            for i in dx:  # i is a number.
                #print i
                if i == 0:
                    continue
                dx1 = pointt[i - 1]
                #print dx1
                if 0 in dx1:
                    loca.append(i - 1)
                else:
                    for j in dx1:
                        if j != locindex + 1:
                            if wl[j - 1] == 1:
                                loca.append(j - 1)
            return loca

        p = Path.circle((lon, lat), radius=0.02)  #0.06
        dis = []
        bps = []
        pa = []
        tlons = []
        tlats = []
        loca = []
        for i in self.b_points:
            if p.contains_point(i):
                bps.append((i[0], i[1]))
                d = math.sqrt((lon - i[0])**2 + (lat - i[1])**2)
                dis.append(d)
        bps = np.array(bps)
        if not dis:
            return None
        else:
            print "Close to boundary."
            dnp = np.array(dis)
            dmin = np.argmin(dnp)
            lonp = bps[dmin][0]
            latp = bps[dmin][1]
            index1 = np.where(self.lonc == lonp)
            index2 = np.where(self.latc == latp)
            elementindex = np.intersect1d(index1, index2)[0]  # location 753'''
            #print index1,index2,elementindex
            loc1 = boundary_location(elementindex, self.pointt, self.wl)
            #print 'loc1',loc1
            loca.extend(loc1)
            loca.insert(1, elementindex)
            for i in range(len(loc1)):
                loc2 = boundary_location(loc1[i], self.pointt, self.wl)
                #print 'loc2',loc2
                if len(loc2) == 1:
                    continue
                for j in loc2:
                    if j != elementindex:
                        if i == 0:
                            loca.insert(0, j)
                        else:
                            loca.append(j)

            for i in loca:
                tlons.append(self.lonc[i])
                tlats.append(self.latc[i])

            for i in xrange(len(tlons)):
                pa.append((tlons[i], tlats[i]))
            #path = Path(pa)#,codes
            return pa

    def current_track(self, jn, point, depth, track_way, leh, bcon):
        cts = []
        (lat, lon) = point
        self.lonl, self.latl = self.shrink_data(lon, lat, self.lonc, self.latc,
                                                leh)
        self.lonk, self.latk = self.shrink_data(lon, lat, self.lons, self.lats,
                                                leh)
        epoints = np.vstack((self.lonl, self.latl)).T
        numep = len(epoints)
        for i in range(numep):
            print '%d of %d, %d' % (i + 1, numep, jn + 1)
            getk, pnu = self.get_track(jn, epoints[i][0], epoints[i][1], depth,
                                       track_way, leh, bcon)
            #print type(getk['lon']),type(getk['lat']),type(getk['layer']),type(getk['spd'])
            ld = min(len(getk['lon']), len(getk['lat']), len(getk['layer']),
                     len(getk['spd']))
            for j in getk:
                if len(getk[j]) > ld:
                    getk[j] = getk[j][:ld]
            pgetk = pd.DataFrame(getk)
            #print pgetk
            cts.append(pgetk)
        return cts, self.points

    def get_track(self, jnu, lon, lat, depth, track_way, leh,
                  bcon):  #,b_index,nvdepth,
        '''
        Get forecast points start at lon,lat
        '''
        modpts = dict(lon=[lon], lat=[lat], layer=[], time=[],
                      spd=[])  #model forecast points
        #uvz = netCDF4.Dataset(self.url)
        #u = uvz.variables['u']; v = uvz.variables['v']; zeta = uvz.variables['zeta']
        #print 'len u',len(u)
        '''if lon>90:
            lon, lat = dm2dd(lon, lat)#'''

        try:
            if self.modelname == "GOM3" or self.modelname == "30yr":
                lonp, latp = self.nearest_point(lon, lat, self.lonl, self.latl,
                                                0.2)
                lonn, latn = self.nearest_point(lon, lat, self.lonk, self.latk,
                                                0.3)
            if self.modelname == "massbay":
                lonp, latp = self.nearest_point(lon, lat, self.lonl, self.latl,
                                                0.03)
                lonn, latn = self.nearest_point(lon, lat, self.lonk, self.latk,
                                                0.05)
            index1 = np.where(self.lonc == lonp)
            index2 = np.where(self.latc == latp)
            elementindex = np.intersect1d(index1, index2)
            index3 = np.where(self.lons == lonn)
            index4 = np.where(self.lats == latn)
            nodeindex = np.intersect1d(index3, index4)
            #print 'here index'
            ################## boundary 1 ####################
            pa = self.eline_path(lon, lat)
            #print 'here boundary_path'

            if track_way == 'backward':  # backwards case
                if self.modelname == "30yr":
                    waterdepth = self.h[nodeindex]
                else:
                    waterdepth = self.h[nodeindex] + self.zeta[-1, nodeindex]
                modpts['time'].append(self.mTime[-1])
            else:
                #print 'h,zeta',self.h[nodeindex],self.zeta[0,nodeindex]
                if self.modelname == "30yr":
                    waterdepth = self.h[nodeindex]
                else:
                    waterdepth = self.h[nodeindex] + self.zeta[0, nodeindex]
                modpts['time'].append(self.mTime[0])
            depth_total = self.siglay[:, nodeindex] * waterdepth
            #print 'Here one'
            layer = np.argmin(abs(depth_total + depth))
            #print 'layer',layer
            modpts['layer'].append(layer)
            if waterdepth < (abs(depth)):
                print 'This point is too shallow.Less than %d meter.' % abs(
                    depth)
                raise Exception()
        except:
            return modpts, 0

        t = abs(self.hours)
        #mapx = Basemap(projection='ortho',lat_0=lat,lon_0=lon,resolution='l')
        for i in xrange(t):
            '''if i!=0 and i%24==0 :
                #print 'layer,lon,lat,i',layer,lon,lat,i
                lonl,latl = self.shrink_data(lon,lat,self.lonc,self.latc,0.5)
                lonk,latk = self.shrink_data(lon,lat,self.lons,self.lats,0.5)#'''
            if i < jnu: continue
            if track_way == 'backward':  # backwards case
                u_t1 = self.u[t - i, layer, elementindex][0] * (-1)
                v_t1 = self.v[t - i, layer, elementindex][0] * (-1)
                #u_t2 = self.u[t-i-1,layer,elementindex][0]*(-1); v_t2 = self.v[t-i-1,layer,elementindex][0]*(-1)
            else:
                u_t1 = self.u[i, layer, elementindex][0]
                v_t1 = self.v[i, layer, elementindex][0]
                #u_t2 = self.u[i+1,layer,elementindex][0]; v_t2 = self.v[i+1,layer,elementindex][0]
            #u_t,v_t = self.uvt(u_t1,v_t1,u_t2,v_t2)
            #u_t = (u_t1+u_t2)/2; v_t = (v_t1+v_t2)/2
            '''if u_t==0 and v_t==0: #There is no water
                print 'Sorry, hits the land,u,v==0'
                return modpts,1 #'''
            #print "u[i,layer,elementindex]",u[i,layer,elementindex]
            dx = 60 * 60 * u_t1
            dy = 60 * 60 * v_t1
            pspeed = math.sqrt(u_t1**2 + v_t1**2)
            modpts['spd'].append(pspeed)
            if i == t - 1:  # stop when got the last point speed.
                return modpts, 2
            #x,y = mapx(lon,lat)
            #temlon,temlat = mapx(x+dx,y+dy,inverse=True)
            temlon = lon + (dx / (111111 * np.cos(lat * np.pi / 180)))
            temlat = lat + dy / 111111  #'''
            if not self.sp.contains_point((temlon, temlat)):
                #break;
                return modpts, 3
            #########case for boundary 1 #############
            if pa:
                pat = Path(pa)
                teml = [(lon, lat), (temlon, temlat)]
                #print temlon,temlat
                tempa = Path(teml)
                if pat.intersects_path(tempa):
                    if bcon == 'stop':
                        modpts['lon'].append(temlon)
                        modpts['lat'].append(temlat)
                        modpts['layer'].append(0)
                        print 'Sorry, point hits land here. Path. Last point:', temlon, temlat
                        return modpts, 1  #'''
                    if bcon == 'reflection':
                        f1 = (lon, lat)
                        f2 = (temlon, temlat)
                        v = (u_t1, v_t1)
                        #fl = Path([f1,f2])
                        for k in range(len(pa) - 1):
                            kl = Path([pa[k], pa[k + 1]])
                            if tempa.intersects_path(kl):
                                print 'Reflection^^^^>>>>>>>>>>>>'
                                (temlon, temlat) = self.uvreflection(
                                    f1, f2, pa[k], pa[k + 1], v)
                                break

            #########case for boundary 2 #############
            '''if pa :
                if not pa.contains_point([temlon,temlat]):
                    print 'Sorry, point hits land here.path'
                    return modpts,1 #'''
            #########################
            lon = temlon
            lat = temlat
            #if i!=(t-1):
            try:
                if self.modelname == "GOM3" or self.modelname == "30yr":
                    lonp, latp = self.nearest_point(lon, lat, self.lonl,
                                                    self.latl, 0.2)
                    lonn, latn = self.nearest_point(lon, lat, self.lonk,
                                                    self.latk, 0.3)
                if self.modelname == "massbay":
                    lonp, latp = self.nearest_point(lon, lat, self.lonl,
                                                    self.latl, 0.03)
                    lonn, latn = self.nearest_point(lon, lat, self.lonk,
                                                    self.latk, 0.05)
                index1 = np.where(self.lonc == lonp)
                index2 = np.where(self.latc == latp)
                elementindex = np.intersect1d(index1, index2)
                #print 'elementindex',elementindex
                index3 = np.where(self.lons == lonn)
                index4 = np.where(self.lats == latn)
                nodeindex = np.intersect1d(index3, index4)

                ################## boundary 1 ####################

                pa = self.eline_path(lon, lat)

                if track_way == 'backward':  # backwards case
                    if self.modelname == "30yr":
                        waterdepth = self.h[nodeindex]
                    else:
                        waterdepth = self.h[nodeindex] + self.zeta[t - i - 1,
                                                                   nodeindex]
                    modpts['time'].append(self.mTime[t - i - 1])
                else:
                    #print 'h,zeta',self.h[nodeindex],self.zeta[0,nodeindex]
                    if self.modelname == "30yr":
                        waterdepth = self.h[nodeindex]
                    else:
                        waterdepth = self.h[nodeindex] + self.zeta[i + 1,
                                                                   nodeindex]
                    modpts['time'].append(self.mTime[i + 1])

                depth_total = self.siglay[:, nodeindex] * waterdepth
                layer = np.argmin(abs(depth_total + depth))
                modpts['lon'].append(lon)
                modpts['lat'].append(lat)
                modpts['layer'].append(layer)
                print '%d,Lat,Lon,Layer,Speed' % (
                    i + 1), temlat, temlon, layer, pspeed
                if waterdepth < (abs(depth)):
                    print 'This point hits the land here.Less than %d meter.' % abs(
                        depth)
                    raise Exception()
            except:
                return modpts, 1
예제 #35
0
def get_raster_on_poly(rasterfile, poly, dtype = 'uint16', verbose = True):
    """Parses through an array of raster values with corresponding latitudes and
    longitudes and returns a list of the values on the shape boundary. """

    # get the extent and the raster values in the bounding box

    xmin = min([x for x, y in poly])
    xmax = max([x for x, y in poly])
    ymin = min([y for x, y in poly])
    ymax = max([y for x, y in poly])

    extent = xmin, ymin, xmax, ymax
    xs, ys, zs = get_raster_table(rasterfile, extent, dtype, locations = True)

    # create a matplotlib path for the polygon for point testing

    path = Path(poly)

    # set up a list for the points on the boundary

    points = []

    # find the bottom row

    n = len(xs[0]) - 1  # index of last column (used a lot)

    bottom = False
    row = 0
    while not bottom:
        row = row - 1
        x_row, y_row = xs[row], ys[row]
        bottom = any([path.contains_point([x,y]) for x, y in zip(x_row, y_row)])

    # start at the left and go right until a point is inside

    j = 0
    while j < n and not path.contains_point([x_row[j], y_row[j]]): j+=1

    # start at the right and go left until a point is inside

    k = n
    while k > 0 and not path.contains_point([x_row[k], y_row[k]]): k = k - 1

    for p in zip(xs[row, j:k+1], ys[row, j:k+1], zs[row, j:k+1]): 
        points.append(p)

    # keep track of the bottom row

    bottom, bleft, bright = row + len(xs), j, k
    
    # find the top row

    top = False
    row = -1
    while not top:
        row+=1
        x_row, y_row = xs[row], ys[row]
        top = any([path.contains_point([x, y]) for x, y in zip(x_row, y_row)])

    # start at the left and go right until a point is inside

    j = 0
    while j < n and not path.contains_point([x_row[j], y_row[j]]): j+=1

    # start at the right and go left until a point is inside

    k = n
    while k > 0 and not path.contains_point([x_row[k], y_row[k]]): k = k - 1

    for p in zip(xs[row, j:k+1], ys[row, j:k+1], zs[row, j:k+1]):
        points.append(p)

    # keep track of the left and right sides of the row above

    top, left, right = row + 1, j, k

    # parse through the rows and look for the first values inside; keep track
    # of the edges from the previous row (left and right)

    for x_row, y_row, z_row in zip(xs[top:bottom - 1], ys[top:bottom - 1], 
                                   zs[top:bottom - 1]):

        # start at the left and go right until a point is inside

        j = 0
        while j < n and not path.contains_point([x_row[j], y_row[j]]): j+=1

        # start at the right and go left until a point is inside

        k = n
        while k > 0 and not path.contains_point([x_row[k], y_row[k]]): k = k - 1

        # add the points from left to last left and right to the last right

        if   j == right: l = range(0)
        elif j <   left: l = range(j, left)
        else:            l = range(left, j + 1)

        if   k ==  left: r = range(0)
        elif k  > right: r = range(right + 1, k + 1)
        else:            r = range(k, right + 1)

        for i in chain(l, r): points.append((x_row[i], y_row[i], z_row[i]))

        if j != right: left  = j
        if k != left:  right = k

    # connect to the last row

    x_row, y_row, z_row = xs[bottom - 1], ys[bottom - 1], zs[bottom - 1]

    l, r = range(left, bleft + 1), range(bright, right + 1)

    for i in chain(l, r): points.append((x_row[i], y_row[i], z_row[i]))

    return points
예제 #36
0
def load_data_shared():

    # Navigate to  main directory in which there are 101 subdirectories containing images.
    mypath = '/Users/manaswipodduturi/Documents/Research/MachineLearning/Data/Caltech/101_ObjectCategories'
    # Process all the folders in the main directory
    folders = [
        folder for folder in listdir(mypath) if isdir(join(mypath, folder))
    ]
    all_images = np.array([])
    all_labels = np.array([], dtype=np.uint8)
    # Process all the images in each and every folder in the main directory.
    for j in xrange(0, len(folders)):
        files = [
            file for file in listdir(join(mypath, folders[j]))
            if (isfile(join(mypath, folders[j], file))
                & bool(file != '.DS_Store'))
        ]
        images = np.empty(len(files), dtype=object)
        labels = np.empty(len(files), dtype=np.uint8)
        #Convert each and every image to array and append array values of all the images
        for n in xrange(0, len(files)):
            images[n] = cv2.imread(join(mypath, folders[j], files[n]))
            labels.fill(j)
        all_images = np.append(all_images, images)
        all_labels = np.append(all_labels, labels)

# Navigate to  main directory in which there are 101 subdirectories containing annotation of each image.
    mypath = '/Users/manaswipodduturi/Documents/Research/MachineLearning/Data/Caltech/Annotations'
    folders = [
        folder for folder in listdir(mypath) if isdir(join(mypath, folder))
    ]
    all_annotations = np.array([])
    for j in xrange(0, len(folders)):
        files = [
            file for file in listdir(join(mypath, folders[j]))
            if (isfile(join(mypath, folders[j], file))
                & bool(file != '.DS_Store'))
        ]
        annotations = np.empty(len(files), dtype=object)
        # Get array of annotations for each image from .mat file and append annotations of all the images
        for n in xrange(0, len(files)):
            annotations[n] = sio.loadmat(join(mypath, folders[j], files[n]))
        all_annotations = np.append(all_annotations, annotations)

# Navigate to  directory where you want to write cropped images.
    os.chdir(
        '/Users/manaswipodduturi/Documents/Research/MachineLearning/Data/Caltech/processed_images1'
    )
    image_with_annotation = np.empty((all_images.shape[0], 150 * 100),
                                     dtype=object)
    for i in xrange(0, all_images.shape[0]):
        image = all_images[i]
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        # Contour is a rough coordinates, which help us find the area of interest in the image
        contour = all_annotations[i]['obj_contour']
        #Box coordinates provides us with rough rectangular outline of our area of interest in the image
        box = all_annotations[i]['box_coord']
        contour = np.transpose(contour)
        contour[:, 0] = contour[:, 0] + box[:, 2]
        contour[:, 1] = contour[:, 1] + box[:, 0]
        # Outline/sketch the ares of interest in the image and find all the points inside that region
        p = Path(contour[:-2])
        mask = np.zeros((image.shape), dtype=np.uint8)
        for y in xrange(box[0, 0], box[0, 1] + 1):
            for x in xrange(box[0, 2], box[0, 3] + 1):
                bool_value = p.contains_point([x, y])
                if bool_value:
                    mask[y - 1, x - 1] = 1
        image = image * mask
        #Write crooped out image to the subfolder
        #cv2.imwrite('image'+str(i+1)+'.jpg',image)
        #append value of each image to an array
        image = cv2.resize(image, (100, 150))
        image_with_annotation[i] = np.ravel(image)
    sio.savemat(
        '/Users/manaswipodduturi/Documents/Research/MachineLearning/NeuralNets/Caltech/caltech_data.mat',
        {
            'features': image_with_annotation,
            'labels': all_labels
        })
예제 #37
0
class Scenario_Frame:
    def __init__(self, parameters={}):

        self.parameters = parameters

        self.timestep = constants.timestep
        self.parameters['timestep'] = self.timestep
        self.simulation_duration = constants.total_monitoring_duration_uni_direction

        self.shift_x = 40  #40#85
        self.shift_y = -10  #30#85

        self.spawn_frequency = int(constants.spawn_pedestrian_frequency /
                                   self.timestep)
        self.adaptive_context_dir = constants.adaptive_dir
        self.monitor_point = self.parameters['monitor_point']

        self.turning_area = Path([(20.0 - self.shift_x, 41.0 + self.shift_y),
                                  (50.0 - self.shift_x, 41.0 + self.shift_y),
                                  (50.0 - self.shift_x, 9.0 + self.shift_y),
                                  (20.0 - self.shift_x, 9.0 + self.shift_y)])
        self.target_final = (35.0 - self.shift_x, -1000.0 + self.shift_y)
        self.turning_up = 9.0 + self.shift_y
        self.target_original = self.parameters['targets'][0]

    def _init_drawing(self, simulation_id):
        self.show_canvas = image_canvas(self.parameters['drawing_width'],
                                        self.parameters['drawing_height'],
                                        self.parameters['pixel_factor'])

    def _tick(self):
        return self._canvas("tick", constants.framerate_limit)

    def _canvas(self, method, *args):
        return getattr(self.show_canvas, method)(*args)

    def _draw(self):
        self._canvas("clear_screen")

        group_population_number = int(force_model.get_population_size())
        for i in range(group_population_number):
            (x, y) = force_model.group_pedestrian_a_property(i, "position")
            if math.isnan(x) == False and math.isnan(y) == False:
                r = force_model.group_pedestrian_a_property(i, "radius")
                group_id = force_model.group_pedestrian_a_property(
                    i, "groupid")
                ped_id = int(
                    force_model.group_pedestrian_a_property(i, "ped_id"))

                is_tracked = self._canvas("is_tracked_pedestrian", x, y)

                if (is_tracked and constants.tracked_pedestrian_id == -1):
                    self._canvas("reset_tracked_position")
                    constants.tracked_pedestrian_id = int(
                        force_model.group_pedestrian_a_property(i, "ped_id"))
                    group_id = force_model.group_pedestrian_a_property(
                        i, "groupid")

                    self.pedestrian_track._reset(
                        self.time, constants.tracked_pedestrian_id)
                    if int(round(group_id)) == 1:
                        self.pedestrian_track._force_color('r.-', 'k.-')

                    else:
                        self.pedestrian_track._force_color('k.-', 'r.-')

                    self.draw_tracked_ped()

                else:
                    if constants.tracked_pedestrian_id == ped_id:  #break in order to not depend on i
                        self.draw_tracked_ped()

                    else:
                        self._canvas("draw_pedestrian", x, y, r, group_id)
            else:
                print("Position is unidentified")
                sys.exit()

        for s in self.parameters['start_areas']:
            self._canvas("draw_start_area", s)

        turning_draw = (20.0 - self.shift_x, 9.0 + self.shift_y,
                        50.0 - self.shift_x, 41.0 + self.shift_y)
        self._canvas("draw_start_area", turning_draw)

        for w in self.parameters['walls']:
            self._canvas("draw_wall", w)

        #monitor = (-1000,self.monitor_point,1000.0,self.monitor_point)
        #self._canvas("draw_wall", monitor)

        #self._canvas("draw_target", self.parameters['targets'][0][0], self.parameters['targets'][0][1])

        #self._canvas("draw_text", "t = %.2f" % self.time)

        self.show_canvas.update()

    def _uninit_drawing(self):
        self._canvas("quit")

    def _spawn_pedestrians(self, context_index):  #re-change by spawn by file

        #first we get current position so that they are not overlap
        population_number = int(force_model.get_population_size())
        #we get new pedestrians
        group_num = [
            int(constants.spawn_rate / 2),
            constants.spawn_rate - int(constants.spawn_rate / 2)
        ]

        #we get from json file rather than add manually by code
        disp_level = self.parameters['out_group_r_strength'] / self.parameters[
            'in_group_r_strength']
        frame_filename = "%s" % (str(disp_level) + "_" + str(context_index) +
                                 "_" + str(int(self.frames)))

        adaptivecontext_file1 = open(
            "%s_1.json" %
            os.path.join(self.adaptive_context_dir, frame_filename))
        json_str = adaptivecontext_file1.read()
        context1 = json.loads(json_str, cls=AdaptiveContextLog_Decoder)
        adaptivecontext_file1.close()

        adaptivecontext_file2 = open(
            "%s_2.json" %
            os.path.join(self.adaptive_context_dir, frame_filename))
        json_str = adaptivecontext_file2.read()
        context2 = json.loads(json_str, cls=AdaptiveContextLog_Decoder)
        adaptivecontext_file2.close()

        population_generator = PopulationGenerator(
            self.parameters, self.parameters['in_group_a_strength'],
            self.parameters['in_group_a_range'],
            self.parameters['in_group_r_strength'],
            self.parameters['in_group_r_range'],
            self.parameters['out_group_a_strength'],
            self.parameters['out_group_a_range'],
            self.parameters['out_group_r_strength'],
            self.parameters['out_group_r_range'],
            self.parameters['target_a_strength'],
            self.parameters['target_a_range'], group_num)

        radii_generator1 = context1._get_radii_generators()
        placement_generator1 = context1._get_placement_generators()

        pedestrian_current_id = population_generator._generate_population(
            radii_generator1, placement_generator1, population_number)
        additional_pedestrians1 = population_generator._get_generated_group_pedestrians_population(
        )
        if additional_pedestrians1 is not None and len(
                additional_pedestrians1) > 0:
            for group_member in additional_pedestrians1:
                position = (group_member['position'][0] - self.shift_x,
                            group_member['position'][1] + self.shift_y)  #
                del group_member['position']
                group_member['position'] = position

                target = (group_member['target'][0] - self.shift_x,
                          group_member['target'][1] + self.shift_y)  #
                del group_member['target']
                group_member['target'] = target

                force_model.add_group_pedestrian(group_member)

        radii_generator2 = context2._get_radii_generators()
        placement_generator2 = context2._get_placement_generators()

        population_generator._generate_population(radii_generator2,
                                                  placement_generator2,
                                                  pedestrian_current_id)
        additional_pedestrians2 = population_generator._get_generated_group_pedestrians_population(
        )
        if additional_pedestrians2 is not None and len(
                additional_pedestrians2) > 0:
            for group_member in additional_pedestrians2:
                position = (group_member['position'][0] - self.shift_x,
                            group_member['position'][1] + self.shift_y)  #
                del group_member['position']
                group_member['position'] = position

                target = (group_member['target'][0] - self.shift_x,
                          group_member['target'][1] + self.shift_y)  #
                del group_member['target']
                group_member['target'] = target

                force_model.add_group_pedestrian(group_member)

    def _done(self):

        if self.time > self.simulation_duration:

            pedestrian_escaped = 0
            # count pedestrians over the monitor point and printout the flow rate
            population_number = int(force_model.get_population_size())
            for i in range(population_number):
                (x, y) = force_model.group_pedestrian_a_property(i, "position")
                if y <= self.monitor_point:
                    pedestrian_escaped += 1.0

            self.flowrate = pedestrian_escaped / self.simulation_duration
            print("flowrate " +
                  str(pedestrian_escaped / self.simulation_duration))

            return True

        return False

    def _revise_target(self):

        population_number = int(force_model.get_population_size())
        for i in range(population_number):
            (x, y) = force_model.group_pedestrian_a_property(i, "position")
            test_in_area = self.turning_area.contains_point((x, y))
            if test_in_area or y < self.turning_up + self.shift_y:
                data = dict(ped_index=int(i), target=self.target_final)
                force_model.target_changed(data)

            else:
                if y > self.turning_up + self.shift_y:
                    data = dict(ped_index=int(i), target=self.target_original)
                    force_model.target_changed(data)

    def replay_frame(self, constant, index_simulation, time_replay,
                     constant_target, constant_target_magnitude):

        frame = (time_replay / constants.frame_store_sample_frequency) * (
            constants.frame_store_sample_frequency / constants.timestep)
        intframe = int(frame)
        filename = str(constant) + "_" + str(index_simulation) + "_" + str(
            intframe)

        frame_log_file = open(
            "%s.json" % os.path.join(constants.framecontext_dir, filename))

        json_str = frame_log_file.read()
        currentframecontext = json.loads(json_str, cls=FrameContextLog_Decoder)

        pedestrian_list = currentframecontext.get_pedestrian_list()
        ''' set parameter '''
        self.parameters['in_group_a_strength'] = 20
        self.parameters['in_group_a_range'] = 2.8
        self.parameters['in_group_r_strength'] = 40
        self.parameters['in_group_r_range'] = 2.0
        self.parameters['out_group_a_strength'] = self.parameters[
            'in_group_a_strength'] * (1 / constant)
        self.parameters['out_group_a_range'] = self.parameters[
            'in_group_a_range']
        self.parameters['out_group_r_strength'] = self.parameters[
            'in_group_r_strength'] * constant
        self.parameters['out_group_r_range'] = self.parameters[
            'in_group_r_range']
        self.parameters['target_a_strength'] = 22000000000
        self.parameters['target_a_range'] = 435

        self.parameters['constant_target'] = constant_target
        self.parameters[
            'constant_target_magnitude'] = constant_target_magnitude

        simulation_id = "Replay frame"
        """ initialize social force model """
        force_model.set_parameters(self.parameters)
        force_model.set_start_simulation_time(time_replay)

        self.time = time_replay
        self.frames = frame

        if pedestrian_list is not None and len(pedestrian_list) > 0:
            for group_member in pedestrian_list:
                position = (group_member['position'][0] - self.shift_x,
                            group_member['position'][1] + self.shift_y)  #
                del group_member['position']
                group_member['position'] = position

                target = (group_member['target'][0] - self.shift_x,
                          group_member['target'][1] + self.shift_y)  #
                del group_member['target']
                group_member['target'] = target

                force_model.add_group_pedestrian(group_member)

        self._init_drawing(simulation_id)

        finished = False

        ###IMPORTANT PART FOR SHARED OBJECT
        constants.tracked_pedestrian_id = -1
        """ initialize the real-time plot """
        self.pedestrian_track = Pedestrian_Track(self.time)
        self.tracking_sample_frequency = int(constants.plot_sample_frequency /
                                             (2 * self.timestep))
        count = 0
        try:
            while self._tick() and not finished:
                force_model.update_pedestrians()

                self._draw()

                self._revise_target()

                if (not self.frames % self.spawn_frequency):
                    self._spawn_pedestrians(index_simulation)

                self.show_canvas.create_image(count)  #self.frames)
                count += 1

                #if not self.frames % self.tracking_sample_frequency:
                #    self._plot_track_ped()

                self.time += self.timestep
                self.frames += 1

                if self._done():
                    finished = True

        except KeyboardInterrupt:
            pass

        self._uninit_drawing()

    def draw_tracked_ped(self):

        target_vector = force_model.group_pedestrian_id_property(
            constants.tracked_pedestrian_id, "target_vector")
        ingroup_vector = force_model.group_pedestrian_id_property(
            constants.tracked_pedestrian_id, "ingroup_vector")
        outgroup_vector = force_model.group_pedestrian_id_property(
            constants.tracked_pedestrian_id, "outgroup_vector")
        wall_vector = force_model.group_pedestrian_id_property(
            constants.tracked_pedestrian_id, "wall_vector")

        (x, y) = force_model.group_pedestrian_id_property(
            constants.tracked_pedestrian_id, "position")
        r = force_model.group_pedestrian_id_property(
            constants.tracked_pedestrian_id, "radius")
        group_id = force_model.group_pedestrian_id_property(
            constants.tracked_pedestrian_id, "groupid")

        if math.isnan(x) == False and math.isnan(y) == False:
            self._canvas("draw_pedestrian_tracking", x, y, r, group_id,
                         target_vector, ingroup_vector, outgroup_vector,
                         wall_vector)

    def _plot_track_ped(self):

        if constants.tracked_pedestrian_id != -1:

            target_force = force_model.group_pedestrian_id_property(
                constants.tracked_pedestrian_id, "target_force")
            if target_force != -999.0:

                wall_force = force_model.group_pedestrian_id_property(
                    constants.tracked_pedestrian_id, "wall_force")
                ingroup_force = force_model.group_pedestrian_id_property(
                    constants.tracked_pedestrian_id, "ingroup_force")
                outgroup_force = force_model.group_pedestrian_id_property(
                    constants.tracked_pedestrian_id, "outgroup_force")
                velocity_x = force_model.group_pedestrian_id_property(
                    constants.tracked_pedestrian_id, "velocity_x")

                (x, y) = force_model.group_pedestrian_id_property(
                    constants.tracked_pedestrian_id, "position")
                current_position = int(round(x + self.shift + 30))

                #plot for force and velocity based on time
                self.pedestrian_track._addsample(self.time, target_force,
                                                 ingroup_force, outgroup_force,
                                                 wall_force, velocity_x,
                                                 current_position)

        data = []
        group_population_number = int(force_model.get_population_size())

        for i in range(group_population_number):
            ped_id = int(force_model.group_pedestrian_a_property(i, "ped_id"))
            target_force = force_model.group_pedestrian_a_property(
                i, "target_force")
            ingroup_force = force_model.group_pedestrian_a_property(
                i, "ingroup_force")
            outgroup_force = force_model.group_pedestrian_a_property(
                i, "outgroup_force")
            wall_force = force_model.group_pedestrian_a_property(
                i, "wall_force")

            velocity_x = force_model.group_pedestrian_a_property(
                i, "velocity_x")
            (x, y) = force_model.group_pedestrian_a_property(i, "position")
            current_position = int(round(x))

            data.append([
                ped_id, target_force, ingroup_force, outgroup_force,
                wall_force, velocity_x, current_position
            ])

        self.pedestrian_track._addsample2(self.time, data)

    def _get_parameters(self):
        return self.parameters
예제 #38
0
    polies = getPolies(state)
    paths = []
    for p in polies:
        # Projected vertices
        p_projected = [m(x[1], x[0]) for x in p]
        # Create the Path
        p_path = Path(p_projected)
        paths.append(p_path)
    # Test points
    for latlon,cnt in codes[state]['flocs'].items():
        lat,lon=latlon.split(',')
        # Test point projection
        p1 = m(float(lat),float(lon))
        insider = False
        for p_path in paths:
            if p_path.contains_point(p1) > 0:
                instate += cnt
                insider = True
                break
        if insider==False :
            outstate += cnt
    insratio[state] = instate / (instate + outstate)


def json2csv():
    import csv
    with open('../data/geocoded.json', 'r') as infile:
        codes = json.load(infile)

    with open('../data/eggs.csv', 'w', newline='') as csvfile:
        writer = csv.writer(csvfile)
예제 #39
0
    def check(self):

        frameNumber = self.acq.GetPointFrameNumber()

        LASI_values = self.acq.GetPoint("LASI").GetValues()
        RASI_values = self.acq.GetPoint("RASI").GetValues()
        LPSI_values = self.acq.GetPoint("LPSI").GetValues()
        RPSI_values = self.acq.GetPoint("RPSI").GetValues()
        sacrum_values = (self.acq.GetPoint("LPSI").GetValues() +
                         self.acq.GetPoint("RPSI").GetValues()) / 2.0
        midAsis_values = (self.acq.GetPoint("LASI").GetValues() +
                          self.acq.GetPoint("RASI").GetValues()) / 2.0

        projectedLASI = np.array(
            [LASI_values[:, 0], LASI_values[:, 1],
             np.zeros((frameNumber))]).T
        projectedRASI = np.array(
            [RASI_values[:, 0], RASI_values[:, 1],
             np.zeros((frameNumber))]).T
        projectedLPSI = np.array(
            [LPSI_values[:, 0], LPSI_values[:, 1],
             np.zeros((frameNumber))]).T
        projectedRPSI = np.array(
            [RPSI_values[:, 0], RPSI_values[:, 1],
             np.zeros((frameNumber))]).T

        for i in range(0, frameNumber):
            verts = [
                projectedLASI[i, 0:2],  # left, bottom
                projectedRASI[i, 0:2],  # left, top
                projectedRPSI[i, 0:2],  # right, top
                projectedLPSI[i, 0:2],  # right, bottom
                projectedLASI[i, 0:2],  # right, top
            ]

            codes = [
                Path.MOVETO,
                Path.LINETO,
                Path.LINETO,
                Path.LINETO,
                Path.CLOSEPOLY,
            ]

            path = Path(verts, codes)

            intersection = geometry.LineLineIntersect(projectedLASI[i, :],
                                                      projectedLPSI[i, :],
                                                      projectedRASI[i, :],
                                                      projectedRPSI[i, :])

            if path.contains_point(intersection[0]):
                LOGGER.logger.error(
                    "[pyCGM2-Checking] wrong Labelling of pelvic markers at frame [%i]"
                    % (i))
                if self.exceptionMode:
                    raise Exception(
                        "[pyCGM2-Checking] wrong Labelling of pelvic markers at frame [%i]"
                        % (i))

                self.state = False
            else:
                # check marker side
                pt1 = RASI_values[i, :]
                pt2 = LASI_values[i, :]
                pt3 = sacrum_values[i, :]
                ptOrigin = midAsis_values[i, :]

                a1 = (pt2 - pt1)
                a1 = np.divide(a1, np.linalg.norm(a1))
                v = (pt3 - pt1)
                v = np.divide(v, np.linalg.norm(v))
                a2 = np.cross(a1, v)
                a2 = np.divide(a2, np.linalg.norm(a2))

                x, y, z, R = frame.setFrameData(a1, a2, "YZX")

                csFrame_L = frame.Frame()
                csFrame_L.setRotation(R)
                csFrame_L.setTranslation(RASI_values[i, :])

                csFrame_R = frame.Frame()
                csFrame_R.setRotation(R)
                csFrame_R.setTranslation(LASI_values[i, :])

                for marker in self.markers:
                    residual = self.acq.GetPoint(marker).GetResidual(i)

                    if marker[0] == "L":
                        local = np.dot(
                            csFrame_L.getRotation().T,
                            self.acq.GetPoint(marker).GetValues()[i, :] -
                            csFrame_L.getTranslation())
                    if marker[0] == "R":
                        local = np.dot(
                            csFrame_R.getRotation().T,
                            self.acq.GetPoint(marker).GetValues()[i, :] -
                            csFrame_R.getTranslation())
                    if residual > 0.0:
                        if marker[0] == "L" and local[1] < 0:
                            LOGGER.logger.error(
                                "[pyCGM2-Checking] check location of the marker [%s] at frame [%i]"
                                % (marker, i))
                            self.state = False
                            if self.exceptionMode:
                                raise Exception(
                                    "[pyCGM2-Checking] check location of the marker [%s] at frame [%i]"
                                    % (marker, i))

                        if marker[0] == "R" and local[1] > 0:
                            LOGGER.logger.error(
                                "[pyCGM2-Checking] check location of the marker [%s] at frame [%i]"
                                % (marker, i))
                            self.state = False
                            if self.exceptionMode:
                                raise Exception(
                                    "[pyCGM2-Checking] check location of the marker [%s] at frame [%i]"
                                    % (marker, i))
                                self.state = False
예제 #40
0
def count_maxima_2_5d(par_obj, time_pt, fileno, reset_max):
    #count maxima won't work properly if have selected a random set of Z
    imfile = par_obj.filehandlers[fileno]

    predMtx = np.zeros((par_obj.height, par_obj.width, imfile.max_z + 1))
    for i in range(imfile.max_z + 1):
        predMtx[:, :, i] = par_obj.data_store['pred_arr'][fileno][time_pt][i]

    [det, xx] = det_hess_2_5d(predMtx, par_obj.min_distance)
    #[det2, xx] = det_hess_2_5d(predMtx, [par_obj.min_distance[0:2],par_obj.min_distance[2]/2])
    #[det3, xx] = det_hess_2_5d(predMtx, [par_obj.min_distance[0:2],par_obj.min_distance[2]*2])
    #det=det2+det+det3

    # if not already set, create. This is then used for the entire image and all
    #subsequent training. A little hacky, but otherwise the normalisation screws everything up
    if not par_obj.max_det or reset_max == True:
        par_obj.max_det = np.max(det)

    detn = det / par_obj.max_det
    par_obj.data_store['maxi_arr'][fileno][time_pt] = {}
    for i in range(imfile.max_z + 1):
        #par_obj.data_store[time_pt]['maxi_arr'][i] = np.sqrt(detn[:,:,i]*par_obj.data_store[time_pt]['pred_arr'][i])
        par_obj.data_store['maxi_arr'][fileno][time_pt][i] = detn[:, :, i]

    pts = peak_local_max(detn,
                         min_distance=par_obj.min_distance,
                         threshold_abs=par_obj.abs_thr)

    pts2keep = []
    for pt2d in pts:

        T = xx[pt2d[0], pt2d[1], pt2d[2]]
        #D = z[pt2d[0],pt2d[1],pt2d[2]]
        # Removes points that are positive definite and therefore minima
        if T > 0:  # and D>0:
            pass
            #print 'point removed'
        else:
            pts2keep.append([pt2d[0], pt2d[1], pt2d[2], 1])

    pts = pts2keep

    #Filter those which are not inside the region.
    if par_obj.data_store['roi_stkint_x'][fileno][time_pt].__len__() > 0:
        pts2keep = []

        for i in par_obj.data_store['roi_stkint_x'][fileno][time_pt]:
            for pt2d in pts:
                if pt2d[2] == i:
                    #Find the region of interest.
                    ppt_x = par_obj.data_store['roi_stkint_x'][fileno][
                        time_pt][i]
                    ppt_y = par_obj.data_store['roi_stkint_y'][fileno][
                        time_pt][i]
                    #Reformat to make the path object.
                    pot = []
                    for b in range(0, ppt_x.__len__()):
                        pot.append([ppt_x[b], ppt_y[b]])
                    p = Path(pot)
                    if p.contains_point([pt2d[1], pt2d[0]]) is True:
                        pts2keep.append(pt2d)

        pts = pts2keep

    par_obj.data_store['pts'][fileno][time_pt] = pts
def isInHull(hull, np_points, x, y):
    #point in hull?
    hull_path = Path(np_points[hull.vertices])
    isInPath = hull_path.contains_point((x, y))
    return (isInPath == True)
예제 #42
0
def count_maxima_2d_v2(par_obj, time_pt, fileno, reset_max):
    #count maxima won't work properly if have selected a random set of Z
    imfile = par_obj.filehandlers[fileno]
    par_obj.min_distance[2] = 0
    det = np.zeros((par_obj.height, par_obj.width, imfile.max_z + 1))
    xx = np.zeros((par_obj.height, par_obj.width, imfile.max_z + 1))
    for i in range(imfile.max_z + 1):
        predIm = par_obj.data_store['pred_arr'][fileno][time_pt][i]
        [deti, xxi] = det_hess_2d(predIm, par_obj.min_distance)
        det[:, :, i] = deti
        xx[:, :, i] = xxi

    # if not already set, create. This is then used for the entire image and all
    #subsequent training. A little hacky, but otherwise the normalisation screws everything up
    if not par_obj.max_det or reset_max == True:
        par_obj.max_det = np.max(det)

    detn = det / par_obj.max_det
    par_obj.data_store['maxi_arr'][fileno][time_pt] = {}
    for i in range(imfile.max_z + 1):
        #par_obj.data_store[time_pt]['maxi_arr'][i] = np.sqrt(detn[:,:,i]*par_obj.data_store[time_pt]['pred_arr'][i])
        par_obj.data_store['maxi_arr'][fileno][time_pt][i] = detn[:, :, i]

    pts = peak_local_max(detn,
                         min_distance=par_obj.min_distance,
                         threshold_abs=par_obj.abs_thr)

    pts2keep = []
    for pt2d in pts:

        T = xx[pt2d[0], pt2d[1], pt2d[2]]
        #D=det[pt2d[0],pt2d[1],pt2d[2]]
        # Removes points that are positive definite and therefore minima
        if T > 0:  # and D>0:
            pass
            #print 'point removed'
        else:
            pts2keep.append([pt2d[0], pt2d[1], pt2d[2], 1])

    pts = pts2keep

    #Filter those which are not inside the region.
    if par_obj.data_store['roi_stkint_x'][fileno][time_pt].__len__() > 0:
        pts2keep = []

        for i in par_obj.data_store['roi_stkint_x'][fileno][time_pt]:
            for pt2d in pts:
                if pt2d[2] == i:
                    #Find the region of interest.
                    ppt_x = par_obj.data_store['roi_stkint_x'][fileno][
                        time_pt][i]
                    ppt_y = par_obj.data_store['roi_stkint_y'][fileno][
                        time_pt][i]
                    #Reformat to make the path object.
                    pot = []
                    for b in range(0, ppt_x.__len__()):
                        pot.append([ppt_x[b], ppt_y[b]])
                    p = Path(pot)
                    if p.contains_point([pt2d[1], pt2d[0]]) is True:
                        pts2keep.append(pt2d)

        pts = pts2keep

    thresh = 2

    clusters = hcluster.fclusterdata(pts, thresh, criterion='distance')

    pts2keep = []

    pts = np.array(pts, dtype='float')
    pts[:, 2] = pts[:, 2] * par_obj.z_cal / 1

    for clno in range(1, clusters.max() + 1):

        cluster_pts = pts[np.where(clusters == clno)[0], :]
        centroid = np.mean(cluster_pts, axis=0)
        centroid[2] = centroid[2] / par_obj.z_cal * 1
        centroid = np.round(centroid).astype('int')
        pts2keep.append([centroid[0], centroid[1], centroid[2], centroid[3]])

    pts = pts2keep

    par_obj.data_store['pts'][fileno][time_pt] = pts
예제 #43
0
def count_maxima_thresh(par_obj, time_pt, fileno, reset_max=False):

    #count maxima won't work properly if have selected a random set of Z
    imfile = par_obj.filehandlers[fileno]
    if par_obj.min_distance[2] == 0 or imfile.max_z == 0:
        count_maxima_2d(par_obj, time_pt, fileno, reset_max)
        return
    predMtx = np.zeros((par_obj.height, par_obj.width, imfile.max_z + 1))
    for i in range(imfile.max_z + 1):
        predMtx[:, :, i] = par_obj.data_store['pred_arr'][fileno][time_pt][i]

    radius = [
        par_obj.min_distance[0], par_obj.min_distance[1],
        par_obj.resize_factor * par_obj.min_distance[2] / imfile.z_calibration
    ]

    [det3, det2, det1] = det_hess_3d(predMtx, radius)

    #if not already set, create. This is then used for the entire image and all subsequent training.
    #A little hacky, but otherwise the normalisation screws everything up
    if not par_obj.max_det:
        par_obj.max_det = np.max(det3)
    # normalise
    det3 = det3 / par_obj.max_det
    det3 = det3 > par_obj.abs_thr
    par_obj.data_store['maxi_arr'][fileno][time_pt] = {}
    for i in range(imfile.max_z + 1):
        par_obj.data_store['maxi_arr'][fileno][time_pt][i] = det3[:, :, i]

    det_bin = det3 > par_obj.abs_thr

    det_label, no_obj = measurements.label(det_bin)

    #par_obj.pts = v2._prune_blobs(par_obj.pts, min_distance=[int(self.count_txt_1.text()),int(self.count_txt_2.text()),int(self.count_txt_3.text())])
    pts2keep = []
    print(no_obj)

    det_com = measurements.center_of_mass(det_bin, det_label,
                                          range(1, no_obj + 1))

    for pt2d in det_com:
        ptuple = tuple(np.round(x).astype('uint') for x in pt2d)

        #determinants of submatrices
        dp = det1[ptuple]
        dp2 = det2[ptuple]
        #dp3 = det3[ptuple]
        #negative definite, therefore maximum (note signs in det calculation)
        #if dp >= 0 and dp2 >= 0: # and dp3>=par_obj.abs_thr:
        #print 'point retained', det[ptuple]<0 , det2[ptuple]<0 , det3[ptuple]<0
        pts2keep.append([ptuple[0], ptuple[1], ptuple[2], 1])
    pts = pts2keep
    par_obj.show_pts = 1

    #Filter those which are not inside the region.
    if par_obj.data_store['roi_stkint_x'][fileno][time_pt].__len__() > 0:
        pts2keep = []

        for i in par_obj.data_store['roi_stkint_x'][fileno][time_pt]:
            for pt2d in pts:

                if pt2d[2] == i:
                    #Find the region of interest.
                    ppt_x = par_obj.data_store['roi_stkint_x'][fileno][
                        time_pt][i]
                    ppt_y = par_obj.data_store['roi_stkint_y'][fileno][
                        time_pt][i]
                    #Reformat to make the path object.
                    pot = []
                    for b in range(0, ppt_x.__len__()):
                        pot.append([ppt_x[b], ppt_y[b]])
                    p = Path(pot)
                    if p.contains_point([pt2d[1], pt2d[0]]) is True:
                        pts2keep.append(pt2d)
        pts = pts2keep

    par_obj.data_store['pts'][fileno][time_pt] = pts
예제 #44
0
def count_maxima_laplace_variable(par_obj, time_pt, fileno, reset_max=False):
    #count maxima won't work properly if have selected a random set of Z
    min_d = [x for x in par_obj.min_distance]
    imfile = par_obj.filehandlers[fileno]
    #if par_obj.min_distance[2] == 0 or par_obj.max_z == 0:
    #    count_maxima_2d(par_obj, time_pt, fileno)
    #    return
    predMtx = np.zeros((par_obj.height, par_obj.width, imfile.max_z + 1))
    for i in range(imfile.max_z + 1):
        predMtx[:, :, i] = par_obj.data_store['pred_arr'][fileno][time_pt][i]

    l1 = filters.gaussian_laplace(predMtx, [0, 0, min_d[2]], mode='constant')
    l2 = filters.gaussian_laplace(predMtx, [0, 0, min_d[2] * 2],
                                  mode='constant') * 2
    l3 = filters.gaussian_laplace(predMtx, [0, 0, min_d[2] * .5],
                                  mode='constant') * .5
    '''l1=-filters.gaussian_laplace(predMtx, min_d, mode='constant')
    l2=-filters.gaussian_laplace(predMtx, [x*.5 for x in min_d], mode='constant')
    l3=-filters.gaussian_laplace(predMtx, [x*2 for x in min_d], mode='constant')
    '''
    l3 = filters.gaussian_laplace(
        (l1 + l2 + l3), [min_d[0], min_d[1], 0], mode='constant') / 3
    #if not already set, create. This is then used for the entire image and all subsequent training.
    #A little hacky, but otherwise the normalisation screws everything up
    if not par_obj.max_det or reset_max:
        par_obj.max_det = np.max(l3)

    l3 = l3 / par_obj.max_det
    par_obj.data_store['maxi_arr'][fileno][time_pt] = {}
    for i in range(imfile.max_z + 1):
        par_obj.data_store['maxi_arr'][fileno][time_pt][i] = l3[:, :, i]

    pts = peak_local_max(l3, min_distance=min_d, threshold_abs=par_obj.abs_thr)

    pts2keep = []
    for pt2d in pts:
        #determinants of submatrices
        pts2keep.append([pt2d[0], pt2d[1], pt2d[2], 1])
    pts = pts2keep
    par_obj.show_pts = 1

    #Filter those which are not inside the region.
    if par_obj.data_store['roi_stkint_x'][fileno][time_pt].__len__() > 0:
        pts2keep = []

        for i in par_obj.data_store['roi_stkint_x'][fileno][time_pt]:
            for pt2d in pts:

                if pt2d[2] == i:
                    #Find the region of interest.
                    ppt_x = par_obj.data_store['roi_stkint_x'][fileno][
                        time_pt][i]
                    ppt_y = par_obj.data_store['roi_stkint_y'][fileno][
                        time_pt][i]
                    #Reformat to make the path object.
                    pot = []
                    for b in range(0, ppt_x.__len__()):
                        pot.append([ppt_x[b], ppt_y[b]])
                    p = Path(pot)
                    if p.contains_point([pt2d[1], pt2d[0]]) is True:
                        pts2keep.append(pt2d)
        pts = pts2keep

    par_obj.data_store['pts'][fileno][time_pt] = pts
예제 #45
0
def _points_inside_poly(points, verts):
    poly = Path(verts)
    return [ind for ind, p in enumerate(points) if poly.contains_point(p)]
def in_healpix_image(x, y):
    """
    Return True if and only if `(x, y)` lies in the image of the HEALPix
    projection of the unit sphere.

    EXAMPLES::

        >>> eps = 0     # Test boundary points.
        >>> hp = [
        ... (-pi - eps, pi/4),
        ... (-3*pi/4, pi/2 + eps),
        ... (-pi/2, pi/4 + eps),
        ... (-pi/4, pi/2 + eps),
        ... (0, pi/4 + eps),
        ... (pi/4, pi/2 + eps),
        ... (pi/2, pi/4 + eps),
        ... (3*pi/4, pi/2 + eps),
        ... (pi + eps, pi/4),
        ... (pi + eps,-pi/4),
        ... (3*pi/4,-pi/2 - eps),
        ... (pi/2,-pi/4 - eps),
        ... (pi/4,-pi/2 - eps),
        ... (0,-pi/4 - eps),
        ... (-pi/4,-pi/2 - eps),
        ... (-pi/2,-pi/4 - eps),
        ... (-3*pi/4,-pi/2 - eps),
        ... (-pi - eps,-pi/4)
        ... ]
        >>> for p in hp:
        ...     if not in_healpix_image(*p):
        ...             print('Fail')
        ...
        >>> in_healpix_image(0, 0)
        True
        >>> in_healpix_image(0, pi/4 + 0.1)
        False

    """
    # matplotlib is a third-party module.
    from matplotlib.path import Path

    # Fuzz to slightly expand HEALPix image boundary so that
    # points on the boundary count as lying in the image.
    eps = 1e-10
    vertices = [
        (-pi - eps, pi / 4 + eps),
        (-3 * pi / 4, pi / 2 + eps),
        (-pi / 2, pi / 4 + eps),
        (-pi / 4, pi / 2 + eps),
        (0, pi / 4 + eps),
        (pi / 4, pi / 2 + eps),
        (pi / 2, pi / 4 + eps),
        (3 * pi / 4, pi / 2 + eps),
        (pi + eps, pi / 4 + eps),
        (pi + eps, -pi / 4 - eps),
        (3 * pi / 4, -pi / 2 - eps),
        (pi / 2, -pi / 4 - eps),
        (pi / 4, -pi / 2 - eps),
        (0, -pi / 4 - eps),
        (-pi / 4, -pi / 2 - eps),
        (-pi / 2, -pi / 4 - eps),
        (-3 * pi / 4, -pi / 2 - eps),
        (-pi - eps, -pi / 4 - eps),
    ]
    poly = Path(vertices)
    return bool(poly.contains_point([x, y]))
예제 #47
0
def get_dates(inps):
    # Given the SLC directory This function extracts the acquisition dates
    # and prepares a dictionary of sentinel slc files such that keys are
    # acquisition dates and values are object instances of sentinelSLC class
    # which is defined in Stack.py

    if inps.bbox is not None:
        bbox = [float(val) for val in inps.bbox.split()]

    if inps.exclude_dates is not None:
        excludeList = inps.exclude_dates.split(',')
    else:
        excludeList = []

    if inps.include_dates is not None:
        includeList = inps.include_dates.split(',')
    else:
        includeList = []

    if os.path.isfile(inps.slc_dirname):
        print('reading SAFE files from: ' + inps.slc_dirname)
        SAFE_files = []
        for line in open(inps.slc_dirname):
            SAFE_files.append(str.replace(line, '\n', '').strip())

    else:
        SAFE_files = glob.glob(os.path.join(
            inps.slc_dirname,
            'S1*_IW_SLC*zip'))  # changed to zip file by Minyan Zhong

    if len(SAFE_files) == 0:
        raise Exception('No SAFE file found')

    elif len(SAFE_files) == 1:
        raise Exception(
            'At least two SAFE file is required. Only one SAFE file found.')

    else:
        print("Number of SAFE files found: " + str(len(SAFE_files)))

    if inps.startDate is not None:
        stackStartDate = datetime.datetime(
            *time.strptime(inps.startDate, "%Y-%m-%d")[0:6])
    else:
        #if startDate is None let's fix it to first JPL's staellite lunch date :)
        stackStartDate = datetime.datetime(
            *time.strptime("1958-01-31", "%Y-%m-%d")[0:6])

    if inps.stopDate is not None:
        stackStopDate = datetime.datetime(
            *time.strptime(inps.stopDate, "%Y-%m-%d")[0:6])
    else:
        stackStopDate = datetime.datetime(
            *time.strptime("2158-01-31", "%Y-%m-%d")[0:6])

    ################################
    # write down the list of SAFE files in a txt file which will be used:
    f = open('SAFE_files.txt', 'w')
    safe_count = 0
    safe_dict = {}
    bbox_poly = [[bbox[0], bbox[2]], [bbox[0], bbox[3]], [bbox[1], bbox[3]],
                 [bbox[1], bbox[2]]]
    for safe in SAFE_files:
        safeObj = sentinelSLC(safe)
        safeObj.get_dates()
        if safeObj.start_date_time < stackStartDate or safeObj.start_date_time > stackStopDate:
            excludeList.append(safeObj.date)
            continue

        safeObj.get_orbit(inps.orbit_dirname, inps.work_dir)

        # check if the date safe file is needed to cover the BBOX
        reject_SAFE = False
        if safeObj.date not in excludeList and inps.bbox is not None:

            reject_SAFE = True
            pnts = safeObj.getkmlQUAD(safe)

            # looping over the corners, keep the SAF is one of the corners is within the BBOX
            lats = []
            lons = []
            for pnt in pnts:
                lon = float(pnt.split(',')[0])
                lat = float(pnt.split(',')[1])

                # keep track of all the corners to see of the product is larger than the bbox
                lats.append(lat)
                lons.append(lon)

                import matplotlib
                from matplotlib.path import Path as Path

                #                bbox = SNWE
                #                polygon = bbox[0] bbox[2]       SW
                #                          bbox[0] bbox[3]       SE
                #                          bbox[1] bbox[3]       NE
                #                          bbox[1] bbox[2]       NW

                poly = Path(bbox_poly)
                point = (lat, lon)
                in_bbox = poly.contains_point(point)

                # product corner falls within BBOX (SNWE)
                if in_bbox:
                    reject_SAFE = False

            # If the product is till being rejected, check if the BBOX corners fall within the frame
            if reject_SAFE:
                for point in bbox_poly:
                    frame = [[a, b] for a, b in zip(lats, lons)]
                    poly = Path(frame)
                    in_frame = poly.contains_point(point)
                    if in_frame:
                        reject_SAFE = False

        if not reject_SAFE:
            if safeObj.date not in safe_dict.keys(
            ) and safeObj.date not in excludeList:
                safe_dict[safeObj.date] = safeObj
            elif safeObj.date not in excludeList:
                safe_dict[safeObj.date].safe_file = safe_dict[
                    safeObj.date].safe_file + ' ' + safe

            # write the SAFE file as it will be used
            f.write(safe + '\n')
            safe_count += 1
    # closing the SAFE file overview
    f.close()
    print("Number of SAFE files to be used (cover BBOX): " + str(safe_count))

    ################################
    dateList = [key for key in safe_dict.keys()]
    dateList.sort()
    print("*****************************************")
    print("Number of dates : " + str(len(dateList)))
    print("List of dates : ")
    print(dateList)

    ################################
    #get the overlap lat and lon bounding box
    S = []
    N = []
    W = []
    E = []
    safe_dict_bbox = {}
    safe_dict_bbox_finclude = {}
    safe_dict_finclude = {}
    safe_dict_frameGAP = {}
    print('date      south      north')
    for date in dateList:
        #safe_dict[date].get_lat_lon()
        safe_dict[date].get_lat_lon_v2()

        #safe_dict[date].get_lat_lon_v3(inps)
        S.append(safe_dict[date].SNWE[0])
        N.append(safe_dict[date].SNWE[1])
        W.append(safe_dict[date].SNWE[2])
        E.append(safe_dict[date].SNWE[3])
        print(date, safe_dict[date].SNWE[0], safe_dict[date].SNWE[1])
        if inps.bbox is not None:
            if safe_dict[date].SNWE[0] <= bbox[0] and safe_dict[date].SNWE[
                    1] >= bbox[1]:
                safe_dict_bbox[date] = safe_dict[date]
                safe_dict_bbox_finclude[date] = safe_dict[date]
            elif date in includeList:
                safe_dict_finclude[date] = safe_dict[date]
                safe_dict_bbox_finclude[date] = safe_dict[date]

        # tracking dates for which there seems to be a gap in coverage
        if not safe_dict[date].frame_nogap:
            safe_dict_frameGAP[date] = safe_dict[date]

    print("*****************************************")
    print(
        "The overlap region among all dates (based on the preview kml files):")
    print(" South   North   East  West ")
    print(max(S), min(N), max(W), min(E))
    print("*****************************************")
    if max(S) > min(N):
        print("""WARNING: 
           There might not be overlap between some dates""")
        print("*****************************************")
    ################################
    print('All dates (' + str(len(dateList)) + ')')
    print(dateList)
    print("")
    if inps.bbox is not None:
        safe_dict = safe_dict_bbox
        dateList = [key for key in safe_dict.keys()]
        dateList.sort()
        print('dates covering the bbox (' + str(len(dateList)) + ')')
        print(dateList)
        print("")

        if len(safe_dict_finclude) > 0:
            # updating the dateList that will be used for those dates that are forced include
            # but which are not covering teh BBOX completely
            safe_dict = safe_dict_bbox_finclude
            dateList = [key for key in safe_dict.keys()]
            dateList.sort()

            # sorting the dates of the forced include
            dateListFinclude = [key for key in safe_dict_finclude.keys()]
            print('dates forced included (do not cover the bbox completely, ' +
                  str(len(dateListFinclude)) + ')')
            print(dateListFinclude)
            print("")

    # report any potential gaps in fame coverage
    if len(safe_dict_frameGAP) > 0:
        dateListframeGAP = [key for key in safe_dict_frameGAP.keys()]
        print('dates for which it looks like there are missing frames')
        print(dateListframeGAP)
        print("")

    if inps.master_date is None:
        if len(dateList) < 1:
            print('*************************************')
            print('Error:')
            print(
                'No acquisition forfills the temporal range and bbox requirement.'
            )
            sys.exit(1)
        inps.master_date = dateList[0]
        print(
            "The master date was not chosen. The first date is considered as master date."
        )

    print("")
    print("All SLCs will be coregistered to : " + inps.master_date)

    slaveList = [key for key in safe_dict.keys()]
    slaveList.sort()
    slaveList.remove(inps.master_date)
    print("slave dates :")
    print(slaveList)
    print("")

    return dateList, inps.master_date, slaveList, safe_dict
예제 #48
0
def contained_polygon(inner, outer):
    """ Determine whether one GeoJSON LinearRing contains another. """
    outer_path = Path(outer)
    return all(outer_path.contains_point([position[0], position[1]]) for position in inner)
예제 #49
0
                                             envelope=envelope)
        response = DataAccessLayer.getGeometryData(req, times=None)
        for city in response:
            cityInfo = str(city.getString('name')) + "," + str(
                city.getNumber('lon')) + "," + str(city.getNumber('lat'))
            cityList.append(cityInfo)
        pickle.dump(cityList, open(homeDir + 'bin/' + rda + 'cities.pck', 'w'))
    else:
        cityList = pickle.load(open(homeDir + 'bin/' + rda + 'cities.pck',
                                    'r'))

    for city in cityList:
        (name, lon, lat) = city.split(',')
        if name[0] != "#":  #for postediting to comment out
            lon, lat = m(lon, lat)
            if path.contains_point([lon, lat]):
                plt.text(lon,
                         lat,
                         ' ' + name + '\n',
                         size=7,
                         va='bottom',
                         ha='left',
                         linespacing=0.5,
                         fontweight=600,
                         clip_on=True)
                plt.scatter(lon, lat, marker='+', c='black')

    plt.savefig(homeDir + 'images/' + rda + '/foreground.png',
                bbox_inches='tight',
                pad_inches=0,
                transparent=True)
예제 #50
0
    dim0, dim1 = latlons[0].shape

    llarray = numpy.dstack([flats, flons])[0]
    res2 = do_kdtree(llarray, (xlat, xlon))
    i0, j0 = res2 % dim0, (res2 - res2 % dim0) / dim0
    print 'KDTree: ', i0, j0, flats[res2], flons[res2]

    from matplotlib.path import Path

    jn0, in0 = res2 % dim0, (res2 - res2 % dim0) / dim0
    sq1 = [(in0, jn0), (in0 + 1, jn0), (in0 + 1, jn0 + 1), (in0, jn0 + 1)]
    print sq1
    llsq1 = toLatlon(latlons, sq1)
    print llsq1
    path1 = Path(llsq1)
    print 'First test: ', path1, path1.contains_point((xlat, xlon))
    sq = {}
    sq['ll'] = [(in0, jn0), (in0 + 1, jn0), (in0 + 1, jn0 + 1), (in0, jn0 + 1)]
    sq['lr'] = [(in0 - 1, jn0), (in0, jn0), (in0, jn0 + 1), (in0 - 1, jn0 + 1)]
    sq['ur'] = [(in0 - 1, jn0 - 1), (in0, jn0 - 1), (in0, jn0), (in0 - 1, jn0)]
    sq['ul'] = [(in0, jn0 - 1), (in0 + 1, jn0 - 1), (in0 + 1, jn0), (in0, jn0)]
    for corner in sq:
        path = sq[corner]
        llpath = Path(toLatlon(latlons, path))
        print 'Corner: ', corner, path, '\n', llpath

        if llpath.contains_point((xlat, xlon)):
            print 'Succes! ', corner, path, llpath
            return path
    print 10 * '*', 'No path found!', xlat, xlon
예제 #51
0
        bb_ = np.dot(XY, baubaul.T).T
        rgb = np.repeat(np.random.randint(0, 255, size=3), len(bb_),
                        axis=0).reshape(-1, len(bb_)).T
        baubals = np.vstack([baubals, np.hstack([bb_, rgb])])

    # generate lights
    n_lights = int(hull.volume)
    x_p = z_slice.x.min() + (np.ptp(z_slice.x.values) *
                             np.random.random_sample(size=n_lights))
    y_p = z_slice.y.min() + (np.ptp(z_slice.y.values) *
                             np.random.random_sample(size=n_lights))
    points = np.vstack([x_p.T, y_p.T]).T

    # ensure they're inside the tree
    hull_path = Path(z_slice[['x', 'y']].loc[z_slice.index[hull.vertices]])
    in_hull = [hull_path.contains_point((x, y)) for x, y in points]
    points = points[np.where(in_hull)]

    # randomise z position a little
    for x, y in points:

        XY = np.identity(4)
        XY[:2, 3] = x, y
        XY[2, 3] = z + np.random.random() * args.vertical_spacing
        if XY[2, 3] > tree_pc.z.max(): continue
        light_ = np.dot(XY, light.T).T
        rgb = np.repeat([255, 248, 220], len(light_),
                        axis=0).reshape(-1, len(light_)).T
        on = np.zeros((len(light_), 1)) + np.random.randint(1, high=3)
        lights = np.vstack([lights, np.hstack([light_, rgb, on])])
예제 #52
0
def main():
	
	# Reading arguments from command
	files = str(sys.argv[1])
	region = str(sys.argv[2])
	value =	float(sys.argv[3]) if len(sys.argv) > 3 else 0.0
	norm = True
	if len(sys.argv) > 4:
		norm = str(sys.argv[4]) in ['true', 'True', '1', 't', 'y','yes']

	# Read in frames file
	f = open(files, 'r')
	inputnames = f.read().splitlines()
	nframes = len(inputnames)
	
	# Loop over all frames and read in mask regions
	for i in range(0,nframes):
		inputname = inputnames[i]+"_ext.fits"
		outputname = inputnames[i]+"_norm.fits"
		hdulist = pyfits.open(inputname)
		primhdr = pyfits.getheader(inputname, 0)
		inframe = hdulist[0].data
		reg = open(region,'r')
		paths=[]
		
		# Loop over all regions 
		for line in reg:
		
			# Detect circle regions and fill them with the mask value
			if 'circle(' in line:
				param = ((line.split('circle(')[1]).split(')')[0]).split(',')
				a, b ,r  = int(float(param[0])), int(float(param[1])), int(float(param[2])) 
				y,x = np.ogrid[-b:inframe.shape[0]-b, -a:inframe.shape[1]-a]
				mask = x*x + y*y <= r*r
				inframe[mask] = value
				
			# Detect polygon regions and add them to the path list
			elif 'polygon(' in line:
				param = map(float, ((line.split('polygon(')[1]).split(')')[0]).split(','))
				param2 = [None]*(len(param)/2)
				for i in xrange(0,len(param2)):	param2[i] = (int(param[2*i]),int(param[2*i+1])) 
				param2.append(param2[0])
				codes = []
				codes.append(Path.MOVETO)
				for i in xrange(1,len(param2)-1): codes.append(Path.LINETO)
				codes.append(Path.CLOSEPOLY)
				path = Path(param2, codes)
				paths.append(path)
		
		# Loop over the image and all polygons and fill them with the mask value	
		nx, ny =inframe.shape[1], inframe.shape[0]
		for i, j, path in product(range(0,nx), range(0,ny), paths):
			inframe[j][i]=value if path.contains_point((i,j)) else inframe[j][i]
		
		# Normalise the frame and write it out
		total = inframe.sum()
		print "The total value of the frame is ", str(total)
		if norm:
			for i, j in product(range(0,nx), range(0,ny)):
				inframe[j][i]=inframe[j][i]/total
		hdu = pyfits.PrimaryHDU(inframe,primhdr)
		hdu.writeto(outputname,clobber=True)	
예제 #53
0
def inpolygon(x, y, xp, yp):
    '''
  Points inside polygon test.

  Based on matplotlib nxutils for old matplotlib versions
  http://matplotlib.sourceforge.net/faq/howto_faq.html#test-whether-a-point-is-inside-a-polygon

  Can also use Path.contains_point, for recent versions, or
  the pnpoly extension - point in polyon test - by W R Franklin,
  http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
  '''

    try:
        from matplotlib.nxutils import pnpoly, points_inside_poly
        _use_mpl = 1
    except ImportError:
        from matplotlib.path import Path
        _use_mpl = 2
    except:
        import pnpoly
        _use_mpl = False

    shape = False
    try:
        if x.ndim > 1:
            shape = x.shape
            x = x.flat
            y = y.flat
    except:
        pass

    if _use_mpl == 1:
        verts = np.array(zip(xp, yp))
        if isiterable(x, y):
            points = np.array(zip(x, y))
            res = points_inside_poly(points, verts)
        else:
            res = pnpoly(x, y, verts) == 1

    elif _use_mpl == 2:
        verts = np.array(zip(xp, yp))
        p = Path(verts)
        if not isiterable(x, y): x, y, itr = [x], [y], 0
        else: itr = 1
        res = [
            p.contains_point((x[i], y[i]), radius=0.) for i in range(len(x))
        ]
        res = np.asarray(res, 'bool')
        if not itr: res = res[0]

    else:
        if not isiterable(x, y): x, y, itr = [x], [y], 0
        else: itr = 1
        res = np.zeros(len(x))
        res = [
            pnpoly.pnpoly(x[i], y[i], xp, yp, len(xp)) for i in range(len(x))
        ]
        res = np.asarray(res) == 1
        if not itr: res = res[0]

    if not shape is False: res.shape = shape
    return res
예제 #54
0
def block_mask(easts, norths, sources, east_ref, north_ref):
    """
    Determine stable and moving observation points dependend on the input
    fault orientation.

    Parameters
    ----------
    easts : :class:`numpy.ndarray`
        east - local coordinates [m] of observations
    norths : :class:`numpy.ndarray`
        north - local coordinates [m] of observations
    sources : list
        of :class:`RectangularSource`
    east_ref : float
        east local coordinate [m] of stable reference
    north_ref : float
        north local coordinate [m] of stable reference

    Returns
    -------
    :class:`numpy.ndarray` with zeros at stable points, ones at moving points
    """
    def get_vertex(outlines, i, j):
        f1 = outlines[i]
        f2 = outlines[j]
        print(f1, f2)
        return utility.line_intersect(f1[0, :], f1[1, :], f2[0, :], f2[1, :])

    tol = 2. * km

    Eline = RS(east_shift=easts.max() + tol,
               north_shift=0.,
               strike=0.,
               dip=90.,
               length=1 * km)
    Nline = RS(east_shift=0.,
               north_shift=norths.max() + tol,
               strike=90,
               dip=90.,
               length=1 * km)
    Sline = RS(east_shift=0.,
               north_shift=norths.min() - tol,
               strike=90,
               dip=90.,
               length=1 * km)

    frame = [Nline, Eline, Sline]

    # collect frame lines
    outlines = []
    for source in sources + frame:
        outline = source.outline(cs='xy')
        outlines.append(utility.swap_columns(outline, 0, 1)[0:2, :])

    # get polygon vertices
    poly_vertices = []
    for i in range(len(outlines) - 1):
        poly_vertices.append(get_vertex(outlines, i, i + 1))
    else:
        poly_vertices.append(get_vertex(outlines, 0, -1))

    print(poly_vertices, outlines)
    polygon = Path(num.vstack(poly_vertices), closed=True)

    ens = num.vstack([easts.flatten(), norths.flatten()]).T
    ref_en = num.array([east_ref, north_ref]).flatten()
    print(ens)
    mask = polygon.contains_points(ens)

    if not polygon.contains_point(ref_en):
        return mask

    else:
        return num.logical_not(mask)
예제 #55
0
        sq['ll'] = [(i0, j0), (i0 + 1, j0), (i0 + 1, j0 + 1), (i0, j0 + 1)]
        sq['lr'] = [(i0 - 1, j0), (i0, j0), (i0, j0 + 1), (i0 - 1, j0 + 1)]
        sq['ur'] = [(i0 - 1, j0 - 1), (i0, j0 - 1), (i0, j0), (i0 - 1, j0)]
        sq['ul'] = [(i0, j0 - 1), (i0 + 1, j0 - 1), (i0 + 1, j0), (i0, j0)]

        # loop over 4 adjacent grid cells:
        for corner in sq:
            path = sq[corner]
            # EvdP BUG!? llpath = Path([(lats[i],lons[i]) for i in path])
            llpath = Path([(self.lats[i], self.lons[i]) for i in path])
            if self.verbose:
                print 'For point ', xlat, xlon, 'from nn ', i0, j0
                print 'Corner: ', corner, path, '\n', llpath

            # if the path contains the point of interest, return the indices:
            if llpath.contains_point((xlat, xlon)):
                if self.verbose:
                    print 'Succes! ', corner, path, llpath
                    # for ll in list(llpath):
                    #     print 'So backsubstitute in lats,lons ',\
                    #           ll,lats[ll],lons[ll]
                return path

        print 10 * '*', 'No path found!', xlat, xlon
        return None
        #  #]
    def interpolate(self, values, latt, lont):
        #  #[ do the actual interpolation
        # EvdP BUG!? ind = lli.find_four_neighbours((latt,lont)) #,verb=True)
        ind = self.find_four_neighbours((latt, lont))  #,verb=True)
        if ind is not None:
예제 #56
0
파일: tmsim.py 프로젝트: sambarluc/tracmass
    def ijklims(self, ii=_empty, jj=_empty, kk=_empty,
                inds=True, geometry="curvilinear"):
        """
        Define the ii, jj, kk indices from which particles will be
        released.
        ii, jj, kk: list, array, or scalar with indices (i, j, k) from which
                    particles will be released (must be integers).
        inds:       if True (default), ii, jj and kk are interpreted
                    as indices, otherwise they are interpreted as lists of
                    exact release positions
        geometry:   only used when inds=False, it describes the MITgcm
                    grid geometry. At the moment, only "curvilinear" and
                    "cartesian" have been implemented.
        """
        self.seed_inds = inds

        if inds:
            if not np.issubdtype(self.ii.dtype, np.integer):
                raise TypeError("Indices must be integers (i-indices).")
            self.ii = np.atleast_1d(np.squeeze(ii))
            if not np.issubdtype(self.jj.dtype, np.integer):
                raise TypeError("Indices must be integers (j-indices).")
            self.jj = np.atleast_1d(np.squeeze(jj))
            if not np.issubdtype(self.kk.dtype, np.integer):
                raise TypeError("Indices must be integers (k-indices).")
            self.kk = np.atleast_1d(np.squeeze(kk))
        else:
            # if MITgcm coordinates are passed, we have to load the model grid
            # and then translate into the "normalised" index coordinates of
            # tracmass
            from . import _get_geometry, _xy2grid
            from matplotlib.path import Path
            from itertools import product

            ii = np.atleast_1d(np.squeeze(ii))
            jj = np.atleast_1d(np.squeeze(jj))
            kk = np.atleast_1d(np.squeeze(kk))

            if (ii.size != jj.size) or (ii.size != kk.size):
                raise ValueError("If inds=False, ii, jj and kk must have "
                                 "all the same dimension.")
            
            grid = mitgcmds(self.mitgcmdir, read_grid=True,
                            iters=[], prefix=["UVEL"], swap_dims=False,
                            geometry=geometry)
            xG, yG = _get_geometry(grid, geometry)
            dX = grid.dxG
            dY = grid.dyG
            cs = grid.CS
            sn = grid.SN
            dZ = (grid.drF * grid.hFacC).to_masked_array()
            zG = np.zeros((dZ.shape[0] + 1, dZ.shape[1], dZ.shape[2]))
            zG[1:, ...] = np.cumsum(dZ, axis=0).filled(0)
            # tracmass has opposite Z order
            zG = zG[::-1, ...]
            self.ii = np.zeros(ii.size) * np.nan
            self.jj = np.zeros(ii.size) * np.nan
            self.kk = np.zeros(ii.size) * np.nan
            trials = ([-1, -1],
                      [-1,  0],
                      [-1,  1],
                      [ 0, -1],
                      [ 0,  1],
                      [ 1, -1],
                      [ 1,  0],
                      [ 1,  1])
            for nn, (xx, yy, zz) in enumerate(zip(ii, jj, kk)):
                for jj, ii in product(range(xG.shape[0]-1), range(xG.shape[1]-1)):
                    bbPath = Path([[xG[jj, ii], yG[jj, ii]],
                                   [xG[jj, ii+1], yG[jj, ii+1]],
                                   [xG[jj+1, ii+1], yG[jj+1, ii+1]],
                                   [xG[jj+1, ii], yG[jj+1, ii]]])
                    if bbPath.contains_point((xx, yy)):
                        nx, ny = _xy2grid(xx - xG[jj, ii], yy - yG[jj, ii],
                                          dX[jj, ii], dY[jj, ii],
                                          cs[jj, ii], sn[jj, ii])
                        # since some corner points are approximate, we must
                        # check the computed nx and ny, and in case seek in
                        # nearby cell
                        if (nx < 0) or (nx > 1) or (ny < 0) or (ny > 1):
                            for ijtry in trials:
                                ii = ii - ijtry[0]
                                jj = jj - ijtry[1]
                                nx, ny = _xy2grid(xx - xG[jj, ii], yy - yG[jj, ii],
                                                  dX[jj, ii], dY[jj, ii],
                                                  cs[jj, ii], sn[jj, ii])
                                if (nx >= 0) and (nx < 1) and (ny >=0) and (ny < 1):
                                    break
                            else:
                                raise ValueError("Could not find the point (x=%f, y=%f)" 
                                                 % xx, yy)
                        z_here = zG[:, jj, ii]
                        if (zz > z_here.max()) or (zz <= z_here.min()):
                            print("Point outside vertical bounds at x,y,z=%.2f,%.2f,%.2f" %
                                  (xx, yy, zz))
                            break
                        kk = np.where(z_here > zz)[0][-1]
                        nz = (zz - z_here[kk]) / (z_here[kk+1] - z_here[kk])
                        self.ii[nn] = ii + nx
                        self.jj[nn] = jj + ny
                        self.kk[nn] = kk + nz
            self.ii = self.ii[np.isfinite(self.ii)]
            self.jj = self.jj[np.isfinite(self.jj)]
            self.kk = self.kk[np.isfinite(self.kk)]
def method3(shears, allDataFile):
    shear1 = np.asarray(shears[0])
    shear2 = np.asarray(shears[1])

    myShears = []
    myShears.extend(shear1)
    myShears.extend(list(reversed(shear2)))
    myShears.append(shear1[0])
    myShears = np.asarray(myShears)

    myPath = Path(myShears, closed=True)

    maxY, minY = max(myShears[:, 1]), min(myShears[:, 1])
    minY = minY - 150  # buffer
    maxY = maxY + 150  # buffer

    maxX, minX = max(myShears[:, 0]), min(myShears[:, 0])
    maxX = maxX + 150
    minX = minX - 150

    xs = allDataFile['x'][:]
    ys = allDataFile['y'][:]

    thickness = allDataFile['thickness'][:]
    vx = allDataFile['VX'][:]
    vy = allDataFile['VY'][:]

    allDataFile.close()

    xx, yy = np.meshgrid(xs, ys)
    XY = np.dstack((xx, yy))
    XYFlat = XY.reshape(-1, 2)

    thicknessFlat = thickness.reshape(-1)
    vxFlat = vx.reshape(-1)
    vyFlat = vy.reshape(-1)

    dataMaxY = XYFlat[0][1]
    dataMinY = XYFlat[-1][1]

    YToCutMax = abs(dataMaxY - maxY) / 150
    YToCutMin = abs(dataMinY - minY) / 150

    numX = 10018
    samplePoints = XYFlat[int(math.floor(YToCutMax) *
                              numX):-(int(math.floor(YToCutMin) * numX))]
    thicknessFlat = thicknessFlat[int(math.floor(YToCutMax) * numX):-(
        int(math.floor(YToCutMin) * numX))]
    vxFlat = vxFlat[int(math.floor(YToCutMax) *
                        numX):-(int(math.floor(YToCutMin) * numX))]
    vyFlat = vyFlat[int(math.floor(YToCutMax) *
                        numX):-(int(math.floor(YToCutMin) * numX))]

    newSamplePoints = []
    newThickness = []
    newVX = []
    newVY = []
    for i in range(numX):
        newSamplePoints.extend(samplePoints[i::numX])
        newThickness.extend(thicknessFlat[i::numX])
        newVX.extend(vxFlat[i::numX])
        newVY.extend(vyFlat[i::numX])

    newSamplePoints = np.asarray(newSamplePoints)
    newThickness = np.asarray(newThickness)
    newVX = np.asarray(newVX)
    newVY = np.asarray(newVY)

    dataMaxX = newSamplePoints[-1][0]
    dataMinX = newSamplePoints[0][0]

    XToCutMax = int(math.floor(abs(dataMaxX - maxX) / 150))
    XToCutMin = int(math.floor(abs(dataMinX - minX) / 150))

    newMaxY, newMinY = newSamplePoints[0][1], newSamplePoints[-1][1]

    numY = int(abs(newMaxY - newMinY) / 150) + 1

    finalSamplePoints = newSamplePoints[XToCutMin * numY:-(XToCutMax * numY)]
    finalThicknessPoints = newThickness[XToCutMin * numY:-(XToCutMax * numY)]
    finalVX = newVX[XToCutMin * numY:-(XToCutMax * numY)]
    finalVY = newVY[XToCutMin * numY:-(XToCutMax * numY)]

    thicknessValuesInShape = []
    velocityValuesInShape = []

    pointsInShape = []

    time0 = time.time()
    for i in range(len(finalSamplePoints)):

        point = [int(finalSamplePoints[i][0]), int(finalSamplePoints[i][1])]

        if myPath.contains_point(point):
            thicknessValuesInShape.append(finalThicknessPoints[i])
            velocityValuesInShape.append(sqrt(finalVX[i]**2 + finalVY[i]**2))
            pointsInShape.append(point)

    trueVolume = sum(thicknessValuesInShape) * 150**2
    trueFlux = sum(velocityValuesInShape) * 150**2
    return trueVolume, trueFlux
예제 #58
0
def matchingFootSideOnForceplate(btkAcq,
                                 enableRefine=True,
                                 forceThreshold=50,
                                 left_markerLabelToe="LTOE",
                                 left_markerLabelHeel="LHEE",
                                 right_markerLabelToe="RTOE",
                                 right_markerLabelHeel="RHEE",
                                 display=False,
                                 mfpa=None):
    """
        Convenient function detecting foot in contact with a force plate

        **synopsis**

        This function firsly assign foot side to FP from minimal distance with the application point of reaction force.
        A refinement is done subsequently, it confirm if foot side is valid. A foot is invalided if :

         - FP output no data superior to the set threshold
         - Foot markers are not contain in the polygon defined by force plate corner

        :Parameters:
           - `btkAcq` (btkAcquisition) - Btk acquisition instance from a c3d
           - `left_markerLabelToe` (str) - label of the left toe marker
           - `left_markerLabelHeel` (str) - label of the left heel marker
           - `right_markerLabelToe` (str) - label of the right toe marker
           - `right_markerLabelHeel` (str) - label of the right heel marker
           - `display` (bool) - display n figures ( n depend on force plate number) presenting relative distance between mid foot and the orgin of the force plate
           - `mfpa` (string or dict) - manual force plate assigmenment from another method. Can be a string (XLRA, A stand for automatic) or a dict returing assigned foot to a Force plate ID.

    """

    appendForcePlateCornerAsMarker(btkAcq)

    ff = btkAcq.GetFirstFrame()
    lf = btkAcq.GetLastFrame()
    appf = btkAcq.GetNumberAnalogSamplePerFrame()

    # --- ground reaction force wrench ---
    pfe = btk.btkForcePlatformsExtractor()
    grwf = btk.btkGroundReactionWrenchFilter()
    pfe.SetInput(btkAcq)
    pfc = pfe.GetOutput()
    grwf.SetInput(pfc)
    grwc = grwf.GetOutput()
    grwc.Update()

    midfoot_L = (btkAcq.GetPoint(left_markerLabelToe).GetValues() +
                 btkAcq.GetPoint(left_markerLabelHeel).GetValues()) / 2.0
    midfoot_R = (btkAcq.GetPoint(right_markerLabelToe).GetValues() +
                 btkAcq.GetPoint(right_markerLabelHeel).GetValues()) / 2.0

    suffix = str()

    if mfpa is not None:
        try:
            pfIDS = []
            for i in range(0, pfc.GetItemNumber()):
                pfIDS.append(
                    re.findall(
                        "\[(.*?)\]",
                        pfc.GetItem(i).GetChannel(0).GetDescription())[0])
        except Exception:
            logging.info("[pyCGM2]: Id of Force plate not detected")
            pass

    for i in range(0, grwc.GetItemNumber()):
        pos = grwc.GetItem(i).GetPosition().GetValues()
        pos_downsample = pos[0:(lf - ff + 1) * appf:appf]  # downsample

        diffL = np.linalg.norm(midfoot_L - pos_downsample, axis=1)
        diffR = np.linalg.norm(midfoot_R - pos_downsample, axis=1)

        if display:
            plt.figure()
            ax = plt.subplot(1, 1, 1)
            plt.title("Force plate " + str(i + 1))
            ax.plot(diffL, '-r')
            ax.plot(diffR, '-b')

        if np.min(diffL) < np.min(diffR):
            logging.debug(" Force plate " + str(i) + " : left foot")
            suffix = suffix + "L"
        else:
            logging.debug(" Force plate " + str(i) + " : right foot")
            suffix = suffix + "R"

    logging.debug("Matched Force plate ===> %s", (suffix))

    if enableRefine:
        # refinement of suffix
        indexFP = 0

        for letter in suffix:

            force = grwc.GetItem(indexFP).GetForce().GetValues()
            force_downsample = force[0:(lf - ff + 1) * appf:appf]  # downsample

            Rz = np.abs(force_downsample[:, 2])

            boolLst = Rz > forceThreshold

            enableDataFlag = False
            for it in boolLst.tolist():
                if it == True:
                    enableDataFlag = True

                    break

            if not enableDataFlag:
                logging.debug(
                    "PF #%s not activated. It provides no data superior to threshold"
                    % (str(indexFP)))
                li = list(suffix)
                li[indexFP] = "X"
                suffix = "".join(li)

            else:

                if letter == "L":
                    hee = btkAcq.GetPoint(left_markerLabelHeel).GetValues()
                    toe = btkAcq.GetPoint(left_markerLabelToe).GetValues()
                elif letter == "R":
                    hee = btkAcq.GetPoint(right_markerLabelHeel).GetValues()
                    toe = btkAcq.GetPoint(right_markerLabelToe).GetValues()

                # polygon builder
                corner0 = btkAcq.GetPoint("fp" + str(indexFP) +
                                          "corner0").GetValues()[0, :]
                corner1 = btkAcq.GetPoint("fp" + str(indexFP) +
                                          "corner1").GetValues()[0, :]
                corner2 = btkAcq.GetPoint("fp" + str(indexFP) +
                                          "corner2").GetValues()[0, :]
                corner3 = btkAcq.GetPoint("fp" + str(indexFP) +
                                          "corner3").GetValues()[0, :]

                verts = [
                    corner0[0:2],  # left, bottom
                    corner1[0:2],  # left, top
                    corner2[0:2],  # right, top
                    corner3[0:2],  # right, bottom
                    corner0[0:2],  # ignored
                ]

                codes = [
                    Path.MOVETO,
                    Path.LINETO,
                    Path.LINETO,
                    Path.LINETO,
                    Path.CLOSEPOLY,
                ]

                path = Path(verts, codes)

                # check if contain both toe and hee marker
                containFlags = list()
                for i in range(0, Rz.shape[0]):
                    if boolLst[i]:
                        if path.contains_point(
                                hee[i, 0:2]) and path.contains_point(toe[i,
                                                                         0:2]):
                            containFlags.append(True)
                        else:
                            containFlags.append(False)

                if not all(containFlags) == True:
                    logging.debug(
                        "PF #%s not activated. While Rz superior to threshold, foot markers are not contained in force plate geometry  "
                        % (str(indexFP)))
                    # replace only one character
                    li = list(suffix)
                    li[indexFP] = "X"
                    suffix = "".join(li)

            indexFP += 1

        # correction with manual assignement
        if mfpa is not None:
            correctedSuffix = ""
            if type(mfpa) == dict:
                logging.warning(
                    "[pyCGM2] : automatic force plate assigment corrected with context associated with the device Id  "
                )
                i = 0
                for id in pfIDS:
                    fpa = mfpa[id]
                    if fpa != "A":
                        correctedSuffix = correctedSuffix + fpa
                    else:
                        correctedSuffix = correctedSuffix + suffix[i]
                    i += 1
            else:
                logging.warning(
                    "[pyCGM2] : automatic force plate assigment corrected  ")
                if len(mfpa) < len(suffix):
                    raise Exception(
                        "[pyCGM2] number of assigned force plate inferior to the number of force plate number. Your assignment should have  %s letters at least"
                        % (str(len(suffix))))
                else:
                    if len(mfpa) > len(suffix):
                        logging.warning(
                            "[pyCGM2]: Your manual force plate assignement mentions more force plates than the number of force plates stored in the c3d"
                        )
                    for i in range(0, len(suffix)):
                        if mfpa[i] != "A":
                            correctedSuffix = correctedSuffix + mfpa[i]
                        else:
                            correctedSuffix = correctedSuffix + suffix[i]
            return correctedSuffix
        else:
            return suffix
예제 #59
0
def filter_alert(params, grid):

    #read shapefile
    #For cylindrical equidistant projection (cyl), this does nothing (i.e. x,y == lon,lat).
    #Therefore the converting from Geographic (lon/lat) to Map Projection (x/y) Coordinates is not necessary here.
    map = Basemap(llcrnrlon=110,
                  llcrnrlat=-90,
                  urcrnrlon=290,
                  urcrnrlat=90,
                  resolution='c',
                  projection='cyl')

    if params['variable'] == 'outlook':
        crw = map.readshapefile(
            util.get_resource('maps', 'layers', 'CRW_Outlook_EEZ'), 'crw')
    elif params['variable'] == 'daily':
        crw = map.readshapefile(
            util.get_resource('maps', 'layers', 'CRW_Outlines'), 'crw')

    collection = []
    max = None

    #    shape_area = country[params['area']]
    shape_area = country[params['area'].lower()]
    for info, shape in zip(map.crw_info, map.crw):
        if info['ID'] == shape_area or info['SUBREGION'] == shape_area:
            collection.append(np.array(shape))

    for polygon in collection:
        path = Path(polygon)
        poly_lons = polygon.T[0]
        poly_lats = polygon.T[1]

        lon_min = np.min(poly_lons)
        lon_max = np.max(poly_lons)
        lat_min = np.min(poly_lats)
        lat_max = np.max(poly_lats)

        lons, lats, data = grid.lons, grid.lats, grid.data
        if lats[0] > lats[-1]:
            flippedlats = np.flipud(lats)
            start_lat = bisect.bisect_left(flippedlats, lat_min)
            end_lat = bisect.bisect_right(flippedlats, lat_max)
            start_latr = start_lat
            end_latr = end_lat
            start_lat = lats.size - end_latr
            end_lat = lats.size - start_latr
        else:
            start_lat = bisect.bisect_left(lats, lat_min)
            end_lat = bisect.bisect_right(lats, lat_max)

        start_lon = bisect.bisect_left(lons, lon_min)
        end_lon = bisect.bisect_right(lons, lon_max)

        lat_clip = lats[start_lat:end_lat]
        lon_clip = lons[start_lon:end_lon]

        x, y = np.meshgrid(lon_clip, lat_clip)
        shape_2d = x.shape

        x_flat = x.flatten()
        y_flat = y.flatten()
        points = zip(x_flat, y_flat)

        mask = [path.contains_point(point) for point in points]
        mask_array = np.array(mask)
        mask_array = mask_array.reshape(shape_2d)
        mask_array_logical_not = np.logical_not(mask_array)

        data_clip = data[start_lat:end_lat, start_lon:end_lon]
        new_mask = np.ma.mask_or(data_clip.mask, mask_array_logical_not)

        data_clip.mask = new_mask
        local_max = np.max(data_clip)
        if local_max > max:
            max = local_max

    return max
예제 #60
0
    # Create Path from boundaries
    verts = []
    print len(x)
    for ind in range(len(x)):
        verts.append((x[ind], y[ind]))
    path = Path(verts)

    # Assign value to grid point inside the boundaries
    grid_num = 0
    for i in range(nlat):
        for j in range(nlon):

            latn = 89 - i * 1
            lonn = 0 + j * 1

            if path.contains_point((lonn, latn)):
                cn_province_map[i][j] = pid
                grid_num += 1
                if pid == 2:
                    print str(latn) + "\t" + str(lonn)

    if not (province == "../heb1_out.txt" or province == "../heb2_out.txt"):

        # print province namelist
        #print province.replace("../","").replace("_out.txt","")
        #flist.write(str(pid) + "\t" + province.replace("../","").replace("_out.txt","") + "\t" + str(grid_num) + "\n")

        pid += 1

flist.close()