def iou_1d(predicted_boundary, target_boundary):
    """Calculates the intersection over union (IOU) based on a span.

    Notes:
        boundaries are provided in the the form of [start, stop].
        boundaries where start = stop are accepted
        boundaries are assumed to be only in range [0, int < inf)

        if no intersection, returns 0.


    Example:

        0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 index
           |-----------------------|                                   predicted
                             |------------------------|                target

        then the predicted boundaries are:

            [ lower, upper ]
            [     1,     9 ] predicted
            [     7,    15 ] target

        then the intersection of these two sequences would be:

             2 = 9 - 7
               = min(9, 15) - max(1, 7)
               = min(p_upper, t_upper) - max(p_lower, t_lower)

        and the union:

            14 = 15 - 1
               = max(15, 9) - min(7, 1)
               = max(t_upper, p_upper) - min(t_lower, p_lower)

        giving as IOU of:

            0.143 ≈ 1 / 7 =  2 / 14

    Args:
        predicted_boundary (list): the [start, stop] of the predicted boundary
        target_boundary (list): the ground truth [start, stop] for which to compare

    Returns:
        iou (float): the IOU bounded in [0, 1]

    """
    p_lower, p_upper = predicted_boundary
    t_lower, t_upper = target_boundary
    assert 0 <= p_lower <= p_upper
    assert 0 <= t_lower <= t_upper
    if p_upper < t_lower or p_lower > t_upper:
        return 0
    if predicted_boundary == target_boundary:
        return 1
    intersection_lower_bound = max(p_lower, t_lower)
    intersection_upper_bound = min(p_upper, t_upper)
    intersection = intersection_upper_bound - intersection_lower_bound
    max_upper = max(t_upper, p_upper)
    min_lower = min(t_lower, p_lower)
    union = max_upper - min_lower
    if intersection == 0:
        intersection = 1
    return intersection / union
