def _compute_indices_of_one_pair_of_mergeable_groups(distance_matrix, min_dist_between_items_in_different_groups):
"""
This function semantically operates on a collection of grouped items.
It returns a pair of indices corresponding to a pair of groups which are close enough to merge.
Args:
distance_matrix (np.ndarray): the matrix containing distances between all groups
min_dist_between_items_in_different_groups (float): the algorithm will only suggest a pair of groups to be
merged if they each contain an item which is within this
value of the other.
Returns:
a pair of indices corresponding to a mergeable pair of groups
"""
# Get all indices (i,j) with i<j, in a 2-element tuple (all rows indices, all column indices).
inds = np.triu_indices_from(distance_matrix, 1) # type: tuple
# Get a boolean vector which indexes both elements of inds and tells whether the pair of groups should be merged
groups_could_be_merged = distance_matrix[inds] < min_dist_between_items_in_different_groups
# Get the subset of indices for groups we can merge
inds_of_mergeable_groups = [inds[i][groups_could_be_merged] for i in range(len(inds))]
# Return the first pair of indices, if any were found
indices_matrix = np.transpose(inds_of_mergeable_groups)
return indices_matrix[0] if len(indices_matrix) > 0 else None
评论列表
文章目录