Ejemplo n.º 1
0
def is_continuation(gold_node, bin_test_list, add_to_list=True, DEBUG=False):
    if not gold_node.has_children():
        return False

    ancestor_node_matches = []
    gold_path_length = SwcNode()

    # find the first matched ancestor of the gold_node
    ancestor_match = None
    ancestor_gold = gold_node.parent
    gold_path_length.path_length = gold_node.data.path_length
    gold_path_length.xy_path_length = gold_node.data.xy_path_length
    gold_path_length.z_path_length = gold_node.data.z_path_length
    is_match = False
    ancestor_match = False
    is_left = False

    if DEBUG:
        print(
            "gold_path_length: path_length {}, xy_path_length {}, z_path_length {}"
            .format(gold_path_length.path_length,
                    gold_path_length.xy_path_length,
                    gold_path_length.z_path_length))
    while ancestor_gold is not None and not ancestor_match:
        if ancestor_gold in g_matches.keys():
            ancestor_match = g_matches[ancestor_gold]
            if is_sub_continuation(gold_node, ancestor_gold, ancestor_match,
                                   is_left, gold_path_length, bin_test_list,
                                   add_to_list):
                return True
            ancestor_match = True
        ancestor_node_matches = get_nearby_node_list(ancestor_gold,
                                                     bin_test_list,
                                                     check_previous_use=True,
                                                     g_matches=g_matches)
        for a_n_node in ancestor_node_matches:
            if is_sub_continuation(gold_node, ancestor_gold, ancestor_match,
                                   is_left, gold_path_length, bin_test_list,
                                   add_to_list):
                return True

        gold_path_length.path_length += ancestor_gold.data.path_length
        gold_path_length.xy_path_length += ancestor_gold.data.xy_path_length
        gold_path_length.z_path_length += ancestor_gold.data.z_path_length
        is_left = ancestor_gold.is_left()
        ancestor_gold = ancestor_gold.parent

    # failed
    return False
Ejemplo n.º 2
0
    def test_rtree(self):
        swctree = SwcTree()
        swctree.load(
            "D:\gitProject\mine\PyNeval\\test\data_example\\test\\30_18_10_test.swc"
        )
        rtree = get_edge_rtree(swctree)
        id_edge_dict = get_idedge_dict(swctree)
        point = SwcNode(center=[22.5822, 172.856, 300.413])
        (line_tuple, dis) = get_nearby_edges(rtree, point, id_edge_dict,
                                             point.radius())[0]

        print(
            point.distance(
                Line(coords=[[15.3249, 130.821, 327.012],
                             [19.8495, 132.384, 323.395]])))
        print("dis = {}, line:{}, {}".format(dis, line_tuple[0]._pos,
                                             line_tuple[1]._pos))
Ejemplo n.º 3
0
def swc_get_foot(swc_node, swc_line_tuple):
    e_node = swc_node.get_center()
    e_line = Line(e_node_1=swc_line_tuple[0].get_center(),
                  e_node_2=swc_line_tuple[1].get_center())

    e_foot = e_node.get_closest_point(e_line)
    swc_foot = SwcNode(center=e_foot, radius=1.0, ntype=0)

    return swc_foot
Ejemplo n.º 4
0
def reconstruct_tree(swc_tree, is_activate, down_pa):
    new_swc_tree = SwcTree()
    node_list = [node for node in PreOrderIter(swc_tree.root())]
    id_node_map = {-1: new_swc_tree.root()}

    for node in node_list:
        if node.is_virtual():
            continue
        if is_activate[node.get_id()]:
            tmp_node = SwcNode()
            tmp_node._id = node.get_id()
            tmp_node._type = node._type
            tmp_node._pos = copy.copy(node._pos)
            tmp_node._radius = node._radius
            pa = id_node_map[down_pa[node].get_id()]
            tmp_node.parent = pa
            id_node_map[node.get_id()] = tmp_node
    return new_swc_tree
