def thresh_at_shared_fpr_level(
    thresholds: torch.Tensor,
    shared_fpr: torch.Tensor,
    fpr_level: float,
) -> tuple[int, float, torch.Tensor]:
    """Return the threshold and its index at the given shared FPR level.

    Three cases are possible:
        - ``fpr_level == 0``: lowest threshold achieving 0 FPR is returned
        - ``fpr_level == 1``: highest threshold achieving 1 FPR is returned
        - ``0 < fpr_level < 1``: threshold achieving closest FPR is returned

    Args:
        thresholds: Thresholds at which shared FPR was computed
        shared_fpr: Shared FPR values
        fpr_level: Shared FPR value at which to get threshold

    Returns:
        tuple containing:
            - index: Index of the threshold
            - threshold: Threshold value
            - actual_fpr: Actual shared FPR value at returned threshold

    Raises:
        ValueError: If inputs are invalid or FPR level is out of range

    Example:
        >>> thresholds = torch.linspace(0, 1, 100)
        >>> shared_fpr = torch.linspace(1, 0, 100)  # Decreasing FPR
        >>> idx, thresh, fpr = thresh_at_shared_fpr_level(
        ...     thresholds,
        ...     shared_fpr,
        ...     fpr_level=0.5
        ... )
    """
    _validate.is_valid_threshold(thresholds)
    _validate.is_rate_curve(shared_fpr, nan_allowed=False, decreasing=True)
    _validate.joint_validate_thresholds_shared_fpr(thresholds, shared_fpr)
    _validate.is_rate(fpr_level, zero_ok=True, one_ok=True)

    shared_fpr_min, shared_fpr_max = shared_fpr.min(), shared_fpr.max()

    if fpr_level < shared_fpr_min:
        msg = (
            "Invalid `fpr_level` because it's out of the range of `shared_fpr` "
            f"= [{shared_fpr_min}, {shared_fpr_max}], and got {fpr_level}."
        )
        raise ValueError(msg)

    if fpr_level > shared_fpr_max:
        msg = (
            "Invalid `fpr_level` because it's out of the range of `shared_fpr` "
            f"= [{shared_fpr_min}, {shared_fpr_max}], and got {fpr_level}."
        )
        raise ValueError(msg)

    # fpr_level == 0 or 1 are special case
    # because there may be multiple solutions, and the chosen should their
    # MINIMUM/MAXIMUM respectively
    if fpr_level == 0.0:
        index = torch.min(torch.where(shared_fpr == fpr_level)[0])

    elif fpr_level == 1.0:
        index = torch.max(torch.where(shared_fpr == fpr_level)[0])

    else:
        index = torch.argmin(torch.abs(shared_fpr - fpr_level))

    index = int(index)
    fpr_level_defacto = shared_fpr[index]
    thresh = thresholds[index]
    return index, thresh, fpr_level_defacto
Return the threshold and its index at the given shared FPR level.

    Three cases are possible:
        - ``fpr_level == 0``: lowest threshold achieving 0 FPR is returned
        - ``fpr_level == 1``: highest threshold achieving 1 FPR is returned
        - ``0 < fpr_level < 1``: threshold achieving closest FPR is returned

    Args:
        thresholds: Thresholds at which shared FPR was computed
        shared_fpr: Shared FPR values
        fpr_level: Shared FPR value at which to get threshold

    Returns:
        tuple containing:
            - index: Index of the threshold
            - threshold: Threshold value
            - actual_fpr: Actual shared FPR value at returned threshold

    Raises:
        ValueError: If inputs are invalid or FPR level is out of range

    Example:
        >>> thresholds = torch.linspace(0, 1, 100)
        >>> shared_fpr = torch.linspace(1, 0, 100)  # Decreasing FPR
        >>> idx, thresh, fpr = thresh_at_shared_fpr_level(
        ...     thresholds,
        ...     shared_fpr,
        ...     fpr_level=0.5
        ... )
    
src/anomalib/metrics/pimo/functional.py