Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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
18 changes: 15 additions & 3 deletions monai/metrics/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from collections.abc import Iterable, Sequence
from functools import cache, partial
from types import ModuleType
from typing import Any
from typing import Any, overload

import numpy as np
import torch
Expand Down Expand Up @@ -55,7 +55,18 @@
]


def ignore_background(y_pred: NdarrayTensor, y: NdarrayTensor) -> tuple[NdarrayTensor, NdarrayTensor]:

Comment thread
ericspod marked this conversation as resolved.
Outdated
@overload
def ignore_background(y_pred: NdarrayTensor, y: NdarrayTensor) -> tuple[NdarrayTensor, NdarrayTensor]: ...


@overload
def ignore_background(y_pred: NdarrayTensor, y: None = ...) -> tuple[NdarrayTensor, None]: ...


def ignore_background(
y_pred: NdarrayTensor, y: NdarrayTensor | None = None
) -> tuple[NdarrayTensor, NdarrayTensor | None]:
Comment thread
ericspod marked this conversation as resolved.
Comment thread
ericspod marked this conversation as resolved.
"""
This function is used to remove background (the first channel) for `y_pred` and `y`.

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

Comment thread
ericspod marked this conversation as resolved.
"""

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