Skip to content
Merged
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
5 changes: 5 additions & 0 deletions monai/metrics/hausdorff_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ def compute_hausdorff_distance(
hd = np.empty((batch_size, n_class))
for b, c in np.ndindex(batch_size, n_class):
(edges_pred, edges_gt) = get_mask_edges(y_pred[b, c], y[b, c])
if not np.any(edges_gt):
warnings.warn(f"the ground truth of class {c} is all 0, this may result in nan/inf distance.")
if not np.any(edges_pred):
warnings.warn(f"the prediction of class {c} is all 0, this may result in nan/inf distance.")

distance_1 = compute_percent_hausdorff_distance(edges_pred, edges_gt, distance_metric, percentile)
if directed:
hd[b, c] = distance_1
Expand Down
5 changes: 5 additions & 0 deletions monai/metrics/surface_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ def compute_average_surface_distance(

for b, c in np.ndindex(batch_size, n_class):
(edges_pred, edges_gt) = get_mask_edges(y_pred[b, c], y[b, c])
if not np.any(edges_gt):
warnings.warn(f"the ground truth of class {c} is all 0, this may result in nan/inf distance.")
if not np.any(edges_pred):
warnings.warn(f"the prediction of class {c} is all 0, this may result in nan/inf distance.")

surface_distance = get_surface_distance(edges_pred, edges_gt, distance_metric=distance_metric)
if surface_distance.shape == (0,):
avg_surface_distance = np.nan
Expand Down
7 changes: 4 additions & 3 deletions monai/metrics/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import warnings
from typing import Tuple, Union

import numpy as np
Expand Down Expand Up @@ -188,15 +187,17 @@ def get_surface_distance(
- ``"euclidean"``, uses Exact Euclidean distance transform.
- ``"chessboard"``, uses `chessboard` metric in chamfer type of transform.
- ``"taxicab"``, uses `taxicab` metric in chamfer type of transform.

Note:
If seg_pred or seg_gt is all 0, may result in nan/inf distance.

"""

if not np.any(seg_gt):
dis = np.inf * np.ones_like(seg_gt)
warnings.warn("ground truth is all 0, this may result in nan/inf distance.")
else:
if not np.any(seg_pred):
dis = np.inf * np.ones_like(seg_gt)
warnings.warn("prediction is all 0, this may result in nan/inf distance.")
return np.asarray(dis[seg_gt])
if distance_metric == "euclidean":
dis = distance_transform_edt(~seg_gt)
Expand Down