def calculate_iou(ground_truth, prediction):
    """Calculate the IoU of a single predicted ground truth box."""
    x1_gt, x2_gt, y1_gt, y2_gt = ground_truth
    x1_p, x2_p, y1_p, y2_p = prediction

    if x1_p > x2_p or y1_p > y2_p:
        raise AssertionError("Prediction box is malformed? {}".format(prediction))

    if x1_gt > x2_gt or y1_gt > y2_gt:
        raise AssertionError("Ground truth box is malformed? {}".format(ground_truth))

    if x2_gt < x1_p or x2_p < x1_gt or y2_gt < y1_p or y2_p < y1_gt:
        return 0.0

    inter_x1 = max([x1_gt, x1_p])
    inter_x2 = min([x2_gt, x2_p])

    inter_y1 = max([y1_gt, y1_p])
    inter_y2 = min([y2_gt, y2_p])

    inter_area = (inter_x2 - inter_x1 + 1) * (inter_y2 - inter_y1 + 1)
    gt_area = (x2_gt - x1_gt + 1) * (y2_gt - y1_gt + 1)
    pred_area = (x2_p - x1_p + 1) * (y2_p - y1_p + 1)
    return float(inter_area) / (gt_area + pred_area - inter_area)