-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add R2 score to metrics #8093
Open
thibaultdvx
wants to merge
9
commits into
Project-MONAI:dev
Choose a base branch
from
thibaultdvx:8085-r2-score
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add R2 score to metrics #8093
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3111c31
add MultiOutput in enum
thibaultdvx 62ecb23
add r2 metric and compute
thibaultdvx 9e34586
add r2 handler
thibaultdvx ee65c1e
unittests
thibaultdvx bfce6e7
docs
thibaultdvx e00db73
fix code issues
thibaultdvx b01b38b
Merge branch 'dev' into 8085-r2-score
thibaultdvx c9ee60c
mypy issues
thibaultdvx 838d0a1
code format
thibaultdvx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright (c) MONAI Consortium | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
from collections.abc import Callable | ||
|
||
from monai.handlers.ignite_metric import IgniteMetricHandler | ||
from monai.metrics import R2Metric | ||
from monai.utils import MultiOutput | ||
|
||
|
||
class R2Score(IgniteMetricHandler): | ||
""" | ||
Computes :math:`R^{2}` score accumulating predictions and the ground-truth during an epoch and applying `compute_r2_score`. | ||
|
||
Args: | ||
multi_output: {``"raw_values"``, ``"uniform_average"``, ``"variance_weighted"``} | ||
Type of aggregation performed on multi-output scores. | ||
Defaults to ``"uniform_average"``. | ||
|
||
- ``"raw_values"``: the scores for each output are returned. | ||
- ``"uniform_average"``: the scores of all outputs are averaged with uniform weight. | ||
- ``"variance_weighted"``: the scores of all outputs are averaged, weighted by the variances | ||
of each individual output. | ||
p: non-negative integer. | ||
Number of independent variables used for regression. ``p`` is used to compute adjusted :math:`R^{2}` score. | ||
Defaults to 0 (standard :math:`R^{2}` score). | ||
output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then | ||
construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or | ||
lists of `channel-first` Tensors. The form of `(y_pred, y)` is required by the `update()`. | ||
`engine.state` and `output_transform` inherit from the ignite concept: | ||
https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: | ||
https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. | ||
|
||
See also: | ||
:py:class:`monai.metrics.R2Metric` | ||
|
||
""" | ||
|
||
def __init__( | ||
self, | ||
multi_output: MultiOutput | str = MultiOutput.UNIFORM, | ||
p: int = 0, | ||
output_transform: Callable = lambda x: x, | ||
) -> None: | ||
metric_fn = R2Metric(multi_output=multi_output, p=p) | ||
super().__init__(metric_fn=metric_fn, output_transform=output_transform, save_details=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
# Copyright (c) MONAI Consortium | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import numpy as np | ||
|
||
if TYPE_CHECKING: | ||
import numpy.typing as npt | ||
|
||
import torch | ||
|
||
from monai.utils import MultiOutput, look_up_option | ||
|
||
from .metric import CumulativeIterationMetric | ||
|
||
|
||
class R2Metric(CumulativeIterationMetric): | ||
r"""Computes :math:`R^{2}` score (coefficient of determination): | ||
|
||
.. math:: | ||
\operatorname {R^{2}}\left(Y, \hat{Y}\right) = 1 - \frac {\sum _{i=1}^{n}\left(y_i-\hat{y_i} \right)^{2}} | ||
{\sum _{i=1}^{n}\left(y_i-\bar{y} \right)^{2}}, | ||
|
||
where :math:`\bar{y}` is the mean of observed :math:`y` ; or adjusted :math:`R^{2}` score: | ||
|
||
.. math:: | ||
\operatorname {\bar{R}^{2}} = 1 - (1-R^{2}) \frac {n-1}{n-p-1}, | ||
|
||
where :math:`p` is the number of independant variables used for the regression. | ||
|
||
More info: https://en.wikipedia.org/wiki/Coefficient_of_determination | ||
|
||
Input `y_pred` is compared with ground truth `y`. | ||
`y_pred` and `y` are expected to be 1D (single-output regression) or 2D (multi-output regression) real-valued | ||
tensors of same shape. | ||
|
||
Example of the typical execution steps of this metric class follows :py:class:`monai.metrics.metric.Cumulative`. | ||
|
||
Args: | ||
multi_output: {``"raw_values"``, ``"uniform_average"``, ``"variance_weighted"``} | ||
Type of aggregation performed on multi-output scores. | ||
Defaults to ``"uniform_average"``. | ||
|
||
- ``"raw_values"``: the scores for each output are returned. | ||
- ``"uniform_average"``: the scores of all outputs are averaged with uniform weight. | ||
- ``"variance_weighted"``: the scores of all outputs are averaged, weighted by the variances of | ||
each individual output. | ||
p: non-negative integer. | ||
Number of independent variables used for regression. ``p`` is used to compute adjusted :math:`R^{2}` score. | ||
Defaults to 0 (standard :math:`R^{2}` score). | ||
|
||
""" | ||
|
||
def __init__(self, multi_output: MultiOutput | str = MultiOutput.UNIFORM, p: int = 0) -> None: | ||
super().__init__() | ||
multi_output, p = _check_r2_params(multi_output, p) | ||
self.multi_output = multi_output | ||
self.p = p | ||
|
||
def _compute_tensor(self, y_pred: torch.Tensor, y: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: # type: ignore[override] | ||
_check_dim(y_pred, y) | ||
return y_pred, y | ||
|
||
def aggregate(self, multi_output: MultiOutput | str | None = None) -> np.ndarray | float | npt.ArrayLike: | ||
""" | ||
Typically `y_pred` and `y` are stored in the cumulative buffers at each iteration, | ||
This function reads the buffers and computes the :math:`R^{2}` score. | ||
|
||
Args: | ||
multi_output: {``"raw_values"``, ``"uniform_average"``, ``"variance_weighted"``} | ||
Type of aggregation performed on multi-output scores. Defaults to `self.multi_output`. | ||
|
||
""" | ||
y_pred, y = self.get_buffer() | ||
return compute_r2_score(y_pred=y_pred, y=y, multi_output=multi_output or self.multi_output, p=self.p) | ||
|
||
|
||
def _check_dim(y_pred: torch.Tensor, y: torch.Tensor) -> None: | ||
if not isinstance(y_pred, torch.Tensor) or not isinstance(y, torch.Tensor): | ||
raise ValueError("y_pred and y must be PyTorch Tensor.") | ||
|
||
if y.shape != y_pred.shape: | ||
raise ValueError(f"data shapes of y_pred and y do not match, got {y_pred.shape} and {y.shape}.") | ||
|
||
dim = y.ndimension() | ||
if dim not in (1, 2): | ||
raise ValueError( | ||
f"predictions and ground truths should be of shape (batch_size, num_outputs) or (batch_size, ), got {y.shape}." | ||
) | ||
|
||
|
||
def _check_r2_params(multi_output: MultiOutput | str, p: int) -> tuple[MultiOutput | str, int]: | ||
multi_output = look_up_option(multi_output, MultiOutput) | ||
if not isinstance(p, int) or p < 0: | ||
raise ValueError(f"`p` must be an integer larger or equal to 0, got {p}.") | ||
|
||
return multi_output, p | ||
|
||
|
||
def _calculate(y_pred: np.ndarray, y: np.ndarray, p: int) -> float: | ||
num_obs = len(y) | ||
rss = np.sum((y_pred - y) ** 2) | ||
tss = np.sum(y**2) - np.sum(y) ** 2 / num_obs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
r2 = 1 - (rss / tss) | ||
r2_adjusted = 1 - (1 - r2) * (num_obs - 1) / (num_obs - p - 1) | ||
|
||
return r2_adjusted # type: ignore[no-any-return] | ||
|
||
|
||
def compute_r2_score( | ||
y_pred: torch.Tensor, y: torch.Tensor, multi_output: MultiOutput | str = MultiOutput.UNIFORM, p: int = 0 | ||
) -> np.ndarray | float | npt.ArrayLike: | ||
"""Computes :math:`R^{2}` score (coefficient of determination). | ||
|
||
Args: | ||
y_pred: input data to compute :math:`R^{2}` score, the first dim must be batch. | ||
For example: shape `[16]` or `[16, 1]` for a single-output regression, shape `[16, x]` for x output variables. | ||
y: ground truth to compute :math:`R^{2}` score, the first dim must be batch. | ||
For example: shape `[16]` or `[16, 1]` for a single-output regression, shape `[16, x]` for x output variables. | ||
multi_output: {``"raw_values"``, ``"uniform_average"``, ``"variance_weighted"``} | ||
Type of aggregation performed on multi-output scores. | ||
Defaults to ``"uniform_average"``. | ||
|
||
- ``"raw_values"``: the scores for each output are returned. | ||
- ``"uniform_average"``: the scores of all outputs are averaged with uniform weight. | ||
- ``"variance_weighted"``: the scores of all outputs are averaged, weighted by the variances | ||
each individual output. | ||
p: non-negative integer. | ||
Number of independent variables used for regression. ``p`` is used to compute adjusted :math:`R^{2}` score. | ||
Defaults to 0 (standard :math:`R^{2}` score). | ||
|
||
Raises: | ||
ValueError: When ``multi_output`` is not one of ["raw_values", "uniform_average", "variance_weighted"]. | ||
ValueError: When ``p`` is not a non-negative integer. | ||
ValueError: When ``y_pred`` or ``y`` are not PyTorch tensors. | ||
ValueError: When ``y_pred`` and ``y`` don't have the same shape. | ||
ValueError: When ``y_pred`` or ``y`` dimension is not one of [1, 2]. | ||
ValueError: When n_samples is less than 2. | ||
ValueError: When ``p`` is greater or equal to n_samples - 1. | ||
|
||
""" | ||
multi_output, p = _check_r2_params(multi_output, p) | ||
_check_dim(y_pred, y) | ||
dim = y.ndimension() | ||
n = y.shape[0] | ||
y = y.cpu().numpy() # type: ignore[assignment] | ||
y_pred = y_pred.cpu().numpy() # type: ignore[assignment] | ||
|
||
if n < 2: | ||
raise ValueError("There is no enough data for computing. Needs at least two samples to calculate r2 score.") | ||
if p >= n - 1: | ||
raise ValueError("`p` must be smaller than n_samples - 1, " f"got p={p}, n_samples={n}.") | ||
|
||
if dim == 2 and y_pred.shape[1] == 1: | ||
y_pred = np.squeeze(y_pred, axis=-1) # type: ignore[assignment] | ||
y = np.squeeze(y, axis=-1) # type: ignore[assignment] | ||
dim = 1 | ||
|
||
if dim == 1: | ||
return _calculate(y_pred, y, p) # type: ignore[arg-type] | ||
|
||
y, y_pred = np.transpose(y, axes=(1, 0)), np.transpose(y_pred, axes=(1, 0)) # type: ignore[assignment] | ||
r2_values = [_calculate(y_pred_, y_, p) for y_pred_, y_ in zip(y_pred, y)] | ||
if multi_output == MultiOutput.RAW: | ||
return r2_values | ||
if multi_output == MultiOutput.UNIFORM: | ||
return np.mean(r2_values) | ||
if multi_output == MultiOutput.VARIANCE: | ||
weights = np.var(y, axis=1) | ||
return np.average(r2_values, weights=weights) # type: ignore[no-any-return] | ||
raise ValueError( | ||
f'Unsupported multi_output: {multi_output}, available options are ["raw_values", "uniform_average", "variance_weighted"].' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ | |
MetaKeys, | ||
Method, | ||
MetricReduction, | ||
MultiOutput, | ||
NdimageMode, | ||
NumpyPadMode, | ||
OrderingTransformations, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As elsewhere it would help to explain where this is used and what it would be for. The math is explained in the function which is fine so no need here, just a high level idea.