import torch


def binary_dice_coefficient(pred: torch.Tensor, gt: torch.Tensor,
                            thresh: float = 0.5, smooth: float = 1e-7):
    """
    A binary dice coefficient

    Parameters
    ----------
    pred : torch.Tensor
        predicted segmentation (of shape NxCx(Dx)HxW)
    gt : torch.Tensor
        target segmentation (of shape NxCx(Dx)HxW)
    thresh : float
        segmentation threshold
    smooth : float
        smoothing value to avoid division by zero

    Returns
    -------
    torch.Tensor
        dice score

    """
    pred_bool = pred > thresh

    intersec = (pred_bool and gt).float()
    return 2 * intersec.sum() / (pred_bool.float().sum()
                                 + gt.float().sum() + smooth)