Skip to content

Cell volumes against the mask

List the cells whose reconstructed mesh volume disagrees with their voxel count.

Why this exists

A reconstruction can lose a cell or misplace part of it while the mesh stays closed, manifold and free of self-intersections, so no topological check sees it. Measured with the default algorithm at min_distance 3: on the 47 benchmark masks 2 of 240 cells, on two 246-timepoint real embryo series 169 of 5 038, and on a 1 866-cell tissue 20 cells, have a mesh volume below half or above one and a half times their voxel count, a non-positive volume, or no surface at all (the benchmark and embryo counts measured before 0.7.1, whose meshes differ slightly; the 1 866-cell tissue's 20 are the same cells on 0.7.0's and on 0.7.1's meshes). Almost all of them are small cells (a few hundred voxels, about two voxels thick) that the seeded watershed never grows out of; before 0.6.0 one of the 1 866-cell tissue's was a single-tetrahedron cell that junction relocation turned inside out, which relocation now refuses to do. Their interfaces are missing or misplaced, and a pressure inferred for such a cell is not a measurement.

This module changes no geometry. It compares each label's mesh volume with its voxel count and reports the labels that fall outside a fixed range, so that a user, or foambryo, can see which cells to distrust.

Conventions

The mesh volume of a label is the divergence-theorem volume with the package's winding convention: +det(p0, p1, p2) / 6 for every triangle whose first label it is, -det / 6 for every triangle whose second label it is. Volumes are in cubic voxels, so the comparison is made on the mesh in mask voxel units, before any pixel-size scaling. Label 0 is the exterior medium and is never listed, nor is an enclosed medium (a lumen) the reconstruction made its own region (media). The range [0.5, 1.5] is inclusive; its bounds are fixed, not tuning parameters.

CellVolumeDisagreementWarning

Bases: UserWarning

Some cells of the last reconstruction have a mesh volume that disagrees with their voxel count.

Emitted by :meth:dw3d.reconstruction_algorithm.MeshReconstructionAlgorithm.construct_mesh_from_segmentation_mask once per reconstruction, when :func:compare_cell_volumes_with_mask lists at least one cell. The list is the algorithm's cell_volume_report. The mesh itself is unchanged. The warning is a subclass of :class:UserWarning, so it can be silenced or promoted to an error on its own with :func:warnings.filterwarnings.

CellVolumeReport dataclass

CellVolumeReport(ratio_range: tuple[float, float] = CELL_VOLUME_RATIO_RANGE, n_cells: int = 0, n_cells_in_mesh: int = 0, cells: list[dict] = list())

The labels whose mesh volume disagrees with their voxel count.

Attributes:

Name Type Description
ratio_range tuple[float, float]

the inclusive range of mesh volume over voxel count that is not listed.

n_cells int

non-zero labels in the mask.

n_cells_in_mesh int

those that at least one mesh triangle carries.

cells list[dict]

one dict per listed label, in ascending label order, with label, voxels, mesh_volume (cubic voxels, None when absent), ratio (mesh_volume / voxels, None when absent) and reason ("absent", "non-positive", "below" or "above", the first that applies in that order).

labels property

labels: list[int]

The listed labels, ascending.

counts

counts() -> dict[str, int]

How many labels are listed for each reason.

as_dict

as_dict() -> dict

A JSON-serialisable copy of the report, with the counts per reason.

voxel_counts

voxel_counts(segmented_mask: NDArray) -> dict[int, int]

Voxel count of every label present in the mask, the medium 0 included when present.

Non-negative integer labels up to _DIRECT_COUNT_MAX are counted block by block with np.bincount; any other mask falls back to np.unique. Both give the same counts.

Parameters:

Name Type Description Default
segmented_mask NDArray

the label image, any shape.

required

Returns:

Type Description
dict[int, int]

dict[int, int]: label to voxel count, labels ascending.

mesh_cell_volumes

mesh_cell_volumes(points: NDArray[float64], triangles: NDArray[integer], labels: NDArray[integer]) -> dict[int, float]

Divergence-theorem volume of every label a triangle carries, with the package's winding.

Parameters:

Name Type Description Default
points NDArray[float64]

(n, 3) vertex coordinates, voxel units.

required
triangles NDArray[integer]

(m, 3) vertex indices.

required
labels NDArray[integer]

(m, 2); a triangle's normal points out of labels[:, 0].

required

Returns:

Type Description
dict[int, float]

dict[int, float]: label to volume in cubic voxels, every label of labels (0 included).

compare_cell_volumes_with_mask

compare_cell_volumes_with_mask(segmented_mask: NDArray, points: NDArray[float64], triangles: NDArray[integer], labels: NDArray[integer], ratio_range: tuple[float, float] = CELL_VOLUME_RATIO_RANGE, *, media: tuple[int, ...] | list[int] = (0,)) -> CellVolumeReport

List every non-zero label whose mesh volume disagrees with its voxel count.

A label is listed when no triangle carries it ("absent"), when its mesh volume is <= 0 ("non-positive"), or when its mesh volume over voxel count is below ("below") or above ("above") the inclusive ratio_range, tested in that order.

Parameters:

Name Type Description Default
segmented_mask NDArray

the label image the mesh was reconstructed from; 0 is the medium.

required
points NDArray[float64]

mesh vertex coordinates in mask voxel units (before any pixel-size scaling, or the ratio is off by the voxel volume).

required
triangles NDArray[integer]

(m, 3) vertex indices.

required
labels NDArray[integer]

(m, 2), the two materials each triangle separates.

required
ratio_range tuple[float, float]

the inclusive range that is not listed.

CELL_VOLUME_RATIO_RANGE
media tuple[int, ...] | list[int]

the labels that are media, not cells (the exterior 0 and the enclosed media a reconstruction made, MeshReconstructionAlgorithm.medium_labels); never listed nor counted. Keyword-only. Defaults to (0,).

(0,)

Returns:

Name Type Description
CellVolumeReport CellVolumeReport

the listed labels, ascending, with their voxel count, volume and reason.

disagreement_message

disagreement_message(report: CellVolumeReport) -> str

The text of :class:CellVolumeDisagreementWarning for one report.