Skip to content

Commit b806031

Browse files
authored
Merge branch 'dev' into optimize/vectorize-dice-metric
2 parents dd13006 + 2dbbd49 commit b806031

6 files changed

Lines changed: 24 additions & 12 deletions

File tree

monai/metrics/active_learning_metrics.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def compute_variance(
137137
n_len = len(y_pred.shape)
138138

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

143143
# Create new shape list
@@ -190,7 +190,10 @@ def label_quality_score(
190190

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

196199
abs_diff_map = torch.abs(y_pred - y)

monai/metrics/average_precision.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@ def _calculate(y_pred: torch.Tensor, y: torch.Tensor) -> float:
8888
raise AssertionError("y and y_pred must be 1 dimension data with same length.")
8989
y_unique = y.unique()
9090
if len(y_unique) == 1:
91-
warnings.warn(f"y values can not be all {y_unique.item()}, skip AP computation and return `Nan`.")
91+
warnings.warn(f"y values can not be all {y_unique.item()}, skip AP computation and return `Nan`.", stacklevel=2)
9292
return float("nan")
9393
if not y_unique.equal(torch.tensor([0, 1], dtype=y.dtype, device=y.device)):
94-
warnings.warn(f"y values must be 0 or 1, but in {y_unique.tolist()}, skip AP computation and return `Nan`.")
94+
warnings.warn(
95+
f"y values must be 0 or 1, but in {y_unique.tolist()}, skip AP computation and return `Nan`.", stacklevel=2
96+
)
9597
return float("nan")
9698

9799
n = len(y)

monai/metrics/confusion_matrix.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def _compute_tensor(self, y_pred: torch.Tensor, y: torch.Tensor) -> torch.Tensor
9393
raise ValueError("y_pred should have at least two dimensions.")
9494
if dims == 2 or (dims == 3 and y_pred.shape[-1] == 1):
9595
if self.compute_sample:
96-
warnings.warn("As for classification task, compute_sample should be False.")
96+
warnings.warn("As for classification task, compute_sample should be False.", stacklevel=2)
9797
self.compute_sample = False
9898

9999
return get_confusion_matrix(y_pred=y_pred, y=y, include_background=self.include_background)

monai/metrics/cumulative_average.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def append(self, val: Any, count: Any | None = 1) -> None:
154154
# account for possible non-finite numbers in val and replace them with 0s
155155
nfin = torch.isfinite(val)
156156
if not torch.all(nfin):
157-
warnings.warn(f"non-finite inputs received: val: {val}, count: {count}")
157+
warnings.warn(f"non-finite inputs received: val: {val}, count: {count}", stacklevel=2)
158158
count = torch.where(nfin, count, torch.zeros_like(count))
159159
val = torch.where(nfin, val, torch.zeros_like(val))
160160

monai/metrics/rocauc.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,14 @@ def _calculate(y_pred: torch.Tensor, y: torch.Tensor) -> float:
7777
raise AssertionError("y and y_pred must be 1 dimension data with same length.")
7878
y_unique = y.unique()
7979
if len(y_unique) == 1:
80-
warnings.warn(f"y values can not be all {y_unique.item()}, skip AUC computation and return `Nan`.")
80+
warnings.warn(
81+
f"y values can not be all {y_unique.item()}, skip AUC computation and return `Nan`.", stacklevel=2
82+
)
8183
return float("nan")
8284
if not y_unique.equal(torch.tensor([0, 1], dtype=y.dtype, device=y.device)):
83-
warnings.warn(f"y values must be 0 or 1, but in {y_unique.tolist()}, skip AUC computation and return `Nan`.")
85+
warnings.warn(
86+
f"y values must be 0 or 1, but in {y_unique.tolist()}, skip AUC computation and return `Nan`.", stacklevel=2
87+
)
8488
return float("nan")
8589

8690
n = len(y)

monai/metrics/utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,14 @@ def get_edge_surface_distance(
341341
if not edges_gt.any():
342342
warnings.warn(
343343
f"the ground truth of class {class_index if class_index != -1 else 'Unknown'} is all 0,"
344-
" this may result in nan/inf distance."
344+
" this may result in nan/inf distance.",
345+
stacklevel=2,
345346
)
346347
if not edges_pred.any():
347348
warnings.warn(
348349
f"the prediction of class {class_index if class_index != -1 else 'Unknown'} is all 0,"
349-
" this may result in nan/inf distance."
350+
" this may result in nan/inf distance.",
351+
stacklevel=2,
350352
)
351353
distances: tuple[torch.Tensor, torch.Tensor] | tuple[torch.Tensor]
352354
if symmetric:
@@ -375,7 +377,7 @@ def is_binary_tensor(input: torch.Tensor, name: str) -> None:
375377
if not isinstance(input, torch.Tensor):
376378
raise ValueError(f"{name} must be of type PyTorch Tensor.")
377379
if not torch.all(input.byte() == input) or input.max() > 1 or input.min() < 0:
378-
warnings.warn(f"{name} should be a binarized tensor.")
380+
warnings.warn(f"{name} should be a binarized tensor.", stacklevel=2)
379381

380382

381383
def remap_instance_id(pred: torch.Tensor, by_size: bool = False) -> torch.Tensor:
@@ -510,7 +512,8 @@ def compute_voronoi_regions_fast(labels: np.ndarray | torch.Tensor) -> torch.Ten
510512
if isinstance(labels, torch.Tensor):
511513
warnings.warn(
512514
"Voronoi computation is running on CPU. "
513-
"To accelerate, move the input tensor to GPU and ensure 'cupy' with 'cupyx.scipy.ndimage' is installed."
515+
"To accelerate, move the input tensor to GPU and ensure 'cupy' with 'cupyx.scipy.ndimage' is installed.",
516+
stacklevel=2,
514517
)
515518
x = labels.cpu().numpy()
516519
else:

0 commit comments

Comments
 (0)