Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/label_checking_classification
Browse files Browse the repository at this point in the history
  • Loading branch information
Borda authored Mar 15, 2024
2 parents 0af30e8 + 0a6ad01 commit 695c75f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fixed case where label prediction tensors in classification metrics were not validated correctly ([#2427](https://github.com/Lightning-AI/torchmetrics/pull/2427))


- Fixed how auc scores are calculated in `PrecisionRecallCurve.plot` methods ([#2437](https://github.com/Lightning-AI/torchmetrics/pull/2437))

## [1.3.1] - 2024-02-12

### Fixed
Expand Down
19 changes: 13 additions & 6 deletions src/torchmetrics/classification/precision_recall_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ def plot(
curve: the output of either `metric.compute` or `metric.forward`. If no value is provided, will
automatically call `metric.compute` and plot that result.
score: Provide a area-under-the-curve score to be displayed on the plot. If `True` and no curve is provided,
will automatically compute the score.
will automatically compute the score. The score is computed by using the trapezoidal rule to compute the
area under the curve.
ax: An matplotlib axis object. If provided will add plot to that axis
Returns:
Expand All @@ -215,7 +216,7 @@ def plot(
curve_computed = (curve_computed[1], curve_computed[0], curve_computed[2])

score = (
_auc_compute_without_check(curve_computed[0], curve_computed[1], 1.0)
_auc_compute_without_check(curve_computed[0], curve_computed[1], direction=-1.0)
if not curve and score is True
else None
)
Expand Down Expand Up @@ -390,7 +391,8 @@ def plot(
curve: the output of either `metric.compute` or `metric.forward`. If no value is provided, will
automatically call `metric.compute` and plot that result.
score: Provide a area-under-the-curve score to be displayed on the plot. If `True` and no curve is provided,
will automatically compute the score.
will automatically compute the score. The score is computed by using the trapezoidal rule to compute the
area under the curve.
ax: An matplotlib axis object. If provided will add plot to that axis
Returns:
Expand All @@ -416,7 +418,9 @@ def plot(
# switch order as the standard way is recall along x-axis and precision along y-axis
curve_computed = (curve_computed[1], curve_computed[0], curve_computed[2])
score = (
_reduce_auroc(curve_computed[0], curve_computed[1], average=None) if not curve and score is True else None
_reduce_auroc(curve_computed[0], curve_computed[1], average=None, direction=-1.0)
if not curve and score is True
else None
)
return plot_curve(
curve_computed, score=score, ax=ax, label_names=("Recall", "Precision"), name=self.__class__.__name__
Expand Down Expand Up @@ -583,7 +587,8 @@ def plot(
curve: the output of either `metric.compute` or `metric.forward`. If no value is provided, will
automatically call `metric.compute` and plot that result.
score: Provide a area-under-the-curve score to be displayed on the plot. If `True` and no curve is provided,
will automatically compute the score.
will automatically compute the score. The score is computed by using the trapezoidal rule to compute the
area under the curve.
ax: An matplotlib axis object. If provided will add plot to that axis
Returns:
Expand All @@ -609,7 +614,9 @@ def plot(
# switch order as the standard way is recall along x-axis and precision along y-axis
curve_computed = (curve_computed[1], curve_computed[0], curve_computed[2])
score = (
_reduce_auroc(curve_computed[0], curve_computed[1], average=None) if not curve and score is True else None
_reduce_auroc(curve_computed[0], curve_computed[1], average=None, direction=-1.0)
if not curve and score is True
else None
)
return plot_curve(
curve_computed, score=score, ax=ax, label_names=("Recall", "Precision"), name=self.__class__.__name__
Expand Down
9 changes: 6 additions & 3 deletions src/torchmetrics/classification/roc.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ def plot(
curve: the output of either `metric.compute` or `metric.forward`. If no value is provided, will
automatically call `metric.compute` and plot that result.
score: Provide a area-under-the-curve score to be displayed on the plot. If `True` and no curve is provided,
will automatically compute the score.
will automatically compute the score. The score is computed by using the trapezoidal rule to compute the
area under the curve.
ax: An matplotlib axis object. If provided will add plot to that axis
Returns:
Expand Down Expand Up @@ -303,7 +304,8 @@ def plot(
curve: the output of either `metric.compute` or `metric.forward`. If no value is provided, will
automatically call `metric.compute` and plot that result.
score: Provide a area-under-the-curve score to be displayed on the plot. If `True` and no curve is provided,
will automatically compute the score.
will automatically compute the score. The score is computed by using the trapezoidal rule to compute the
area under the curve.
ax: An matplotlib axis object. If provided will add plot to that axis
Returns:
Expand Down Expand Up @@ -461,7 +463,8 @@ def plot(
curve: the output of either `metric.compute` or `metric.forward`. If no value is provided, will
automatically call `metric.compute` and plot that result.
score: Provide a area-under-the-curve score to be displayed on the plot. If `True` and no curve is provided,
will automatically compute the score.
will automatically compute the score. The score is computed by using the trapezoidal rule to compute the
area under the curve.
ax: An matplotlib axis object. If provided will add plot to that axis
Returns:
Expand Down
5 changes: 3 additions & 2 deletions src/torchmetrics/functional/classification/auroc.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ def _reduce_auroc(
tpr: Union[Tensor, List[Tensor]],
average: Optional[Literal["macro", "weighted", "none"]] = "macro",
weights: Optional[Tensor] = None,
direction: float = 1.0,
) -> Tensor:
"""Reduce multiple average precision score into one number."""
if isinstance(fpr, Tensor) and isinstance(tpr, Tensor):
res = _auc_compute_without_check(fpr, tpr, 1.0, axis=1)
res = _auc_compute_without_check(fpr, tpr, direction=direction, axis=1)
else:
res = torch.stack([_auc_compute_without_check(x, y, 1.0) for x, y in zip(fpr, tpr)])
res = torch.stack([_auc_compute_without_check(x, y, direction=direction) for x, y in zip(fpr, tpr)])
if average is None or average == "none":
return res
if torch.isnan(res).any():
Expand Down
4 changes: 2 additions & 2 deletions src/torchmetrics/utilities/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ def _auc_compute_without_check(x: Tensor, y: Tensor, direction: float, axis: int
"""
with torch.no_grad():
auc_: Tensor = torch.trapz(y, x, dim=axis) * direction
return auc_
auc_score: Tensor = torch.trapz(y, x, dim=axis) * direction
return auc_score


def _auc_compute(x: Tensor, y: Tensor, reorder: bool = False) -> Tensor:
Expand Down

0 comments on commit 695c75f

Please sign in to comment.