Exemple #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
Exemple #2
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
Exemple #3
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