import torch
import math


def continuous_dice_coefficient(output, target):
    """
    Continuous version of the Dice coefficient.

    References
    ----------
    https://www.biorxiv.org/content/biorxiv/early/2018/04/25/306977.full.pdf
    """
    pred = torch.sigmoid(output)

    batch_size = output.size(0)
    pred = pred.view(batch_size, -1)
    truth = target.view(batch_size, -1)

    pred_size = pred.sum(1)
    truth_size = truth.sum(1)

    intersection = (pred * truth).sum(1)

    if intersection > 0:
        c =  (intersection / (truth * (1-math.exp(1e3*pred))).sum()).sum()
    else:
        c = 1

    continuous_dice = (2 * intersection) / (c * truth_size + pred_size)

    return continuous_dice