Skip to content
Open
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
12 changes: 7 additions & 5 deletions monai/metrics/froc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from __future__ import annotations

from typing import Any, cast
from typing import Any

import numpy as np
import torch
Expand Down Expand Up @@ -67,12 +67,14 @@ def compute_fp_tp_probs_nd(

hittedlabel = evaluation_mask[tuple(coords.T)]
fp_probs = probs[np.where(hittedlabel == 0)]
num_targets = 0
for i in range(1, max_label + 1):
if i not in labels_to_exclude and i in hittedlabel:
tp_probs[i - 1] = probs[np.where(hittedlabel == i)].max()
if i not in labels_to_exclude:
num_targets += 1
if i in hittedlabel:
tp_probs[i - 1] = probs[np.where(hittedlabel == i)].max()

num_targets = max_label - len(labels_to_exclude)
return fp_probs, tp_probs, cast(int, num_targets)
return fp_probs, tp_probs, num_targets


def compute_fp_tp_probs(
Expand Down
32 changes: 31 additions & 1 deletion tests/metrics/test_compute_froc.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,34 @@
3,
]

TEST_CASE_EXCLUDE_ABSENT = [
{
"probs": torch.tensor([1, 0.6, 0.8]),
"y_coord": torch.tensor([0, 2, 3]),
"x_coord": torch.tensor([3, 0, 1]),
"evaluation_mask": np.array([[0, 0, 1, 1], [2, 2, 0, 0], [0, 3, 3, 0], [0, 3, 3, 3]]),
"labels_to_exclude": [5],
"resolution_level": 0,
},
np.array([0.6]),
np.array([1, 0, 0.8]),
3,
]

TEST_CASE_EXCLUDE_DUPLICATE = [
{
"probs": torch.tensor([1, 0.6, 0.8]),
"y_coord": torch.tensor([0, 2, 3]),
"x_coord": torch.tensor([3, 0, 1]),
"evaluation_mask": np.array([[0, 0, 1, 1], [2, 2, 0, 0], [0, 3, 3, 0], [0, 3, 3, 3]]),
"labels_to_exclude": [2, 2],
"resolution_level": 0,
},
np.array([0.6]),
np.array([1, 0, 0.8]),
2,
]

TEST_CASE_4 = [
{
"fp_probs": np.array([0.8, 0.6]),
Expand Down Expand Up @@ -112,7 +140,9 @@

class TestComputeFpTp(unittest.TestCase):

@parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_3])
@parameterized.expand(
[TEST_CASE_1, TEST_CASE_2, TEST_CASE_3, TEST_CASE_EXCLUDE_ABSENT, TEST_CASE_EXCLUDE_DUPLICATE]
)
def test_value(self, input_data, expected_fp, expected_tp, expected_num):
fp_probs, tp_probs, num_tumors = compute_fp_tp_probs(**input_data)
np.testing.assert_allclose(fp_probs, expected_fp, rtol=1e-5)
Expand Down
Loading