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
7 changes: 5 additions & 2 deletions monai/metrics/active_learning_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def compute_variance(
n_len = len(y_pred.shape)

if n_len < 4 and spatial_map:
warnings.warn("Spatial map requires a 2D/3D image with N-repeats and C-channels")
warnings.warn("Spatial map requires a 2D/3D image with N-repeats and C-channels", stacklevel=2)
return None

# Create new shape list
Expand Down Expand Up @@ -190,7 +190,10 @@ def label_quality_score(

n_len = len(y_pred.shape)
if n_len < 4 and scalar_reduction == "none":
warnings.warn("Reduction set to None, Spatial map return requires a 2D/3D image of B-Batchsize and C-channels")
warnings.warn(
"Reduction set to None, Spatial map return requires a 2D/3D image of B-Batchsize and C-channels",
stacklevel=2,
)
return None

abs_diff_map = torch.abs(y_pred - y)
Expand Down
6 changes: 4 additions & 2 deletions monai/metrics/average_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ def _calculate(y_pred: torch.Tensor, y: torch.Tensor) -> float:
raise AssertionError("y and y_pred must be 1 dimension data with same length.")
y_unique = y.unique()
if len(y_unique) == 1:
warnings.warn(f"y values can not be all {y_unique.item()}, skip AP computation and return `Nan`.")
warnings.warn(f"y values can not be all {y_unique.item()}, skip AP computation and return `Nan`.", stacklevel=2)
return float("nan")
if not y_unique.equal(torch.tensor([0, 1], dtype=y.dtype, device=y.device)):
warnings.warn(f"y values must be 0 or 1, but in {y_unique.tolist()}, skip AP computation and return `Nan`.")
warnings.warn(
f"y values must be 0 or 1, but in {y_unique.tolist()}, skip AP computation and return `Nan`.", stacklevel=2
)
return float("nan")

n = len(y)
Expand Down
2 changes: 1 addition & 1 deletion monai/metrics/confusion_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _compute_tensor(self, y_pred: torch.Tensor, y: torch.Tensor) -> torch.Tensor
raise ValueError("y_pred should have at least two dimensions.")
if dims == 2 or (dims == 3 and y_pred.shape[-1] == 1):
if self.compute_sample:
warnings.warn("As for classification task, compute_sample should be False.")
warnings.warn("As for classification task, compute_sample should be False.", stacklevel=2)
self.compute_sample = False

return get_confusion_matrix(y_pred=y_pred, y=y, include_background=self.include_background)
Expand Down
2 changes: 1 addition & 1 deletion monai/metrics/cumulative_average.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def append(self, val: Any, count: Any | None = 1) -> None:
# account for possible non-finite numbers in val and replace them with 0s
nfin = torch.isfinite(val)
if not torch.all(nfin):
warnings.warn(f"non-finite inputs received: val: {val}, count: {count}")
warnings.warn(f"non-finite inputs received: val: {val}, count: {count}", stacklevel=2)
count = torch.where(nfin, count, torch.zeros_like(count))
val = torch.where(nfin, val, torch.zeros_like(val))

Expand Down
8 changes: 6 additions & 2 deletions monai/metrics/rocauc.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ def _calculate(y_pred: torch.Tensor, y: torch.Tensor) -> float:
raise AssertionError("y and y_pred must be 1 dimension data with same length.")
y_unique = y.unique()
if len(y_unique) == 1:
warnings.warn(f"y values can not be all {y_unique.item()}, skip AUC computation and return `Nan`.")
warnings.warn(
f"y values can not be all {y_unique.item()}, skip AUC computation and return `Nan`.", stacklevel=2
)
return float("nan")
if not y_unique.equal(torch.tensor([0, 1], dtype=y.dtype, device=y.device)):
warnings.warn(f"y values must be 0 or 1, but in {y_unique.tolist()}, skip AUC computation and return `Nan`.")
warnings.warn(
f"y values must be 0 or 1, but in {y_unique.tolist()}, skip AUC computation and return `Nan`.", stacklevel=2
)
return float("nan")

n = len(y)
Expand Down
11 changes: 7 additions & 4 deletions monai/metrics/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,14 @@ def get_edge_surface_distance(
if not edges_gt.any():
warnings.warn(
f"the ground truth of class {class_index if class_index != -1 else 'Unknown'} is all 0,"
" this may result in nan/inf distance."
" this may result in nan/inf distance.",
stacklevel=2,
)
if not edges_pred.any():
warnings.warn(
f"the prediction of class {class_index if class_index != -1 else 'Unknown'} is all 0,"
" this may result in nan/inf distance."
" this may result in nan/inf distance.",
stacklevel=2,
)
distances: tuple[torch.Tensor, torch.Tensor] | tuple[torch.Tensor]
if symmetric:
Expand Down Expand Up @@ -375,7 +377,7 @@ def is_binary_tensor(input: torch.Tensor, name: str) -> None:
if not isinstance(input, torch.Tensor):
raise ValueError(f"{name} must be of type PyTorch Tensor.")
if not torch.all(input.byte() == input) or input.max() > 1 or input.min() < 0:
warnings.warn(f"{name} should be a binarized tensor.")
warnings.warn(f"{name} should be a binarized tensor.", stacklevel=2)


def remap_instance_id(pred: torch.Tensor, by_size: bool = False) -> torch.Tensor:
Expand Down Expand Up @@ -510,7 +512,8 @@ def compute_voronoi_regions_fast(labels: np.ndarray | torch.Tensor) -> torch.Ten
if isinstance(labels, torch.Tensor):
warnings.warn(
"Voronoi computation is running on CPU. "
"To accelerate, move the input tensor to GPU and ensure 'cupy' with 'cupyx.scipy.ndimage' is installed."
"To accelerate, move the input tensor to GPU and ensure 'cupy' with 'cupyx.scipy.ndimage' is installed.",
stacklevel=2,
)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
x = labels.cpu().numpy()
else:
Expand Down
Loading