def _match_munkres(l1, l2, dist_matrix, thresh):
    """Matches two lists using the Munkres algorithm.

    Returns pairs of matching indices from the two lists by minimizing the sum
    of the distance between the linked elements and taking only the elements
    which have the distance between them less (or equal) than the threshold.
    """
    equal_dist_matches = set()
    m = Munkres()
    indices = m.compute(dist_matrix)

    for l1_idx, l2_idx in indices:
        dst = dist_matrix[l1_idx][l2_idx]
        if dst > thresh:
            continue
        for eq_l2_idx, eq_val in enumerate(dist_matrix[l1_idx]):
            if abs(dst - eq_val) < 1e-9:
                equal_dist_matches.add((l1_idx, eq_l2_idx))
        for eq_l1_idx, eq_row in enumerate(dist_matrix):
            if abs(dst - eq_row[l2_idx]) < 1e-9:
                equal_dist_matches.add((eq_l1_idx, l2_idx))

    return [(l1[l1_idx], l2[l2_idx]) for l1_idx, l2_idx in equal_dist_matches]
Matches two lists using the Munkres algorithm.

    Returns pairs of matching indices from the two lists by minimizing the sum
    of the distance between the linked elements and taking only the elements
    which have the distance between them less (or equal) than the threshold.