import torch


def weighted_intersection_over_minimum(
    mask0: torch.Tensor, mask1: torch.Tensor
) -> float:
    """Calculates the weighted intersection over minimum (IoM) between two segmentation
    masks.

    Parameters
    ----------
    mask0 : torch.Tensor
        Instance segmentation mask to compare to mask1.
    mask1 : torch.Tensor
        Instance segmentation mask to compare to mask0. Should have the same shape as
        mask0.

    Returns
    -------
    iom : float
        Weighted intersection over union of mask0 and mask1.

    Examples
    --------
    >>> mask0 = torch.tensor([0.0, 0.5, 0.5, 0.0])
    >>> mask1 = torch.tensor([1.0, 1.0, 0.0, 0.0])
    >>> weighted_intersection_over_minimum(mask0, mask1)
    0.5
    """
    return float(torch.minimum(mask0, mask1).sum()) / min(
        float(mask0.sum()), float(mask1.sum())
    )