def merge_groups(groups, dist_tol):
    """ Merges untill all groups have at least
    `dist_tol` distance between them
    Parameters
    ----------
    groups :obj:list of `Group`
        groups found using adjacency
    dist_tol : int
        minimum euclidean distance
        to be in same cluster
    Returns
    -------
    :obj:list of `Group`
    """
    #give groups initial labels = indexes
    for i in range(len(groups)):
        groups[i].updateLabel(i)

    uf = UnionFind(len(groups))

    #find labels to merge
    for i in range(len(groups)):
        curr_group = groups[i]
        #only look at groups farther in list
        for j in range(i, len(groups)):
            other_group = groups[j]

            #short circuit if already connected (minimize nearest neighbor calls)
            if not uf.find(curr_group.label, other_group.label):
                if Group.nearby(curr_group, other_group, dist_tol):
                    uf.union(curr_group.label, other_group.label)

    merged_groups = []
    unmerged_groups = []
    #iterate until all merges have been made
    while len(groups) > 0:
        curr_group = groups[0]
        merged_groups.append(curr_group)
        for j in range(1, len(groups)):
            other_group = groups[j]

            #on each iteration, one merged group moves to the new array
            if uf.find(curr_group.label, other_group.label):
                curr_group.merge(other_group)
            #all other groups are kept in the old array
            else:
                unmerged_groups.append(other_group)

        groups = unmerged_groups
        unmerged_groups = []
        
    for g in merged_groups:
        g.compute_info()

    return merged_groups
Beispiel #2
0
def merge_groups(groups, tol):
    #give groups initial labels = indexes
    for i in range(len(groups)):
        groups[i].updateLabel(i)

    uf = UnionFind(len(groups))

    #find labels to merge
    for i in range(len(groups)):
        curr_group = groups[i]
        #only look at groups farther in list
        for j in range(i, len(groups)):
            other_group = groups[j]

            #short circuit if already connected (minimize nearest neighbor calls)
            if not uf.find(curr_group.label, other_group.label):
                if Group.nearby(curr_group, other_group, tol):
                    uf.union(curr_group.label, other_group.label)

    merged_groups = []
    unmerged_groups = []
    #iterate until all merges have been made
    while len(groups) > 0:
        curr_group = groups[0]
        merged_groups.append(curr_group)
        for j in range(1, len(groups)):
            other_group = groups[j]

            #on each iteration, one merged group moves to the new array
            if uf.find(curr_group.label, other_group.label):
                curr_group.merge(other_group)
            #all other groups are kept in the old array
            else:
                unmerged_groups.append(other_group)

        groups = unmerged_groups
        unmerged_groups = []

    return merged_groups