Skip to content

digitopo.evaluation

Topological evaluation of a segmentation against a reference.

Overlap scores (Dice, IoU) barely change when a thin vessel is cut in two, when two cells are merged by one pixel, or when a hole is filled, although each of these errors changes what is measured afterwards (number of objects, branches, cycles). The two functions of this module look at the topology instead, for any segmentation, including one produced by a neural network.

  • topology_errors: differences of the number of objects, holes (2D) or tunnels (3D), cavities and of the Euler characteristic.
  • cl_dice: centreline Dice of Shit et al. (2021), an overlap of the skeletons that rewards preserving networks (vessels, roots, roads).

These functions are additions of digitopo; they have no equivalent in pink26.

References

S. Shit, J. C. Paetzold, A. Sekuboyina, I. Ezhov, A. Unger, A. Zhylka, J. P. W. Pluim, U. Bauer, B. H. Menze, "clDice -- a novel topology-preserving loss function for tubular structure segmentation", CVPR 2021, pp. 16560-16569.

topology_errors

topology_errors(
    prediction: ArrayLike,
    reference: ArrayLike,
    connectivity: int,
) -> Topology

Return the topology of prediction minus the topology of reference.

Each field of the result is a signed difference: objects=2 means two objects too many (a cut vessel gives +1), holes=-1 one hole missing (a filled hole, or a merge that closes no cycle gives 0). All fields are 0 when the two masks have the same topology, which does not mean that the objects are in the same place: compare also an overlap score.

The counts are global (Betti numbers of the whole image); they do not say which object is wrong, and an extra object and a missing one in another place cancel out.

Parameters:

Name Type Description Default
prediction array_like

2D or 3D masks of the same shape (bool or integers; any nonzero value is an object point).

required
reference array_like

2D or 3D masks of the same shape (bool or integers; any nonzero value is an object point).

required
connectivity int

4 or 8 in 2D, 6 or 26 in 3D.

required

Returns:

Type Description
Topology

Signed differences (objects, holes, cavities, euler).

Raises:

Type Description
TypeError

If prediction or reference has a floating-point dtype, or connectivity is not an integer.

ValueError

If prediction or reference is not 2D or 3D or has an empty axis, their shapes differ, or connectivity does not exist in their dimension.

DigitopoError

If connectivity is 18, or the image is too large for the core.

Examples:

>>> import numpy as np, digitopo
>>> reference = np.zeros((5, 9), dtype=bool)
>>> reference[2, 1:8] = True                  # one vessel
>>> prediction = reference.copy()
>>> prediction[2, 4] = False                  # cut in two
>>> digitopo.evaluation.topology_errors(prediction, reference, 8)
Topology(objects=1, holes=0, cavities=0, euler=1)

cl_dice

cl_dice(
    prediction: ArrayLike,
    reference: ArrayLike,
    connectivity: int,
) -> float

Centreline Dice of a segmentation against a reference (Shit et al., 2021).

With S_P and S_R the skeletons of the prediction P and of the reference R, the topological precision is |S_P ∩ R| / |S_P| (the skeleton of the prediction lies inside the reference), the topological sensitivity |S_R ∩ P| / |S_R| (the skeleton of the reference is covered by the prediction), and clDice is their harmonic mean, between 0 and 1.

The skeletons are curvilinear and topology preserving: the symmetric parallel skeleton (critical kernels) for connectivity 8 or 26, the sequential curvilinear skeleton for 4 or 6. The original article used the skeletonization of scikit-image; values may differ slightly. Two empty masks give 1.0, one empty mask 0.0.

Parameters:

Name Type Description Default
prediction array_like

2D or 3D masks of the same shape (bool or integers; any nonzero value is an object point).

required
reference array_like

2D or 3D masks of the same shape (bool or integers; any nonzero value is an object point).

required
connectivity int

4 or 8 in 2D, 6 or 26 in 3D.

required

Returns:

Type Description
float

clDice, between 0 and 1.

Raises:

Type Description
TypeError

If prediction or reference has a floating-point dtype, or connectivity is not an integer.

ValueError

If prediction or reference is not 2D or 3D or has an empty axis, their shapes differ, or connectivity does not exist in their dimension.

DigitopoError

If connectivity is 18, or the image is too large for the core.

Examples:

>>> import numpy as np, digitopo
>>> reference = np.zeros((7, 21), dtype=bool)
>>> reference[2:5, 1:20] = True               # a vessel 3 pixels wide
>>> digitopo.evaluation.cl_dice(reference, reference, 8)
1.0
>>> cut = reference.copy()
>>> cut[:, 10] = False                        # one column missing
>>> round(digitopo.evaluation.cl_dice(cut, reference, 8), 3) < 1.0
True