Skip to content

Commit 4f09ed6

Browse files
KirscherericspodCopilot
authored
Fix: make y optional in ignore_background (#8647)
Fixes # . ### Description This PR addresses a TODO in `monai/metrics/active_learning_metrics.py` by making the `y` argument optional in the `ignore_background` utility function. This allows for cleaner code when only `y_pred` needs to be processed, as seen in `active_learning_metrics.py`. ### Types of changes - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [x] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [x] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: Tristan Kirscher <85068746+Kirscher@users.noreply.github.com> Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 27a03ef commit 4f09ed6

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

monai/metrics/active_learning_metrics.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,7 @@ def compute_variance(
129129
y_pred = y_pred.float()
130130

131131
if not include_background:
132-
y = y_pred
133-
# TODO If this utils is made to be optional for 'y' it would be nice
134-
y_pred, y = ignore_background(y_pred=y_pred, y=y)
132+
y_pred, _ = ignore_background(y_pred=y_pred)
135133

136134
# Set any values below 0 to threshold
137135
y_pred[y_pred <= 0] = threshold

monai/metrics/utils.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from collections.abc import Iterable, Sequence
1616
from functools import cache, partial
1717
from types import ModuleType
18-
from typing import Any
18+
from typing import Any, overload
1919

2020
import numpy as np
2121
import torch
@@ -55,19 +55,30 @@
5555
]
5656

5757

58-
def ignore_background(y_pred: NdarrayTensor, y: NdarrayTensor) -> tuple[NdarrayTensor, NdarrayTensor]:
58+
@overload
59+
def ignore_background(y_pred: NdarrayTensor, y: NdarrayTensor) -> tuple[NdarrayTensor, NdarrayTensor]: ...
60+
61+
62+
@overload
63+
def ignore_background(y_pred: NdarrayTensor, y: None = ...) -> tuple[NdarrayTensor, None]: ...
64+
65+
66+
def ignore_background(
67+
y_pred: NdarrayTensor, y: NdarrayTensor | None = None
68+
) -> tuple[NdarrayTensor, NdarrayTensor | None]:
5969
"""
6070
This function is used to remove background (the first channel) for `y_pred` and `y`.
6171
6272
Args:
6373
y_pred: predictions. As for classification tasks,
6474
`y_pred` should has the shape [BN] where N is larger than 1. As for segmentation tasks,
6575
the shape should be [BNHW] or [BNHWD].
66-
y: ground truth, the first dim is batch.
76+
y: optional ground truth, the first dim is batch.
6777
6878
"""
6979

70-
y = y[:, 1:] if y.shape[1] > 1 else y # type: ignore[assignment]
80+
if y is not None:
81+
y = y[:, 1:] if y.shape[1] > 1 else y # type: ignore[assignment]
7182
y_pred = y_pred[:, 1:] if y_pred.shape[1] > 1 else y_pred # type: ignore[assignment]
7283
return y_pred, y
7384

0 commit comments

Comments
 (0)