Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion monai/metrics/rocauc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import warnings
from typing import Union, cast

import numpy as np
Expand Down Expand Up @@ -66,7 +67,8 @@ def _calculate(y_pred: torch.Tensor, y: torch.Tensor) -> float:
if not (y.ndimension() == y_pred.ndimension() == 1 and len(y) == len(y_pred)):
raise AssertionError("y and y_pred must be 1 dimension data with same length.")
if not y.unique().equal(torch.tensor([0, 1], dtype=y.dtype, device=y.device)):
raise AssertionError("y values must be 0 or 1, can not be all 0 or all 1.")
warnings.warn("y values must be 0 or 1, can not be all 0 or all 1, skip AUC computation and return `Nan`.")
return float("nan")
n = len(y)
indices = y_pred.argsort()
y = y[indices].cpu().numpy()
Expand Down
17 changes: 15 additions & 2 deletions tests/test_compute_roc_auc.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,20 @@
0.62,
]

TEST_CASE_8 = [
torch.tensor([[0.1, 0.9], [0.3, 1.4], [0.2, 0.1], [0.1, 0.5]]),
torch.tensor([[0], [0], [0], [0]]),
True,
2,
"macro",
float("nan"),
]


class TestComputeROCAUC(unittest.TestCase):
@parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_3, TEST_CASE_4, TEST_CASE_5, TEST_CASE_6, TEST_CASE_7])
@parameterized.expand(
[TEST_CASE_1, TEST_CASE_2, TEST_CASE_3, TEST_CASE_4, TEST_CASE_5, TEST_CASE_6, TEST_CASE_7, TEST_CASE_8]
)
def test_value(self, y_pred, y, softmax, to_onehot, average, expected_value):
y_pred_trans = Compose([ToTensor(), Activations(softmax=softmax)])
y_trans = Compose([ToTensor(), AsDiscrete(to_onehot=to_onehot)])
Expand All @@ -79,7 +90,9 @@ def test_value(self, y_pred, y, softmax, to_onehot, average, expected_value):
result = compute_roc_auc(y_pred=y_pred, y=y, average=average)
np.testing.assert_allclose(expected_value, result, rtol=1e-5)

@parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_3, TEST_CASE_4, TEST_CASE_5, TEST_CASE_6, TEST_CASE_7])
@parameterized.expand(
[TEST_CASE_1, TEST_CASE_2, TEST_CASE_3, TEST_CASE_4, TEST_CASE_5, TEST_CASE_6, TEST_CASE_7, TEST_CASE_8]
)
def test_class_value(self, y_pred, y, softmax, to_onehot, average, expected_value):
y_pred_trans = Compose([ToTensor(), Activations(softmax=softmax)])
y_trans = Compose([ToTensor(), AsDiscrete(to_onehot=to_onehot)])
Expand Down