Ejemplo n.º 5
0
def adjust_vertical_tree(node, line_tuple_a, line_tuple_b, vertical_tree,
                         vertical_id):
    # adjust vertical tree
    swc_foot_a = swc_get_foot(node, line_tuple_a)
    swc_foot_b = swc_get_foot(node.parent, line_tuple_b)
    tmp_node = SwcNode(center=node._pos,
                       radius=node.radius() / 2,
                       ntype=node._type)
    tmp_node_p = SwcNode(center=node.parent._pos,
                         radius=node.parent.radius() / 2,
                         ntype=node.parent._type)

    tmp_node.set_id(vertical_id)
    vertical_id += 1
    vertical_tree.append(tmp_node.to_swc_str(-1))

    tmp_node_p.set_id(vertical_id)
    vertical_id += 1
    vertical_tree.append(tmp_node_p.to_swc_str(-1))

    swc_foot_a.set_id(vertical_id)
    vertical_id += 1
    vertical_tree.append(swc_foot_a.to_swc_str(tmp_node.get_id()))

    swc_foot_b.set_id(vertical_id)
    vertical_id += 1
    vertical_tree.append(swc_foot_b.to_swc_str(tmp_node_p.get_id()))
    return vertical_id
Ejemplo n.º 6
0
def get_des_in_for_continuation(first_node,
                                ancestor_node,
                                ancestor_match,
                                ancestor_trajectory,
                                path_length_map,
                                bin_test_list,
                                DEBUG=False):
    spe_ancestor_trajectory = ancestor_trajectory
    test_matches = []

    stack = queue.LifoQueue()
    stack.put(first_node)
    while not stack.empty():
        gold_node = stack.get()
        des_tra = gold_node.data.parent_trajectory
        # print("gold_node id = {}".format(gold_node.data.get_id()))
        if gold_node in g_matches:
            test_matches.append(g_matches[gold_node])
        else:
            test_matches = get_nearby_node_list(gold_node,
                                                bin_test_list,
                                                check_previous_use=False)

        prev_path_length = path_length_map[gold_node.parent]
        gold_path_length = SwcNode()
        gold_path_length.path_length = gold_node.data.path_length + prev_path_length.path_length
        gold_path_length.xy_path_length = gold_node.data.xy_path_length + prev_path_length.xy_path_length
        gold_path_length.z_path_length = gold_node.data.z_path_length + prev_path_length.z_path_length
        path_length_map[gold_node] = gold_path_length

        for child_match in test_matches:
            test_path_length = SwcNode()
            test_path_length.add_data(child_match.data)
            tmp_node = child_match.parent
            # print("child match id = {}".format(
            #     child_match.data.get_id()))
            done = (tmp_node == ancestor_match)
            while not done:
                if tmp_node in path_length_map.keys():
                    prev_path_length = path_length_map[tmp_node]
                    test_path_length.add_length(prev_path_length)
                    done = True
                else:
                    test_path_length.add_length(tmp_node.data)
                    tmp_node = tmp_node.parent
                    if tmp_node is None:
                        done = True

                if tmp_node == ancestor_match:
                    done = True

            if tmp_node is None:
                if DEBUG:
                    print("[info]: descendant not match")
            else:
                path_length_map[child_match] = test_path_length
                if ancestor_trajectory.get_x(
                ) == -1.0 or ancestor_trajectory.get_z() == -1.0:
                    spe_ancestor_trajectory = get_trajectory_for_path(
                        ancestor_node, gold_node)
                    if ancestor_trajectory.get_x != -1.0:
                        spe_ancestor_trajectory.set_x(
                            ancestor_trajectory.get_x())
                        spe_ancestor_trajectory.set_y(
                            ancestor_trajectory.get_y())
                    if ancestor_trajectory.get_z != -1.0:
                        spe_ancestor_trajectory.set_z(
                            ancestor_trajectory.get_z())

                test_xy_path_length = test_path_length.xy_path_length \
                                      + get_end_node_XY_dis_diff(ancestor_node.data, spe_ancestor_trajectory, ancestor_match.data) \
                                      + get_end_node_XY_dis_diff(gold_node.data, des_tra, child_match.data)
                test_z_path_length = test_path_length.z_path_length \
                                      + get_end_node_Z_dis_diff(ancestor_node.data, spe_ancestor_trajectory, ancestor_match.data) \
                                      + get_end_node_Z_dis_diff(gold_node.data, des_tra, child_match.data)

                if path_length_matches(gold_path_length, test_xy_path_length,
                                       test_z_path_length) < 1:
                    return child_match

        if gold_node.has_children() and gold_node not in g_matches:
            stack.put(gold_node.left_son)
            stack.put(gold_node.right_son)

    return None
