def vertex_and_edge_lists_from_svg_elements(node_group, edge_group): """Read a graph data structure from a circular embedding. The graph must be a circular embedding, with all nodes lying on a circle, and all edges as chords of the circle. Args: node_group: An SVG group element containing circle elements, the center points of which must coincide with the end of the lines in the edge_group, in order to correlate these nodes with those edges. edge_group: An SVG group element containing line elements, the end points of which must coincide with the centres of the circles in the node_group, in order to correlate these edges with those nodes. Returns: A 2-tuple where the first element is a sequence of integer vertex labels, and the second is a sequence of 2-tuples representing undirected edges. The vertices will be ordered anticlockwise from the negative x axis. """ geometric_vertices = [] for i, node in enumerate(node_group): geometric_vertices.append( Point2(x=float(node.attrib['cx']), y=float(node.attrib['cy']))) center = Point2(x=mean(v.x for v in geometric_vertices), y=mean(v.y for v in geometric_vertices)) geometric_vertices.sort(key=lambda v: (v - center).atan2()) geometric_edges = [] for edge in edge_group: source_vertex = Point2(x=float(edge.attrib['x1']), y=float(edge.attrib['y1'])) target_vertex = Point2(x=float(edge.attrib['x2']), y=float(edge.attrib['y2'])) edge = (source_vertex, target_vertex) geometric_edges.append(edge) vertices = range(len(geometric_vertices)) edges = [(geometric_vertices.index(from_vertex), geometric_vertices.index(to_vertex)) for from_vertex, to_vertex in geometric_edges] return vertices, edges
def find_mount_right_boundary(gray_image, threshold=40): # Find the top edge by walking down on each row until we pass the threshhold gray_pixels = gray_image.load() mount_boundary_points = [] for y in range(gray_image.height): for x in reversed(range(gray_image.width)): value = gray_pixels[x, y] if value >= threshold: mount_boundary_points.append((x, y)) gray_pixels[x, y] = 255 break mount_boundary_points_x = numpy.fromiter((p[0] for p in mount_boundary_points), float) mount_boundary_points_y = numpy.fromiter((p[1] for p in mount_boundary_points), float).reshape(-1, 1) model_ransac = linear_model.RANSACRegressor() model_ransac.fit(mount_boundary_points_y, mount_boundary_points_x) start = Point2(model_ransac.predict(0)[0], 0) end = Point2(model_ransac.predict(gray_image.height - 1)[0], gray_image.height) return start, end
def from_config(cls, config): try: position = config["position"] left = position["left"] right = position["right"] depth = position["depth"] crs = config.get("coordinate-reference-system", {}) return cls(left_xy=Point2(left["x"], float(left["y"])), right_xy=Point2(float(right["x"]), float(right["y"])), top_z=float(depth["top"]), bottom_z=float(depth["bottom"]), coordinate_reference_system=CoordinateReferenceSystem( map_projection=str(crs.get("map-projection", "")), horizontal_units=str(crs.get( "horizontal-units", "")), vertical_units=str(crs.get("vertical-units", "")), zone_id=str(crs.get("zone-id", "")), )) except (KeyError, ValueError) as e: raise ConfigurationError(str(e)) from e
def find_peak_base(sequence, maxima_index, minima_index): """Find the index of the base of the peak. Args: sequence: A sequence of data in which to locate the peak base. maxima_index: The index of an item in sequence on or near the top of the peak. minima_index: The index of an item in sequence off or away from the peak. Returns: The index of the estimated base of the peak. """ p = Point2(maxima_index, sequence[maxima_index]) q = Point2(minima_index, sequence[minima_index]) basis_line = Line2.through_points(p, q) query_points = query( Point2(index, sequence[index]) for index in range(minima_index, maxima_index)) base_point = query_points \ .select(basis_line.distance_to) \ .max_index(start=minima_index) return base_point.index