def centroid_mst_costs(G): root = G.graph['root'] centroidp = centroid(G) root_cdist = point_dist(root, centroidp) mcost = root_cdist scost = 0 for point in points: if point != root: cdist = point_dist(point, centroidp) mcost += cdist scost += cdist + root_cdist return mcost, scost
def remove_close_points(point, unmarked_points, remove_radius=1): close_points = [] for unmarked_point in unmarked_points: if point_dist(point, unmarked_point) <= remove_radius: close_points.append(unmarked_point) for close_point in close_points: unmarked_points.remove(close_point)
def can_extend(G, unmarked_points, trial_length, r_puncta): for u in G.nodes(): coord1 = G.node[u]['coord'] for coord2 in unmarked_points: if point_dist(coord1, coord2) <= trial_length + r_puncta: return True return False
def complete_graph(G): H = G.copy() H.remove_edges_from(H.edges()) for u, v in combinations(H.nodes(), 2): H.add_edge(u, v) H[u][v]['length'] = point_dist(H.node[u]['coord'], H.node[v]['coord']) return H
def points_to_graph(points): point_graph = nx.Graph() for p1, p2 in combinations(points, 2): point_graph.add_edge(p1, p2) length = point_dist(p1, p2) point_graph[p1][p2]['length'] = length return point_graph
def annihilate(G, u, detection_radius): coord1 = G.node[u]['coord'] for v in G.nodes_iter(): if v == u: continue coord2 = G.node[v]['coord'] if point_dist(coord1, coord2) <= detection_radius: return True return False
def best_satellite_cost(G): best_cost = 0 root = G.graph['root'] root_coord = G.node[root]['coord'] costs = [] for u in G.nodes(): if u == root: continue cost = point_dist(root_coord, G.node[u]['coord']) #best_cost += cost costs.append(cost) #return best_cost return sum(sorted(costs))
def connect_to_synapses(G, u, unmarked_points, puncta_radius=1, remove_radius=1): coord = G.node[u]['coord'] added_points = [] for point in unmarked_points: if point_dist(coord, point) <= puncta_radius: added_points.append(point) if len(added_points) == 0: return None new_point = choice(added_points) remove_close_points(new_point, unmarked_points, remove_radius=remove_radius) new_node = add_new_node(G, new_point, u, synapse=True) return new_node
def random_point_graph(num_points=10, xmin=-10, xmax=10,\ ymin=-10, ymax=10,\ zmin=-10, zmax=10): points = random_points(num_points, xmin, xmax, ymin, ymax, zmin, zmax) G = nx.Graph() for i, point in enumerate(points): G.add_node(i) G.node[i]['coord'] = point G.graph['root'] = choice(list(G.nodes())) for u, v in combinations(G.nodes(), 2): G.add_edge(u, v) G[u][v]['length'] = point_dist(points[u], points[v]) return G
def dist_error(G): assert 'root' in G.graph root = G.graph['root'] assert 'coord' in G.node[root] root_coord = G.node[root]['coord'] error = 0 for u in G.nodes(): assert 'droot' in G.node[u] droot = G.node[u]['droot'] assert 'coord' in G.node[u] coord = G.node[u]['coord'] min_dist = point_dist(coord, root_coord) if droot < min_dist: error += min_dist - droot return error
def scost_error(G): scost = 0 scost_error = 0 droot = {} assert 'root' in G.graph root = G.graph['root'] droot[root] = 0 assert 'coord' in G.node[root] root_coord = G.node[root]['coord'] parent = {} parent[root] = None queue = [root] visited = set() while len(queue) > 0: curr = queue.pop(0) if curr in visited: print "not a tree; graph has cycles" assert False visited.add(curr) for child in G.neighbors(curr): if child != parent[curr]: assert 'length' in G[curr][child] length = G[curr][child]['length'] child_droot = length + droot[curr] scost += child_droot assert 'coord' in G.node[child] coord = G.node[child]['coord'] min_dist = point_dist(root_coord, coord) if child_droot < min_dist: error = min_dist - child_droot scost_error += error droot[child] = child_droot parent[child] = curr queue.append(child) if len(visited) < G.number_of_nodes(): scost = float("inf") # graph is not connected return scost, scost_error
def check_dists(G): assert 'root' in G.graph root = G.graph['root'] assert 'coord' in G.node[root] root_coord = G.node[root]['coord'] for u in G.nodes(): assert 'droot' in G.node[u] droot = G.node[u]['droot'] assert 'coord' in G.node[u] coord = G.node[u]['coord'] min_dist = point_dist(coord, root_coord) if droot < min_dist: print "bad node", u print "droot", droot, "min_dist", min_dist print type(droot), type(min_dist) print droot - min_dist assert droot >= min_dist
def init_lengths(G): for u, v in G.edges_iter(): p1, p2 = G.node[u]['coord'], G.node[v]['coord'] G[u][v]['length'] = point_dist(p1, p2)