def kdconnect(root_nodes, trees=None, tol=0.75): from networkx.algorithms.components import connected_components import networkx as nx cnt1, cnt2 = 0, 0 # make trees if they were not added before if trees is None: trees = [] for node in root_nodes: tr = gp.KDTreeIndex(fwd=True, bkwd=True)(node) trees.append(tr) # get a node count for validation for node in root_nodes: cnt1 += len(list(node.__iter__(fwd=True, bkwd=True))) gg = nx.Graph() for i in range(len(trees)): ti = trees[i] for j in range(i+1, len(trees)): tj = trees[j] res_ij = ti.query_ball_tree(tj, tol) adj = [(di, x) for di, x in enumerate(res_ij) if len(x) > 0] if any(adj): # unique indicies from tree_i and tree_j un_tix = np.unique([i for i, j in adj]) un_tjx = np.unique([j for i, j in adj]) # closest points between unique positions dists = distance.cdist(ti.data[un_tix], tj.data[un_tjx]) armi = np.unravel_index(np.argmin(dists, axis=None), dists.shape) # retrieve node ids from respective trees, and connect node_id1 = ti[un_tix[armi[0]]] node_id2 = tj[un_tjx[armi[1]]] ndi = gutil.node_with_id(root_nodes[i], node_id1) ndj = gutil.node_with_id(root_nodes[j], node_id2) ndi.connect_to(ndj, is_pipe=True) # add root indexes to component graph gg.add_edge(i, j) final_roots = [] # gather new root nodes using connected_component algorithm for component in connected_components(gg): # pick a random index from the set ... ix = list(component)[0] a_root = root_nodes[ix] cnt2 += len(list(a_root.__iter__(fwd=True, bkwd=True))) final_roots.append(a_root) # sanity check - number of nodes should not have changed assert cnt1 == cnt2, 'unequal number of nodes before and after merge' return final_roots
def test_elbow_1(self): node_id = 9674342137 node = gutils.node_with_id(self.s.root, node_id) print(node.get('$create')) assert node is not None mgr = rvt.make_actions_for(node) print(mgr._state) a = mgr.next_action() print(a[0:3], '\n', mgr._state) assert isinstance(a, list), msg('list', a) assert a[0] == _Cmd_pipe, msg(_Cmd_pipe, a[0]) mgr.on_success(a) a = mgr.next_action() print(a[0:3], '\n', mgr._state) assert a[0] == _Cmd_elb, msg(_Cmd_elb, a[0]) mgr.on_success(a) print(mgr._state) a = mgr.next_action() print(a, '\n', mgr._state) assert a is None, msg(None, a) e = node.successors(edges=True, ix=0) self._edge_stat(e, True, True, not True)
def resolve_heads(root_node, spr_points, tol=1.): kdprop = gp.KDTreeIndex()(root_node) data = np.array(kdprop.data) for six, spr in enumerate(spr_points): cdist = distance.cdist([spr], data)[0] if np.min(cdist) < tol: best_ix = np.argmin(cdist) node = gutil.node_with_id(root_node, kdprop[best_ix]) connect_heads2(node) return kdprop
def test_elbow_fail(self): node_id = 9674342137 node = gutils.node_with_id(self.s.root, node_id) # print(node.get('$create')) assert node is not None mgr = rvt.make_actions_for(node) a = mgr.next_action() # print(a) assert a[0] == _Cmd_pipe, msg(_Cmd_pipe, a[0]) mgr.on_success(a) a = mgr.next_action() # print(a) assert a[0] == _Cmd_elb, msg(_Cmd_elb, a[0]) mgr.on_fail(a) a = mgr.next_action() self._edge_stat(node.successors(edges=True, ix=0), True, True, not True)
def test_elbow_f(self): node_id = 9674342137 node = gutils.node_with_id(self.s.root, node_id) res = [1, 0, 1, 1, 1] self._test_scenario(node, res)
def test_tee2(self): node_id = 5566475637 node = gutils.node_with_id(self.s.root, node_id) self._single_node_success(node)
def test_tee1(self): node_id = 390499279 node = gutils.node_with_id(self.s.root, node_id) assert node is not None self._test_node_success(node, _Cmd_tee)