import torch


def masked_precision(pred: torch.Tensor, target: torch.Tensor, eps: float=1e-6) -> torch.Tensor:
    """
    Function to calculate the masked precision. Like precision,
    but with usability mask in channel in the first channel
    of the target, i.e. target should have one more channel
    than prediction.

    :param pred: (torch.Tensor) predictions, logits or binary
    :param target: (torch.Tensor) target, logits or binary
    :param eps: (eps) epsilon for numerical stability

    :returns precision: (torch.Tensor)
    """

    mask = (target[:, 0] > 0.).float()
    mask = mask[:, None]

    pred = mask*(pred > 0).float()
    target = mask*(target[:, 1:] > 0).float()

    tp = ((pred == 1) * (target == 1)).float()

    return tp.sum() / (pred.sum() + eps)