Testing

This module define some simple metrics.

The metrics defined here can’t be used as loss function, contrarly to the module biom3d.metrics.

class biom3d.utils.eval_metrics.MONAIMetricFactory(metric_name, average_classes=False, **metric_kwargs)[source]

MONAI metric wrapper that mimics simple NumPy metric behavior. Designed for batch size = 1 and evaluation only.

biom3d.utils.eval_metrics.dice(inputs: ndarray, targets: ndarray, smooth: float = 1.0, axis: tuple[int] = (-3, -2, -1)) float[source]

Compute the Dice coefficient between inputs and targets.

Parameters:
  • inputs (numpy.ndarray) – Binary array or one-hot encoded mask of predictions.

  • targets (numpy.ndarray) – Binary array or one-hot encoded mask of ground truth.

  • smooth (float, default=1.0) – Smoothing factor to avoid division by zero.

  • axis (tuple of int, default is last three axes (supposed spatial axis)) – Axes along which to compute the Dice score.

Returns:

Mean Dice score over specified axes.

Return type:

float

biom3d.utils.eval_metrics.iou(inputs: ndarray, targets: ndarray, smooth: float = 1.0) float[source]

Calculate the Intersection over Union (IoU) score between two binary masks.

Parameters:
  • inputs (numpy.ndarray) – Binary array representing the predicted mask.

  • targets (numpy.ndarray) – Binary array representing the ground truth mask.

  • smooth (float, default=1.0) – Smoothing factor to avoid division by zero.

Returns:

IoU score between inputs and targets.

Return type:

float

biom3d.utils.eval_metrics.versus_one(fct: Callable, input_img: ndarray, target_img: ndarray, num_classes: int, single_class: int | None = None, is_sigmoid_output: bool = False) float | None[source]

Compare input and target images using a given metric function.

This function: - Converts label images to one-hot encoding if needed. - Optionally selects a single class channel. - Binarizes masks. - Removes background class channel if present. - Checks shape compatibility. - Applies the provided comparison function fct on processed masks.

Parameters:
  • fct (callable) – A function that takes two binary masks and returns a metric score (e.g., IoU or Dice).

  • input_img (numpy.ndarray) – Input image as label indices or one-hot encoded mask.

  • target_img (numpy.ndarray) – Target (ground truth) image as label indices or one-hot encoded mask.

  • num_classes (int) – Number of classes expected in input and target images.

  • single_class (int or None, optional) – Index of class to compare individually. If None, compares all classes.

  • is_sigmoid_output (bool, optional) – Whether the first dimension is a label dimension (CZYX), typically output of sigmoid layer. For instance a 3D image could have the following dimensions: (2, 64, 111, 110) Otherwise, dimensions are supposed to be ZYX.

Returns:

The score returned by fct.

Return type:

float or None

Notes

If shapes after processing don’t match, prints an error message and returns None. Copy the images so no side effect.