import torch


def xywh2YOLO(box: torch.Tensor, stride: float,
              anchor: tuple):
    """
        Returns the bounding box in a format similar to the last output
        layer of the YOLO

        Arguments:
            box (torch.Tensor): Boudning box tensor in xywh format
            stride (float): stride of the current layer to decrease the size
            anchor (list): it is a list of tuples of anchor boxes

        Outputs:
            x_coor (int): x coordinate of the detection grid for the given box
            y_coor (int): y coordinate of the detection grid for the given box
            x   (float): x output of the YOLO layer for the corresponding box
            y   (float): y output of the YOLO layer for the corresponding box
            w   (float): w output of the YOLO layer for the corresponding box
            h   (float): h output of the YOLO layer for the corresponding box
    """
    x = box[..., 0].item()/stride
    x_coor = int(box[..., 0].item()/stride)
    y = box[..., 1].item()/stride
    y_coor = int(box[..., 1].item()/stride)
    x -= x_coor
    y -= y_coor
    w = torch.log(box[..., 2] / anchor[0] + 1e-16).item()
    h = torch.log(box[..., 3] / anchor[1] + 1e-16).item()
    return y_coor, x_coor, y, x, w, h