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
4 changes: 1 addition & 3 deletions monai/metrics/active_learning_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ def compute_variance(
y_pred = y_pred.float()

if not include_background:
y = y_pred
# TODO If this utils is made to be optional for 'y' it would be nice
y_pred, y = ignore_background(y_pred=y_pred, y=y)
y_pred, _ = ignore_background(y_pred=y_pred)

# Set any values below 0 to threshold
y_pred[y_pred <= 0] = threshold
Expand Down
7 changes: 5 additions & 2 deletions monai/metrics/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
]


def ignore_background(y_pred: NdarrayTensor, y: NdarrayTensor) -> tuple[NdarrayTensor, NdarrayTensor]:
def ignore_background(
y_pred: NdarrayTensor, y: NdarrayTensor | None = None
) -> tuple[NdarrayTensor, NdarrayTensor | None]:
Comment on lines +54 to +56
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updated type signature makes y optional, but it also changes the return type for all callers to tuple[NdarrayTensor, NdarrayTensor | None]. This will propagate Optional[...] into existing call sites that always pass y (many metrics do), and is likely to fail the repo’s mypy checks. Consider using @overload so that when y is provided the return type remains tuple[NdarrayTensor, NdarrayTensor], and only returns None in the second position when y is omitted/None.

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Kirscher we are seeing this issue in the mypy tests now, please look at what was said here to see how to resolve it. There are other uses of ignore_background you can update to remove the unneeded y=y argument.

"""
This function is used to remove background (the first channel) for `y_pred` and `y`.

Expand All @@ -63,7 +65,8 @@ def ignore_background(y_pred: NdarrayTensor, y: NdarrayTensor) -> tuple[NdarrayT

Comment on lines 58 to 65
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignore_background now allows y=None, but the docstring still describes y as a required ground truth tensor. Please update the docstring to reflect that y is optional and clarify what is returned when y is None (e.g., second tuple element is None).

Copilot uses AI. Check for mistakes.
"""

y = y[:, 1:] if y.shape[1] > 1 else y # type: ignore[assignment]
if y is not None:
y = y[:, 1:] if y.shape[1] > 1 else y # type: ignore[assignment]
y_pred = y_pred[:, 1:] if y_pred.shape[1] > 1 else y_pred # type: ignore[assignment]
return y_pred, y

Expand Down
Loading