Ejemplo n.º 7
0
def get_best_match(match_list, gold_node, bin_test_list, DEBUG=False):
    if DEBUG:
        print("Determining best match")
    confirm_list = []

    gold_data = gold_node.data
    des_branch_left = {}
    trajactory_map = {}

    if gold_node.has_children():
        nearby_list = []
        path_length_map = {}
        path_length_map[gold_node] = SwcNode()

        target_dis_list = []
        for test_node in match_list:
            target_distances = SwcNode()
            target_distances.xy_path_length = gold_node.data.distance(
                test_node.data, _2D)
            target_distances.z_path_length = gold_node.data.distance(
                test_node.data, _2D)
            target_dis_list.append(target_distances)

        current_list = []
        current_list.append(gold_node.left_son)
        trajactory_map[gold_node.left_son] = gold_node.data.left_trajectory
        des_branch_left[gold_node.left_son] = True

        current_list.append(gold_node.right_son)
        trajactory_map[gold_node.right_son] = gold_node.data.right_trajectory
        des_branch_left[gold_node.right_son] = False

        target_tra = EuclideanPoint
        while len(current_list) > 0 and len(confirm_list) == 0:
            next_list = []
            for des_node in current_list:
                target_tra = trajactory_map[des_node]

                if target_tra.get_x() == -1.0 or target_tra.get_z() == -1.0:
                    tmp_tra = get_trajectory_for_path(gold_node, des_node)
                    if target_tra.get_x() == -1.0:
                        target_tra.set_x(tmp_tra.get_x())
                        target_tra.set_y(tmp_tra.get_y())
                    if target_tra.get_z() == -1.0:
                        target_tra.set_z(tmp_tra.get_z())
                des_tra = des_node.data.parent_trajectory

                if des_node.has_children():
                    next_list.append(des_node.left_son)
                    next_list.append(des_node.right_son)
                    trajactory_map[des_node.left_son] = target_tra
                    trajactory_map[des_node.right_son] = target_tra

                gold_path_length = SwcNode()
                gold_path_length.add_length(des_node.data)
                parent_path_length = path_length_map[des_node.parent]
                gold_path_length.add_length(parent_path_length)
                path_length_map[des_node] = gold_path_length

                nearby_list = get_nearby_node_list(des_node,
                                                   bin_test_list,
                                                   check_previous_use=True,
                                                   g_matches=g_matches)

                for test_node in nearby_list:
                    test_des_node = test_node
                    test_path_length = SwcNode()

                    des_dis = SwcNode()
                    des_dis.xy_path_length = des_node.data.distance(
                        test_node.data, _2D)
                    des_dis.z_path_length = math.fabs(des_node.data.get_z() -
                                                      test_node.data.get_z())

                    while test_node.parent is not None:
                        test_path_length.add_length(test_node.data)
                        test_node = test_node.parent
                        tmp_list = []
                        for match in match_list:
                            if test_node == match:
                                test_XY_path_length = test_path_length.xy_path_length + \
                                                      get_end_node_XY_dis_diff(gold_data, target_tra, test_node.data) + \
                                                      get_end_node_XY_dis_diff(des_node.data, des_tra, test_des_node.data)
                                test_Z_path_length = test_path_length.z_path_length + \
                                                     get_end_node_Z_dis_diff(gold_data, target_tra, test_node.data) + \
                                                     get_end_node_Z_dis_diff(des_node.data, des_tra, test_des_node.data)

                                if path_length_matches(gold_path_length,
                                                       test_XY_path_length,
                                                       test_Z_path_length) < 1:
                                    confirm_list.append(match)
                                    tmp_list.append(match)

                        for node in tmp_list:
                            match_list.remove(node)
                        while len(tmp_list):
                            tmp_list.pop()

            current_list = nearby_list
    if len(confirm_list) == 1:
        return confirm_list[0]

    if len(confirm_list) == 0:
        confirm_list = match_list

    dis = 0
    closest_dis = -1
    closest_match = None
    for test_node in confirm_list:
        dis = test_node.data.distance(gold_node.data)
        if closest_dis == -1 or dis < closest_dis:
            closest_dis = dis
            closest_match = test_node

    return closest_match
