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

Code before:
                         ' Use `multiclass_auroc` for multi class classification.')

    if max_fpr is None or max_fpr == 1:
        fpr, tpr, _ = __roc(pred, target, sample_weight, pos_label)
        return auc(fpr, tpr)
    if not (isinstance(max_fpr, float) and 0 < max_fpr <= 1):
        raise ValueError(f"`max_fpr` should be a float in range (0, 1], got: {max_fpr}")
    if LooseVersion(torch.__version__) < LooseVersion('1.6.0'):
        raise RuntimeError('`max_fpr` argument requires `torch.bucketize` which'
                           ' is not available below PyTorch version 1.6')

    fpr, tpr, _ = __roc(pred, target, sample_weight, pos_label)
    max_fpr = torch.tensor(max_fpr, device=fpr.device)
    # 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
    partial_auc = auc(fpr, tpr)

    # 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))


def multiclass_auroc(
        pred: torch.Tensor,
        target: torch.Tensor,
        sample_weight: Optional[Sequence] = None,
        num_classes: Optional[int] = None,
) -> torch.Tensor:
    """
    Compute Area Under the Receiver Operating Characteristic Curve (ROC AUC) from multiclass
    prediction scores

    Args:
        pred: estimated probabilities, with shape [N, C]
        target: ground-truth labels, with shape [N,]
        sample_weight: sample weights
        num_classes: number of classes (default: None, computes automatically from data)

    Return:
        Tensor containing ROCAUC score

    Example:


Review comment (suggestion):
```suggestion
    min_area = 0.5 * max_fpr ** 2
```

Code after:
                         ' Use `multiclass_auroc` for multi class classification.')

    if max_fpr is None or max_fpr == 1:
        fpr, tpr, _ = __roc(pred, target, sample_weight, pos_label)
        return auc(fpr, tpr)
    if not (isinstance(max_fpr, float) and 0 < max_fpr <= 1):
        raise ValueError(f"`max_fpr` should be a float in range (0, 1], got: {max_fpr}")
    if LooseVersion(torch.__version__) < LooseVersion('1.6.0'):
        raise RuntimeError('`max_fpr` argument requires `torch.bucketize` which'
                           ' is not available below PyTorch version 1.6')

    fpr, tpr, _ = __roc(pred, target, sample_weight, pos_label)
    max_fpr = torch.tensor(max_fpr, device=fpr.device)
    # 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
    partial_auc = auc(fpr, tpr)

    # 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))


def multiclass_auroc(
        pred: torch.Tensor,
        target: torch.Tensor,
        sample_weight: Optional[Sequence] = None,
        num_classes: Optional[int] = None,
) -> torch.Tensor:
    """
    Compute Area Under the Receiver Operating Characteristic Curve (ROC AUC) from multiclass
    prediction scores

    Args:
        pred: estimated probabilities, with shape [N, C]
        target: ground-truth labels, with shape [N,]
        sample_weight: sample weights
        num_classes: number of classes (default: None, computes automatically from data)

    Return:
        Tensor containing ROCAUC score

    Example: