Repository: Lightning-AI/pytorch-lightning
File: pytorch_lightning/metrics/functional/classification.py (Python)

Code before:
    if any(target > 1):
        raise ValueError('AUROC metric is meant for binary classification, but'
                         ' target tensor contains value different from 0 and 1.'
                         ' Multiclass is currently not supported.')

    # @auc_decorator(reorder=True)
    def _auroc(pred, target, sample_weight, pos_label, max_fpr):
        if max_fpr is None or max_fpr == 1:
            fpr, tpr, _ = roc(pred, target, sample_weight, pos_label)
            return auc(fpr, tpr, reorder=True)
        if max_fpr <= 0 or max_fpr > 1:
            raise ValueError("Expected max_fpr in range (0, 1], got: %r" % max_fpr)

        fpr, tpr, _ = roc(pred, target, sample_weight, pos_label)
        max_fpr = torch.tensor(max_fpr, device=fpr.device)
        # torch.bucketize is slightly more performant than torch.searchsorted
        # for 1-D arrays
        # Add a single point at max_fpr and interpolate its tpr value
        stop = torch.bucketize(max_fpr, fpr, out_int32=True, right=True)
        weight = (max_fpr - fpr[stop - 1]) / (fpr[stop] - fpr[stop - 1])
        interp_tpr = torch.lerp(tpr[stop - 1], tpr[stop], weight)
        tpr = torch.cat([tpr[:stop], interp_tpr.view(1)])
        fpr = torch.cat([fpr[:stop], max_fpr.view(1)])
        # Compute partial AUC
        # Do i need to reorder?
        partial_auc = auc(fpr, tpr, reorder=True)

        # McClish correction: standardize result to be 0.5 if non-discriminant
        # and 1 if maximal
        min_area = 0.5 * max_fpr**2
        max_area = max_fpr
        return 0.5 * (1 + (partial_auc - min_area) / (max_area - min_area))

    return _auroc(pred=pred, target=target, sample_weight=sample_weight, pos_label=pos_label, max_fpr=max_fpr)


def average_precision(
        pred: torch.Tensor,
        target: torch.Tensor,
        sample_weight: Optional[Sequence] = None,
        pos_label: int = 1.,
) -> torch.Tensor:
    """
    Compute average precision from prediction scores

    Args:
        pred: estimated probabilities
        target: ground-truth labels
        sample_weight: sample weights
        pos_label: the label for the positive class


Review comment (suggestion):
```suggestion
        partial_auc = auc(fpr, tpr, reorder=False)
```

Code after:
# todo: remove in 1.2
def fbeta_score(
        pred: torch.Tensor,
        target: torch.Tensor,
        beta: float,
        num_classes: Optional[int] = None,
        class_reduction: str = 'micro',
) -> torch.Tensor:
    """
    Computes the F-beta score which is a weighted harmonic mean of precision and recall.

    .. warning :: Deprecated in favor of :func:`~pytorch_lightning.metrics.functional.f_beta.fbeta`
    """
    rank_zero_warn(
        "This `average_precision` was deprecated in v1.0.x in favor of"
        " `from pytorch_lightning.metrics.functional.f_beta import fbeta`."
        " It will be removed in v1.2.0", DeprecationWarning
    )
    if num_classes is None:
        num_classes = get_num_classes(pred, target)
    return __fb(preds=pred, target=target, beta=beta, num_classes=num_classes, average=class_reduction)


# todo: remove in 1.2
def f1_score(
        pred: torch.Tensor,
        target: torch.Tensor,
        num_classes: Optional[int] = None,
        class_reduction: str = 'micro',
) -> torch.Tensor:
    """
    Computes the F1-score (a.k.a F-measure), which is the harmonic mean of the precision and recall.

    .. warning :: Deprecated in favor of :func:`~pytorch_lightning.metrics.functional.f_beta.f1`
    """
    rank_zero_warn(
        "This `average_precision` was deprecated in v1.0.x in favor of"
        " `from pytorch_lightning.metrics.functional.f_beta import f1`."
        " It will be removed in v1.2.0", DeprecationWarning
    )
    if num_classes is None:
        num_classes = get_num_classes(pred, target)
    return __f1(preds=pred, target=target, num_classes=num_classes, average=class_reduction)