Ejemplo n.º 8
0
def get_match_path_length_difference(gold_node, test_node, bin_gold_list,
                                     bin_test_list):
    if test_node.parent is None:
        return 0
    gold_target = gold_node

    gold_swc_path_length = SwcNode()
    gold_swc_path_length.add_length(gold_node.data)
    test_swc_path_length = SwcNode()
    test_swc_path_length.add_length(test_node.data)

    test_path_XY_mod = get_end_node_XY_dis_diff(
        gold_node.data, gold_node.data.parent_trajectory, test_node.data)
    test_path_Z_mod = get_end_node_Z_dis_diff(gold_node.data,
                                              gold_node.data.parent_trajectory,
                                              test_node.data)
    test_swc_path_length.xy_path_length += test_path_XY_mod
    test_swc_path_length.z_path_length += test_path_Z_mod

    is_branch_left = gold_node.is_left()
    gold_node = gold_node.parent
    test_node = test_node.parent

    ancestor_trajectory = EuclideanPoint()
    checked_node = set()
    no_match = True
    no_done = True

    while no_match and no_done:
        if is_in_threshold(gold_node, test_node):
            gold_data = gold_node.data

            if is_branch_left:
                ancestor_trajectory = gold_data.left_trajectory
            else:
                ancestor_trajectory = gold_data.right_trajectory

            if ancestor_trajectory.get_x() == TRAJECTORY_NONE or \
                ancestor_trajectory.get_z() == TRAJECTORY_NONE:
                tmp_trajectory = get_trajectory_for_path(
                    gold_node, gold_target)
                if ancestor_trajectory.get_x() == TRAJECTORY_NONE:
                    ancestor_trajectory.set_x(tmp_trajectory.get_x())
                    ancestor_trajectory.set_y(tmp_trajectory.get_y())
                if ancestor_trajectory.get_z() == TRAJECTORY_NONE:
                    ancestor_trajectory.set_z(tmp_trajectory.get_z())

            test_XY_path_length = test_swc_path_length.xy_path_length + \
                                  get_end_node_XY_dis_diff(gold_data, ancestor_trajectory, test_node.data)
            test_Z_path_length = test_swc_path_length.z_path_length + \
                                 get_end_node_Z_dis_diff(gold_data, ancestor_trajectory, test_node.data)
            percent_error = path_length_matches(gold_swc_path_length, \
                                               test_XY_path_length, \
                                               test_Z_path_length)
            if percent_error < 1:
                return percent_error
            else:
                return 1
        else:
            if gold_swc_path_length.path_length < test_swc_path_length.path_length:
                if gold_node.parent is None:
                    no_done = False
                else:
                    nearby_nodes = get_nearby_node_list(
                        gold_node, bin_test_list, False)
                    for node in nearby_nodes:
                        if node in checked_node:
                            no_done = False
                            break
                    checked_node.add(gold_node)
                    gold_swc_path_length.add_length(gold_node.data)
                    is_branch_left = gold_node.is_left()
                    gold_node = gold_node.parent
            else:
                if test_node.parent is None:
                    no_done = False
                else:
                    nearby_nodes = get_nearby_node_list(
                        test_node, bin_gold_list, False)
                    for node in nearby_nodes:
                        if node in checked_node:
                            no_done = False
                            break

                    checked_node.add(test_node)
                    test_swc_path_length.add_length(test_node.data)
                    test_node = test_node.parent
    return 1
Ejemplo n.º 9
0
def LCA(node1, node2, kca):
    node1_list = []

    node1 = node1.parent
    while node1 != kca:
        node1_list.append(node1)
        node1 = node1.parent

    node1 = node2.parent
    while node2 != kca and node2 is not None:
        if node2 in node1_list:
            return node2
        node2 = node2.parent

    return None


def is_within_dis_match_threshold(node1, node2):
    if isinstance(node1, BinaryNode):
        node1 = node1.data
    if isinstance(node2, BinaryNode):
        node2 = node2.data
    return node1.distance(node2, _2D) <= g_xy_threshold * 3 \
           and math.fabs(node1.get_z() - node2.get_z()) < g_z_threshold * 3 + 0.1


if __name__ == "__main__":
    gold = SwcNode(center=[2, 3, 4])
    test = SwcNode(center=[1, -1, 5])
    is_in_threshold(gold, test)